Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/__fixtures__/added-empty-file
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
diff --git a/src/empty b/src/empty
new file mode 100644
index 0000000..e69de29
3 changes: 3 additions & 0 deletions src/__fixtures__/deleted-empty-file
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
diff --git a/src/empty b/src/empty
deleted file mode 100644
index e69de29..0000000
4 changes: 4 additions & 0 deletions src/__fixtures__/renamed-empty
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
diff --git a/src/empty b/src/renamed-empty
similarity index 100%
rename from src/empty
rename to src/renamed-empty
14 changes: 14 additions & 0 deletions src/__tests__/__snapshots__/added-empty-file.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`added-empty-file parse \`added-empty-file\` 1`] = `
{
"files": [
{
"chunks": [],
"path": "src/empty",
"type": "AddedFile",
},
],
"type": "GitDiff",
}
`;
14 changes: 14 additions & 0 deletions src/__tests__/__snapshots__/deleted-empty-file.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`deleted-empty-file parse \`deleted-empty-file\` 1`] = `
{
"files": [
{
"chunks": [],
"path": "src/empty",
"type": "DeletedFile",
},
],
"type": "GitDiff",
}
`;
15 changes: 15 additions & 0 deletions src/__tests__/__snapshots__/renamed-empty.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`renamed-empty parse \`renamed-empty\` 1`] = `
{
"files": [
{
"chunks": [],
"pathAfter": "src/renamed-empty",
"pathBefore": "src/empty",
"type": "RenamedFile",
},
],
"type": "GitDiff",
}
`;
10 changes: 10 additions & 0 deletions src/__tests__/added-empty-file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getFixture } from './test-utils';
import parseGitDiff from '../parse-git-diff';

describe('added-empty-file', () => {
const fixture = getFixture('added-empty-file');

it('parse `added-empty-file`', () => {
expect(parseGitDiff(fixture)).toMatchSnapshot();
});
});
10 changes: 10 additions & 0 deletions src/__tests__/deleted-empty-file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getFixture } from './test-utils';
import parseGitDiff from '../parse-git-diff';

describe('deleted-empty-file', () => {
const fixture = getFixture('deleted-empty-file');

it('parse `deleted-empty-file`', () => {
expect(parseGitDiff(fixture)).toMatchSnapshot();
});
});
10 changes: 10 additions & 0 deletions src/__tests__/renamed-empty-file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getFixture } from './test-utils';
import parseGitDiff from '../parse-git-diff';

describe('renamed-empty', () => {
const fixture = getFixture('renamed-empty');

it('parse `renamed-empty`', () => {
expect(parseGitDiff(fixture)).toMatchSnapshot();
});
});
10 changes: 10 additions & 0 deletions src/__tests__/renamed-empty.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getFixture } from './test-utils';
import parseGitDiff from '../parse-git-diff';

describe('renamed-empty', () => {
const fixture = getFixture('renamed-empty');

it('parse `renamed-empty`', () => {
expect(parseGitDiff(fixture)).toMatchSnapshot();
});
});
48 changes: 33 additions & 15 deletions src/parse-git-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function parseFileChange(ctx: Context): AnyFileChange | undefined {
if (!isComparisonInputLine(ctx.getCurLine())) {
return;
}
ctx.nextLine();
const comparisonLineParsed = pasreComparisonInputLine(ctx);

let isDeleted = false;
let isNew = false;
Expand All @@ -55,11 +55,18 @@ function parseFileChange(ctx: Context): AnyFileChange | undefined {
let pathAfter = '';
while (!ctx.isEof()) {
const extHeader = parseExtendedHeader(ctx);

if (!extHeader) {
break;
}
if (extHeader.type === ExtendedHeader.Deleted) isDeleted = true;
if (extHeader.type === ExtendedHeader.NewFile) isNew = true;
if (extHeader.type === ExtendedHeader.Deleted) {
isDeleted = true;
pathBefore = comparisonLineParsed?.from || '';
}
if (extHeader.type === ExtendedHeader.NewFile) {
isNew = true;
pathAfter = comparisonLineParsed?.to || '';
}
if (extHeader.type === ExtendedHeader.RenameFrom) {
isRename = true;
pathBefore = extHeader.path as string;
Expand All @@ -73,33 +80,30 @@ function parseFileChange(ctx: Context): AnyFileChange | undefined {
const changeMarkers = parseChangeMarkers(ctx);
const chunks = parseChunks(ctx);

if (isDeleted && changeMarkers) {
if (isDeleted && chunks.length && chunks[0].type === 'BinaryFilesChunk') {
return {
type: FileType.Deleted,
chunks,
path: changeMarkers.deleted,
path: chunks[0].pathBefore,
};
} else if (
isDeleted &&
chunks.length &&
chunks[0].type === 'BinaryFilesChunk'
) {
}
if (isDeleted) {
return {
type: FileType.Deleted,
chunks,
path: chunks[0].pathBefore,
path: changeMarkers?.deleted || pathBefore,
};
} else if (isNew && changeMarkers) {
} else if (isNew && chunks.length && chunks[0].type === 'BinaryFilesChunk') {
return {
type: FileType.Added,
chunks,
path: changeMarkers.added,
path: chunks[0].pathAfter,
};
} else if (isNew && chunks.length && chunks[0].type === 'BinaryFilesChunk') {
} else if (isNew) {
return {
type: FileType.Added,
chunks,
path: chunks[0].pathAfter,
path: changeMarkers?.added || pathAfter,
};
} else if (isRename) {
return {
Expand Down Expand Up @@ -133,6 +137,20 @@ function isComparisonInputLine(line: string): boolean {
return line.indexOf('diff') === 0;
}

function pasreComparisonInputLine(
ctx: Context
): { from: string; to: string } | null {
const line = ctx.getCurLine();
const splitted = line.split(' ').reverse();
const to = splitted.find((p) => p.startsWith('b/'))?.replace('b/', '');
const from = splitted.find((p) => p.startsWith('a/'))?.replace('a/', '');
ctx.nextLine();
if (to && from) {
return { to, from };
}
return null;
}

function parseChunks(context: Context): AnyChunk[] {
const chunks: AnyChunk[] = [];

Expand Down
Loading