Skip to content

Commit ac9472f

Browse files
authored
Merge pull request #47 from fsahmad/feature/git-modes
Parse modes from git diffs
2 parents 147a32d + 5630958 commit ac9472f

File tree

4 files changed

+76
-6
lines changed

4 files changed

+76
-6
lines changed

__tests__/parse.spec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ index 123..456 789
2828
const file = files[0];
2929
expect(file.from).toBe("file");
3030
expect(file.to).toBe("file");
31+
expect(file.oldMode).toBe("789");
32+
expect(file.newMode).toBe("789");
3133
expect(file.deletions).toBe(1);
3234
expect(file.additions).toBe(1);
3335
expect(file.chunks.length).toBe(1);
@@ -85,6 +87,8 @@ Binary files a/Artsy_Tests/ReferenceImages/ARTopMenuViewControllerSpec/selects '
8587
expect(file.to).toBe(
8688
"Artsy_Tests/ReferenceImages/ARTopMenuViewControllerSpec/selects 'home' by default as ipad@2x.png"
8789
);
90+
expect(file.oldMode).toBe("100644");
91+
expect(file.newMode).toBe("100644");
8892
});
8993

9094
it("should parse diff with new file mode line", function () {
@@ -102,6 +106,7 @@ index 0000000..db81be4
102106
expect(files.length).toBe(1);
103107
const file = files[0];
104108
expect(file.new).toBeTruthy();
109+
expect(file.newMode).toBe("100644");
105110
expect(file.from).toBe("/dev/null");
106111
expect(file.to).toBe("test");
107112
expect(file.chunks[0].content).toBe("@@ -0,0 +1,2 @@");
@@ -125,6 +130,7 @@ index db81be4..0000000
125130
expect(files.length).toBe(1);
126131
const file = files[0];
127132
expect(file.deleted).toBeTruthy();
133+
expect(file.oldMode).toBe("100644");
128134
expect(file.from).toBe("test");
129135
expect(file.to).toBe("/dev/null");
130136
expect(file.chunks[0].content).toBe("@@ -1,2 +0,0 @@");
@@ -133,6 +139,35 @@ index db81be4..0000000
133139
expect(file.chunks[0].changes[1].content).toBe("-line2");
134140
});
135141

142+
it("should parse diff with old and new mode lines", function () {
143+
const diff = `\
144+
diff --git a/file b/file
145+
old mode 100644
146+
new mode 100755
147+
index 123..456
148+
--- a/file
149+
+++ b/file
150+
@@ -1,2 +1,2 @@
151+
- line1
152+
+ line2\
153+
`;
154+
const files = parse(diff);
155+
expect(files.length).toBe(1);
156+
const file = files[0];
157+
expect(file.oldMode).toBe("100644");
158+
expect(file.newMode).toBe("100755");
159+
expect(file.from).toBe("file");
160+
expect(file.to).toBe("file");
161+
expect(file.deletions).toBe(1);
162+
expect(file.additions).toBe(1);
163+
expect(file.chunks.length).toBe(1);
164+
const chunk = file.chunks[0];
165+
expect(chunk.content).toBe("@@ -1,2 +1,2 @@");
166+
expect(chunk.changes.length).toBe(2);
167+
expect(chunk.changes[0].content).toBe("- line1");
168+
expect(chunk.changes[1].content).toBe("+ line2");
169+
});
170+
136171
it("should parse diff with single line files", function () {
137172
const diff = `\
138173
diff --git a/file1 b/file1
@@ -154,6 +189,7 @@ index 0000000..db81be4
154189
expect(files.length).toBe(2);
155190
let file = files[0];
156191
expect(file.deleted).toBeTruthy();
192+
expect(file.oldMode).toBe("100644");
157193
expect(file.from).toBe("file1");
158194
expect(file.to).toBe("/dev/null");
159195
expect(file.chunks[0].content).toBe("@@ -1 +0,0 @@");
@@ -162,6 +198,7 @@ index 0000000..db81be4
162198
expect(file.chunks[0].changes[0].type).toBe("del");
163199
file = files[1];
164200
expect(file.new).toBeTruthy();
201+
expect(file.newMode).toBe("100644");
165202
expect(file.from).toBe("/dev/null");
166203
expect(file.to).toBe("file2");
167204
expect(file.chunks[0].content).toBe("@@ -0,0 +1 @@");
@@ -196,13 +233,17 @@ index 123..456 789
196233
let file = files[0];
197234
expect(file.from).toBe("file1");
198235
expect(file.to).toBe("file1");
236+
expect(file.oldMode).toBe("789");
237+
expect(file.newMode).toBe("789");
199238
expect(file.chunks[0].content).toBe("@@ -1,1 +1,1 @@");
200239
expect(file.chunks[0].changes.length).toBe(2);
201240
expect(file.chunks[0].changes[0].content).toBe("- line1");
202241
expect(file.chunks[0].changes[1].content).toBe("+ line2");
203242
file = files[1];
204243
expect(file.from).toBe("file2");
205244
expect(file.to).toBe("file2");
245+
expect(file.oldMode).toBe("789");
246+
expect(file.newMode).toBe("789");
206247
expect(file.chunks[0].content).toBe("@@ -1,1 +1,1 @@");
207248
expect(file.chunks[0].changes.length).toBe(2);
208249
expect(file.chunks[0].changes[0].content).toBe("- line1");
@@ -226,6 +267,8 @@ index 123..456 789
226267
const file = files[0];
227268
expect(file.from).toBe("file1");
228269
expect(file.to).toBe("file1");
270+
expect(file.oldMode).toBe("789");
271+
expect(file.newMode).toBe("789");
229272
const chunk = file.chunks[0];
230273
expect(chunk.content).toBe("@@ -1,1 +1,1 @@");
231274
expect(chunk.changes.length).toBe(4);
@@ -343,6 +386,7 @@ index 0000000..e6a2e28\
343386
const file = files[0];
344387
expect(file.from).toBe("/dev/null");
345388
expect(file.to).toBe("newFile.txt");
389+
expect(file.newMode).toBe("100644");
346390
});
347391

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

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

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

439486
expect(file1.from).toBe(`file1`);
440487
expect(file1.to).toBe(`file1`);
488+
expect(file1.oldMode).toBe("789");
489+
expect(file1.newMode).toBe("789");
441490
expect(file1.chunks[0].changes.length).toBe(3);
442491

443492
expect(file2.from).toBe(`file2`);
444493
expect(file2.to).toBe(`file2`);
494+
expect(file2.oldMode).toBe("789");
495+
expect(file2.newMode).toBe("789");
445496
expect(file2.chunks[0].changes.length).toBe(3);
446497
});
447498
});

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ declare namespace parseDiff {
77
additions: number;
88
from?: string;
99
to?: string;
10+
oldMode?: string;
11+
newMode?: string;
1012
index?: string[];
1113
deleted?: true;
1214
new?: true;

0 commit comments

Comments
 (0)