-
Notifications
You must be signed in to change notification settings - Fork 698
Expand file tree
/
Copy pathrevwalk.js
More file actions
362 lines (321 loc) · 10.5 KB
/
revwalk.js
File metadata and controls
362 lines (321 loc) · 10.5 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
var assert = require("assert");
var RepoUtils = require("../utils/repository_setup");
var promisify = require("promisify-node");
var fse = promisify(require("fs-extra"));
var path = require("path");
var local = path.join.bind(path, __dirname);
describe("Revwalk", function() {
var NodeGit = require("../../");
var Repository = NodeGit.Repository;
var Revwalk = NodeGit.Revwalk;
var Oid = NodeGit.Oid;
var reposPath = local("../repos/workdir");
// Set a reasonable timeout here now that our repository has grown.
this.timeout(120000);
beforeEach(function() {
var test = this;
return Repository.open(reposPath)
.then(function(repository) {
test.repository = repository;
return test.repository.getBranchCommit("rev-walk");
})
.then(function(commit) {
test.commit = commit;
});
});
beforeEach(function() {
this.walker = this.repository.createRevWalk();
this.walker.sorting(NodeGit.Revwalk.SORT.TIME);
this.walker.push(this.commit.id());
});
it("can create a walker", function() {
assert.ok(this.walker instanceof Revwalk);
});
it("can push an object", function() {
var sha = this.commit.sha();
return this.walker.next()
.then(function(commit) {
assert.equal(sha, commit);
});
});
it("can hide an object", function() {
var test = this;
return next(test.walker, 4)
.then(function(commit) {
assert.equal(commit.toString(),
"b8a94aefb22d0534cc0e5acf533989c13d8725dc");
test.walker = test.repository.createRevWalk();
test.walker.push(test.commit.id());
test.walker.hide(
Oid.fromString("b8a94aefb22d0534cc0e5acf533989c13d8725dc"));
return next(test.walker, 3);
})
.then(function(commit) {
assert.equal(commit.toString(),
"95f695136203a372751c19b6353aeb5ae32ea40e");
return next(test.walker, 1);
})
.then(function(commit) {
assert.equal(commit, undefined);
});
});
it("can simplify to first parent", function() {
var test = this;
test.walker.simplifyFirstParent();
return next(test.walker, 3)
.then(function(commit) {
assert.equal(commit.toString(),
"b8a94aefb22d0534cc0e5acf533989c13d8725dc");
});
});
it("can get a specified number of commits", function() {
var test = this;
var storedCommits;
return test.walker.getCommits(10)
.then(function(commits) {
assert.equal(commits.length, 10);
storedCommits = commits;
test.walker = test.repository.createRevWalk();
test.walker.push(test.commit.id());
return test.walker.getCommits(8);
})
.then(function(commits) {
assert.equal(commits.length, 8);
for (var i = 0; i < 8; i++) {
assert.equal(commits[i].toString(), storedCommits[i].toString());
}
});
});
it("can get the largest number of commits within a specified range",
function() {
var test = this;
var storedCommits;
return test.walker.getCommits(991)
.then(function(commits) {
assert.equal(commits.length, 990);
storedCommits = commits;
test.walker = test.repository.createRevWalk();
test.walker.push(test.commit.id());
});
});
it("will return all commits from the revwalk if nothing matches", function() {
var test = this;
var magicSha = "notintherepoatallwhatsoeverisntthatcool";
function checkCommit(commit) {
return commit.toString() != magicSha;
}
return test.walker.getCommitsUntil(checkCommit)
.then(function(commits) {
assert.equal(commits.length, 990);
});
});
it("can get commits until you tell it not to", function() {
var test = this;
var magicSha = "b8a94aefb22d0534cc0e5acf533989c13d8725dc";
function checkCommit(commit) {
return commit.toString() != magicSha;
}
return test.walker.getCommitsUntil(checkCommit)
.then(function(commits) {
assert.equal(commits.length, 4);
assert.equal(commits[commits.length-1].toString(), magicSha);
});
});
it("can do a fast walk", function() {
var test = this;
var magicSha = "b8a94aefb22d0534cc0e5acf533989c13d8725dc";
return test.walker.fastWalk(10)
.then(function(commitOids) {
assert.equal(commitOids.length, 10);
assert.equal(commitOids[3].toString(), magicSha);
});
});
it("can get the history of a file", function() {
var test = this;
var magicShas = [
"6ed3027eda383d417457b99b38c73f88f601c368",
"95cefff6aabd3c1f6138ec289f42fec0921ff610",
"7ad92a7e4d26a1af93f3450aea8b9d9b8069ea8c",
"96f077977eb1ffcb63f9ce766cdf110e9392fdf5",
"694adc5369687c47e02642941906cfc5cb21e6c2",
"eebd0ead15d62eaf0ba276da53af43bbc3ce43ab",
"1273fff13b3c28cfdb13ba7f575d696d2a8902e1"
];
return test.walker.fileHistoryWalk("include/functions/copy.h", 1000)
.then(function(results) {
var shas = results.map(function(result) {
return result.commit.sha();
});
assert.equal(magicShas.length, shas.length);
magicShas.forEach(function(sha, i) {
assert.equal(sha, shas[i]);
});
});
});
it("can get the history of a file while ignoring parallel branches",
function() {
var test = this;
var magicShas = [
"f80e085e3118bbd6aad49dad7c53bdc37088bf9b",
"907b29d8a3b765570435c922a59cd849836a7b51"
];
var shas;
var walker = test.repository.createRevWalk();
walker.sorting(NodeGit.Revwalk.SORT.TIME);
walker.push("115d114e2c4d5028c7a78428f16a4528c51be7dd");
return walker.fileHistoryWalk("README.md", 15)
.then(function(results) {
shas = results.map(function(result) {
return result.commit.sha();
});
assert.equal(magicShas.length, shas.length);
magicShas.forEach(function(sha, i) {
assert.equal(sha, shas[i]);
});
magicShas = [
"4a34168b80fe706f52417106821c9cbfec630e47",
"f80e085e3118bbd6aad49dad7c53bdc37088bf9b",
"694b2d703a02501f288269bea7d1a5d643a83cc8",
"907b29d8a3b765570435c922a59cd849836a7b51"
];
walker = test.repository.createRevWalk();
walker.sorting(NodeGit.Revwalk.SORT.TIME);
walker.push("d46f7da82969ca6620864d79a55b951be0540bda");
return walker.fileHistoryWalk("README.md", 50);
})
.then(function(results) {
shas = results.map(function(result) {
return result.commit.sha();
});
assert.equal(magicShas.length, shas.length);
magicShas.forEach(function(sha, i) {
assert.equal(sha, shas[i]);
});
});
});
it("can yield information about renames in a file history walk",
function() {
var treeOid;
var repo;
var fileNameA = "a.txt";
var fileNameB = "b.txt";
var repoPath = local("../repos/renamedFileRepo");
var signature = NodeGit.Signature.create("Foo bar",
"foo@bar.com", 123456789, 60);
var headCommit;
return RepoUtils.createRepository(repoPath)
.then(function(r) {
repo = r;
return RepoUtils.commitFileToRepo(
repo,
fileNameA,
"line1\nline2\nline3\n"
);
})
.then(function() {
return fse.move(
path.join(repoPath, fileNameA),
path.join(repoPath, fileNameB)
);
})
.then(function() {
return repo.refreshIndex();
})
.then(function(index) {
return index.addByPath(fileNameB)
.then(function() {
return index.removeByPath(fileNameA);
})
.then(function() {
return index.write();
})
.then(function() {
return index.writeTree();
});
})
.then(function(oidResult) {
treeOid = oidResult;
return NodeGit.Reference.nameToId(repo, "HEAD");
})
.then(function(head) {
return repo.getCommit(head);
})
.then(function(head) {
return repo.createCommit("HEAD", signature, signature,
"renamed commit", treeOid, [head]);
})
.then(function() {
return NodeGit.Reference.nameToId(repo, "HEAD");
})
.then(function(commitOid) {
headCommit = commitOid.tostrS();
var walker = repo.createRevWalk();
walker.sorting(NodeGit.Revwalk.SORT.TIME);
walker.push(commitOid.tostrS());
return walker.fileHistoryWalk(fileNameB, 5);
})
.then(function(results) {
assert.equal(results[0].status, NodeGit.Diff.DELTA.RENAMED);
assert.equal(results[0].newName, fileNameB);
assert.equal(results[0].oldName, fileNameA);
})
.then(function() {
var walker = repo.createRevWalk();
walker.sorting(NodeGit.Revwalk.SORT.TIME);
walker.push(headCommit);
return walker.fileHistoryWalk(fileNameA, 5);
})
.then(function(results) {
assert.equal(results[0].status, NodeGit.Diff.DELTA.RENAMED);
assert.equal(results[0].newName, fileNameB);
assert.equal(results[0].oldName, fileNameA);
})
.then(function() {
return fse.remove(repoPath);
});
});
// This test requires forcing garbage collection, so mocha needs to be run
// via node rather than npm, with a la `node --expose-gc [pathtohmoca]
// [testglob]`
var testGC = global.gc ? it : it.skip;
testGC("doesnt segfault when accessing .author() twice", function(done) {
Repository.open(reposPath).then(function(repository) {
var walker = repository.createRevWalk();
repository.getMasterCommit().then(function(firstCommitOnMaster) {
walker.walk(firstCommitOnMaster.id(), function(err, commit) {
if (err && err.errno === NodeGit.Error.CODE.ITEROVER) {
return done();
}
for (var i = 0; i < 500; i++) {
commit.author().name();
commit.author().email();
if ( i % 250 === 0) {
global.gc();
}
}
});
});
});
});
function next(walker, count) {
var promise = null;
for (var i = 0; i < count; i++) {
if (!promise) {
promise = walker.next();
}
else {
promise = promise.then(getNext);
}
}
return promise.catch(function(error) {
if (error && error.errno === NodeGit.Error.CODE.ITEROVER) {
return Promise.resolve();
} else {
throw error;
}
});
function getNext() {
return walker.next();
}
}
});