File size: 7,622 Bytes
1dbc34b | 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 | /**
* Shared diff parsing utilities.
*
* Extracted from commit-worktree-dialog, discard-worktree-changes-dialog,
* stash-changes-dialog and git-diff-panel to eliminate duplication.
*/
export interface ParsedDiffHunk {
header: string;
lines: {
type: 'context' | 'addition' | 'deletion' | 'header';
content: string;
lineNumber?: { old?: number; new?: number };
}[];
}
export interface ParsedFileDiff {
filePath: string;
hunks: ParsedDiffHunk[];
isNew?: boolean;
isDeleted?: boolean;
isRenamed?: boolean;
/** Pre-computed count of added lines across all hunks */
additions: number;
/** Pre-computed count of deleted lines across all hunks */
deletions: number;
}
/**
* Parse unified diff format into structured data.
*
* Note: The regex `diff --git a\/(.*?) b\/(.*)` uses a non-greedy match for
* the `a/` path and a greedy match for `b/`. This can mis-handle paths that
* literally contain " b/" or are quoted by git. In practice this covers the
* vast majority of real-world paths; exotic cases will fall back to "unknown".
*/
export function parseDiff(diffText: string): ParsedFileDiff[] {
if (!diffText) return [];
const files: ParsedFileDiff[] = [];
const lines = diffText.split('\n');
let currentFile: ParsedFileDiff | null = null;
let currentHunk: ParsedDiffHunk | null = null;
let oldLineNum = 0;
let newLineNum = 0;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (line.startsWith('diff --git')) {
if (currentFile) {
if (currentHunk) currentFile.hunks.push(currentHunk);
files.push(currentFile);
}
const match = line.match(/diff --git a\/(.*?) b\/(.*)/);
currentFile = {
filePath: match ? match[2] : 'unknown',
hunks: [],
additions: 0,
deletions: 0,
};
currentHunk = null;
continue;
}
if (line.startsWith('new file mode')) {
if (currentFile) currentFile.isNew = true;
continue;
}
if (line.startsWith('deleted file mode')) {
if (currentFile) currentFile.isDeleted = true;
continue;
}
if (line.startsWith('rename from') || line.startsWith('rename to')) {
if (currentFile) currentFile.isRenamed = true;
continue;
}
if (line.startsWith('index ') || line.startsWith('--- ') || line.startsWith('+++ ')) {
continue;
}
if (line.startsWith('@@')) {
if (currentHunk && currentFile) currentFile.hunks.push(currentHunk);
const hunkMatch = line.match(/@@ -(\d+)(?:,\d+)? \+(\d+)(?:,\d+)? @@/);
oldLineNum = hunkMatch ? parseInt(hunkMatch[1], 10) : 1;
newLineNum = hunkMatch ? parseInt(hunkMatch[2], 10) : 1;
currentHunk = {
header: line,
lines: [{ type: 'header', content: line }],
};
continue;
}
if (currentHunk) {
// Skip trailing empty line produced by split('\n') to avoid phantom context line
if (line === '' && i === lines.length - 1) {
continue;
}
if (line.startsWith('+')) {
currentHunk.lines.push({
type: 'addition',
content: line.substring(1),
lineNumber: { new: newLineNum },
});
newLineNum++;
if (currentFile) currentFile.additions++;
} else if (line.startsWith('-')) {
currentHunk.lines.push({
type: 'deletion',
content: line.substring(1),
lineNumber: { old: oldLineNum },
});
oldLineNum++;
if (currentFile) currentFile.deletions++;
} else if (line.startsWith(' ') || line === '') {
currentHunk.lines.push({
type: 'context',
content: line.substring(1) || '',
lineNumber: { old: oldLineNum, new: newLineNum },
});
oldLineNum++;
newLineNum++;
}
}
}
if (currentFile) {
if (currentHunk) currentFile.hunks.push(currentHunk);
files.push(currentFile);
}
return files;
}
/**
* Reconstruct old (original) and new (modified) file content from a single-file
* unified diff string. Used by the CodeMirror merge diff viewer which needs
* both document versions to compute inline highlighting.
*
* For new files (entire content is additions), oldContent will be empty.
* For deleted files (entire content is deletions), newContent will be empty.
*/
export function reconstructFilesFromDiff(diffText: string): {
oldContent: string;
newContent: string;
} {
if (!diffText) return { oldContent: '', newContent: '' };
const lines = diffText.split('\n');
const oldLines: string[] = [];
const newLines: string[] = [];
let inHunk = false;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
// Skip diff header lines
if (
line.startsWith('diff --git') ||
line.startsWith('index ') ||
line.startsWith('--- ') ||
line.startsWith('+++ ') ||
line.startsWith('new file mode') ||
line.startsWith('deleted file mode') ||
line.startsWith('rename from') ||
line.startsWith('rename to') ||
line.startsWith('similarity index') ||
line.startsWith('old mode') ||
line.startsWith('new mode')
) {
continue;
}
// Hunk header
if (line.startsWith('@@')) {
inHunk = true;
continue;
}
if (!inHunk) continue;
// Skip trailing empty line produced by split('\n')
if (line === '' && i === lines.length - 1) {
continue;
}
// "\ No newline at end of file" marker
if (line.startsWith('\\')) {
continue;
}
if (line.startsWith('+')) {
newLines.push(line.substring(1));
} else if (line.startsWith('-')) {
oldLines.push(line.substring(1));
} else {
// Context line (starts with space or is empty within hunk)
const content = line.startsWith(' ') ? line.substring(1) : line;
oldLines.push(content);
newLines.push(content);
}
}
return {
oldContent: oldLines.join('\n'),
newContent: newLines.join('\n'),
};
}
/**
* Split a combined multi-file diff string into per-file diff strings.
* Each entry in the returned array is a complete diff block for a single file.
*/
export function splitDiffByFile(
combinedDiff: string
): { filePath: string; diff: string; isNew: boolean; isDeleted: boolean }[] {
if (!combinedDiff) return [];
const results: { filePath: string; diff: string; isNew: boolean; isDeleted: boolean }[] = [];
const lines = combinedDiff.split('\n');
let currentLines: string[] = [];
let currentFilePath = '';
let currentIsNew = false;
let currentIsDeleted = false;
for (const line of lines) {
if (line.startsWith('diff --git')) {
// Push previous file if exists
if (currentLines.length > 0 && currentFilePath) {
results.push({
filePath: currentFilePath,
diff: currentLines.join('\n'),
isNew: currentIsNew,
isDeleted: currentIsDeleted,
});
}
currentLines = [line];
const match = line.match(/diff --git a\/(.*?) b\/(.*)/);
currentFilePath = match ? match[2] : 'unknown';
currentIsNew = false;
currentIsDeleted = false;
} else {
if (line.startsWith('new file mode')) currentIsNew = true;
if (line.startsWith('deleted file mode')) currentIsDeleted = true;
currentLines.push(line);
}
}
// Push last file
if (currentLines.length > 0 && currentFilePath) {
results.push({
filePath: currentFilePath,
diff: currentLines.join('\n'),
isNew: currentIsNew,
isDeleted: currentIsDeleted,
});
}
return results;
}
|