From 6608751ad4f2f79126ac93e029660fde6df4b5c8 Mon Sep 17 00:00:00 2001 From: Kyle Smith Date: Thu, 3 Mar 2016 17:28:09 -0700 Subject: [PATCH 1/2] Tests: generalized createConflict in index utils Leave the repository in a conflicted state and return the index rather than the IndexEntries of the ancestor, ours, and theirs --- test/tests/index.js | 6 +++++- test/utils/index_setup.js | 22 +++++++--------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/test/tests/index.js b/test/tests/index.js index a1f91b204..ff0fb6d35 100644 --- a/test/tests/index.js +++ b/test/tests/index.js @@ -311,7 +311,7 @@ describe("Index", function() { it("can add a conflict to the index", function() { var repo; - var repoPath = path.join(__dirname, "..", "repos", "index"); + var repoPath = local("../repos/index"); var ourBranchName = "ours"; var theirBranchName = "theirs"; var fileName = "testFile.txt"; @@ -329,6 +329,10 @@ describe("Index", function() { fileName ); }) + .then(function(index) { + assert.ok(index.hasConflicts()); + return index.conflictGet(fileName); + }) .then(function(indexEntries) { // Store all indexEntries for conflict ancestorIndexEntry = indexEntries.ancestor_out; diff --git a/test/utils/index_setup.js b/test/utils/index_setup.js index a55e95924..eafcd5b52 100644 --- a/test/utils/index_setup.js +++ b/test/utils/index_setup.js @@ -1,4 +1,3 @@ -var assert = require("assert"); var NodeGit = require("../../"); var path = require("path"); var promisify = require("promisify-node"); @@ -75,25 +74,18 @@ var IndexSetup = { return repository.createCommit(ourBranch.name(), ourSignature, ourSignature, "we made a commit", oid, [ourCommit]); }) - .then(function(commitOid) { - var opts = { - checkoutStrategy: NodeGit.Checkout.STRATEGY.FORCE - }; - - return NodeGit.Checkout.head(repository, opts); + .then(function() { + return repository.checkoutBranch( + ourBranch, + new NodeGit.CheckoutOptions() + ); }) .then(function() { return repository.mergeBranches(ourBranchName, theirBranchName); }) - .then(function(commit) { - assert.fail(commit, undefined, - "The index should have been thrown due to merge conflicts"); - }) .catch(function(index) { - assert.ok(index); - assert.ok(index.hasConflicts()); - - return index.conflictGet(fileName); + return NodeGit.Checkout.index(repository, index) + .then(function() { return index; }); }); } }; From b60cc8e546ca992e5527bf76fd0f9f5e3b4c5c80 Mon Sep 17 00:00:00 2001 From: Kyle Smith Date: Thu, 3 Mar 2016 17:58:23 -0700 Subject: [PATCH 2/2] Exposed function `mergeheadForeach` --- generate/input/descriptor.json | 5 +++- lib/repository.js | 10 ++++++++ test/tests/repository.js | 46 ++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/generate/input/descriptor.json b/generate/input/descriptor.json index f7aef8c27..f51d219ba 100644 --- a/generate/input/descriptor.json +++ b/generate/input/descriptor.json @@ -1759,7 +1759,10 @@ "ignore": true }, "git_repository_mergehead_foreach": { - "ignore": true + "isAsync": true, + "return": { + "isErrorCode": true + } }, "git_repository_message": { "ignore": true diff --git a/lib/repository.js b/lib/repository.js index c6c851b70..8004d944d 100644 --- a/lib/repository.js +++ b/lib/repository.js @@ -1200,6 +1200,16 @@ Repository.prototype.fetchheadForeach = function(callback) { return fetchheadForeach.call(this, callback, null); }; +var mergeheadForeach = Repository.prototype.mergeheadForeach; +/** + * @async + * @param {MergeheadForeachCb} callback The callback function to be called on + * each entry + */ +Repository.prototype.mergeheadForeach = function(callback) { + return mergeheadForeach.call(this, callback, null); +}; + /** * Stages or unstages line selection of a specified file * diff --git a/test/tests/repository.js b/test/tests/repository.js index 6f710ef10..5f7555ac4 100644 --- a/test/tests/repository.js +++ b/test/tests/repository.js @@ -3,6 +3,8 @@ var path = require("path"); var promisify = require("promisify-node"); var fse = promisify(require("fs-extra")); var local = path.join.bind(path, __dirname); +var IndexUtils = require("../utils/index_setup"); +var RepoUtils = require("../utils/repository_setup"); describe("Repository", function() { var NodeGit = require("../../"); @@ -260,4 +262,48 @@ describe("Repository", function() { }); }); }); + + it("can get all merge heads in a repo with mergeheadForeach", function() { + var repo; + var repoPath = local("../repos/merge-head"); + var ourBranchName = "ours"; + var theirBranchName = "theirs"; + var theirBranch; + var fileName = "testFile.txt"; + var numMergeHeads = 0; + var assertBranchTargetIs = function (theirBranch, mergeHead) { + assert.equal(theirBranch.target(), mergeHead.toString()); + numMergeHeads++; + }; + + return RepoUtils.createRepository(repoPath) + .then(function(_repo) { + repo = _repo; + return IndexUtils.createConflict( + repo, + ourBranchName, + theirBranchName, + fileName + ); + }) + .then(function() { + return repo.getBranch(theirBranchName); + }) + .then(function(_theirBranch) { + // Write the MERGE_HEAD file manually since createConflict does not + theirBranch = _theirBranch; + return fse.writeFile( + path.join(repoPath, ".git", "MERGE_HEAD"), + theirBranch.target().toString() + "\n" + ); + }) + .then(function() { + return repo.mergeheadForeach( + assertBranchTargetIs.bind(this, theirBranch) + ); + }) + .then(function() { + assert.equal(numMergeHeads, 1); + }); + }); });