fallow-output 3.48.0

Output contract types for fallow reports
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
//! One reader for unified diff text.
//!
//! The reader owns the header grammar, the path decoding and the hunk state.
//! It reads a file header only outside a hunk, so an added source line such
//! as `++count;` (which arrives as `+++count;`) stays an added line. It
//! decodes the C-quoted paths of git (`"b/src/caf\303\251.ts"`) and removes
//! the tab that git writes after a path with a space.

use std::borrow::Cow;

/// The header of one file section of a unified diff.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DiffFileHeader<'a> {
    /// Base path, from the `--- a/<path>` line.
    pub old_path: Option<Cow<'a, str>>,
    /// Head path, from the `+++ b/<path>` line. A section with rename lines
    /// and no `+++` line takes it from the `rename to` line.
    pub new_path: Option<Cow<'a, str>>,
    /// Base path of a rename. Set only when the section has a `rename from`
    /// line and a `rename to` line.
    pub renamed_from: Option<Cow<'a, str>>,
    /// The section has a `--- /dev/null` line.
    pub is_new: bool,
    /// The section has a `+++ /dev/null` line.
    pub is_deleted: bool,
}

/// One item of a unified diff, in text order.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DiffEvent<'a> {
    /// The header of a file section. It comes before the hunks of the file.
    File(DiffFileHeader<'a>),
    /// A `@@ -a,b +c,d @@` line. A side without a length has length 1.
    Hunk {
        /// First base line of the hunk (`a`).
        old_start: u64,
        /// Number of base lines in the hunk (`b`).
        old_len: u64,
        /// First head line of the hunk (`c`).
        new_start: u64,
        /// Number of head lines in the hunk (`d`).
        new_len: u64,
    },
    /// An added line, without its `+`.
    Added(&'a str),
    /// A removed line, without its `-`.
    Removed(&'a str),
    /// An unchanged line, without its leading space.
    Context(&'a str),
    /// A `\ No newline at end of file` line.
    NoNewline,
}

/// Read `diff` as a sequence of [`DiffEvent`]s.
#[must_use]
pub fn unified_diff_events(diff: &str) -> UnifiedDiffEvents<'_> {
    UnifiedDiffEvents {
        lines: diff.lines(),
        pending: None,
        queued: None,
        rename_from: None,
        in_hunk: false,
        old_left: 0,
        new_left: 0,
    }
}

/// The iterator of [`unified_diff_events`].
#[derive(Debug)]
pub struct UnifiedDiffEvents<'a> {
    lines: std::str::Lines<'a>,
    /// The header of the current section, until its first hunk.
    pending: Option<DiffFileHeader<'a>>,
    /// The hunk that follows a `File` event of the same `@@` line.
    queued: Option<DiffEvent<'a>>,
    /// The `rename from` path of the current section.
    rename_from: Option<Cow<'a, str>>,
    /// A `@@` line came after the last header line.
    in_hunk: bool,
    /// Base lines that the current hunk still expects.
    old_left: u64,
    /// Head lines that the current hunk still expects.
    new_left: u64,
}

impl<'a> Iterator for UnifiedDiffEvents<'a> {
    type Item = DiffEvent<'a>;

    fn next(&mut self) -> Option<DiffEvent<'a>> {
        if let Some(event) = self.queued.take() {
            return Some(event);
        }
        loop {
            let Some(line) = self.lines.next() else {
                // `take` clears `pending`, so the next call returns `None`.
                return self.pending.take().map(DiffEvent::File);
            };
            if self.in_hunk
                && let Some(event) = self.hunk_line(line)
            {
                return Some(event);
            }
            if let Some(event) = self.header_line(line) {
                return Some(event);
            }
        }
    }
}

impl<'a> UnifiedDiffEvents<'a> {
    /// Classify a line inside a hunk. `None` when the line is not hunk
    /// content. The counters decide only if a `--- ` or `+++ ` line is a
    /// header: a hunk that counts too few lines stays open.
    fn hunk_line(&mut self, line: &'a str) -> Option<DiffEvent<'a>> {
        match line.as_bytes().first() {
            Some(b'+') => {
                if self.new_left == 0 && line.starts_with("+++ ") {
                    return None;
                }
                self.new_left = self.new_left.saturating_sub(1);
                Some(DiffEvent::Added(&line[1..]))
            }
            Some(b'-') => {
                if self.old_left == 0 && line.starts_with("--- ") {
                    return None;
                }
                self.old_left = self.old_left.saturating_sub(1);
                Some(DiffEvent::Removed(&line[1..]))
            }
            Some(b' ') | None => {
                self.old_left = self.old_left.saturating_sub(1);
                self.new_left = self.new_left.saturating_sub(1);
                Some(DiffEvent::Context(line.get(1..).unwrap_or("")))
            }
            Some(b'\\') => Some(DiffEvent::NoNewline),
            Some(_) => None,
        }
    }

    fn header_line(&mut self, line: &'a str) -> Option<DiffEvent<'a>> {
        if line.starts_with("diff --git ") {
            self.in_hunk = false;
            self.rename_from = None;
            // `replace` stores the header of the new section and returns the
            // header of the section before it.
            return self
                .pending
                .replace(DiffFileHeader::default())
                .map(DiffEvent::File);
        }
        if let Some(rest) = line.strip_prefix("rename from ") {
            self.rename_from = decode_git_path(rest);
            return None;
        }
        if let Some(rest) = line.strip_prefix("rename to ") {
            if let (Some(from), Some(to)) = (self.rename_from.take(), decode_git_path(rest)) {
                let header = self.pending.get_or_insert_with(DiffFileHeader::default);
                header.renamed_from = Some(from);
                header.new_path = Some(to);
            }
            return None;
        }
        if let Some(rest) = line.strip_prefix("--- ") {
            self.in_hunk = false;
            let header = self.pending.get_or_insert_with(DiffFileHeader::default);
            match header_side(rest, "a/") {
                HeaderSide::DevNull => header.is_new = true,
                HeaderSide::Path(path) => header.old_path = Some(path),
                HeaderSide::Other => {}
            }
            return None;
        }
        if let Some(rest) = line.strip_prefix("+++ ") {
            self.in_hunk = false;
            let header = self.pending.get_or_insert_with(DiffFileHeader::default);
            match header_side(rest, "b/") {
                HeaderSide::DevNull => header.is_deleted = true,
                HeaderSide::Path(path) => header.new_path = Some(path),
                HeaderSide::Other => {}
            }
            return None;
        }
        if let Some(rest) = line.strip_prefix("@@ ") {
            let (old_start, old_len, new_start, new_len) = parse_hunk_header(rest)?;
            self.in_hunk = true;
            self.old_left = old_len;
            self.new_left = new_len;
            let hunk = DiffEvent::Hunk {
                old_start,
                old_len,
                new_start,
                new_len,
            };
            // `take` clears `pending`: each section gives one `File` event.
            return Some(match self.pending.take() {
                Some(header) => {
                    self.queued = Some(hunk);
                    DiffEvent::File(header)
                }
                None => hunk,
            });
        }
        line.starts_with('\\').then_some(DiffEvent::NoNewline)
    }
}

/// One side of a `---` or `+++` line.
enum HeaderSide<'a> {
    DevNull,
    Path(Cow<'a, str>),
    Other,
}

/// Decode the path of a `---` or `+++` line and strip its `a/` or `b/`
/// prefix. A quoted path ends at its closing quote. An unquoted path ends at
/// the first tab, which git writes after a path with a space.
fn header_side<'a>(rest: &'a str, prefix: &str) -> HeaderSide<'a> {
    if rest.starts_with('"') {
        let Some(decoded) = unquote_git_path(rest) else {
            return HeaderSide::Other;
        };
        return decoded
            .strip_prefix(prefix)
            .map_or(HeaderSide::Other, |path| {
                HeaderSide::Path(Cow::Owned(path.to_owned()))
            });
    }
    let text = rest.split_once('\t').map_or(rest, |(path, _)| path);
    if text == "/dev/null" {
        return HeaderSide::DevNull;
    }
    text.strip_prefix(prefix).map_or(HeaderSide::Other, |path| {
        HeaderSide::Path(Cow::Borrowed(path))
    })
}

/// Decode the path of a `rename from` or `rename to` line. `None` for a
/// quoted path that does not decode.
fn decode_git_path(text: &str) -> Option<Cow<'_, str>> {
    if text.starts_with('"') {
        return unquote_git_path(text).map(Cow::Owned);
    }
    Some(Cow::Borrowed(text))
}

/// Decode a path in the C quoting of git, from the opening quote to the
/// closing quote. Text after the closing quote is ignored. `None` for a path
/// with no closing quote or with an unknown escape.
fn unquote_git_path(quoted: &str) -> Option<String> {
    let mut chars = quoted.strip_prefix('"')?.bytes();
    let mut bytes = Vec::with_capacity(quoted.len());
    loop {
        match chars.next()? {
            b'"' => return Some(String::from_utf8_lossy(&bytes).into_owned()),
            b'\\' => {
                let byte = match chars.next()? {
                    b'\\' => b'\\',
                    b'"' => b'"',
                    b'a' => 0x07,
                    b'b' => 0x08,
                    b'f' => 0x0c,
                    b'n' => b'\n',
                    b'r' => b'\r',
                    b't' => b'\t',
                    b'v' => 0x0b,
                    first @ b'0'..=b'3' => {
                        let mut value = first - b'0';
                        for _ in 0..2 {
                            let digit = chars.next()?;
                            if !(b'0'..=b'7').contains(&digit) {
                                return None;
                            }
                            value = value * 8 + (digit - b'0');
                        }
                        value
                    }
                    _ => return None,
                };
                bytes.push(byte);
            }
            byte => bytes.push(byte),
        }
    }
}

/// Parse `-a[,b] +c[,d]` from the first two words of the text after `@@ `.
fn parse_hunk_header(rest: &str) -> Option<(u64, u64, u64, u64)> {
    let mut words = rest.split_whitespace();
    let (old_start, old_len) = parse_hunk_range(words.next()?.strip_prefix('-')?)?;
    let (new_start, new_len) = parse_hunk_range(words.next()?.strip_prefix('+')?)?;
    Some((old_start, old_len, new_start, new_len))
}

/// Parse `start,len`, or `start` with length 1.
fn parse_hunk_range(range: &str) -> Option<(u64, u64)> {
    match range.split_once(',') {
        Some((start, len)) => Some((start.parse().ok()?, len.parse().ok()?)),
        None => Some((range.parse().ok()?, 1)),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn events(diff: &str) -> Vec<DiffEvent<'_>> {
        unified_diff_events(diff).collect()
    }

    fn headers(diff: &str) -> Vec<DiffFileHeader<'_>> {
        unified_diff_events(diff)
            .filter_map(|event| match event {
                DiffEvent::File(header) => Some(header),
                _ => None,
            })
            .collect()
    }

    fn header(old: Option<&str>, new: Option<&str>) -> DiffFileHeader<'static> {
        DiffFileHeader {
            old_path: old.map(|path| Cow::Owned(path.to_owned())),
            new_path: new.map(|path| Cow::Owned(path.to_owned())),
            ..DiffFileHeader::default()
        }
    }

    #[test]
    fn plus_plus_line_in_a_hunk_is_an_added_line() {
        let diff = "diff --git a/src/a.js b/src/a.js\n--- a/src/a.js\n+++ b/src/a.js\n@@ -1,0 +2,2 @@ let count = 0;\n+++count;\n+--count;\n";
        assert_eq!(
            events(diff),
            vec![
                DiffEvent::File(header(Some("src/a.js"), Some("src/a.js"))),
                DiffEvent::Hunk {
                    old_start: 1,
                    old_len: 0,
                    new_start: 2,
                    new_len: 2
                },
                DiffEvent::Added("++count;"),
                DiffEvent::Added("--count;"),
            ]
        );
    }

    #[test]
    fn minus_minus_line_in_a_hunk_is_a_removed_line() {
        let diff = "diff --git a/a.js b/a.js\n--- a/a.js\n+++ b/a.js\n@@ -1,2 +0,0 @@\n---count;\n--- a/other.js\n";
        let all = events(diff);
        assert_eq!(
            all.iter()
                .filter(|event| matches!(event, DiffEvent::File(_)))
                .count(),
            1
        );
        assert_eq!(
            &all[2..],
            &[
                DiffEvent::Removed("--count;"),
                DiffEvent::Removed("-- a/other.js")
            ]
        );
    }

    #[test]
    fn header_shaped_added_line_does_not_start_a_file() {
        let diff = "diff --git a/a.js b/a.js\n--- a/a.js\n+++ b/a.js\n@@ -0,0 +1,2 @@\n+++ b/other.js\n+x\n";
        let all = events(diff);
        assert_eq!(
            all.iter()
                .filter(|event| matches!(event, DiffEvent::File(_)))
                .count(),
            1
        );
        assert_eq!(
            &all[2..],
            &[DiffEvent::Added("++ b/other.js"), DiffEvent::Added("x")]
        );
    }

    #[test]
    fn quoted_path_is_decoded() {
        let diff =
            "--- \"a/src/caf\\303\\251.ts\"\n+++ \"b/src/caf\\303\\251.ts\"\n@@ -1,0 +2 @@\n+x2\n";
        assert_eq!(
            headers(diff),
            vec![header(Some("src/café.ts"), Some("src/café.ts"))]
        );
        let quote = headers("+++ \"b/src/q\\\"uote.ts\"\n");
        assert_eq!(quote[0].new_path.as_deref(), Some("src/q\"uote.ts"));
        let tab = headers("+++ \"b/src/tab\\tname.ts\"\n");
        assert_eq!(tab[0].new_path.as_deref(), Some("src/tab\tname.ts"));
    }

    #[test]
    fn tab_after_a_path_is_not_part_of_the_path() {
        let space = headers("+++ b/src/my file.ts\t\n");
        assert_eq!(space[0].new_path.as_deref(), Some("src/my file.ts"));
        let quoted = headers("--- \"a/src/del \\303\\251.ts\"\t\n");
        assert_eq!(quoted[0].old_path.as_deref(), Some("src/del Ă©.ts"));
    }

    #[test]
    fn rename_lines_set_the_old_and_new_path() {
        let diff = "diff --git a/src/old name.ts b/src/new name.ts\nsimilarity index 82%\nrename from src/old name.ts\nrename to src/new name.ts\n--- a/src/old name.ts\t\n+++ b/src/new name.ts\t\n@@ -6,0 +7 @@ six\n+seven\n";
        let found = headers(diff);
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].renamed_from.as_deref(), Some("src/old name.ts"));
        assert_eq!(found[0].new_path.as_deref(), Some("src/new name.ts"));

        let pure =
            "diff --git a/x.ts b/y.ts\nsimilarity index 100%\nrename from x.ts\nrename to y.ts\n";
        let found = headers(pure);
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].renamed_from.as_deref(), Some("x.ts"));
        assert_eq!(found[0].new_path.as_deref(), Some("y.ts"));

        let quoted =
            "diff --git a b\nrename from \"src/caf\\303\\251.ts\"\nrename to src/cafe.ts\n";
        let found = headers(quoted);
        assert_eq!(found[0].renamed_from.as_deref(), Some("src/café.ts"));
        assert_eq!(found[0].new_path.as_deref(), Some("src/cafe.ts"));
    }

    #[test]
    fn new_and_deleted_files_set_their_flags() {
        let added =
            headers("diff --git a/n.ts b/n.ts\n--- /dev/null\n+++ b/n.ts\n@@ -0,0 +1 @@\n+n\n");
        assert!(added[0].is_new);
        assert!(!added[0].is_deleted);
        assert_eq!(added[0].new_path.as_deref(), Some("n.ts"));

        let deleted =
            headers("diff --git a/d.ts b/d.ts\n--- a/d.ts\n+++ /dev/null\n@@ -1 +0,0 @@\n-d\n");
        assert!(deleted[0].is_deleted);
        assert!(!deleted[0].is_new);
        assert_eq!(deleted[0].old_path.as_deref(), Some("d.ts"));
        assert_eq!(deleted[0].new_path, None);
    }

    #[test]
    fn no_newline_marker_after_the_last_hunk_line_is_one_event() {
        let diff = "diff --git a/src/my file.ts b/src/my file.ts\n--- a/src/my file.ts\t\n+++ b/src/my file.ts\t\n@@ -1,0 +2 @@ y\n+y2\n\\ No newline at end of file\n";
        let all = events(diff);
        assert_eq!(all.last(), Some(&DiffEvent::NoNewline));
        assert_eq!(
            all.iter()
                .filter(|event| **event == DiffEvent::NoNewline)
                .count(),
            1
        );
    }

    #[test]
    fn second_file_without_a_git_header_starts_after_a_complete_hunk() {
        let diff = "--- a/x\n+++ b/x\n@@ -1 +1 @@\n-a\n+b\n--- a/y\n+++ b/y\n@@ -1 +1 @@\n-c\n+d\n";
        let paths: Vec<_> = headers(diff)
            .into_iter()
            .map(|header| header.new_path.map(Cow::into_owned))
            .collect();
        assert_eq!(paths, vec![Some("x".to_owned()), Some("y".to_owned())]);
    }

    #[test]
    fn hunk_header_that_counts_too_few_lines_keeps_the_hunk_open() {
        let diff = "--- a/x\n+++ b/x\n@@ -0,0 +1 @@\n+a\n+b\n+c\n";
        let added = events(diff)
            .into_iter()
            .filter(|event| matches!(event, DiffEvent::Added(_)))
            .count();
        assert_eq!(added, 3);
    }

    #[test]
    fn hunk_header_with_section_text_is_parsed() {
        let all = events("--- a/x\n+++ b/x\n@@ -6,0 +7 @@ six\n+seven\n");
        assert_eq!(
            all[1],
            DiffEvent::Hunk {
                old_start: 6,
                old_len: 0,
                new_start: 7,
                new_len: 1
            }
        );
    }
}