Skip to content

Commit 223f811

Browse files
committed
Fixes wrong fileName/originalFileName sometimes
Reworks file status parsing -- uses consistent regex now
1 parent 97456ad commit 223f811

File tree

1 file changed

+50
-29
lines changed

1 file changed

+50
-29
lines changed

src/git/parsers/logParser.ts

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ interface LogEntry {
2727
}
2828

2929
const diffRegex = /diff --git a\/(.*) b\/(.*)/;
30+
const fileStatusRegex = /(\S)\S*\t([^\t\n]+)(?:\t(.+))?/;
3031
const logFileSimpleRegex = /^<r> (.*)\s*(?:(?:diff --git a\/(.*) b\/(.*))|(?:\S+\t([^\t\n]+)(?:\t(.+))?))/gm;
3132

3233
const emptyEntry: LogEntry = {};
@@ -68,6 +69,9 @@ export class GitLogParser {
6869
const commits: Map<string, GitLogCommit> = new Map();
6970
let truncationCount = maxCount;
7071

72+
let match;
73+
let renamedFileName;
74+
7175
while (true) {
7276
next = lines.next();
7377
if (next.done) break;
@@ -151,44 +155,60 @@ export class GitLogParser {
151155
if (line.startsWith('warning:')) continue;
152156

153157
if (type === GitCommitType.Branch) {
154-
const status = {
155-
status: line[0] as GitFileStatus,
156-
fileName: line.substring(1),
157-
originalFileName: undefined
158-
};
159-
this.parseFileName(status);
160-
161-
if (status.fileName) {
158+
match = fileStatusRegex.exec(line);
159+
if (match != null) {
162160
if (entry.files === undefined) {
163161
entry.files = [];
164162
}
165-
entry.files.push(status);
163+
164+
renamedFileName = match[3];
165+
if (renamedFileName !== undefined) {
166+
entry.files.push({
167+
status: match[1] as GitFileStatus,
168+
fileName: renamedFileName,
169+
originalFileName: match[2]
170+
});
171+
}
172+
else {
173+
entry.files.push({
174+
status: match[1] as GitFileStatus,
175+
fileName: match[2]
176+
});
177+
}
166178
}
167179
}
168-
else if (line.startsWith('diff')) {
169-
const matches = diffRegex.exec(line);
170-
if (matches != null) {
171-
let originalFileName;
172-
[, entry.fileName, originalFileName] = matches;
173-
if (entry.fileName !== originalFileName) {
174-
entry.originalFileName = originalFileName;
175-
entry.status = 'R';
180+
else {
181+
match = diffRegex.exec(line);
182+
if (match != null) {
183+
[, entry.originalFileName, entry.fileName] = match;
184+
if (entry.fileName === entry.originalFileName) {
185+
entry.originalFileName = undefined;
186+
entry.status = 'M';
176187
}
177188
else {
178-
entry.status = 'M';
189+
entry.status = 'R';
179190
}
180-
}
181191

182-
while (true) {
183-
next = lines.next();
184-
if (next.done || next.value === '</f>') break;
192+
while (true) {
193+
next = lines.next();
194+
if (next.done || next.value === '</f>') break;
195+
}
196+
break;
197+
}
198+
else {
199+
match = fileStatusRegex.exec(line);
200+
if (match != null) {
201+
entry.status = match[1] as GitFileStatus;
202+
renamedFileName = match[3];
203+
if (renamedFileName !== undefined) {
204+
entry.fileName = renamedFileName;
205+
entry.originalFileName = match[2];
206+
}
207+
else {
208+
entry.fileName = match[2];
209+
}
210+
}
185211
}
186-
break;
187-
}
188-
else {
189-
entry.status = line[0] as GitFileStatus;
190-
entry.fileName = line.substring(1);
191-
this.parseFileName(entry);
192212
}
193213
}
194214

@@ -287,7 +307,8 @@ export class GitLogParser {
287307
}
288308
}
289309

290-
const originalFileName = relativeFileName !== entry.fileName ? entry.fileName : undefined;
310+
const originalFileName =
311+
entry.originalFileName || (relativeFileName !== entry.fileName ? entry.fileName : undefined);
291312
if (type === GitCommitType.File) {
292313
entry.files = [
293314
{

0 commit comments

Comments
 (0)