Skip to content

Commit aad1782

Browse files
committed
fix: handle empty file added/deleted
1 parent e8ed56d commit aad1782

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

src/__fixtures__/added-empty-file

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
diff --git a/src/empty b/src/empty
2+
new file mode 100644
3+
index 0000000..e69de29
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`added-empty-file parse \`added-empty-file\` 1`] = `
4+
{
5+
"files": [
6+
{
7+
"chunks": [],
8+
"path": "src/empty",
9+
"type": "AddedFile",
10+
},
11+
],
12+
"type": "GitDiff",
13+
}
14+
`;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { getFixture } from './test-utils';
2+
import parseGitDiff from '../parse-git-diff';
3+
4+
describe('added-empty-file', () => {
5+
const fixture = getFixture('added-empty-file');
6+
7+
it('parse `added-empty-file`', () => {
8+
expect(parseGitDiff(fixture)).toMatchSnapshot();
9+
});
10+
});

src/parse-git-diff.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function parseFileChange(ctx: Context): AnyFileChange | undefined {
4646
if (!isComparisonInputLine(ctx.getCurLine())) {
4747
return;
4848
}
49-
ctx.nextLine();
49+
const comparisonLineParsed = pasreComparisonInputLine(ctx);
5050

5151
let isDeleted = false;
5252
let isNew = false;
@@ -55,11 +55,15 @@ function parseFileChange(ctx: Context): AnyFileChange | undefined {
5555
let pathAfter = '';
5656
while (!ctx.isEof()) {
5757
const extHeader = parseExtendedHeader(ctx);
58+
5859
if (!extHeader) {
5960
break;
6061
}
6162
if (extHeader.type === ExtendedHeader.Deleted) isDeleted = true;
62-
if (extHeader.type === ExtendedHeader.NewFile) isNew = true;
63+
if (extHeader.type === ExtendedHeader.NewFile) {
64+
isNew = true;
65+
pathAfter = comparisonLineParsed?.to || '';
66+
}
6367
if (extHeader.type === ExtendedHeader.RenameFrom) {
6468
isRename = true;
6569
pathBefore = extHeader.path as string;
@@ -89,11 +93,11 @@ function parseFileChange(ctx: Context): AnyFileChange | undefined {
8993
chunks,
9094
path: chunks[0].pathBefore,
9195
};
92-
} else if (isNew && changeMarkers) {
96+
} else if (isNew) {
9397
return {
9498
type: FileType.Added,
9599
chunks,
96-
path: changeMarkers.added,
100+
path: changeMarkers ? changeMarkers.added : pathAfter,
97101
};
98102
} else if (isNew && chunks.length && chunks[0].type === 'BinaryFilesChunk') {
99103
return {
@@ -133,6 +137,20 @@ function isComparisonInputLine(line: string): boolean {
133137
return line.indexOf('diff') === 0;
134138
}
135139

140+
function pasreComparisonInputLine(
141+
ctx: Context
142+
): { from: string; to: string } | null {
143+
const line = ctx.getCurLine();
144+
const splitted = line.split(' ').reverse();
145+
const to = splitted.find((p) => p.startsWith('b/'))?.replace('b/', '');
146+
const from = splitted.find((p) => p.startsWith('a/'))?.replace('a/', '');
147+
ctx.nextLine();
148+
if (to && from) {
149+
return { to, from };
150+
}
151+
return null;
152+
}
153+
136154
function parseChunks(context: Context): AnyChunk[] {
137155
const chunks: AnyChunk[] = [];
138156

0 commit comments

Comments
 (0)