Skip to content

Commit e7b8dbd

Browse files
committed
Fixes issues with rename parsing
Using `-z` & `--numstat` changes the format for renames
1 parent a1f82ac commit e7b8dbd

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

src/git/parsers/diffParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export function parseGitApplyFiles(container: Container, data: string, repoPath:
195195
line = line.trim();
196196
if (!line) continue;
197197

198-
const match = /(rename) (.*?)\{(.+?)\s+=>\s+(.+?)\}(?: \(\d+%\))|(create|delete) mode \d+ (.+)/.exec(line);
198+
const match = /(rename) (.*?)\{?([^{]+?)\s+=>\s+(.+?)\}?(?: \(\d+%\))|(create|delete) mode \d+ (.+)/.exec(line);
199199
if (match == null) continue;
200200

201201
let [, rename, renameRoot, renameOriginalPath, renamePath, createOrDelete, createOrDeletePath] = match;

src/git/parsers/logParser.ts

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -385,32 +385,49 @@ export function createLogParserWithFilesAndStats<T extends Record<string, unknow
385385
if (fieldCount < keys.length) {
386386
entry[keys[fieldCount++]] = field.value as ParsedEntryWithFilesAndStats<T>[keyof T];
387387
} else {
388-
const [additions, deletions, path] = field.value.split('\t');
388+
let [additions, deletions, path] = field.value.split('\t');
389+
additions = additions.trim();
390+
deletions = deletions.trim();
391+
path = path.trim();
392+
393+
let originalPath;
394+
let status;
395+
// If we don't get a path it is likely a renamed file (because `-z` screws up the format)
396+
if (!path) {
397+
field = fields.next();
398+
path = field.value.trim();
399+
field = fields.next();
400+
originalPath = field.value.trim();
401+
status = 'R';
402+
} else {
403+
// Handle renamed files which show as path/to/file => new/path/to/file
404+
const renameIndex = path.indexOf(' => ');
405+
if (renameIndex !== -1) {
406+
originalPath = path.substring(0, renameIndex);
407+
path = path.substring(renameIndex + 4);
408+
status = 'R';
409+
}
410+
}
411+
389412
// Skip binary files which show as - for both additions and deletions
390413
if (additions === '-' && deletions === '-') continue;
391414

392415
const file: ParsedEntryFileWithStats = {
393416
status:
394-
additions === '0' && deletions === '0'
417+
status ??
418+
(additions === '0' && deletions === '0'
395419
? 'M'
396420
: additions === '0'
397421
? 'D'
398422
: deletions === '0'
399423
? 'A'
400-
: 'M',
424+
: 'M'),
401425
path: path,
426+
originalPath: originalPath,
402427
additions: additions === '-' ? 0 : parseInt(additions, 10),
403428
deletions: deletions === '-' ? 0 : parseInt(deletions, 10),
404429
};
405430

406-
// Handle renamed files which show as path/to/file => new/path/to/file
407-
if (path.includes(' => ')) {
408-
const [originalPath, newPath] = path.split(' => ');
409-
file.originalPath = originalPath;
410-
file.path = newPath;
411-
file.status = 'R';
412-
}
413-
414431
files.push(file);
415432
}
416433
}

0 commit comments

Comments
 (0)