Skip to content

Commit fc8ff1f

Browse files
committed
Add Diff.prototype.findSimilar
Also DiffFindOptions
1 parent b080622 commit fc8ff1f

3 files changed

Lines changed: 60 additions & 9 deletions

File tree

generate/input/descriptor.json

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,20 @@
524524
"ignore": true
525525
},
526526
"git_diff_find_similar": {
527-
"ignore": true
527+
"args": {
528+
"diff": {
529+
"isSelf": true
530+
},
531+
"options": {
532+
"isOptional": true
533+
}
534+
},
535+
"return": {
536+
"cppClassName": "Number",
537+
"jsClassName": "Number",
538+
"isErrorCode": true
539+
},
540+
"isAsync": true
528541
},
529542
"git_diff_foreach": {
530543
"ignore": true
@@ -639,12 +652,12 @@
639652
}
640653
},
641654
"diff_find_options": {
655+
"hasConstructor": true,
642656
"fields": {
643657
"git_diff_similarity_metric": {
644658
"ignore": true
645659
}
646-
},
647-
"ignore": true
660+
}
648661
},
649662
"diff_format_email_options": {
650663
"ignore": true

lib/diff.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,11 @@ Diff.treeToWorkdirWithIndex = function(repo, tree, opts) {
5656
return treeToWorkdirWithIndex(repo, tree, opts);
5757
};
5858

59+
// Override Diff.findSimilar to normalize opts
60+
var findSimilar = Diff.prototype.findSimilar;
61+
Diff.prototype.findSimilar = function(opts) {
62+
opts = normalizeOptions(opts, NodeGit.DiffFindOptions);
63+
return findSimilar.call(this, opts);
64+
};
65+
5966
module.exports = Diff;

test/tests/diff.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ describe("Diff", function() {
1414
var diffFilename = "wddiff.txt";
1515
var diffFilepath = local("../repos/workdir", diffFilename);
1616

17+
var moveFromFile = "README.md";
18+
var moveToFile = "MOVED_README.md";
19+
20+
var moveFromPath = local("../repos/workdir", moveFromFile);
21+
var moveToPath = local("../repos/workdir", moveToFile);
22+
1723
beforeEach(function() {
1824
var test = this;
1925

@@ -45,6 +51,9 @@ describe("Diff", function() {
4551

4652
return fse.writeFile(diffFilepath, "1 line\n2 line\n3 line\n\n4");
4753
})
54+
.then(function() {
55+
return fse.move(moveFromPath, moveToPath);
56+
})
4857
.then(function() {
4958
return Diff.treeToWorkdirWithIndex(
5059
test.repository,
@@ -56,7 +65,10 @@ describe("Diff", function() {
5665
test.workdirDiff = workdirDiff;
5766
})
5867
.then(function() {
59-
var opts = { flags: Diff.OPTION.INCLUDE_UNTRACKED };
68+
var opts = {
69+
flags: Diff.OPTION.INCLUDE_UNTRACKED |
70+
Diff.OPTION.RECURSE_UNTRACKED_DIRS
71+
};
6072

6173
return Diff.indexToWorkdir(test.repository, test.index, opts);
6274
})
@@ -66,6 +78,9 @@ describe("Diff", function() {
6678
.then(function() {
6779
return fse.remove(diffFilepath);
6880
})
81+
.then(function() {
82+
return fse.move(moveToPath, moveFromPath);
83+
})
6984
.catch(function(e) {
7085
return fse.remove(diffFilepath)
7186
.then(function() {
@@ -106,14 +121,14 @@ describe("Diff", function() {
106121

107122
it("can diff the workdir with index", function() {
108123
var patches = this.workdirDiff.patches();
109-
assert.equal(patches.length, 1);
110-
assert(patches[0].isUntracked());
124+
assert.equal(patches.length, 3);
125+
assert(patches[2].isUntracked());
111126

112-
var oldFile = patches[0].delta.oldFile();
127+
var oldFile = patches[2].delta.oldFile();
113128
assert.equal(oldFile.path(), "wddiff.txt");
114129
assert.equal(oldFile.size(), 0);
115130

116-
var newFile = patches[0].delta.newFile();
131+
var newFile = patches[2].delta.newFile();
117132
assert.equal(newFile.path(), "wddiff.txt");
118133
assert.equal(newFile.size(), 23);
119134
});
@@ -151,6 +166,22 @@ describe("Diff", function() {
151166
});
152167

153168
it("can diff index to workdir", function() {
154-
assert.equal(this.indexToWorkdirDiff.patches().length, 1);
169+
assert.equal(this.indexToWorkdirDiff.patches().length, 3);
170+
});
171+
172+
it("can find similar files in a diff", function() {
173+
var diff = this.indexToWorkdirDiff;
174+
var opts = {
175+
flags: Diff.FIND.RENAMES |
176+
Diff.FIND.RENAMES_FROM_REWRITES |
177+
Diff.FIND.FOR_UNTRACKED
178+
};
179+
180+
assert.equal(diff.patches().length, 3);
181+
182+
diff.findSimilar(opts).then(function() {
183+
// Renamed file now treated as one diff, so 3 patches -> 2
184+
assert.equal(diff.patches().length, 2);
185+
});
155186
});
156187
});

0 commit comments

Comments
 (0)