Skip to content

Commit ec56e43

Browse files
committed
Merge pull request #1000 from smith-kyle/diff-merge
Added `git_diff_merge`
2 parents 377366c + a336802 commit ec56e43

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

generate/input/descriptor.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,15 @@
741741
"ignore": true
742742
},
743743
"git_diff_merge": {
744-
"ignore": true
744+
"isAsync": true,
745+
"args": {
746+
"onto": {
747+
"isSelf": true
748+
}
749+
},
750+
"return": {
751+
"isErrorCode": true
752+
}
745753
},
746754
"git_diff_num_deltas_of_type": {
747755
"ignore": true

test/tests/diff.js

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,31 @@
11
var assert = require("assert");
22
var path = require("path");
33
var promisify = require("promisify-node");
4+
var _ = require("lodash");
45
var fse = promisify(require("fs-extra"));
56
var local = path.join.bind(path, __dirname);
67

8+
function getLinesFromDiff(diff) {
9+
return diff.patches()
10+
.then(function(patches) {
11+
return Promise.all(_.map(patches, function(patch) {
12+
return patch.hunks();
13+
}));
14+
})
15+
.then(function(listsOfHunks) {
16+
var hunks = _.flatten(listsOfHunks);
17+
return Promise.all(_.map(hunks, function(hunk) {
18+
return hunk.lines();
19+
}));
20+
})
21+
.then(function(listsOfLines) {
22+
var lines = _.flatten(listsOfLines);
23+
return _.map(lines, function(line) {
24+
return line.content();
25+
});
26+
});
27+
}
28+
729
describe("Diff", function() {
830
var NodeGit = require("../../");
931
var Repository = NodeGit.Repository;
@@ -144,7 +166,7 @@ describe("Diff", function() {
144166
});
145167
});
146168

147-
it("can resolve individual line chages from the patch hunks", function() {
169+
it("can resolve individual line changes from the patch hunks", function() {
148170
return this.workdirDiff.patches()
149171
.then(function(patches) {
150172
var result = [];
@@ -326,6 +348,43 @@ describe("Diff", function() {
326348
});
327349
});
328350

351+
352+
it("can merge two diffs", function() {
353+
var linesOfFirstDiff;
354+
var linesOfSecondDiff;
355+
var firstDiff = this.diff[0];
356+
var secondDiff;
357+
var oid = "c88d39e70585199425b111c6a2c7fa7b4bc617ad";
358+
return this.repository.getCommit(oid)
359+
.then(function(testCommit) {
360+
return testCommit.getDiff();
361+
})
362+
.then(function(_secondDiff) {
363+
secondDiff = _secondDiff[0];
364+
return Promise.all([
365+
getLinesFromDiff(firstDiff),
366+
getLinesFromDiff(secondDiff)
367+
]);
368+
})
369+
.then(function(listOfLines) {
370+
linesOfFirstDiff = listOfLines[0];
371+
linesOfSecondDiff = listOfLines[1];
372+
return firstDiff.merge(secondDiff);
373+
})
374+
.then(function() {
375+
return getLinesFromDiff(firstDiff);
376+
})
377+
.then(function(linesOfMergedDiff) {
378+
var allDiffLines = _.flatten([
379+
linesOfFirstDiff,
380+
linesOfSecondDiff
381+
]);
382+
_.forEach(allDiffLines, function(diffLine) {
383+
assert.ok(_.includes(linesOfMergedDiff, diffLine));
384+
});
385+
});
386+
});
387+
329388
// This wasn't working before. It was only passing because the promise chain
330389
// was broken
331390
it.skip("can find similar files in a diff", function() {

0 commit comments

Comments
 (0)