Skip to content

Commit e1caaa5

Browse files
author
John Haley
committed
Added tests for branch
create delete isHead
1 parent dd5acb9 commit e1caaa5

5 files changed

Lines changed: 150 additions & 7 deletions

File tree

generate/descriptor.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@
8585
},
8686
"branch": {
8787
"functions": {
88-
"git_branch_next": {
89-
"ignore": true
90-
},
9188
"git_branch_create": {
9289
"args": {
9390
"force": {
@@ -100,6 +97,20 @@
10097
"isOptional": true
10198
}
10299
}
100+
},
101+
"git_branch_next": {
102+
"ignore": true
103+
},
104+
"git_branch_set_upstream": {
105+
"isAsync": false,
106+
"args": {
107+
"upstream_name": {
108+
"isOptional": true
109+
}
110+
}
111+
},
112+
"git_branch_upstream": {
113+
"isAsync": false
103114
}
104115
}
105116
},

lib/branch.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var NodeGit = require("../");
2+
3+
var Branch = NodeGit.Branch;
4+
5+
module.exports = Branch;

lib/refs.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var NodeGit = require("../");
22

33
var Reference = NodeGit.Refs;
4+
var Branch = NodeGit.Branch;
45

56
Reference.Type = {
67
Oid: 1,
@@ -32,4 +33,12 @@ Reference.prototype.toString = function() {
3233
return this.name();
3334
};
3435

36+
/**
37+
* Returns if the ref is pointed at by HEAD
38+
* @return {bool}
39+
*/
40+
Reference.prototype.isHead = function() {
41+
return Branch.isHead(this);
42+
};
43+
3544
module.exports = Reference;

lib/repository.js

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,71 @@ Object.defineProperty(Repository.prototype, "openIndex", {
1818
});
1919

2020
/**
21-
* Look up a branch"s most recent commit.
21+
* Creates a branch with the passed in name pointing to the commit
22+
*
23+
* @param {String} name Branch name, e.g. "master"
24+
* @param {Commit|String|Oid} commit The commit the branch will point to
25+
* @param {bool} force Overwrite branch if it exists
26+
* @param {Signature} signature Identity to use to populate reflog
27+
* @param {String} logMessage One line message to be appended to the reflog
28+
* @return {Ref}
29+
*/
30+
Repository.prototype.createBranch =
31+
function(name, commit, force, signature, logMessage) {
32+
var repo = this;
33+
34+
if (commit instanceof Commit) {
35+
return NodeGit.Branch.create(
36+
repo,
37+
name,
38+
commit,
39+
force ? 1 : 0,
40+
signature,
41+
logMessage);
42+
}
43+
else {
44+
return repo.getCommit(commit).then(function(commit) {
45+
return NodeGit.Branch.create(
46+
repo,
47+
name,
48+
commit,
49+
force ? 1 : 0,
50+
signature,
51+
logMessage);
52+
});
53+
}
54+
};
55+
56+
/**
57+
* Look up a branch
58+
*
59+
* @param {String} name Branch name, e.g. "master"
60+
* @param {Function} callback
61+
* @return {Ref}
62+
*/
63+
Repository.prototype.getBranch = function(name, callback) {
64+
name = ~name.indexOf("refs/heads/") ? name : "refs/heads/" + name;
65+
66+
return this.getReference(name).then(function(reference) {
67+
if (typeof callback === "function") {
68+
callback(null, reference);
69+
}
70+
71+
return reference;
72+
}, callback);
73+
};
74+
75+
/**
76+
* Look up a branch's most recent commit.
2277
*
2378
* @param {String} name Branch name, e.g. "master"
2479
* @param {Function} callback
25-
* @return {Branch}
80+
* @return {Commit}
2681
*/
2782
Repository.prototype.getBranchCommit = function(name, callback) {
2883
var repository = this;
2984

30-
return this.getReference("refs/heads/" + name).then(function(reference) {
85+
return this.getBranch(name).then(function(reference) {
3186
return repository.getCommit(reference.target()).then(function(commit) {
3287
if (typeof callback === "function") {
3388
callback(null, commit);
@@ -265,7 +320,7 @@ Repository.prototype.createRevWalk = function() {
265320
* Retrieve the master branch commit.
266321
*
267322
* @param {Function} callback
268-
* @return {Branch}
323+
* @return {Commit}
269324
*/
270325
Repository.prototype.getMasterCommit = function(callback) {
271326
return this.getBranchCommit("master", callback);

test/tests/branch.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
var assert = require("assert");
2+
var path = require("path");
3+
4+
describe("Branch", function() {
5+
var reposPath = path.resolve("test/repos/workdir/.git");
6+
7+
var Repository = require("../../lib/repository");
8+
var Branch = require("../../lib/branch");
9+
var branchName = "test-branch";
10+
var fullBranchName = "refs/heads/" + branchName;
11+
12+
before(function() {
13+
var test = this;
14+
15+
return Repository.open(reposPath).then(function(repository) {
16+
test.repo = repository;
17+
});
18+
});
19+
20+
beforeEach(function() {
21+
var test = this;
22+
var repo = test.repo;
23+
24+
return repo.getMasterCommit().then(function(masterCommit) {
25+
test.masterCommit = masterCommit;
26+
27+
return repo.createBranch(branchName, masterCommit, true)
28+
.then(function(branch) {
29+
test.branch = branch;
30+
});
31+
});
32+
});
33+
34+
it("can create a branch", function() {
35+
var branch = this.branch;
36+
var masterCommit = this.masterCommit;
37+
38+
assert.equal(branch.name(), fullBranchName);
39+
assert.equal(branch.target().toString(), masterCommit.sha());
40+
});
41+
42+
it("can delete a branch", function() {
43+
var repo = this.repo;
44+
45+
Branch.delete(this.branch);
46+
47+
return repo.getBranch(branchName).then(function() {
48+
// branch wasn't deleted
49+
assert.ok(false);
50+
}, function() {
51+
// branch was deleted!
52+
assert.ok(true);
53+
});
54+
});
55+
56+
it("can see if the branch is pointed to by head", function() {
57+
var repo = this.repo;
58+
59+
return repo.getBranch("master").then(function(branch) {
60+
assert.ok(branch.isHead());
61+
});
62+
});
63+
});

0 commit comments

Comments
 (0)