Open History to see the last commit
1export type ParsedDiffLineType = "context" | "add" | "delete" | "meta" | "hunk";
2
3export interface ParsedDiffLine {
4 readonly id: string;
5 readonly type: ParsedDiffLineType;
6 readonly oldLine: number | null;
7 readonly newLine: number | null;
8 readonly content: string;
9}
10
11export interface ParsedDiffFile {
12 readonly id: string;
13 readonly oldPath: string | null;
14 readonly newPath: string | null;
15 readonly lines: ReadonlyArray<ParsedDiffLine>;
16}
17
18function parseHunkStart(
19 line: string,
20): { readonly oldLine: number; readonly newLine: number } | null {
21 const match = line.match(/^@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/);
22 if (!match) {
23 return null;
24 }
25
26 return {
27 oldLine: Number.parseInt(match[1] ?? "0", 10),
28 newLine: Number.parseInt(match[2] ?? "0", 10),
29 };
30}
31
32function parseDiffPath(line: string, prefix: "--- " | "+++ "): string | null {
33 if (!line.startsWith(prefix)) {
34 return null;
35 }
36 const raw = line.slice(prefix.length).trim();
37 if (raw === "/dev/null") {
38 return null;
39 }
40 return raw.replace(/^[ab]\//, "");
41}
42
43export function parseUnifiedDiff(diff: string): ReadonlyArray<ParsedDiffFile> {
44 const files: ParsedDiffFile[] = [];
45 let current: {
46 oldPath: string | null;
47 newPath: string | null;
48 lines: ParsedDiffLine[];
49 } | null = null;
50 let oldLine: number | null = null;
51 let newLine: number | null = null;
52
53 const pushCurrent = () => {
54 if (!current) {
55 return;
56 }
57 files.push({
58 id: `${current.oldPath ?? "null"}:${current.newPath ?? "null"}:${files.length}`,
59 oldPath: current.oldPath,
60 newPath: current.newPath,
61 lines: current.lines,
62 });
63 };
64
65 for (const rawLine of diff.replace(/\r\n/g, "\n").split("\n")) {
66 if (rawLine.startsWith("diff --git ")) {
67 pushCurrent();
68 const match = rawLine.match(/^diff --git a\/(.+) b\/(.+)$/);
69 current = {
70 oldPath: match?.[1] ?? null,
71 newPath: match?.[2] ?? null,
72 lines: [],
73 };
74 oldLine = null;
75 newLine = null;
76 continue;
77 }
78
79 if (!current) {
80 if (rawLine.trim().length === 0) {
81 continue;
82 }
83 current = { oldPath: null, newPath: null, lines: [] };
84 }
85
86 const oldPath = parseDiffPath(rawLine, "--- ");
87 if (oldPath !== null || rawLine === "--- /dev/null") {
88 current.oldPath = oldPath;
89 continue;
90 }
91
92 const newPath = parseDiffPath(rawLine, "+++ ");
93 if (newPath !== null || rawLine === "+++ /dev/null") {
94 current.newPath = newPath;
95 continue;
96 }
97
98 const hunk = parseHunkStart(rawLine);
99 if (hunk) {
100 oldLine = hunk.oldLine;
101 newLine = hunk.newLine;
102 current.lines.push({
103 id: `${current.lines.length}:hunk`,
104 type: "hunk",
105 oldLine: null,
106 newLine: null,
107 content: rawLine,
108 });
109 continue;
110 }
111
112 if (oldLine === null || newLine === null) {
113 current.lines.push({
114 id: `${current.lines.length}:meta`,
115 type: "meta",
116 oldLine: null,
117 newLine: null,
118 content: rawLine,
119 });
120 continue;
121 }
122
123 const marker = rawLine[0];
124 const content = rawLine.length > 0 ? rawLine.slice(1) : "";
125 if (marker === "+") {
126 current.lines.push({
127 id: `${current.lines.length}:add:${newLine}`,
128 type: "add",
129 oldLine: null,
130 newLine,
131 content,
132 });
133 newLine += 1;
134 } else if (marker === "-") {
135 current.lines.push({
136 id: `${current.lines.length}:delete:${oldLine}`,
137 type: "delete",
138 oldLine,
139 newLine: null,
140 content,
141 });
142 oldLine += 1;
143 } else {
144 current.lines.push({
145 id: `${current.lines.length}:context:${oldLine}:${newLine}`,
146 type: "context",
147 oldLine,
148 newLine,
149 content: marker === " " ? content : rawLine,
150 });
151 oldLine += 1;
152 newLine += 1;
153 }
154 }
155
156 pushCurrent();
157 return files.filter((file) => file.lines.length > 0 || file.oldPath || file.newPath);
158}