This repository was archived by the owner on Dec 24, 2017. It is now read-only.
forked from nodegit/nodegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.js
More file actions
508 lines (460 loc) · 15 KB
/
diff.js
File metadata and controls
508 lines (460 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
var assert = require("assert");
var path = require("path");
var promisify = require("promisify-node");
var _ = require("lodash");
var fse = promisify(require("fs-extra"));
var local = path.join.bind(path, __dirname);
function getLinesFromDiff(diff) {
return diff.patches()
.then(function(patches) {
return Promise.all(_.map(patches, function(patch) {
return patch.hunks();
}));
})
.then(function(listsOfHunks) {
var hunks = _.flatten(listsOfHunks);
return Promise.all(_.map(hunks, function(hunk) {
return hunk.lines();
}));
})
.then(function(listsOfLines) {
var lines = _.flatten(listsOfLines);
return _.map(lines, function(line) {
return line.content();
});
});
}
describe("Diff", function() {
var NodeGit = require("../../");
var Repository = NodeGit.Repository;
var Diff = NodeGit.Diff;
var Blob = NodeGit.Blob;
var reposPath = local("../repos/workdir");
var oid = "fce88902e66c72b5b93e75bdb5ae717038b221f6";
var diffFilename = "wddiff.txt";
var diffFilepath = local("../repos/workdir", diffFilename);
var moveFromFile = "README.md";
var moveToFile = "MOVED_README.md";
var moveFromPath = local("../repos/workdir", moveFromFile);
var moveToPath = local("../repos/workdir", moveToFile);
beforeEach(function() {
var test = this;
return Repository.open(reposPath).then(function(repository) {
test.repository = repository;
return repository.refreshIndex();
})
.then(function(index) {
test.index = index;
return test.repository.getBranchCommit("master");
})
.then(function(masterCommit) {
return masterCommit.getTree();
})
.then(function(tree) {
test.masterCommitTree = tree;
return test.repository.getCommit(oid);
})
.then(function(commit) {
test.commit = commit;
return commit.getDiff();
})
.then(function(diff) {
test.diff = diff;
return fse.writeFile(diffFilepath, "1 line\n2 line\n3 line\n\n4");
})
.then(function() {
return fse.move(moveFromPath, moveToPath);
})
.then(function() {
return Diff.treeToWorkdirWithIndex(
test.repository,
test.masterCommitTree,
{ flags: Diff.OPTION.INCLUDE_UNTRACKED }
);
})
.then(function(workdirDiff) {
test.workdirDiff = workdirDiff;
})
.then(function() {
var opts = {
flags: Diff.OPTION.INCLUDE_UNTRACKED |
Diff.OPTION.RECURSE_UNTRACKED_DIRS
};
return Diff.indexToWorkdir(test.repository, test.index, opts);
})
.then(function(diff) {
test.indexToWorkdirDiff = diff;
})
.then(function() {
return fse.remove(diffFilepath);
})
.then(function() {
return fse.move(moveToPath, moveFromPath);
})
.catch(function(e) {
return fse.remove(diffFilepath)
.then(function() {
return Promise.reject(e);
});
});
});
it("can walk a DiffList", function() {
return this.diff[0].patches()
.then(function(patches) {
var patch = patches[0];
assert.equal(patch.oldFile().path(), "README.md");
assert.equal(patch.newFile().path(), "README.md");
assert.equal(patch.size(), 1);
assert.ok(patch.isModified());
return patch.hunks();
})
.then(function(hunks) {
var hunk = hunks[0];
assert.equal(hunk.size(), 5);
return hunk.lines();
})
.then(function(lines) {
assert.equal(lines[0].origin(), Diff.LINE.CONTEXT);
assert.equal(lines[1].origin(), Diff.LINE.CONTEXT);
assert.equal(lines[2].origin(), Diff.LINE.CONTEXT);
var oldContent = "__Before submitting a pull request, please ensure " +
"both unit tests and lint checks pass.__\n";
assert.equal(lines[3].content(), oldContent);
assert.equal(lines[3].origin(), Diff.LINE.DELETION);
assert.equal(lines[3].content().length, oldContent.length);
var newContent = "__Before submitting a pull request, please ensure " +
"both that you've added unit tests to cover your shiny new code, " +
"and that all unit tests and lint checks pass.__\n";
assert.equal(lines[4].content(), newContent);
assert.equal(lines[4].origin(), Diff.LINE.ADDITION);
assert.equal(lines[4].content().length, newContent.length);
});
});
it("can diff the workdir with index", function() {
return this.workdirDiff.patches()
.then(function(patches) {
assert.equal(patches.length, 3);
assert(patches[2].isUntracked());
var oldFile = patches[2].oldFile();
assert.equal(oldFile.path(), "wddiff.txt");
assert.equal(oldFile.size(), 0);
var newFile = patches[2].newFile();
assert.equal(newFile.path(), "wddiff.txt");
assert.equal(newFile.size(), 23);
});
});
it("can resolve individual line changes from the patch hunks", function() {
return this.workdirDiff.patches()
.then(function(patches) {
var result = [];
var hunkPromises = [];
patches.forEach(function(patch) {
hunkPromises.push(patch.hunks()
.then(function(hunks) {
result = result.concat(hunks);
})
);
});
return Promise.all(hunkPromises)
.then(function() {
return result;
});
})
.then(function(hunks) {
var result = [];
var linePromises = [];
hunks.forEach(function(hunk) {
linePromises.push(hunk.lines()
.then(function(lines) {
result = result.concat(lines);
})
);
});
return Promise.all(linePromises)
.then(function() {
return result;
});
})
.then(function(lines) {
lines.forEach(function(line) {
assert(/\n/.exec(line.content()));
assert(/\n/.exec(line.rawContent()));
});
});
});
it("can diff the contents of a file to a string", function(done) {
this.repository.getBranchCommit("master")
.then(function(commit) {
return commit.getEntry("LICENSE");
})
.then(function(entry) {
var _entry = entry;
return _entry.getBlob();
})
.then(function(blob) {
var buffer = "New Text";
return Diff.blobToBuffer(
blob,
null,
buffer,
null,
null,
null,
null,
function(delta, hunk, payload) {
assert.equal(hunk.oldStart(), 1);
assert.equal(hunk.oldLines(), 19);
assert.equal(hunk.newStart(), 1);
assert.equal(hunk.newLines(), 1);
assert.equal(
hunk.header().substring(0, hunk.headerLen() - 1),
"@@ -1,19 +1 @@"
);
done();
});
});
});
it("can diff the contents of a file to a string with unicode characters",
function(done) {
var evilString = "Unicode’s fun!\nAnd it’s good for you!\n";
var buffer = new Buffer(evilString);
var oid = Blob.createFromBuffer(this.repository, buffer, buffer.length);
Blob.lookup(this.repository, oid)
.then(function(blob) {
blob.repo = this.repository;
return Diff.blobToBuffer(
blob,
null,
evilString,
null,
null,
null,
null,
function(delta, hunk, payload) {
assert.fail(
"There aren't any changes so this shouldn't be called.");
done();
});
})
.then(function() {
done();
});
});
it("can diff with a null tree", function() {
var repo = this.repository;
var tree = this.masterCommitTree;
return Diff.treeToTree(repo, null, tree, null)
.then(function(diff) {
return diff.patches();
})
.then(function(patches) {
// Number of patches returned is 84 or 85 depending
// on something unknown at this time. Hopefully we can
// eventually resolve the root cause of the difference.
// https://github.com/nodegit/nodegit/issues/746
assert.ok(patches.length === 84 || patches.length === 85);
});
});
it("can diff the initial commit of a repository", function() {
var repo = this.repository;
var oid = "99c88fd2ac9c5e385bd1fe119d89c83dce326219"; // First commit
return repo.getCommit(oid)
.then(function(commit) {
return commit.getDiff();
})
.then(function(diffs) {
return diffs[0].patches();
})
.then(function(patches) {
assert.equal(patches.length, 8);
});
});
it("can diff tree to index", function() {
var repo = this.repository;
var tree = this.masterCommitTree;
var index = this.index;
var opts = { flags: Diff.OPTION.INCLUDE_UNTRACKED };
return Diff.treeToIndex(repo, tree, index, opts)
.then(function(diff) {
return diff.patches();
})
.then(function(patches) {
assert.equal(patches.length, 0);
});
});
it("can diff index to workdir", function() {
return this.indexToWorkdirDiff.patches()
.then(function(patches) {
assert.equal(patches.length, 3);
});
});
it("can pass undefined pathspec as option to indexToWorkdir", function() {
var test = this;
return Repository.open(reposPath).then(function(repository) {
test.repository = repository;
return repository.refreshIndex();
})
.then(function(index) {
test.index = index;
return test.repository.getBranchCommit("master");
})
.then(function() {
var opts = {
flags: Diff.OPTION.INCLUDE_UNTRACKED |
Diff.OPTION.RECURSE_UNTRACKED_DIRS,
pathspec: undefined
};
// should not segfault
return Diff.indexToWorkdir(test.repository, test.index, opts);
});
});
it("can merge two commit diffs", function() {
var linesOfFirstDiff;
var linesOfSecondDiff;
var firstDiff = this.diff[0];
var secondDiff;
var oid = "c88d39e70585199425b111c6a2c7fa7b4bc617ad";
return this.repository.getCommit(oid)
.then(function(testCommit) {
return testCommit.getDiff();
})
.then(function(_secondDiff) {
secondDiff = _secondDiff[0];
return Promise.all([
getLinesFromDiff(firstDiff),
getLinesFromDiff(secondDiff)
]);
})
.then(function(listOfLines) {
linesOfFirstDiff = listOfLines[0];
linesOfSecondDiff = listOfLines[1];
return firstDiff.merge(secondDiff);
})
.then(function() {
return getLinesFromDiff(firstDiff);
})
.then(function(linesOfMergedDiff) {
var allDiffLines = _.flatten([
linesOfFirstDiff,
linesOfSecondDiff
]);
_.forEach(allDiffLines, function(diffLine) {
assert.ok(_.includes(linesOfMergedDiff, diffLine));
});
});
});
describe(
"merge between commit diff and workdir and index diff", function() {
beforeEach(function() {
var test = this;
var diffOptions = new NodeGit.DiffOptions();
var IGNORE_CASE_FLAG = 1 << 10;
diffOptions.flags = diffOptions.flags |= IGNORE_CASE_FLAG;
return fse.writeFile(
path.join(test.repository.workdir(), "newFile.txt"), "some line\n"
)
.then(function() {
return test.index.addAll(undefined, undefined, function() {
// ensure that there is no deadlock if we call
// a sync libgit2 function from the callback
test.repository.path();
return 0; // confirm add
});
})
.then(function() {
return test.repository.getHeadCommit();
})
.then(function(headCommit) {
return headCommit.getTree();
})
.then(function(headTree) {
return Promise.all([
Diff.treeToWorkdirWithIndex(test.repository, headTree, diffOptions),
test.commit.getDiffWithOptions(diffOptions)
]);
})
.then(function(diffs) {
test.workDirWithIndexDiff = diffs[0];
// The second item in `diffs` is the commit diff which contains and
// array of diffs, one for each parent
test.commitDiff = diffs[1][0];
});
});
it("can merge a diff from a commit into a diff from a work dir and index",
function() {
var test = this;
var linesOfWorkDirWithIndexDiff;
var linesOfCommitDiff;
return Promise.all([
getLinesFromDiff(test.workDirWithIndexDiff),
getLinesFromDiff(test.commitDiff)
])
.then(function(linesOfDiffs) {
linesOfWorkDirWithIndexDiff = linesOfDiffs[0];
linesOfCommitDiff = linesOfDiffs[1];
return test.workDirWithIndexDiff.merge(test.commitDiff);
})
.then(function() {
return getLinesFromDiff(test.workDirWithIndexDiff);
})
.then(function(linesOfMergedDiff) {
var allDiffLines = _.flatten([
linesOfWorkDirWithIndexDiff,
linesOfCommitDiff
]);
_.forEach(allDiffLines, function(diffLine) {
assert.ok(_.includes(linesOfMergedDiff, diffLine));
});
});
});
it("can merge a diff from a workdir and index into a diff from a commit",
function() {
var test = this;
var linesOfWorkDirWithIndexDiff;
var linesOfCommitDiff;
return Promise.all([
getLinesFromDiff(test.workDirWithIndexDiff),
getLinesFromDiff(test.commitDiff)
])
.then(function(linesOfDiffs) {
linesOfWorkDirWithIndexDiff = linesOfDiffs[0];
linesOfCommitDiff = linesOfDiffs[1];
return test.commitDiff.merge(test.workDirWithIndexDiff);
})
.then(function() {
return getLinesFromDiff(test.commitDiff);
})
.then(function(linesOfMergedDiff) {
var allDiffLines = _.flatten([
linesOfWorkDirWithIndexDiff,
linesOfCommitDiff
]);
_.forEach(allDiffLines, function(diffLine) {
assert.ok(_.includes(linesOfMergedDiff, diffLine));
});
});
});
});
// This wasn't working before. It was only passing because the promise chain
// was broken
it.skip("can find similar files in a diff", function() {
var diff = this.indexToWorkdirDiff;
var opts = {
flags: Diff.FIND.RENAMES |
Diff.FIND.RENAMES_FROM_REWRITES |
Diff.FIND.FOR_UNTRACKED
};
return diff.patches()
.then(function(patches) {
assert.equal(patches.length, 3);
return diff.findSimilar(opts);
})
.then(function() {
return diff.patches();
})
.then(function(patches) {
// Renamed file now treated as one diff, so 3 patches -> 2
assert.equal(patches.length, 2);
});
});
});