Skip to content
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
11 changes: 6 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* 解析 gitdiff 消息
*
* @param {string} source gitdiff消息内容
* @param {string} noPrefix flag indicating if diff was generated with --no-prefix
* @return {Object}
*/
parse: function (source) {
parse: function (source, noPrefix) {
var infos = [];
var stat = STAT_START;
var currentInfo;
Expand Down Expand Up @@ -102,16 +103,16 @@
var oldPath = simiLine.slice(spaceIndex + 1);
var newPath = lines[++i].slice(4); // next line must be "+++ xxx"
if (oldPath === '/dev/null') {
newPath = newPath.slice(2);
newPath = newPath.slice(!noPrefix ? 2 : 0);
currentInfoType = 'add';
}
else if (newPath === '/dev/null') {
oldPath = oldPath.slice(2);
oldPath = oldPath.slice(!noPrefix ? 2 : 0);
currentInfoType = 'delete';
} else {
currentInfoType = 'modify';
oldPath = oldPath.slice(2);
newPath = newPath.slice(2);
oldPath = oldPath.slice(!noPrefix ? 2 : 0);
newPath = newPath.slice(!noPrefix ? 2 : 0);
}

currentInfo.oldPath = oldPath;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "gitdiff-parser",
"version": "0.2.2",
"version": "0.2.3",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"devDependencies": {
"parse-diff": "0.4.0",
"jest": "^24.1.0"
"jest": "^24.1.0",
"parse-diff": "0.4.0"
},
"files": [
"index.js",
Expand Down
55 changes: 55 additions & 0 deletions test/git-no-prefix.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const path = require('path');
const fs = require('fs');
const parser = require('../index');

describe("git specific tests with no-prefix", () => {

const parse = (filename) => {
return parser.parse( fs.readFileSync(path.resolve(__dirname, "git", filename), 'utf-8'), true );
};

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

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

it("should have type rename", () => {
const diff = parse("mv-no-prefix.diff");
const file = diff[0];
expect(file.type).toBe("rename");
expect(file.oldPath).toBe("myApp/b.txt");
expect(file.newPath).toBe("myApp/c.txt");
});

it("should have type modify", () => {
const diff = parse("edit-no-prefix.diff");
const file = diff[0];
expect(file.type).toBe("modify");
expect(file.oldPath).toBe("myApp/a.txt");
expect(file.newPath).toBe("myApp/a.txt");
expect(file.oldMode).toBe('100644');
expect(file.newMode).toBe('100644');
});

it("should parse filename correctly if whitespace included", () => {
const diff = parse("edit-ws-no-prefix.diff");
const file = diff[0];
expect(file.type).toBe("modify");
expect(file.oldPath).toBe("myApp/a b/a.txt");
expect(file.newPath).toBe("myApp/a b/a.txt");
});

});
7 changes: 7 additions & 0 deletions test/git/add-no-prefix.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
diff --git myApp/a.txt myApp/a.txt
new file mode 100644
index 0000000..7898192
--- /dev/null
+++ myApp/a.txt
@@ -0,0 +1 @@
+a
7 changes: 7 additions & 0 deletions test/git/edit-no-prefix.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
diff --git myApp/a.txt myApp/a.txt
index 7898192..7e8a165 100644
--- myApp/a.txt
+++ myApp/a.txt
@@ -1 +1,2 @@
a
+a
7 changes: 7 additions & 0 deletions test/git/edit-ws-no-prefix.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
diff --git myApp/a b/a.txt myApp/a b/a.txt
index 7898192..7e8a165 100644
--- myApp/a b/a.txt
+++ myApp/a b/a.txt
@@ -1 +1,2 @@
a
+a
4 changes: 4 additions & 0 deletions test/git/mv-no-prefix.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
diff --git myApp/b.txt myApp/c.txt
similarity index 100%
rename from myApp/b.txt
rename to myApp/c.txt
7 changes: 7 additions & 0 deletions test/git/rm-no-prefix.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
diff --git myApp/a.txt myApp/a.txt
deleted file mode 100644
index 7898192..0000000
--- myApp/a.txt
+++ /dev/null
@@ -1 +0,0 @@
-a