Skip to content

Commit b7fc308

Browse files
author
John Haley
committed
Fix getHeadCommit dying on empty repo
If you open a repo in NodeGit that's empty (i.e. has no commits yet) doing `Repository.prototype.getHeadCommit` will error in the promise chain. I think that really should just return null instead of killing the promise chain. There are also probably more of these in the library and we'll have to add them as we find them.
1 parent 0256bfd commit b7fc308

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

lib/repository.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,13 @@ Repository.prototype.getMasterCommit = function(callback) {
431431
Repository.prototype.getHeadCommit = function(callback) {
432432
var repo = this;
433433

434-
return Reference.nameToId(repo, "HEAD").then(function(head) {
435-
return repo.getCommit(head, callback);
436-
});
434+
return Reference.nameToId(repo, "HEAD")
435+
.then(function(head) {
436+
return repo.getCommit(head, callback);
437+
})
438+
.catch(function() {
439+
return null;
440+
});
437441
};
438442

439443
/**

test/tests/repository.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ describe("Repository", function() {
1212
var Signature = NodeGit.Signature;
1313

1414
var reposPath = local("../repos/workdir");
15-
var newRepo = local("../repos/newrepo");
15+
var newRepoPath = local("../repos/newrepo");
16+
var emptyRepoPath = local("../repos/empty");
1617

1718
beforeEach(function() {
1819
var test = this;
1920

2021
return Repository.open(reposPath)
2122
.then(function(repository) {
2223
test.repository = repository;
24+
})
25+
.then(function() {
26+
return Repository.open(emptyRepoPath);
27+
})
28+
.then(function(emptyRepo) {
29+
test.emptyRepo = emptyRepo;
2330
});
2431
});
2532

@@ -44,16 +51,16 @@ describe("Repository", function() {
4451
});
4552

4653
it("can initialize a repository into a folder", function() {
47-
return Repository.init(newRepo, 1)
54+
return Repository.init(newRepoPath, 1)
4855
.then(function(path, isBare) {
49-
return Repository.open(newRepo);
56+
return Repository.open(newRepoPath);
5057
});
5158
});
5259

5360
it("can utilize repository init options", function() {
54-
return fse.remove(newRepo)
61+
return fse.remove(newRepoPath)
5562
.then(function() {
56-
return Repository.initExt(newRepo, {
63+
return Repository.initExt(newRepoPath, {
5764
flags: Repository.INIT_FLAG.MKPATH
5865
});
5966
});
@@ -183,12 +190,29 @@ describe("Repository", function() {
183190
var initFlags = NodeGit.Repository.INIT_FLAG.NO_REINIT |
184191
NodeGit.Repository.INIT_FLAG.MKPATH |
185192
NodeGit.Repository.INIT_FLAG.MKDIR;
186-
return fse.remove(newRepo)
193+
return fse.remove(newRepoPath)
187194
.then(function() {
188-
return NodeGit.Repository.initExt(newRepo, { flags: initFlags });
195+
return NodeGit.Repository.initExt(newRepoPath, { flags: initFlags });
189196
})
190197
.then(function() {
191-
return NodeGit.Repository.open(newRepo);
198+
return NodeGit.Repository.open(newRepoPath);
199+
});
200+
});
201+
202+
it("can get the head commit", function() {
203+
return this.repository.getHeadCommit()
204+
.then(function(commit) {
205+
assert.equal(
206+
commit.toString(),
207+
"32789a79e71fbc9e04d3eff7425e1771eb595150"
208+
);
209+
});
210+
});
211+
212+
it("returns null if there is no head commit", function() {
213+
return this.emptyRepo.getHeadCommit()
214+
.then(function(commit) {
215+
assert(!commit);
192216
});
193217
});
194218
});

0 commit comments

Comments
 (0)