Skip to content

Fix parsing diffs for empty files #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
if (leftStr.indexOf('file mode') === 0) {
currentInfo[infoType === 'new' ? 'newMode' : 'oldMode'] = leftStr.slice(10);
}
currentInfoType = infoType === 'new' ? 'add' : 'delete';
var prevLine = lines[i - 1];
var fileName = prevLine.substring(prevLine.indexOf(' b/') + 3);
currentInfo.newPath = infoType === 'new' ? fileName : '/dev/null';
currentInfo.oldPath = infoType === 'new' ? '/dev/null' : fileName;
break;

case 'similarity':
Expand Down
18 changes: 18 additions & 0 deletions test/git.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ describe("git specific tests", () => {
expect(file.newMode).toBe('100644');
});

it("should have type add for empty files", () => {
const diff = parse("add-empty.diff");
const file = diff[0];
expect(file.type).toBe("add");
expect(file.oldPath).toBe("/dev/null");
expect(file.newPath).toBe("a.txt");
expect(file.newMode).toBe('100644');
});

it("should have type delete", () => {
const diff = parse("rm.diff");
const file = diff[0];
Expand All @@ -26,6 +35,15 @@ describe("git specific tests", () => {
expect(file.newPath).toBe("/dev/null");
});

it("should have type delete for empty files", () => {
const diff = parse("rm-empty.diff");
const file = diff[0];
expect(file.type).toBe("delete");
expect(file.oldPath).toBe("a.txt");
expect(file.oldMode).toBe('100644');
expect(file.newPath).toBe("/dev/null");
});

it("should have type rename", () => {
const diff = parse("mv.diff");
const file = diff[0];
Expand Down
3 changes: 3 additions & 0 deletions test/git/add-empty.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
diff --git a/a.txt b/a.txt
new file mode 100644
index 0000000..7898192
3 changes: 3 additions & 0 deletions test/git/rm-empty.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
diff --git a/a.txt b/a.txt
deleted file mode 100644
index 7898192..0000000