//! A unified diff (`git diff`, `diff -u`), read a line at a time: which //! file each section is about, what each line is, and where a hunk line //! stands in the old and the new file. The syntax painter (syntax.zig //! highlightDiff) and a look on a diff line (look.zig) both walk it. //! //! A hunk's extent comes from the counts in its `@@` header, as patch //! reads it, so a removed line that itself starts `--` (`--- x`) is not //! taken for a file header. const std = @import("std"); pub const Kind = enum { /// Anything outside a file header and a hunk: a commit message, /// `Only in ...`, a mail's headers. other, /// `diff ...`, `index ...`, `new file mode ...`, `rename from ...`, /// `Binary files ... differ`: said of the section, naming no line. meta, /// `--- ` old_path, /// `+++ ` new_path, /// `@@ -a,b +c,d @@` hunk, context, added, removed, /// `\ No newline at end of file` no_newline, }; /// One line, walked. pub const Line = struct { kind: Kind, /// Where it is in the old and the new file, from 1. A hunk header has /// its first lines; a removed line the new line now standing where it /// was (the one after it); an added line the old line after it. old: usize = 0, new: usize = 0, }; pub const Walk = struct { /// The section's paths, borrowed from the lines walked: timestamps /// cut, and in a git section git's own side prefix (`strip`) dropped, /// so they are the repository's names. old_path: []const u8 = "", new_path: []const u8 = "", /// `diff --git a/x b/y`'s new side, for a section with no `+++` yet /// (a mode change, a rename, a binary file). git_path: []const u8 = "", /// The section's `diff --git` line has git's side prefixes on both its /// paths (gitHeader), so its `---`/`+++` paths have them too. strip: bool = false, old_left: usize = 0, new_left: usize = 0, old_at: usize = 0, new_at: usize = 0, /// The hunk's header numbers, which a look past its end clamps to. new_start: usize = 0, new_count: usize = 0, /// Rows a terminal wrapped: inside a hunk, a row that starts with no /// prefix at all continues the line above instead of ending the hunk. continued_rows: bool = false, last: Kind = .other, pub fn inHunk(w: *const Walk) bool { return w.old_left > 0 or w.new_left > 0; } /// The file the section is about: the new one, else (deleted) the old /// one, else what its `diff --git` line names. pub fn path(w: *const Walk) []const u8 { if (w.new_path.len > 0 and !isDevNull(w.new_path)) return w.new_path; if (w.old_path.len > 0 and !isDevNull(w.old_path)) return w.old_path; return w.git_path; } /// Whether the section deletes its file (`+++ /dev/null`). pub fn deleted(w: *const Walk) bool { return isDevNull(w.new_path); } /// A `---`/`+++` path as the repository names it: git's side prefix /// dropped in a section whose `diff --git` line has them. pub fn side(w: *const Walk, path_raw: []const u8) []const u8 { if (w.strip and !isDevNull(path_raw)) return prefixed(path_raw) orelse path_raw; return path_raw; } pub fn step(w: *Walk, line_raw: []const u8) Line { const line = std.mem.trimEnd(u8, line_raw, "\r"); const got = w.stepInner(line); w.last = got.kind; return got; } fn stepInner(w: *Walk, line: []const u8) Line { if (w.inHunk()) { // A blank context line, its space stripped by a mailer or an // editor, is still context. const c: u8 = if (line.len == 0) ' ' else line[0]; switch (c) { ' ' => if (w.old_left > 0 and w.new_left > 0) { const at: Line = .{ .kind = .context, .old = w.old_at, .new = w.new_at }; w.old_left -= 1; w.new_left -= 1; w.old_at += 1; w.new_at += 1; return at; }, '-' => if (w.old_left > 0) { const at: Line = .{ .kind = .removed, .old = w.old_at, .new = w.new_at }; w.old_left -= 1; w.old_at += 1; return at; }, '+' => if (w.new_left > 0) { const at: Line = .{ .kind = .added, .old = w.old_at, .new = w.new_at }; w.new_left -= 1; w.new_at += 1; return at; }, '\\' => return .{ .kind = .no_newline, .old = w.old_at, .new = w.new_at }, else => if (w.continued_rows and w.last != .hunk and w.last != .other) return .{ .kind = w.last, .old = w.old_at, .new = w.new_at }, } // Counts run out, or a line no hunk has: the hunk was cut short. w.old_left = 0; w.new_left = 0; } if (line.len > 0 and line[0] == '\\' and w.last != .other and w.last != .meta) return .{ .kind = .no_newline, .old = w.old_at, .new = w.new_at }; if (std.mem.startsWith(u8, line, "diff ")) { w.* = .{ .continued_rows = w.continued_rows }; if (gitHeader(line)) |h| { w.git_path = h.new; w.strip = h.strip; } return .{ .kind = .meta }; } if (std.mem.startsWith(u8, line, "--- ")) { // A second `---` with no `+++` between starts another section // (plain `diff -u` output has no `diff` line of its own). w.* = .{ .continued_rows = w.continued_rows, .git_path = w.git_path, .strip = w.strip }; w.old_path = w.side(cutPath(line[4..])); return .{ .kind = .old_path }; } if (std.mem.startsWith(u8, line, "+++ ")) { w.new_path = w.side(cutPath(line[4..])); return .{ .kind = .new_path }; } if (parseHunk(line)) |h| { w.old_left = h.old_count; w.new_left = h.new_count; w.old_at = h.old_start; w.new_at = h.new_start; w.new_start = h.new_start; w.new_count = h.new_count; return .{ .kind = .hunk, .old = h.old_start, .new = h.new_start }; } for (meta_words) |word| if (std.mem.startsWith(u8, line, word)) return .{ .kind = .meta }; return .{ .kind = .other }; } }; const meta_words = [_][]const u8{ "index ", "new file mode ", "deleted file mode ", "old mode ", "new mode ", "similarity index ", "rename from ", "rename to ", "copy from ", "copy to ", "Binary files ", "dissimilarity ", }; fn isDevNull(path: []const u8) bool { return std.mem.eql(u8, path, "/dev/null"); } /// A `---`/`+++` path with what follows it cut: `diff -u`'s tab and /// timestamp, and git's quotes round a name it had to escape. fn cutPath(rest: []const u8) []const u8 { var name = rest[0 .. std.mem.indexOfScalar(u8, rest, '\t') orelse stampAt(rest) orelse rest.len]; name = std.mem.trimEnd(u8, name, " \r"); if (name.len >= 2 and name[0] == '"' and name[name.len - 1] == '"') name = name[1 .. name.len - 1]; return name; } /// Where `diff -u`'s timestamp starts when a terminal turned the tab before /// it into blanks (a command pane's output): the start of the blanks that /// run up to one. A tab may become a single blank, so one is enough; the /// stamp itself is strict, so a name with blanks in it is not cut. fn stampAt(rest: []const u8) ?usize { var i: usize = 0; while (std.mem.indexOfScalarPos(u8, rest, i, ' ')) |blank| { var end = blank; while (end < rest.len and rest[end] == ' ') end += 1; if (isStamp(rest[end..])) return blank; i = end; } return null; } /// `2026-09-30 10:00:00...` (GNU diff) or `Wed Sep 30 10:00:00 2026` (ctime). fn isStamp(t: []const u8) bool { const d = std.ascii.isDigit; if (t.len >= 19 and d(t[0]) and d(t[1]) and d(t[2]) and d(t[3]) and t[4] == '-' and d(t[5]) and d(t[6]) and t[7] == '-' and d(t[8]) and d(t[9]) and t[10] == ' ' and d(t[11]) and d(t[12]) and t[13] == ':' and d(t[14]) and d(t[15]) and t[16] == ':' and d(t[17]) and d(t[18])) return true; if (t.len < 24 or t[3] != ' ' or t[7] != ' ') return false; for ([_][]const u8{ "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }) |day| if (std.mem.eql(u8, t[0..3], day)) { const clock = t[11..19]; return (d(t[8]) or t[8] == ' ') and d(t[9]) and t[10] == ' ' and d(clock[0]) and d(clock[1]) and clock[2] == ':' and d(clock[3]) and d(clock[4]) and clock[5] == ':'; }; return false; } /// The one-letter side prefixes git writes: `a/`/`b/`, and with /// `diff.mnemonicPrefix` `c/` (commit), `i/` (index), `w/` (work tree) and /// `o/` (object). const side_letters = "abciow"; /// `path` past a side prefix, or null when it has none. fn prefixed(path: []const u8) ?[]const u8 { if (path.len > 2 and path[1] == '/' and std.mem.indexOfScalar(u8, side_letters, path[0]) != null) return path[2..]; return null; } pub const GitHeader = struct { /// The new side, its prefix dropped when `strip`. new: []const u8, /// Both paths carry a side prefix, and different ones, as git writes /// them (`a/x b/x`, `c/x w/x`, `i/x w/x`). `--no-prefix` writes none, /// so `a/x a/x` there is a real directory `a`, kept. strip: bool, }; /// `diff --git `, split where both sides are prefixed (the /// names equal, else the last such split), else where the halves are /// equal (`--no-prefix`), else at the last blank. A name with a blank in /// it splits wrongly only in a rename, and only on a matching prefix. pub fn gitHeader(line_raw: []const u8) ?GitHeader { const line = std.mem.trimEnd(u8, line_raw, "\r "); if (!std.mem.startsWith(u8, line, "diff --git ")) return null; const rest = line["diff --git ".len..]; var split: ?usize = null; var at: usize = 0; while (std.mem.indexOfScalarPos(u8, rest, at, ' ')) |blank| : (at = blank + 1) { const old = rest[0..blank]; const new = rest[blank + 1 ..]; const o = prefixed(old) orelse continue; const n = prefixed(new) orelse continue; if (old[0] == new[0]) continue; split = blank; if (std.mem.eql(u8, o, n)) break; } if (split) |blank| return .{ .new = rest[blank + 3 ..], .strip = true }; if (rest.len % 2 == 1 and rest[rest.len / 2] == ' ' and std.mem.eql(u8, rest[0 .. rest.len / 2], rest[rest.len / 2 + 1 ..])) return .{ .new = rest[rest.len / 2 + 1 ..], .strip = false }; const blank = std.mem.lastIndexOfScalar(u8, rest, ' ') orelse return .{ .new = rest, .strip = false }; return .{ .new = rest[blank + 1 ..], .strip = false }; } pub const HunkHeader = struct { old_start: usize, old_count: usize, new_start: usize, new_count: usize }; /// `@@ -a[,b] +c[,d] @@ ...`, a count left out meaning one. pub fn parseHunk(line: []const u8) ?HunkHeader { if (!std.mem.startsWith(u8, line, "@@ -")) return null; var at: usize = "@@ -".len; const old = range(line, &at) orelse return null; if (at + 1 >= line.len or line[at] != ' ' or line[at + 1] != '+') return null; at += 2; const new = range(line, &at) orelse return null; if (!std.mem.startsWith(u8, line[at..], " @@")) return null; return .{ .old_start = old[0], .old_count = old[1], .new_start = new[0], .new_count = new[1] }; } fn range(line: []const u8, at: *usize) ?[2]usize { const start = number(line, at) orelse return null; if (at.* < line.len and line[at.*] == ',') { at.* += 1; return .{ start, number(line, at) orelse return null }; } return .{ start, 1 }; } fn number(line: []const u8, at: *usize) ?usize { const from = at.*; var n: usize = 0; while (at.* < line.len and std.ascii.isDigit(line[at.*])) : (at.* += 1) n = std.math.add(usize, std.math.mul(usize, n, 10) catch return null, line[at.*] - '0') catch return null; return if (at.* > from) n else null; } /// Where a walk that ends up at byte `pos` of `content` may safely start: /// the start of the nearest `diff ` line before it (no hunk line starts /// with a `d`), else the top. pub fn anchorBefore(content: []const u8, pos: usize) usize { var end = @min(pos, content.len); while (end > 0) { const nl = std.mem.lastIndexOfScalar(u8, content[0..end], '\n') orelse break; if (std.mem.startsWith(u8, content[nl + 1 ..], "diff ") and nl + 1 <= pos) return nl + 1; end = nl; } return 0; } /// The same for a list of lines: the nearest `diff ` line at or above `row`. pub fn anchorRow(lines: []const []const u8, row: usize) usize { var r = @min(row, lines.len); while (r > 0) { r -= 1; if (std.mem.startsWith(u8, lines[r], "diff ")) return r; } return 0; } /// Whether text a command printed is a diff: a `diff --git` line, or a /// `--- ` line with a `+++ ` line under it, in its first rows (after /// `git show`'s commit header, say). pub fn looksLikeDiff(lines: []const []const u8) bool { const n = @min(lines.len, 200); for (lines[0..n], 0..) |line, i| { if (std.mem.startsWith(u8, line, "diff --git ")) return true; if (std.mem.startsWith(u8, line, "--- ") and i + 1 < lines.len and std.mem.startsWith(u8, lines[i + 1], "+++ ") and i + 2 < lines.len and parseHunk(lines[i + 2]) != null) return true; } return false; } /// A look on one line of a diff: the file and line it names. pub const Target = struct { /// The line's own kind. kind: Kind, /// The repository's name in a git section (Walk.side), else as written. path: []const u8, /// From 1; 0 for the file alone (a `---`/`+++` line). line: usize, /// The look address for it, as one would select it by hand: `path` /// or `path:line`. pub fn address(t: Target, buf: []u8) ?[]const u8 { if (t.line == 0) return std.fmt.bufPrint(buf, "{s}", .{t.path}) catch null; return std.fmt.bufPrint(buf, "{s}:{d}", .{ t.path, t.line }) catch null; } }; /// What a look at `lines[row]` opens, or null for a line that is no part /// of a file section (a commit message, `index ...`). The whole line is /// the look's: a click on a `+++`, `---` or `@@` marker is one on its path /// or its numbers. /// /// `diff --git a/x b/y` opens y, and `+++ y` opens y. `--- x` opens x, /// unless the `+++` under it names another file (a rename, a new file, /// `diff -u old/x new/x`): then that one, the old name being the one /// likely gone. /// /// A removed line opens the new file at the line now standing where it /// was: the new line after it, or the hunk's last new line when it went /// from the end of the file. In a section that deletes its file there is /// no new file, so a removed or context line opens the old one at its old /// line (it is there while the diff is not applied). pub fn targetAt(lines: []const []const u8, row: usize, continued_rows: bool) ?Target { if (row >= lines.len) return null; var w: Walk = .{ .continued_rows = continued_rows }; var r = anchorRow(lines, row); var got: Line = .{ .kind = .other }; while (r <= row) : (r += 1) got = w.step(lines[r]); const line = std.mem.trimEnd(u8, lines[row], "\r"); switch (got.kind) { .other, .meta, .no_newline => { // `diff --git a/x b/y`: the file, as its `+++` would. if (got.kind == .meta and std.mem.startsWith(u8, line, "diff ") and w.git_path.len > 0) return .{ .kind = got.kind, .path = w.git_path, .line = 0 }; return null; }, .old_path, .new_path => { const own = w.side(cutPath(line[4..])); // `---` names the old file, but the new one on the `+++` under // it wins when they differ: a new file (`--- /dev/null`), a // rename, `diff -u old/x new/x`. The old name is the one // likely gone. if (got.kind == .old_path and row + 1 < lines.len and std.mem.startsWith(u8, lines[row + 1], "+++ ")) { const new = w.side(cutPath(std.mem.trimEnd(u8, lines[row + 1], "\r")[4..])); if (!isDevNull(new) and (isDevNull(own) or !std.mem.eql(u8, new, own))) return .{ .kind = got.kind, .path = new, .line = 0 }; } if (!isDevNull(own)) return .{ .kind = got.kind, .path = own, .line = 0 }; if (w.path().len == 0) return null; return .{ .kind = got.kind, .path = w.path(), .line = 0 }; }, .hunk => { if (w.path().len == 0) return null; if (w.deleted()) return .{ .kind = got.kind, .path = w.path(), .line = @max(1, got.old) }; return .{ .kind = got.kind, .path = w.path(), .line = @max(1, got.new) }; }, .context, .added, .removed => { if (w.path().len == 0) return null; if (w.deleted()) return .{ .kind = got.kind, .path = w.path(), .line = @max(1, got.old) }; var at = got.new; // Gone from the end of the file: nothing stands after it. if (got.kind == .removed and w.new_count > 0 and at >= w.new_start + w.new_count) at = w.new_start + w.new_count - 1; return .{ .kind = got.kind, .path = w.path(), .line = @max(1, at) }; }, } } // ---- tests ---- const testing = std.testing; fn splitLines(comptime text: []const u8) [std.mem.count(u8, text, "\n")][]const u8 { @setEvalBranchQuota(100_000); var out: [std.mem.count(u8, text, "\n")][]const u8 = undefined; var it = std.mem.splitScalar(u8, text, '\n'); for (&out) |*line| line.* = it.next().?; return out; } const git_diff = \\diff --git a/src/a.zig b/src/a.zig \\index 1111111..2222222 100644 \\--- a/src/a.zig \\+++ b/src/a.zig \\@@ -10,4 +10,4 @@ pub fn main() void { \\ const a = 1; \\-const b = 2; \\+const b = 3; \\+const c = 4; \\ const d = 5; \\--- x \\@@ -40,2 +41,2 @@ \\ keep(); \\-gone(); \\+added(); \\diff --git a/old.py b/old.py \\deleted file mode 100644 \\--- a/old.py \\+++ /dev/null \\@@ -1,2 +0,0 @@ \\-def f(): \\- return 1 \\ ; test "diff every line of a section that deletes its file is about a file this diff deletes" { const lines = splitLines(git_diff); var rows: usize = 0; for (lines, 0..) |line, row| { if (row < 15) continue; // the deleting section: `diff --git a/old.py` on const t = targetAt(&lines, row, false) orelse continue; try testing.expectEqualStrings("old.py", t.path); _ = line; rows += 1; } // diff --git, ---, +++, @@ and the two removed lines. try testing.expectEqual(@as(usize, 6), rows); } test "diff a walk follows the hunk counts, so a removed `--` line is no header" { const lines = splitLines(git_diff); var w: Walk = .{}; var kinds: [lines.len]Kind = undefined; for (lines, 0..) |line, i| kinds[i] = w.step(line).kind; try testing.expectEqualSlices(Kind, &.{ .meta, .meta, .old_path, .new_path, .hunk, .context, .removed, .added, .added, .context, .removed, .hunk, .context, .removed, .added, .meta, .meta, .old_path, .new_path, .hunk, .removed, .removed, }, &kinds); } test "diff a look on each kind of line names the new file and its line" { const lines = splitLines(git_diff); const expect = struct { fn at(ls: []const []const u8, row: usize, path: []const u8, line: usize) !void { const t = targetAt(ls, row, false) orelse return error.NoTarget; try testing.expectEqualStrings(path, t.path); try testing.expectEqual(line, t.line); } }; try expect.at(&lines, 0, "src/a.zig", 0); // diff --git try testing.expect(targetAt(&lines, 1, false) == null); // index try expect.at(&lines, 2, "src/a.zig", 0); // --- try expect.at(&lines, 3, "src/a.zig", 0); // +++ try expect.at(&lines, 4, "src/a.zig", 10); // @@ try expect.at(&lines, 5, "src/a.zig", 10); // context try expect.at(&lines, 6, "src/a.zig", 11); // removed: the line now there try expect.at(&lines, 7, "src/a.zig", 11); // added try expect.at(&lines, 8, "src/a.zig", 12); try expect.at(&lines, 9, "src/a.zig", 13); // `--- x` is a removed line, gone from past the hunk's new lines. try expect.at(&lines, 10, "src/a.zig", 13); try expect.at(&lines, 11, "src/a.zig", 41); try expect.at(&lines, 12, "src/a.zig", 41); try expect.at(&lines, 13, "src/a.zig", 42); try expect.at(&lines, 14, "src/a.zig", 42); // A removed line of a deleted file: its old line in the old file. const gone = targetAt(&lines, 21, false).?; try testing.expectEqualStrings("old.py", gone.path); try testing.expectEqual(@as(usize, 2), gone.line); try testing.expect(targetAt(&lines, 1, false) == null); } test "diff a removed line at the end of a file opens the hunk's last new line" { const lines = splitLines( \\--- a/f.c \\+++ b/f.c \\@@ -1,3 +1,2 @@ \\ one \\ two \\-three \\ ); try testing.expectEqual(@as(usize, 2), targetAt(&lines, 5, false).?.line); } test "diff plain diff -u output: no a/ b/, timestamps after the names" { const lines = splitLines("--- old/x.zig\t2026-09-30 10:00:00.000000000 +0000\n" ++ "+++ new/x.zig\t2026-09-30 11:00:00.000000000 +0000\n" ++ "@@ -3 +3,2 @@\n" ++ "-const a = 1;\n" ++ "+const a = 2;\n" ++ "+const b = 3;\n"); try testing.expect(looksLikeDiff(&lines)); const t = targetAt(&lines, 5, false).?; try testing.expectEqualStrings("new/x.zig", t.path); try testing.expectEqual(@as(usize, 4), t.line); // `--- old/x.zig` with `+++ new/x.zig` under it: the new file. try testing.expectEqualStrings("new/x.zig", targetAt(&lines, 0, false).?.path); } test "diff -u in a command pane, its tabs turned to blanks by the terminal, names its files without the stamps" { // As a terminal shows `diff -u old/x.zig new/x.zig`: each tab gone to // the blanks up to the next tab stop, one of them a single blank. const lines = splitLines("--- old/x.zig 2026-09-30 10:00:00.000000000 +0000\n" ++ "+++ new/a b.zig 2026-09-30 11:00:00.000000000 +0000\n" ++ "@@ -3 +3,2 @@\n" ++ "-const a = 1;\n" ++ "+const a = 2;\n" ++ "+const b = 3;\n"); try testing.expect(looksLikeDiff(&lines)); try testing.expectEqualStrings("new/a b.zig", targetAt(&lines, 5, false).?.path); try testing.expectEqualStrings("x.zig", cutPath("x.zig Wed Sep 30 10:00:00 2026")); // A name with blanks and no stamp after them is whole. try testing.expectEqualStrings("my file 2026 notes.txt", cutPath("my file 2026 notes.txt")); } test "diff a --- line opens the new name when the +++ under it names another" { const lines = splitLines( \\diff --git a/old name.zig b/new name.zig \\similarity index 90% \\rename from old name.zig \\rename to new name.zig \\--- a/old name.zig \\+++ b/new name.zig \\@@ -1 +1 @@ \\-a \\+b \\ ); try testing.expectEqualStrings("new name.zig", targetAt(&lines, 0, false).?.path); try testing.expectEqualStrings("new name.zig", targetAt(&lines, 4, false).?.path); try testing.expectEqualStrings("new name.zig", targetAt(&lines, 5, false).?.path); try testing.expect(targetAt(&lines, 2, false) == null); } test "diff a new file's --- /dev/null opens the file it makes" { const lines = splitLines( \\--- /dev/null \\+++ b/made.zig \\@@ -0,0 +1 @@ \\+const x = 1; \\ ); try testing.expectEqualStrings("b/made.zig", targetAt(&lines, 0, false).?.path); try testing.expectEqual(@as(usize, 1), targetAt(&lines, 2, false).?.line); try testing.expectEqual(@as(usize, 1), targetAt(&lines, 3, false).?.line); } test "diff output is told from other text by its first rows" { const git = [_][]const u8{ "$ git diff", "diff --git a/x b/x", "index 1..2" }; try testing.expect(looksLikeDiff(&git)); const show = [_][]const u8{ "commit abc", "Author: a", "", " msg", "", "diff --git a/x b/x" }; try testing.expect(looksLikeDiff(&show)); const prose = [_][]const u8{ "--- a heading", "+++ not a path", "text" }; try testing.expect(!looksLikeDiff(&prose)); const nothing = [_][]const u8{ "ls", "a.zig b.zig" }; try testing.expect(!looksLikeDiff(¬hing)); } test "diff hunk headers: counts left out are one, a broken one is none" { try testing.expectEqual(HunkHeader{ .old_start = 3, .old_count = 1, .new_start = 4, .new_count = 1 }, parseHunk("@@ -3 +4 @@").?); try testing.expectEqual(HunkHeader{ .old_start = 1, .old_count = 0, .new_start = 0, .new_count = 0 }, parseHunk("@@ -1,0 +0,0 @@ fn x").?); try testing.expect(parseHunk("@@ -a +1 @@") == null); try testing.expect(parseHunk("@@ -1 +1") == null); try testing.expect(parseHunk("@@@ -1 -1 +1 @@@") == null); } test "diff a terminal's wrapped row stays in its line" { const lines = [_][]const u8{ "--- a/x.c", "+++ b/x.c", "@@ -1,2 +1,2 @@", "+int a = 1; /* a long", "comment */", " int b;", "-int c;" }; try testing.expectEqual(@as(usize, 2), targetAt(&lines, 5, true).?.line); var w: Walk = .{ .continued_rows = true }; var last: Kind = .other; for (lines[0..5]) |line| last = w.step(line).kind; try testing.expectEqual(Kind.added, last); } test "diff git's side prefixes: dropped when both sides carry different ones, else kept" { const cases = [_]struct { header: []const u8, new: []const u8, strip: bool }{ .{ .header = "diff --git a/src/x.zig b/src/x.zig", .new = "src/x.zig", .strip = true }, // diff.mnemonicPrefix: commit, index, work tree. .{ .header = "diff --git c/src/x.zig w/src/x.zig", .new = "src/x.zig", .strip = true }, .{ .header = "diff --git i/src/x.zig w/src/x.zig", .new = "src/x.zig", .strip = true }, .{ .header = "diff --git a/old name.zig b/new name.zig", .new = "new name.zig", .strip = true }, // --no-prefix: no prefix at all, and a real top-level `a/` kept. .{ .header = "diff --git src/x.zig src/x.zig", .new = "src/x.zig", .strip = false }, .{ .header = "diff --git a/x.zig a/x.zig", .new = "a/x.zig", .strip = false }, .{ .header = "diff --git a/b x a/b x", .new = "a/b x", .strip = false }, }; for (cases) |c| { const h = gitHeader(c.header).?; try testing.expectEqualStrings(c.new, h.new); try testing.expectEqual(c.strip, h.strip); } try testing.expect(gitHeader("diff -u old new") == null); } fn pathsOf(comptime text: []const u8) ![3][]const u8 { const lines = splitLines(text); var w: Walk = .{}; for (lines) |line| _ = w.step(line); const t = targetAt(&lines, 3, false) orelse return error.NoTarget; return .{ w.old_path, w.new_path, t.path }; } test "diff a section's paths are the repository's names under every prefix git writes, and as written elsewhere" { const expect = struct { fn paths(got: [3][]const u8, old: []const u8, new: []const u8) !void { try testing.expectEqualStrings(old, got[0]); try testing.expectEqualStrings(new, got[1]); try testing.expectEqualStrings(new, got[2]); } }; try expect.paths(try pathsOf("diff --git a/src/x.zig b/src/x.zig\n--- a/src/x.zig\n+++ b/src/x.zig\n@@ -1 +1 @@\n-a\n+b\n"), "src/x.zig", "src/x.zig"); try expect.paths(try pathsOf("diff --git c/src/x.zig w/src/x.zig\n--- c/src/x.zig\n+++ w/src/x.zig\n@@ -1 +1 @@\n-a\n+b\n"), "src/x.zig", "src/x.zig"); try expect.paths(try pathsOf("diff --git i/src/x.zig w/src/x.zig\n--- i/src/x.zig\n+++ w/src/x.zig\n@@ -1 +1 @@\n-a\n+b\n"), "src/x.zig", "src/x.zig"); // --no-prefix, in a repository with a real directory `a`. try expect.paths(try pathsOf("diff --git a/x.zig a/x.zig\n--- a/x.zig\n+++ a/x.zig\n@@ -1 +1 @@\n-a\n+b\n"), "a/x.zig", "a/x.zig"); try expect.paths(try pathsOf("diff --git src/x.zig src/x.zig\n--- src/x.zig\n+++ src/x.zig\n@@ -1 +1 @@\n-a\n+b\n"), "src/x.zig", "src/x.zig"); // Plain diff -u never strips, even a name that looks prefixed. try expect.paths(try pathsOf("--- old/x.zig\t2026-09-30 10:00:00 +0000\n+++ new/x.zig\t2026-09-30 11:00:00 +0000\n@@ -1 +1 @@\n-a\n"), "old/x.zig", "new/x.zig"); try expect.paths(try pathsOf("--- a/x.zig\n+++ b/x.zig\n@@ -1 +1 @@\n-a\n"), "a/x.zig", "b/x.zig"); // /dev/null stays itself. const made = try pathsOf("diff --git a/n.zig b/n.zig\n--- /dev/null\n+++ b/n.zig\n@@ -0,0 +1 @@\n+a\n"); try testing.expectEqualStrings("/dev/null", made[0]); try testing.expectEqualStrings("n.zig", made[1]); }