diff --git a/lib/repository.js b/lib/repository.js index 37e9833ff..729bb1415 100644 --- a/lib/repository.js +++ b/lib/repository.js @@ -665,17 +665,18 @@ Repository.prototype.mergeBranches = function(to, from, signature) { signature = signature || repo.defaultSignature(); return Promise.all([ - repo.getBranch(to), - repo.getBranch(from) - ]).then(function(branches) { - toBranch = branches[0]; - fromBranch = branches[1]; + repo.getBranch(to), + repo.getBranch(from) + ]).then(function(objects) { + toBranch = objects[0]; + fromBranch = objects[1]; return Promise.all([ repo.getBranchCommit(toBranch), repo.getBranchCommit(fromBranch) ]); - }).then(function(branchCommits) { + }) + .then(function(branchCommits) { var toCommitOid = branchCommits[0].toString(); var fromCommitOid = branchCommits[1].toString(); @@ -694,12 +695,24 @@ Repository.prototype.mergeBranches = function(to, from, signature) { " to branch " + fromBranch.shorthand(); - return toBranch.setTarget( - fromCommitOid, - signature, - message) + return branchCommits[1].getTree() + .then(function(tree) { + if (toBranch.isHead()) { + // Checkout the tree if we're on the branch + var opts = { + checkoutStrategy: NodeGit.Checkout.STRATEGY.SAFE_CREATE + }; + return NodeGit.Checkout.tree(repo, tree, opts); + } + }) .then(function() { - return fromCommitOid; + return toBranch.setTarget( + fromCommitOid, + signature, + message) + .then(function() { + return fromCommitOid; + }); }); } else { @@ -804,24 +817,29 @@ Repository.prototype.getStatusExt = function(opts) { */ Repository.prototype.checkoutBranch = function(branch, opts) { var repo = this; - + var reference; opts = opts || {}; - opts.checkoutStrategy = opts.checkoutStrategy || Checkout.STRATEGY.SAFE; - + opts.checkoutStrategy = opts.checkoutStrategy || + Checkout.STRATEGY.SAFE_CREATE; return repo.getReference(branch) .then(function(ref) { if (!ref.isBranch()) { return false; } - - var name = ref.name(); - + reference = ref; + return repo.getBranchCommit(ref.name()); + }) + .then(function(commit) { + return commit.getTree(); + }) + .then(function(tree) { + return Checkout.tree(repo, tree, opts); + }) + .then(function() { + var name = reference.name(); return repo.setHead(name, repo.defaultSignature(), "Switch HEAD to " + name); - }) - .then(function() { - return Checkout.head(repo, opts); }); }; diff --git a/test/tests/checkout.js b/test/tests/checkout.js index fbe565125..05c3105a9 100644 --- a/test/tests/checkout.js +++ b/test/tests/checkout.js @@ -86,18 +86,20 @@ describe("Checkout", function() { it("can checkout a branch", function() { var test = this; - return test.repository.checkoutBranch(checkoutBranchName, { - checkoutStrategy: Checkout.STRATEGY.FORCE - }) + return test.repository.checkoutBranch(checkoutBranchName) .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 - }); + return test.repository.getStatus(); + }) + .then(function(statuses) { + assert.equal(statuses.length, 0); + }) + .then(function() { + return test.repository.checkoutBranch("master"); }) .then(function() { var packageContent = fse.readFileSync(packageJsonPath, "utf-8"); diff --git a/test/tests/merge.js b/test/tests/merge.js index 14f4b4a2f..b08de4bc3 100644 --- a/test/tests/merge.js +++ b/test/tests/merge.js @@ -214,6 +214,10 @@ describe("Merge", function() { theirCommit = commit; }); }) + .then(function() { + var opts = {checkoutStrategy: NodeGit.Checkout.STRATEGY.FORCE}; + return repository.checkoutBranch(ourBranchName, opts); + }) .then(function() { return repository.mergeBranches( ourBranchName, @@ -228,6 +232,13 @@ describe("Merge", function() { .then(function(branchCommit) { assert.equal(oid.toString(), branchCommit.toString()); }); + }) + .then(function() { + return repository.getStatus(); + }) + .then(function(statuses) { + // make sure we didn't change the index + assert.equal(statuses.length, 0); }); });