-
Notifications
You must be signed in to change notification settings - Fork 696
Expand file tree
/
Copy pathrepository.js
More file actions
394 lines (351 loc) · 11.9 KB
/
repository.js
File metadata and controls
394 lines (351 loc) · 11.9 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
var assert = require("assert");
var path = require("path");
var fse = require("fs-extra");
var local = path.join.bind(path, __dirname);
var IndexUtils = require("../utils/index_setup");
var RepoUtils = require("../utils/repository_setup");
describe("Repository", function() {
var NodeGit = require("../../");
var Repository = NodeGit.Repository;
var Index = NodeGit.Index;
var Signature = NodeGit.Signature;
var constReposPath = local("../repos/constworkdir");
var reposPath = local("../repos/workdir");
var newRepoPath = local("../repos/newrepo");
var emptyRepoPath = local("../repos/empty");
beforeEach(function() {
var test = this;
return Repository.open(constReposPath)
.then(function(constRepository) {
test.constRepository = constRepository;
})
.then(function() {
return Repository.open(reposPath);
})
.then(function(repository) {
test.repository = repository;
})
.then(function() {
return Repository.open(emptyRepoPath);
})
.then(function(emptyRepo) {
test.emptyRepo = emptyRepo;
});
});
it("cannot instantiate a repository", function() {
assert.throws(
function() { new Repository(); },
undefined,
"hello"
);
});
it("can open a valid repository", function() {
assert.ok(this.repository instanceof Repository);
});
it("cannot open an invalid repository", function() {
return Repository.open("repos/nonrepo")
.then(null, function(err) {
assert.ok(err instanceof Error);
});
});
it("does not try to open paths that don't exist", function() {
var missingPath = "/surely/this/directory/does/not/exist/on/this/machine";
return Repository.open(missingPath)
.then(null, function(err) {
assert.ok(err instanceof Error);
});
});
it("can initialize a repository into a folder", function() {
return Repository.init(newRepoPath, 1)
.then(function(path, isBare) {
return Repository.open(newRepoPath);
});
});
it("can utilize repository init options", function() {
return fse.remove(newRepoPath)
.then(function() {
return Repository.initExt(newRepoPath, {
flags: Repository.INIT_FLAG.MKPATH
});
});
});
it("can be cleaned", function() {
this.repository.cleanup();
// try getting a commit after cleanup (to test that the repo is usable)
return this.repository.getHeadCommit()
.then(function(commit) {
assert.equal(
commit.toString(),
"32789a79e71fbc9e04d3eff7425e1771eb595150"
);
});
});
it("can read the index", function() {
return this.repository.index()
.then(function(index) {
assert.ok(index instanceof Index);
});
});
it("can list remotes", function() {
return this.repository.getRemoteNames()
.then(function(remotes) {
assert.equal(remotes.length, 1);
assert.equal(remotes[0], "origin");
});
});
it("can get the current branch", function() {
return this.repository.getCurrentBranch()
.then(function(branch) {
assert.equal(branch.shorthand(), "master");
});
});
it("can get a reference commit", function() {
return this.repository.getReferenceCommit("master")
.then(function(commit) {
assert.equal(
"32789a79e71fbc9e04d3eff7425e1771eb595150",
commit.toString()
);
});
});
it("can get the default signature", function() {
this.repository.defaultSignature()
.then((sig) => {
assert(sig instanceof Signature);
});
});
it("gets statuses with StatusFile", function() {
var fileName = "my-new-file-that-shouldnt-exist.file";
var fileContent = "new file from repository test";
var repo = this.repository;
var filePath = path.join(repo.workdir(), fileName);
return fse.writeFile(filePath, fileContent)
.then(function() {
return repo.getStatus().then(function(statuses) {
assert.equal(statuses.length, 1);
assert.equal(statuses[0].path(), fileName);
assert.ok(statuses[0].isNew());
});
})
.then(function() {
return fse.remove(filePath);
})
.catch(function (e) {
return fse.remove(filePath)
.then(function() {
return Promise.reject(e);
});
});
});
it("gets extended statuses", function() {
var fileName = "my-new-file-that-shouldnt-exist.file";
var fileContent = "new file from repository test";
var repo = this.repository;
var filePath = path.join(repo.workdir(), fileName);
return fse.writeFile(filePath, fileContent)
.then(function() {
return repo.getStatusExt();
})
.then(function(statuses) {
assert.equal(statuses.length, 1);
assert.equal(statuses[0].path(), fileName);
assert.equal(statuses[0].indexToWorkdir().newFile().path(), fileName);
assert.ok(statuses[0].isNew());
})
.then(function() {
return fse.remove(filePath);
})
.catch(function (e) {
return fse.remove(filePath)
.then(function() {
return Promise.reject(e);
});
});
});
it("gets fetch-heads", function() {
var repo = this.repository;
var foundMaster;
return repo.fetch("origin", {
credentials: function(url, userName) {
return NodeGit.Credential.sshKeyFromAgent(userName);
},
certificateCheck: () => 0
})
.then(function() {
return repo.fetchheadForeach(function(refname, remoteUrl, oid, isMerge) {
if (refname == "refs/heads/master") {
foundMaster = true;
assert.equal(refname, "refs/heads/master");
assert.equal(remoteUrl, "https://github.com/nodegit/test");
assert.equal(
oid.toString(),
"32789a79e71fbc9e04d3eff7425e1771eb595150");
assert.equal(isMerge, 1);
}
});
})
.then(function() {
if (!foundMaster) {
throw new Error("Couldn't find master in iteration of fetch heads");
}
});
});
function discover(ceiling) {
var testPath = path.join(reposPath, "lib", "util", "normalize_oid.js");
var expectedPath = path.join(reposPath, ".git");
return NodeGit.Repository.discover(testPath, 0, ceiling)
.then(function(foundPath) {
assert.equal(expectedPath, foundPath);
});
}
it("can discover if a path is part of a repository, null ceiling",
function() {
return discover(null);
});
it("can discover if a path is part of a repository, empty ceiling",
function() {
return discover("");
});
it("can create a repo using initExt", function() {
var initFlags = NodeGit.Repository.INIT_FLAG.NO_REINIT |
NodeGit.Repository.INIT_FLAG.MKPATH |
NodeGit.Repository.INIT_FLAG.MKDIR;
return fse.remove(newRepoPath)
.then(function() {
return NodeGit.Repository.initExt(newRepoPath, { flags: initFlags });
})
.then(function() {
return NodeGit.Repository.open(newRepoPath);
});
});
it("will throw when a repo cannot be initialized using initExt", function() {
var initFlags = NodeGit.Repository.INIT_FLAG.NO_REINIT |
NodeGit.Repository.INIT_FLAG.MKPATH |
NodeGit.Repository.INIT_FLAG.MKDIR;
var nonsensePath = "gibberish";
return NodeGit.Repository.initExt(nonsensePath, { flags: initFlags })
.then(function() {
assert.fail("Should have thrown an error.");
})
.catch(function(error) {
assert(error, "Should have thrown an error.");
});
});
it("can get the head commit", function() {
return this.repository.getHeadCommit()
.then(function(commit) {
assert.equal(
commit.toString(),
"32789a79e71fbc9e04d3eff7425e1771eb595150"
);
});
});
it("returns null if there is no head commit", function() {
return this.emptyRepo.getHeadCommit()
.then(function(commit) {
assert(!commit);
});
});
it("can commit on head on a empty repo with createCommitOnHead", function() {
const fileName = "my-new-file-that-shouldnt-exist.file";
const fileContent = "new file from repository test";
const repo = this.emptyRepo;
const filePath = path.join(repo.workdir(), fileName);
const commitMsg = "Doug this has been commited";
let authSig;
let commitSig;
return repo.defaultSignature()
.then((sig) => {
authSig = sig;
commitSig = sig;
return fse.writeFile(filePath, fileContent);
})
.then(() => {
return repo.createCommitOnHead(
[fileName],
authSig,
commitSig,
commitMsg
);
})
.then((oidResult) => {
return repo.getHeadCommit()
.then(function(commit) {
assert.equal(
commit.toString(),
oidResult.toString()
);
});
});
});
it("can get all merge heads in a repo with mergeheadForeach", function() {
var repo;
var repoPath = local("../repos/merge-head");
var ourBranchName = "ours";
var theirBranchName = "theirs";
var theirBranch;
var fileName = "testFile.txt";
var numMergeHeads = 0;
var assertBranchTargetIs = function (theirBranch, mergeHead) {
assert.equal(theirBranch.target(), mergeHead.toString());
numMergeHeads++;
};
return RepoUtils.createRepository(repoPath)
.then(function(_repo) {
repo = _repo;
return IndexUtils.createConflict(
repo,
ourBranchName,
theirBranchName,
fileName
);
})
.then(function() {
return repo.getBranch(theirBranchName);
})
.then(function(_theirBranch) {
// Write the MERGE_HEAD file manually since createConflict does not
theirBranch = _theirBranch;
return fse.writeFile(
path.join(repoPath, ".git", "MERGE_HEAD"),
theirBranch.target().toString() + "\n"
);
})
.then(function() {
return repo.mergeheadForeach(
assertBranchTargetIs.bind(this, theirBranch)
);
})
.then(function() {
assert.equal(numMergeHeads, 1);
});
});
it("can obtain statistics from a valid constant repository", function() {
return this.constRepository.statistics()
.then(function(analysisReport) {
assert.equal(analysisReport.repositorySize.commits.count, 992);
assert.equal(analysisReport.repositorySize.commits.size, 265544);
assert.equal(analysisReport.repositorySize.trees.count, 2416);
assert.equal(analysisReport.repositorySize.trees.size, 1188325);
assert.equal(analysisReport.repositorySize.trees.entries, 32571);
assert.equal(analysisReport.repositorySize.blobs.count, 4149);
assert.equal(analysisReport.repositorySize.blobs.size, 48489622);
assert.equal(analysisReport.repositorySize.annotatedTags.count, 1);
assert.equal(analysisReport.repositorySize.references.count, 8);
assert.equal(analysisReport.biggestObjects.commits.maxSize, 956);
assert.equal(analysisReport.biggestObjects.commits.maxParents, 2);
assert.equal(analysisReport.biggestObjects.trees.maxEntries, 93);
assert.equal(analysisReport.biggestObjects.blobs.maxSize, 1077756);
assert.equal(analysisReport.historyStructure.maxDepth, 931);
assert.equal(analysisReport.historyStructure.maxTagDepth, 1);
assert.equal(analysisReport.biggestCheckouts.numDirectories, 128);
assert.equal(analysisReport.biggestCheckouts.maxPathDepth, 10);
assert.equal(analysisReport.biggestCheckouts.maxPathLength, 107);
assert.equal(analysisReport.biggestCheckouts.numFiles, 514);
assert.equal(analysisReport.biggestCheckouts.totalFileSize, 5160886);
assert.equal(analysisReport.biggestCheckouts.numSymlinks, 2);
assert.equal(analysisReport.biggestCheckouts.numSubmodules, 4);
// console.log(JSON.stringify(analysisReport,null,2));
});
});
});