Skip to content

Commit 6678d35

Browse files
committed
Parse modes from git diffs
1 parent e7d1e7f commit 6678d35

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");
@@ -225,6 +266,8 @@ index 123..456 789
225266
const file = files[0];
226267
expect(file.from).toBe("file1");
227268
expect(file.to).toBe("file1");
269+
expect(file.oldMode).toBe("789");
270+
expect(file.newMode).toBe("789");
228271
const chunk = file.chunks[0];
229272
expect(chunk.content).toBe("@@ -1,1 +1,1 @@");
230273
expect(chunk.changes.length).toBe(3);
@@ -340,6 +383,7 @@ index 0000000..e6a2e28\
340383
const file = files[0];
341384
expect(file.from).toBe("/dev/null");
342385
expect(file.to).toBe("newFile.txt");
386+
expect(file.newMode).toBe("100644");
343387
});
344388

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

358403
it("should parse rename diff with space in path with no changes", function () {
@@ -408,6 +453,8 @@ index 9daeafb..88bd214 100644
408453
const [file] = files;
409454
expect(file.from).toBe(`file \\"space\\"`);
410455
expect(file.to).toBe(`file \\"space\\"`);
456+
expect(file.oldMode).toBe("100644");
457+
expect(file.newMode).toBe("100644");
411458
});
412459

413460
it("should parse files with additional '-' and '+'", function () {
@@ -435,10 +482,14 @@ index 123..456 789
435482

436483
expect(file1.from).toBe(`file1`);
437484
expect(file1.to).toBe(`file1`);
485+
expect(file1.oldMode).toBe("789");
486+
expect(file1.newMode).toBe("789");
438487
expect(file1.chunks[0].changes.length).toBe(3);
439488

440489
expect(file2.from).toBe(`file2`);
441490
expect(file2.to).toBe(`file2`);
491+
expect(file2.oldMode).toBe("789");
492+
expect(file2.newMode).toBe("789");
442493
expect(file2.chunks[0].changes.length).toBe(3);
443494
});
444495
});

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)