Skip to content

Commit f286aaa

Browse files
committed
Merge pull request nodegit#279 from nodegit/new-test-branch
Tests for branch class
2 parents 5b869a1 + e1caaa5 commit f286aaa

11 files changed

Lines changed: 199 additions & 50 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,12 @@ var open = require("nodegit").Repository.open;
134134
open("tmp")
135135
// Open the master branch.
136136
.then(function(repo) {
137-
return repo.getMaster();
137+
return repo.getMasterCommit();
138138
})
139139
// Display information about commits on master.
140-
.then(function(branch) {
140+
.then(function(firstCommitOnMaster) {
141141
// Create a new history event emitter.
142-
var history = branch.history();
142+
var history = firstCommitOnMaster.history();
143143

144144
// Create a counter to only show up to 9 entries.
145145
var count = 0;

example/apps/git_profanity_check.js

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,25 @@ else {
3535
}
3636

3737
// Open repository.
38-
git.Repo.open(path, function(err, repo) {
39-
if (err) {
40-
throw new Error(err);
41-
}
42-
38+
git.Repo.open(path)
39+
.then(function(repo) {
4340
// Open branch, default to master.
44-
repo.getBranch(branch, function(err, branch) {
45-
if (err) {
46-
throw new Error(err);
41+
return repo.getBranchCommit(branch);
42+
}).then(function(firstCommit) {
43+
// Iterate history
44+
var history = firstCommit.history();
45+
46+
// Iterate over every commit message and test for words.
47+
history.on('commit', function(commit) {
48+
var message = commit.message();
49+
50+
if (reCurse.test(message)) {
51+
console.log('Curse detected in commit', commit.sha());
52+
console.log('=> ', message);
53+
return;
4754
}
48-
49-
// Iterate history
50-
var history = branch.history();
51-
52-
// Iterate over every commit message and test for words.
53-
history.on('commit', function(commit) {
54-
var message = commit.message();
55-
56-
if (reCurse.test(message)) {
57-
console.log('Curse detected in commit', commit.sha());
58-
console.log('=> ', message);
59-
return;
60-
}
61-
});
62-
63-
// Start history iteration.
64-
history.start();
6555
});
56+
57+
// Start history iteration.
58+
history.start();
6659
});

example/walk-history.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ var nodegit = require('../'),
66
// that look very similar to calling `git log` from the command line
77

88
nodegit.Repository.open(path.resolve(__dirname, '../.git')).then(function(repo) {
9-
return repo.getMaster();
10-
}).then(function(branch){
9+
return repo.getMasterCommit();
10+
}).then(function(firstCommitOnMaster){
1111
// History returns an event.
12-
var history = branch.history(sort.Time);
12+
var history = firstCommitOnMaster.history(sort.Time);
1313

1414
// History emits 'commit' event for each commit in the branch's history
1515
history.on('commit', function(commit) {

example/walk-tree.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ var nodegit = require('../'),
66
// tree (directory), or a file.
77

88
nodegit.Repository.open(path.resolve(__dirname, '../.git')).then(function(repo) {
9-
return repo.getMaster();
10-
}).then(function(branch) {
11-
return branch.getTree();
9+
return repo.getMasterCommit();
10+
}).then(function(firstCommitOnMaster) {
11+
return firstCommitOnMaster.getTree();
1212
}).then(function(tree) {
1313
// `walk()` returns an event.
1414
var walker = tree.walk();

generate/descriptor.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,32 @@
8585
},
8686
"branch": {
8787
"functions": {
88+
"git_branch_create": {
89+
"args": {
90+
"force": {
91+
"isOptional": true
92+
},
93+
"signature": {
94+
"isOptional": true
95+
},
96+
"log_message": {
97+
"isOptional": true
98+
}
99+
}
100+
},
88101
"git_branch_next": {
89102
"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
90114
}
91115
}
92116
},

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: 62 additions & 7 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
2258
*
2359
* @param {String} name Branch name, e.g. "master"
2460
* @param {Function} callback
25-
* @return {Branch}
61+
* @return {Ref}
2662
*/
2763
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.
77+
*
78+
* @param {String} name Branch name, e.g. "master"
79+
* @param {Function} callback
80+
* @return {Commit}
81+
*/
82+
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);
@@ -262,13 +317,13 @@ Repository.prototype.createRevWalk = function() {
262317
};
263318

264319
/**
265-
* Retrieve the master branch.
320+
* Retrieve the master branch commit.
266321
*
267322
* @param {Function} callback
268-
* @return {Branch}
323+
* @return {Commit}
269324
*/
270-
Repository.prototype.getMaster = function(callback) {
271-
return this.getBranch("master", callback);
325+
Repository.prototype.getMasterCommit = function(callback) {
326+
return this.getBranchCommit("master", callback);
272327
};
273328

274329
/**

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+
});

test/tests/commit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ describe("Commit", function() {
177177
it("can fetch the master branch HEAD", function() {
178178
var repository = this.repository;
179179

180-
return repository.getBranch("master").then(function(branch) {
181-
return repository.getCommit(branch.sha());
180+
return repository.getBranchCommit("master").then(function(commit) {
181+
return repository.getCommit(commit.sha());
182182
});
183183
});
184184

0 commit comments

Comments
 (0)