-
Notifications
You must be signed in to change notification settings - Fork 696
Expand file tree
/
Copy pathindex.js
More file actions
421 lines (363 loc) · 12 KB
/
index.js
File metadata and controls
421 lines (363 loc) · 12 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
var assert = require("assert");
var path = require("path");
var local = path.join.bind(path, __dirname);
var fse = require("fs-extra");
describe("Index", function() {
var IndexUtils = require("../utils/index_setup");
var RepoUtils = require("../utils/repository_setup");
var NodeGit = require("../../");
var Repository = NodeGit.Repository;
var ErrorCodes = NodeGit.Error.CODE;
var reposPath = local("../repos/workdir");
beforeEach(function() {
var test = this;
return Repository.open(reposPath)
.then(function(repo) {
test.repository = repo;
return repo.refreshIndex();
})
.then(function(index) {
test.index = index;
});
});
after(function() {
this.index.clear();
});
it("can get the index of a repo and examine entries", function() {
var entries = this.index.entries();
assert.equal(entries[0].path, ".gitignore");
});
it("can add all entries to the index", function() {
var repo = this.repository;
var index = this.index;
var fileContent = {
newFile1: "this has some content",
newFile2: "and this will have more content"
};
var fileNames = Object.keys(fileContent);
var test = this;
var addCallbacksCount = 0;
return Promise.all(fileNames.map(function(fileName) {
return fse.writeFile(
path.join(repo.workdir(), fileName),
fileContent[fileName], {});
}))
.then(function() {
return index.addAll(undefined, undefined, function() {
addCallbacksCount++;
// 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() {
assert.equal(addCallbacksCount, 2);
var newFiles = index.entries().filter(function(entry) {
return ~fileNames.indexOf(entry.path);
});
assert.equal(newFiles.length, 2);
})
.then(function() {
return Promise.all(fileNames.map(function(fileName) {
return fse.remove(path.join(repo.workdir(), fileName));
}));
})
.then(function() {
return index.clear();
});
});
it("can remove entries from the index", function() {
var repo = this.repository;
var index = this.index;
var fileContent = {
newFile1: "this has some content",
newFile2: "and this will have more content",
differentFileName: "this has a different name and shouldn't be deleted"
};
var fileNames = Object.keys(fileContent);
var removeCallbacksCount = 0;
return Promise.all(fileNames.map(function(fileName) {
return fse.writeFile(
path.join(repo.workdir(), fileName),
fileContent[fileName], {});
}))
.then(function() {
return index.addAll();
})
.then(function() {
var newFiles = index.entries().filter(function(entry) {
return ~fileNames.indexOf(entry.path);
});
assert.equal(newFiles.length, 3);
return index.removeAll("newFile*", function() {
removeCallbacksCount++;
return 0; // confirm remove
});
})
.then(function() {
assert.equal(removeCallbacksCount, 2);
var newFiles = index.entries().filter(function(entry) {
return ~fileNames.indexOf(entry.path);
});
assert.equal(newFiles.length, 1);
})
.then(function() {
return Promise.all(fileNames.map(function(fileName) {
return fse.remove(path.join(repo.workdir(), fileName));
}));
})
.then(function() {
return index.clear();
});
});
it("can update entries in the index", function() {
var repo = this.repository;
var index = this.index;
var fileContent = {
newFile1: "this has some content",
newFile2: "and this will have more content"
};
var fileNames = Object.keys(fileContent);
var updateCallbacksCount = 0;
return Promise.all(fileNames.map(function(fileName) {
return fse.writeFile(
path.join(repo.workdir(), fileName),
fileContent[fileName], {});
}))
.then(function() {
return index.addAll();
})
.then(function() {
var newFiles = index.entries().filter(function(entry) {
return ~fileNames.indexOf(entry.path);
});
assert.equal(newFiles.length, 2);
return fse.remove(path.join(repo.workdir(), fileNames[0]));
})
.then(function() {
return index.updateAll("newFile*", function() {
updateCallbacksCount++;
return 0; // confirm update
});
})
.then(function() {
assert.equal(updateCallbacksCount, 1);
var newFiles = index.entries().filter(function(entry) {
return ~fileNames.indexOf(entry.path);
});
assert.equal(newFiles.length, 1);
return fse.remove(path.join(repo.workdir(), fileNames[1]));
});
});
it("can get a conflict from the index", function() {
var fileName = "everyonesFile.txt";
var rebaseReposPath = local("../repos/rebase");
var ourBranchName = "ours";
var theirBranchName = "theirs";
var baseFileContent = "How do you feel about Toll Roads?\n";
var ourFileContent = "I like Toll Roads. I have an EZ-Pass!\n";
var theirFileContent = "I'm skeptical about Toll Roads\n";
var ourSignature = NodeGit.Signature.create
("Ron Paul", "RonPaul@TollRoadsRBest.info", 123456789, 60);
var theirSignature = NodeGit.Signature.create
("Greg Abbott", "Gregggg@IllTollYourFace.us", 123456789, 60);
var repository;
var ourCommit;
var ourBranch;
var theirBranch;
return Repository.init(rebaseReposPath, 0)
.then(function(repo) {
repository = repo;
return fse.writeFile(path.join(repository.workdir(), fileName),
baseFileContent);
})
.then(function() {
return RepoUtils.addFileToIndex(repository, fileName);
})
.then(function(oid) {
assert.equal(oid.toString(),
"044704f62399fecbe22da6d7d47b14e52625630e");
return repository.createCommit("HEAD", ourSignature,
ourSignature, "initial commit", oid, []);
})
.then(function(commitOid) {
assert.equal(commitOid.toString(),
"80111c46ac73b857a3493b24c81df08639b5de99");
return repository.getCommit(commitOid).then(function(commit) {
ourCommit = commit;
}).then(function() {
return repository.createBranch(ourBranchName, commitOid)
.then(function(branch) {
ourBranch = branch;
return repository.createBranch(theirBranchName, commitOid);
});
});
})
.then(function(branch) {
theirBranch = branch;
return fse.writeFile(path.join(repository.workdir(), fileName),
baseFileContent + theirFileContent);
})
.then(function() {
return RepoUtils.addFileToIndex(repository, fileName);
})
.then(function(oid) {
assert.equal(oid.toString(),
"b826e989aca7647bea64810f0a2a38acbbdd4c1a");
return repository.createCommit(theirBranch.name(), theirSignature,
theirSignature, "they made a commit", oid, [ourCommit]);
})
.then(function(commitOid) {
assert.equal(commitOid.toString(),
"b3c355bb606ec7da87174dfa1a0b0c0e3dc97bc0");
return fse.writeFile(path.join(repository.workdir(), fileName),
baseFileContent + ourFileContent);
})
.then(function() {
return RepoUtils.addFileToIndex(repository, fileName);
})
.then(function(oid) {
assert.equal(oid.toString(),
"e7fe41bf7c0c28766887a63ffe2f03f624276fbe");
return repository.createCommit(ourBranch.name(), ourSignature,
ourSignature, "we made a commit", oid, [ourCommit]);
})
.then(function(commitOid) {
assert.equal(commitOid.toString(),
"28cfeb17f66132edb3c4dacb7ff38e8dd48a1844");
var opts = {
checkoutStrategy: NodeGit.Checkout.STRATEGY.FORCE
};
return NodeGit.Checkout.head(repository, opts);
})
.then(function() {
return repository.mergeBranches(ourBranchName, theirBranchName);
})
.then(function(commit) {
assert.fail(commit, undefined,
"The index should have been thrown due to merge conflicts");
})
.catch(function(index) {
assert.ok(index);
assert.ok(index.hasConflicts());
return index.conflictGet(fileName);
})
.then(function(conflict) {
var promises = [];
promises.push(repository.getBlob(conflict.ancestor_out.id)
.then(function(blob) {
assert.equal(blob.toString(), baseFileContent);
}));
promises.push(repository.getBlob(conflict.our_out.id)
.then(function(blob) {
assert.equal(blob.toString(), baseFileContent + ourFileContent);
}));
promises.push(repository.getBlob(conflict.their_out.id)
.then(function(blob) {
assert.equal(blob.toString(), baseFileContent + theirFileContent);
}));
return Promise.all(promises);
});
});
it("can add a conflict to the index", function() {
var repo;
var repoPath = local("../repos/index");
var ourBranchName = "ours";
var theirBranchName = "theirs";
var fileName = "testFile.txt";
var ancestorIndexEntry;
var ourIndexEntry;
var theirIndexEntry;
return RepoUtils.createRepository(repoPath)
.then(function(_repo) {
repo = _repo;
return IndexUtils.createConflict(
repo,
ourBranchName,
theirBranchName,
fileName
);
})
.then(function(index) {
assert.ok(index.hasConflicts());
return index.conflictGet(fileName);
})
.then(function(indexEntries) {
// Store all indexEntries for conflict
ancestorIndexEntry = indexEntries.ancestor_out;
ourIndexEntry = indexEntries.our_out;
theirIndexEntry = indexEntries.their_out;
// Stage conflicted file
return RepoUtils.addFileToIndex(repo, fileName);
})
.then(function() {
return repo.index();
})
.then(function(index) {
assert.ok(!index.hasConflicts());
return index.conflictAdd(
ancestorIndexEntry,
ourIndexEntry,
theirIndexEntry
);
})
.then(function() {
return repo.index();
})
.then(function(index) {
assert(index.hasConflicts());
});
});
it("can find the specified file in the index", function() {
var test = this;
return test.index.find("src/wrapper.cc")
.then(function(position) {
assert.notEqual(position, null);
});
});
it("cannot find the specified file in the index", function() {
var test = this;
return test.index.find("src/thisisfake.cc")
.then(function(position) {
assert.fail("the item should not be found");
})
.catch(function(error) {
assert.strictEqual(error.errno, ErrorCodes.ENOTFOUND);
});
});
it("cannot find the directory in the index", function() {
var test = this;
return test.index.find("src")
.then(function(position) {
assert.fail("the item should not be found");
})
.catch(function(error) {
assert.strictEqual(error.errno, ErrorCodes.ENOTFOUND);
});
});
it("can find the specified prefix in the index", function() {
var test = this;
return test.index.findPrefix("src/")
.then(function(position) {
assert.notEqual(position, null);
});
});
it("cannot find the specified prefix in the index", function() {
var test = this;
return test.index.find("testing123/")
.then(function(position) {
assert.fail("the item should not be found");
})
.catch(function(error) {
assert.strictEqual(error.errno, ErrorCodes.ENOTFOUND);
});
});
it("can find the prefix when a file shares the name", function() {
var test = this;
return test.index.find("LICENSE")
.then(function(position) {
assert.notEqual(position, null);
});
});
});