Skip to content

Commit 3497a68

Browse files
committed
fix: handle deleted empty files
1 parent aad1782 commit 3497a68

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

src/__fixtures__/deleted-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+
deleted file mode 100644
3+
index e69de29..0000000
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[`deleted-empty-file parse \`deleted-empty-file\` 1`] = `
4+
{
5+
"files": [
6+
{
7+
"chunks": [],
8+
"path": "src/empty",
9+
"type": "DeletedFile",
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('deleted-empty-file', () => {
5+
const fixture = getFixture('deleted-empty-file');
6+
7+
it('parse `deleted-empty-file`', () => {
8+
expect(parseGitDiff(fixture)).toMatchSnapshot();
9+
});
10+
});

src/parse-git-diff.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ function parseFileChange(ctx: Context): AnyFileChange | undefined {
5959
if (!extHeader) {
6060
break;
6161
}
62-
if (extHeader.type === ExtendedHeader.Deleted) isDeleted = true;
62+
if (extHeader.type === ExtendedHeader.Deleted) {
63+
isDeleted = true;
64+
pathBefore = comparisonLineParsed?.from || '';
65+
}
6366
if (extHeader.type === ExtendedHeader.NewFile) {
6467
isNew = true;
6568
pathAfter = comparisonLineParsed?.to || '';
@@ -77,33 +80,30 @@ function parseFileChange(ctx: Context): AnyFileChange | undefined {
7780
const changeMarkers = parseChangeMarkers(ctx);
7881
const chunks = parseChunks(ctx);
7982

80-
if (isDeleted && changeMarkers) {
83+
if (isDeleted && chunks.length && chunks[0].type === 'BinaryFilesChunk') {
8184
return {
8285
type: FileType.Deleted,
8386
chunks,
84-
path: changeMarkers.deleted,
87+
path: chunks[0].pathBefore,
8588
};
86-
} else if (
87-
isDeleted &&
88-
chunks.length &&
89-
chunks[0].type === 'BinaryFilesChunk'
90-
) {
89+
}
90+
if (isDeleted) {
9191
return {
9292
type: FileType.Deleted,
9393
chunks,
94-
path: chunks[0].pathBefore,
94+
path: changeMarkers?.deleted || pathBefore,
9595
};
96-
} else if (isNew) {
96+
} else if (isNew && chunks.length && chunks[0].type === 'BinaryFilesChunk') {
9797
return {
9898
type: FileType.Added,
9999
chunks,
100-
path: changeMarkers ? changeMarkers.added : pathAfter,
100+
path: chunks[0].pathAfter,
101101
};
102-
} else if (isNew && chunks.length && chunks[0].type === 'BinaryFilesChunk') {
102+
} else if (isNew) {
103103
return {
104104
type: FileType.Added,
105105
chunks,
106-
path: chunks[0].pathAfter,
106+
path: changeMarkers?.added || pathAfter,
107107
};
108108
} else if (isRename) {
109109
return {

0 commit comments

Comments
 (0)