Skip to content

Commit

Permalink
Merge pull request #47 from fsahmad/feature/git-modes
Browse files Browse the repository at this point in the history
Parse modes from git diffs
  • Loading branch information
sergeyt authored Jan 19, 2023
2 parents 147a32d + 5630958 commit ac9472f
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
51 changes: 51 additions & 0 deletions __tests__/parse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ index 123..456 789
const file = files[0];
expect(file.from).toBe("file");
expect(file.to).toBe("file");
expect(file.oldMode).toBe("789");
expect(file.newMode).toBe("789");
expect(file.deletions).toBe(1);
expect(file.additions).toBe(1);
expect(file.chunks.length).toBe(1);
Expand Down Expand Up @@ -85,6 +87,8 @@ Binary files a/Artsy_Tests/ReferenceImages/ARTopMenuViewControllerSpec/selects '
expect(file.to).toBe(
"Artsy_Tests/ReferenceImages/ARTopMenuViewControllerSpec/selects 'home' by default as ipad@2x.png"
);
expect(file.oldMode).toBe("100644");
expect(file.newMode).toBe("100644");
});

it("should parse diff with new file mode line", function () {
Expand All @@ -102,6 +106,7 @@ index 0000000..db81be4
expect(files.length).toBe(1);
const file = files[0];
expect(file.new).toBeTruthy();
expect(file.newMode).toBe("100644");
expect(file.from).toBe("/dev/null");
expect(file.to).toBe("test");
expect(file.chunks[0].content).toBe("@@ -0,0 +1,2 @@");
Expand All @@ -125,6 +130,7 @@ index db81be4..0000000
expect(files.length).toBe(1);
const file = files[0];
expect(file.deleted).toBeTruthy();
expect(file.oldMode).toBe("100644");
expect(file.from).toBe("test");
expect(file.to).toBe("/dev/null");
expect(file.chunks[0].content).toBe("@@ -1,2 +0,0 @@");
Expand All @@ -133,6 +139,35 @@ index db81be4..0000000
expect(file.chunks[0].changes[1].content).toBe("-line2");
});

it("should parse diff with old and new mode lines", function () {
const diff = `\
diff --git a/file b/file
old mode 100644
new mode 100755
index 123..456
--- a/file
+++ b/file
@@ -1,2 +1,2 @@
- line1
+ line2\
`;
const files = parse(diff);
expect(files.length).toBe(1);
const file = files[0];
expect(file.oldMode).toBe("100644");
expect(file.newMode).toBe("100755");
expect(file.from).toBe("file");
expect(file.to).toBe("file");
expect(file.deletions).toBe(1);
expect(file.additions).toBe(1);
expect(file.chunks.length).toBe(1);
const chunk = file.chunks[0];
expect(chunk.content).toBe("@@ -1,2 +1,2 @@");
expect(chunk.changes.length).toBe(2);
expect(chunk.changes[0].content).toBe("- line1");
expect(chunk.changes[1].content).toBe("+ line2");
});

it("should parse diff with single line files", function () {
const diff = `\
diff --git a/file1 b/file1
Expand All @@ -154,6 +189,7 @@ index 0000000..db81be4
expect(files.length).toBe(2);
let file = files[0];
expect(file.deleted).toBeTruthy();
expect(file.oldMode).toBe("100644");
expect(file.from).toBe("file1");
expect(file.to).toBe("/dev/null");
expect(file.chunks[0].content).toBe("@@ -1 +0,0 @@");
Expand All @@ -162,6 +198,7 @@ index 0000000..db81be4
expect(file.chunks[0].changes[0].type).toBe("del");
file = files[1];
expect(file.new).toBeTruthy();
expect(file.newMode).toBe("100644");
expect(file.from).toBe("/dev/null");
expect(file.to).toBe("file2");
expect(file.chunks[0].content).toBe("@@ -0,0 +1 @@");
Expand Down Expand Up @@ -196,13 +233,17 @@ index 123..456 789
let file = files[0];
expect(file.from).toBe("file1");
expect(file.to).toBe("file1");
expect(file.oldMode).toBe("789");
expect(file.newMode).toBe("789");
expect(file.chunks[0].content).toBe("@@ -1,1 +1,1 @@");
expect(file.chunks[0].changes.length).toBe(2);
expect(file.chunks[0].changes[0].content).toBe("- line1");
expect(file.chunks[0].changes[1].content).toBe("+ line2");
file = files[1];
expect(file.from).toBe("file2");
expect(file.to).toBe("file2");
expect(file.oldMode).toBe("789");
expect(file.newMode).toBe("789");
expect(file.chunks[0].content).toBe("@@ -1,1 +1,1 @@");
expect(file.chunks[0].changes.length).toBe(2);
expect(file.chunks[0].changes[0].content).toBe("- line1");
Expand All @@ -226,6 +267,8 @@ index 123..456 789
const file = files[0];
expect(file.from).toBe("file1");
expect(file.to).toBe("file1");
expect(file.oldMode).toBe("789");
expect(file.newMode).toBe("789");
const chunk = file.chunks[0];
expect(chunk.content).toBe("@@ -1,1 +1,1 @@");
expect(chunk.changes.length).toBe(4);
Expand Down Expand Up @@ -343,6 +386,7 @@ index 0000000..e6a2e28\
const file = files[0];
expect(file.from).toBe("/dev/null");
expect(file.to).toBe("newFile.txt");
expect(file.newMode).toBe("100644");
});

it("should parse file names for a deleted file", function () {
Expand All @@ -356,6 +400,7 @@ index e6a2e28..0000000\
const file = files[0];
expect(file.from).toBe("deletedFile.txt");
expect(file.to).toBe("/dev/null");
expect(file.oldMode).toBe("100644");
});

it("should parse rename diff with space in path with no changes", function () {
Expand Down Expand Up @@ -411,6 +456,8 @@ index 9daeafb..88bd214 100644
const [file] = files;
expect(file.from).toBe(`file \\"space\\"`);
expect(file.to).toBe(`file \\"space\\"`);
expect(file.oldMode).toBe("100644");
expect(file.newMode).toBe("100644");
});

it("should parse files with additional '-' and '+'", function () {
Expand Down Expand Up @@ -438,10 +485,14 @@ index 123..456 789

expect(file1.from).toBe(`file1`);
expect(file1.to).toBe(`file1`);
expect(file1.oldMode).toBe("789");
expect(file1.newMode).toBe("789");
expect(file1.chunks[0].changes.length).toBe(3);

expect(file2.from).toBe(`file2`);
expect(file2.to).toBe(`file2`);
expect(file2.oldMode).toBe("789");
expect(file2.newMode).toBe("789");
expect(file2.chunks[0].changes.length).toBe(3);
});
});
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ declare namespace parseDiff {
additions: number;
from?: string;
to?: string;
oldMode?: string;
newMode?: string;
index?: string[];
deleted?: true;
new?: true;
Expand Down
Loading

0 comments on commit ac9472f

Please sign in to comment.