Skip to content

Commit 3ff031d

Browse files
committed
Merge pull request nodegit#466 from nodegit/add-checkout-branch
Add checkoutBranch convenience method
2 parents 25feff1 + 9e6d958 commit 3ff031d

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

lib/repository.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Promise = require("nodegit-promise");
22
var NodeGit = require("../");
33
var Blob = NodeGit.Blob;
4+
var Checkout = NodeGit.Checkout;
45
var Commit = NodeGit.Commit;
56
var normalizeOptions = NodeGit.Utils.normalizeOptions;
67
var Reference = NodeGit.Reference;
@@ -599,7 +600,6 @@ Repository.prototype.getRemote = function(remote, callback) {
599600
Repository.prototype.fetch = function(
600601
remote,
601602
remoteCallbacks,
602-
603603
callback)
604604
{
605605
var repo = this;
@@ -786,4 +786,31 @@ Repository.prototype.getStatusExt = function(opts) {
786786
});
787787
};
788788

789+
/**
790+
* This will set the HEAD to point to the local branch and then attempt
791+
* to update the index and working tree to match the content of the
792+
* latest commit on that branch
793+
* @param {String|Reference} branch the branch to checkout
794+
* @param {Object|CheckoutOptions} opts the options to use for the checkout
795+
*/
796+
Repository.prototype.checkoutBranch = function(branch, opts) {
797+
var repo = this;
798+
799+
opts = opts || {};
800+
opts.checkoutStrategy = opts.checkoutStrategy || Checkout.STRATEGY.SAFE;
801+
802+
return repo.getReference(branch)
803+
.then(function(ref) {
804+
if (!ref.isBranch()) {
805+
return false;
806+
}
807+
808+
var name = ref.name();
809+
810+
repo.setHead(name, repo.defaultSignature(), "Switch HEAD to " + name);
811+
812+
return Checkout.head(repo, opts);
813+
});
814+
};
815+
789816
module.exports = Repository;

test/runner.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ before(function() {
2727
.then(function() {
2828
return exec("git checkout rev-walk", {cwd: workdirPath});
2929
})
30+
.then(function() {
31+
return exec("git checkout checkout-test", {cwd: workdirPath});
32+
})
3033
.then(function() {
3134
return exec("git checkout master", {cwd: workdirPath});
3235
})
@@ -41,9 +44,12 @@ before(function() {
4144

4245
beforeEach(function() {
4346
return exec("git clean -xdf", {cwd: workdirPath})
44-
.then(function() {
45-
return exec("git reset --hard", {cwd: workdirPath});
46-
});
47+
.then(function() {
48+
return exec("git checkout master", {cwd: workdirPath});
49+
})
50+
.then(function() {
51+
return exec("git reset --hard", {cwd: workdirPath});
52+
});
4753
});
4854

4955
afterEach(function(done) {

test/tests/checkout.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ describe("Checkout", function() {
1010

1111
var readMeName = "README.md";
1212
var packageJsonName = "package.json";
13-
var packageJsonOid = "0fa56e90e096a4c24c785206b826ab914ea3de1e";
1413
var reposPath = local("../repos/workdir");
1514
var readMePath = local("../repos/workdir/" + readMeName);
1615
var packageJsonPath = local("../repos/workdir/" + packageJsonName);
16+
var checkoutBranchName = "checkout-test";
1717

1818
beforeEach(function() {
1919
var test = this;
@@ -28,13 +28,10 @@ describe("Checkout", function() {
2828
var test = this;
2929

3030
return Checkout.head(test.repository)
31-
.then(function() {
32-
return test.repository.getBlob(packageJsonOid);
33-
})
3431
.then(function(blob) {
35-
var packageJson = blob.toString();
32+
var packageContent = fse.readFileSync(packageJsonPath, "utf-8");
3633

37-
assert.ok(~packageJson.indexOf("\"ejs\": \"~1.0.0\","));
34+
assert.ok(~packageContent.indexOf("\"ejs\": \"~1.0.0\","));
3835
});
3936
});
4037

@@ -85,4 +82,27 @@ describe("Checkout", function() {
8582
assert.equal(commit, "32789a79e71fbc9e04d3eff7425e1771eb595150");
8683
});
8784
});
85+
86+
it("can checkout a branch", function() {
87+
var test = this;
88+
89+
return test.repository.checkoutBranch(checkoutBranchName, {
90+
checkoutStrategy: Checkout.STRATEGY.FORCE
91+
})
92+
.then(function() {
93+
var packageContent = fse.readFileSync(packageJsonPath, "utf-8");
94+
95+
assert.ok(!~packageContent.indexOf("\"ejs\": \"~1.0.0\","));
96+
})
97+
.then(function() {
98+
return test.repository.checkoutBranch("master", {
99+
checkoutStrategy: Checkout.STRATEGY.FORCE
100+
});
101+
})
102+
.then(function() {
103+
var packageContent = fse.readFileSync(packageJsonPath, "utf-8");
104+
105+
assert.ok(~packageContent.indexOf("\"ejs\": \"~1.0.0\","));
106+
});
107+
});
88108
});

0 commit comments

Comments
 (0)