MCPcopy Create free account
hub / github.com/HKUDS/OpenSpace / parseDiff

Function parseDiff

frontend/src/utils/diffParser.ts:20–85  ·  view source on GitHub ↗
(raw: string | null | undefined)

Source from the content-addressed store, hash-verified

18}
19
20export function parseDiff(raw: string | null | undefined): DiffFile[] {
21 if (!raw || typeof raw !== 'string' || raw.length === 0) {
22 return [];
23 }
24
25 const files: DiffFile[] = [];
26 let currentFile: DiffFile | null = null;
27 let awaitingNewFilePath = false;
28
29 const finalizeCurrentFile = () => {
30 if (currentFile && hasRenderableHunks(currentFile)) {
31 files.push(currentFile);
32 }
33 currentFile = null;
34 awaitingNewFilePath = false;
35 };
36
37 for (const line of raw.split('\n')) {
38 if (line.startsWith('--- a/')) {
39 finalizeCurrentFile();
40 currentFile = { path: line.slice(6), hunks: [] };
41 continue;
42 }
43
44 if (line === '--- /dev/null') {
45 finalizeCurrentFile();
46 awaitingNewFilePath = true;
47 continue;
48 }
49
50 if (line.startsWith('+++ b/')) {
51 if (awaitingNewFilePath || !currentFile) {
52 currentFile = { path: line.slice(6), hunks: [] };
53 }
54 awaitingNewFilePath = false;
55 continue;
56 }
57
58 if (line === '+++ /dev/null') {
59 awaitingNewFilePath = false;
60 continue;
61 }
62
63 if (line.startsWith('@@') && currentFile) {
64 currentFile.hunks.push({ header: line, lines: [] });
65 continue;
66 }
67
68 if (!currentFile || currentFile.hunks.length === 0) {
69 continue;
70 }
71
72 const hunk = currentFile.hunks[currentFile.hunks.length - 1];
73
74 if (line.startsWith('+')) {
75 hunk.lines.push({ type: 'add', text: line.slice(1) });
76 } else if (line.startsWith('-')) {
77 hunk.lines.push({ type: 'del', text: line.slice(1) });

Callers 1

SkillVersionDrawerFunction · 0.90

Calls 1

finalizeCurrentFileFunction · 0.85

Tested by

no test coverage detected