diffs-rs 0.1.1

Fast terminal diff viewer and embeddable Rust diff engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
use std::{
    io::{self, BufRead, BufReader, Read, Write},
    ops::{Deref, Range, RangeInclusive},
    sync::Arc,
};

use anyhow::{Context, Result, ensure};

use crate::{
    model::{Document as Parsed, Kind, Row, RowKind},
    projection::{Projection, Rows},
    source_spans::SourceSpans,
};

mod metadata;
pub use metadata::{FileMetadata, HunkMetadata};

mod bytes;
pub(crate) use bytes::private_tempfile;
pub use bytes::{ByteStore, SourceRef, StoreReader, StoreWriter};
mod reader;
pub(crate) use reader::ReadWindow;

#[cfg(test)]
pub(crate) mod tests;

/// An original patch record, independent of its current screen row.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct SourceId {
    store: bytes::StoreId,
    record: usize,
}

struct Spooling<R> {
    input: R,
    writer: StoreWriter,
}
impl<R: Read> Read for Spooling<R> {
    fn read(&mut self, out: &mut [u8]) -> io::Result<usize> {
        let n = self.input.read(out)?;
        self.writer.append(&out[..n]).map_err(io::Error::other)?;
        Ok(n)
    }
}

#[derive(Clone, Copy, Debug)]
pub struct LineInfo {
    pub kind: Kind,
    pub old: Option<u32>,
    pub new: Option<u32>,
    pub no_newline: bool,
}

#[derive(Debug)]
pub struct FileIndex {
    pub id: usize,
    pub source_base: usize,
    pub split_base: usize,
    pub unified_base: usize,
    pub meta: FileMetadata,
    pub spool: Arc<ByteStore>,
    source: SourceSpans,
    projection: Projection,
}

impl Deref for FileIndex {
    type Target = FileMetadata;
    fn deref(&self) -> &Self::Target {
        &self.meta
    }
}

impl FileIndex {
    pub(crate) fn hunk_row(&self, index: usize, split: bool) -> usize {
        self.projection.hunk_row(index, split)
    }
    pub fn source_end(&self) -> usize {
        self.source_base + self.source.len()
    }
    pub fn row_base(&self, split: bool) -> usize {
        if split {
            self.split_base
        } else {
            self.unified_base
        }
    }
    pub fn rows(&self, split: bool) -> Rows<'_> {
        self.projection.rows(split, self.id, self.source_base)
    }
    pub fn row_end(&self, split: bool) -> usize {
        self.row_base(split) + self.rows(split).len()
    }
    pub fn row_for_source(&self, id: usize, split: bool) -> usize {
        self.row_base(split) + self.projection.row_for_source(id - self.source_base, split)
    }
    pub fn info(&self, id: usize) -> LineInfo {
        let n = id - self.source_base;
        let (kind, old, new) = self.projection.info(n);
        LineInfo {
            kind,
            old,
            new,
            no_newline: self.source.no_newline(n),
        }
    }
    pub fn bytes(&self, id: usize) -> Result<Vec<u8>> {
        self.spool.read_source(self.source_ref(id)?)
    }
    pub fn source_id(&self, id: usize) -> Result<SourceId> {
        ensure!(
            (self.source_base..self.source_end()).contains(&id),
            "Source line is outside this file"
        );
        Ok(SourceId {
            store: self.spool.id(),
            record: id,
        })
    }
    pub fn source_ref(&self, id: usize) -> Result<SourceRef> {
        self.source_id(id)?;
        let (offset, len) = self.source.range(id - self.source_base)?;
        self.spool.source_ref(offset, len)
    }
    pub(crate) fn source_refs(
        &self,
        range: Range<usize>,
    ) -> Result<impl Iterator<Item = Result<SourceRef>> + '_> {
        ensure!(
            range.start >= self.source_base
                && range.start <= range.end
                && range.end <= self.source_end(),
            "Source range is outside this file"
        );
        Ok(self
            .source
            .cursor(range.start - self.source_base..range.end - self.source_base)?
            .map(|span| {
                let (offset, len) = span?;
                self.spool.source_ref(offset, len)
            }))
    }
    pub fn partner(&self, id: usize) -> Option<usize> {
        let row = self
            .rows(true)
            .get(self.row_for_source(id, true) - self.split_base)?;
        let RowKind::Code {
            left: Some(left),
            right: Some(right),
        } = row.kind
        else {
            return None;
        };
        if left == right {
            None
        } else {
            Some(if id == left { right } else { left })
        }
    }
    pub fn source_index_bytes(&self) -> usize {
        self.source.bytes()
    }
    pub fn source_index_stored_bytes(&self) -> u64 {
        self.source.stored_bytes()
    }
    pub fn projection_bytes(&self) -> usize {
        self.projection.bytes()
    }
    pub fn source_run_bytes(&self) -> usize {
        self.projection.source_run_bytes()
    }
    pub fn shared_block_bytes(&self) -> usize {
        self.projection.block_bytes()
    }
    pub fn side_index_bytes(&self) -> usize {
        self.projection.side_index_bytes()
    }
    pub fn marker_index_bytes(&self) -> usize {
        self.projection.marker_bytes()
    }
    pub fn boundary_index_bytes(&self) -> usize {
        self.projection.boundary_bytes()
    }
    pub fn metadata_bytes(&self) -> usize {
        self.raw_path.len()
            + self.meta.hunks.len() * size_of::<HunkMetadata>()
            + self
                .meta
                .hunks
                .iter()
                .map(|h| h.header.len())
                .sum::<usize>()
            + [
                &self.path,
                &self.old_path,
                &self.status,
                &self.old_mode,
                &self.new_mode,
            ]
            .iter()
            .map(|s| s.len())
            .sum::<usize>()
    }

    fn selected_intervals<'a>(
        &'a self,
        old: bool,
        lines: RangeInclusive<u32>,
        check: &'a impl Fn() -> Result<()>,
    ) -> impl Iterator<Item = Result<(Range<usize>, u64)>> + 'a {
        let first = u64::from(*lines.start());
        let limit = u64::from(*lines.end()) + 1;
        self.projection
            .source_intervals(old)
            .filter_map(move |(ids, line)| {
                if let Err(error) = check() {
                    return Some(Err(error));
                }
                let line = u64::from(line?);
                let start = first.max(line);
                let end = limit.min(line + ids.len() as u64);
                (start < end).then(|| {
                    let base = self.source_base + ids.start;
                    Ok((
                        base + (start - line) as usize..base + (end - line) as usize,
                        start,
                    ))
                })
            })
    }

    pub(crate) fn write_source_lines(
        &self,
        old: bool,
        lines: RangeInclusive<u32>,
        output: &mut impl Write,
        check: &impl Fn() -> Result<()>,
    ) -> Result<u64> {
        ensure!(
            *lines.start() != 0 && lines.start() <= lines.end(),
            "Invalid source line range"
        );
        let limit = u64::from(*lines.end()) + 1;
        let mut next = u64::from(*lines.start());
        let mut bounds: Option<Range<usize>> = None;
        for interval in self.selected_intervals(old, lines.clone(), check) {
            let (ids, line) = interval?;
            check()?;
            ensure!(
                line == next,
                "Requested source range contains lines missing from the patch"
            );
            next += ids.len() as u64;
            if let Some(bounds) = &mut bounds {
                bounds.end = ids.end;
            } else {
                bounds = Some(ids);
            }
            if next == limit {
                break;
            }
        }
        check()?;
        ensure!(
            next == limit,
            "Requested source range contains lines missing from the patch"
        );
        let bounds = bounds.context("The requested side has no supplied source lines")?;
        let first = bounds.start - self.source_base;
        let last = bounds.end - self.source_base - 1;
        let (offset, _) = self
            .source
            .cursor(first..first + 1)?
            .next()
            .context("Missing first source span")??;
        let (end, len) = self
            .source
            .cursor(last..last + 1)?
            .next()
            .context("Missing last source span")??;
        let end = end
            .checked_add(len as u64)
            .context("Source span overflow")?;
        let len = end
            .checked_sub(offset)
            .context("Source spans are out of order")?;
        let mut body = BufReader::with_capacity(64 * 1024, self.spool.reader(offset, len)?);
        let mut position = offset;
        let mut written = 0u64;
        let mut intervals = self.selected_intervals(old, lines, check);
        let mut selected = intervals
            .next()
            .context("Missing selected source interval")??
            .0;
        let spans = self.source.cursor(first..last + 1)?;
        for (id, span) in bounds.zip(spans) {
            check()?;
            let (offset, len) = span?;
            if id >= selected.end {
                selected = intervals
                    .next()
                    .context("Missing selected source interval")??
                    .0;
            }
            if id < selected.start {
                continue;
            }
            let skip = offset
                .checked_sub(position)
                .context("Source spans overlap or are out of order")?;
            if skip > body.buffer().len() as u64 {
                let remaining = end
                    .checked_sub(offset)
                    .context("Source span exceeds selected range")?;
                body.consume(body.buffer().len());
                *body.get_mut() = self.spool.reader(offset, remaining)?;
            } else {
                body.consume(skip as usize);
            }
            copy_checked(&mut body, output, len as u64, check)?;
            position = offset
                .checked_add(len as u64)
                .context("Source span overflow")?;
            written = written
                .checked_add(len as u64)
                .context("Source output length overflow")?;
        }
        check()?;
        Ok(written)
    }

    pub fn write_canonical(&self, output: &mut impl Write) -> Result<()> {
        output.write_all(b"F")?;
        output.write_all(&(self.id as u64).to_le_bytes())?;
        let mut body = None;
        let mut position = 0;
        for hunk in &self.hunks {
            output.write_all(b"H")?;
            output.write_all(&u64::from(hunk.old_start).to_le_bytes())?;
            output.write_all(&u64::from(hunk.new_start).to_le_bytes())?;
            let spans = self
                .source
                .cursor(hunk.lines.start - self.source_base..hunk.lines.end - self.source_base)?;
            for (id, span) in hunk.lines.clone().zip(spans) {
                let info = self.info(id);
                let sign = match info.kind {
                    Kind::Context => b' ',
                    Kind::Addition => b'+',
                    Kind::Deletion => b'-',
                };
                let (offset, len) = span?;
                if body.is_none() {
                    let last = self.source.len() - 1;
                    let (end, len) = self
                        .source
                        .cursor(last..last + 1)?
                        .next()
                        .context("Missing final source span")??;
                    let length = (end + len as u64)
                        .checked_sub(offset)
                        .context("Patch source spans are out of order")?;
                    body = Some(BufReader::with_capacity(
                        64 * 1024,
                        self.spool.reader(offset, length)?,
                    ));
                    position = offset;
                }
                let body = body.as_mut().context("Missing canonical source reader")?;
                let skip = offset
                    .checked_sub(position)
                    .context("Patch source spans overlap or are out of order")?;
                copy_exact(body, &mut io::sink(), skip)
                    .context("Cannot advance to the next patch source span")?;
                output.write_all(&[b'L', sign])?;
                output.write_all(&u64::from(info.old.unwrap_or(0)).to_le_bytes())?;
                output.write_all(&u64::from(info.new.unwrap_or(0)).to_le_bytes())?;
                output.write_all(&[u8::from(info.no_newline)])?;
                output.write_all(&(len as u64).to_le_bytes())?;
                copy_exact(body, output, len as u64)
                    .context("Cannot copy the patch source span")?;
                position = offset + len as u64;
            }
        }
        Ok(())
    }
}

fn copy_exact(input: &mut impl BufRead, output: &mut impl Write, len: u64) -> Result<()> {
    copy_checked(input, output, len, &|| Ok(()))
}

pub(crate) fn copy_checked(
    input: &mut impl BufRead,
    output: &mut impl Write,
    mut len: u64,
    check: &impl Fn() -> Result<()>,
) -> Result<()> {
    while len != 0 {
        check()?;
        let bytes = match input.fill_buf() {
            Ok(bytes) => bytes,
            Err(error) if error.kind() == io::ErrorKind::Interrupted => continue,
            Err(error) => return Err(error.into()),
        };
        if bytes.is_empty() {
            return Err(io::Error::from(io::ErrorKind::UnexpectedEof).into());
        }
        let count = len.min(bytes.len() as u64) as usize;
        output.write_all(&bytes[..count])?;
        input.consume(count);
        len -= count as u64;
    }
    Ok(())
}

#[derive(Default, Debug)]
pub struct Document {
    pub files: Vec<Arc<FileIndex>>,
}

impl Document {
    pub fn append(&mut self, file: Arc<FileIndex>) -> Result<()> {
        ensure!(
            file.id == self.files.len()
                && file.source_base == self.source_len()
                && file.split_base == self.row_len(true)
                && file.unified_base == self.row_len(false),
            "Input worker published files out of order"
        );
        self.files.push(file);
        Ok(())
    }
    pub fn source_len(&self) -> usize {
        self.files.last().map_or(0, |f| f.source_end())
    }
    pub fn row_len(&self, split: bool) -> usize {
        self.files.last().map_or(0, |f| f.row_end(split))
    }
    pub fn file_for_source(&self, id: usize) -> &Arc<FileIndex> {
        &self.files[self.files.partition_point(|f| f.source_end() <= id)]
    }
    pub fn info(&self, id: usize) -> LineInfo {
        self.file_for_source(id).info(id)
    }
    pub fn bytes(&self, id: usize) -> Result<Vec<u8>> {
        self.file_for_source(id).bytes(id)
    }
    pub fn row(&self, index: usize, split: bool) -> Option<Row> {
        let f = self
            .files
            .get(self.files.partition_point(|f| f.row_end(split) <= index))?;
        f.rows(split).get(index - f.row_base(split))
    }
    pub fn row_for_source(&self, id: usize, split: bool) -> usize {
        self.file_for_source(id).row_for_source(id, split)
    }
    pub fn boundary(&self, row: usize, split: bool, forward: bool, file: bool) -> Option<usize> {
        let current = self.files.partition_point(|f| f.row_end(split) <= row);
        let f = self.files.get(current)?;
        if let Some(target) = f
            .rows(split)
            .boundary(row - f.row_base(split), forward, file)
        {
            return Some(f.row_base(split) + target);
        }
        if forward {
            self.files[current + 1..].iter().find_map(|f| {
                f.rows(split)
                    .first_boundary(true, file)
                    .map(|r| f.row_base(split) + r)
            })
        } else {
            self.files[..current].iter().rev().find_map(|f| {
                f.rows(split)
                    .first_boundary(false, file)
                    .map(|r| f.row_base(split) + r)
            })
        }
    }
}

pub fn read(
    input: impl Read,
    mut publish: impl FnMut(Arc<FileIndex>) -> Result<()>,
) -> Result<Arc<ByteStore>> {
    let writer = StoreWriter::new()?;
    read_with(input, writer, &|| Ok(()), |file, _| publish(file))
}

pub(crate) fn read_with(
    input: impl Read,
    writer: StoreWriter,
    check: &impl Fn() -> Result<()>,
    mut publish: impl FnMut(Arc<FileIndex>, crate::model::PatchIdentity) -> Result<()>,
) -> Result<Arc<ByteStore>> {
    let spool = writer.store();
    let mut index = StoreWriter::sharing(&spool)?;
    let input = io::BufReader::new(Spooling { input, writer });
    let (mut file_id, mut source_base, mut split_base, mut unified_base) = (0, 0, 0, 0);
    // Text between file patches, such as commit headers, is skipped. Each unit carries
    // its own offset in the original stream, which the spool retains unchanged.
    let total = crate::ingest::visit_files(input, |unit_base, unit| {
        check()?;
        let mut parsed = Parsed::parse_file(unit)?;
        for mut meta in std::mem::take(&mut parsed.files) {
            let identity = std::mem::take(&mut meta.identity);
            let projection = Projection::new(&parsed, &meta);
            let range = meta.hunks.first().map_or(0, |h| h.lines.start)
                ..meta.hunks.last().map_or(0, |h| h.lines.end);
            let source = SourceSpans::new(
                &mut index,
                unit_base,
                parsed.lines[range.clone()]
                    .iter()
                    .map(|line| (line.offset as u64, line.bytes.len(), line.no_newline)),
                spool.len(),
                check,
            )?;
            for hunk in &mut meta.hunks {
                hunk.lines = (hunk.lines.start - range.start + source_base)
                    ..(hunk.lines.end - range.start + source_base);
            }
            let file = Arc::new(FileIndex {
                id: file_id,
                source_base,
                split_base,
                unified_base,
                meta: meta.into(),
                spool: spool.clone(),
                source,
                projection,
            });
            source_base = file.source_end();
            split_base = file.row_end(true);
            unified_base = file.row_end(false);
            file_id += 1;
            publish(file, identity)?;
        }
        Ok(())
    })?;
    ensure!(
        total == spool.len(),
        "Spool and parsed patch lengths disagree"
    );
    Ok(spool)
}