FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add checkoutBranch convenience method by johnhaley81 · Pull Request #466 · nodegit/nodegit · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .js  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
29 changes: 28 additions & 1 deletion lib/repository.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var Promise = require("nodegit-promise");
var NodeGit = require("../");
var Blob = NodeGit.Blob;
var Checkout = NodeGit.Checkout;
var Commit = NodeGit.Commit;
var normalizeOptions = NodeGit.Utils.normalizeOptions;
var Reference = NodeGit.Reference;
Expand Down Expand Up @@ -599,7 +600,6 @@ Repository.prototype.getRemote = function(remote, callback) {
Repository.prototype.fetch = function(
remote,
remoteCallbacks,

callback)
{
var repo = this;
Expand Down Expand Up @@ -786,4 +786,31 @@ Repository.prototype.getStatusExt = function(opts) {
});
};

/**
* This will set the HEAD to point to the local branch and then attempt
* to update the index and working tree to match the content of the
* latest commit on that branch
* @param {String|Reference} branch the branch to checkout
* @param {Object|CheckoutOptions} opts the options to use for the checkout
*/
Repository.prototype.checkoutBranch = function(branch, opts) {
var repo = this;

opts = opts || {};
opts.checkoutStrategy = opts.checkoutStrategy || Checkout.STRATEGY.SAFE;

return repo.getReference(branch)
.then(function(ref) {
if (!ref.isBranch()) {
return false;
}

var name = ref.name();

repo.setHead(name, repo.defaultSignature(), "Switch HEAD to " + name);

return Checkout.head(repo, opts);
});
};

module.exports = Repository;
12 changes: 9 additions & 3 deletions test/runner.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ before(function() {
.then(function() {
return exec("git checkout rev-walk", {cwd: workdirPath});
})
.then(function() {
return exec("git checkout checkout-test", {cwd: workdirPath});
})
.then(function() {
return exec("git checkout master", {cwd: workdirPath});
})
Expand All @@ -41,9 +44,12 @@ before(function() {

beforeEach(function() {
return exec("git clean -xdf", {cwd: workdirPath})
.then(function() {
return exec("git reset --hard", {cwd: workdirPath});
});
.then(function() {
return exec("git checkout master", {cwd: workdirPath});
})
.then(function() {
return exec("git reset --hard", {cwd: workdirPath});
});
});

afterEach(function(done) {
Expand Down
32 changes: 26 additions & 6 deletions test/tests/checkout.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ describe("Checkout", function() {

var readMeName = "README.md";
var packageJsonName = "package.json";
var packageJsonOid = "0fa56e90e096a4c24c785206b826ab914ea3de1e";
var reposPath = local("../repos/workdir");
var readMePath = local("../repos/workdir/" + readMeName);
var packageJsonPath = local("../repos/workdir/" + packageJsonName);
var checkoutBranchName = "checkout-test";

beforeEach(function() {
var test = this;
Expand All @@ -28,13 +28,10 @@ describe("Checkout", function() {
var test = this;

return Checkout.head(test.repository)
.then(function() {
return test.repository.getBlob(packageJsonOid);
})
.then(function(blob) {
var packageJson = blob.toString();
var packageContent = fse.readFileSync(packageJsonPath, "utf-8");

assert.ok(~packageJson.indexOf("\"ejs\": \"~1.0.0\","));
assert.ok(~packageContent.indexOf("\"ejs\": \"~1.0.0\","));
});
});

Expand Down Expand Up @@ -85,4 +82,27 @@ describe("Checkout", function() {
assert.equal(commit, "32789a79e71fbc9e04d3eff7425e1771eb595150");
});
});

it("can checkout a branch", function() {
var test = this;

return test.repository.checkoutBranch(checkoutBranchName, {
checkoutStrategy: Checkout.STRATEGY.FORCE
})
.then(function() {
var packageContent = fse.readFileSync(packageJsonPath, "utf-8");

assert.ok(!~packageContent.indexOf("\"ejs\": \"~1.0.0\","));
})
.then(function() {
return test.repository.checkoutBranch("master", {
checkoutStrategy: Checkout.STRATEGY.FORCE
});
})
.then(function() {
var packageContent = fse.readFileSync(packageJsonPath, "utf-8");

assert.ok(~packageContent.indexOf("\"ejs\": \"~1.0.0\","));
});
});
});

Back | FazBrowse Home | New Git URL