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
297 lines (262 loc) · 8.3 KB
/
diff.js
File metadata and controls
297 lines (262 loc) · 8.3 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
var assert = require("assert");
var path = require("path");
var promisify = require("thenify-all");
var Promise = require("bluebird");
var fse = promisify(require("fs-extra"),
["remove", "move", "writeFile"]);
var local = path.join.bind(path, __dirname);
describe("Diff", function() {
var NodeGit = require("../../");
var Repository = NodeGit.Repository;
var Diff = NodeGit.Diff;
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.openIndex();
})
.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].rawContent(), oldContent);
assert.equal(lines[3].origin(), Diff.LINE.DELETION);
assert.equal(lines[3].contentLen(), 90);
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].rawContent(), newContent);
assert.equal(lines[4].origin(), Diff.LINE.ADDITION);
assert.equal(lines[4].contentLen(), 162);
});
});
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].delta.oldFile();
assert.equal(oldFile.path(), "wddiff.txt");
assert.equal(oldFile.size(), 0);
var newFile = patches[2].delta.newFile();
assert.equal(newFile.path(), "wddiff.txt");
assert.equal(newFile.size(), 23);
});
});
it("can resolve individual line chages 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.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.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,
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 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) {
assert.equal(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);
});
});
// 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);
});
});
});