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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion generate/input/descriptor.json
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,15 @@
}
},
"git_checkout_index": {
"ignore": true
"args": {
"opts": {
"isOptional": true
}
},
"isAsync": true,
"return": {
"isErrorCode": true
}
},
"git_checkout_tree": {
"args": {
Expand Down
24 changes: 20 additions & 4 deletions lib/checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ var NodeGit = require("../");
var normalizeOptions = NodeGit.Utils.normalizeOptions;

var Checkout = NodeGit.Checkout;
var head = Checkout.head;
var tree = Checkout.tree;
var _head = Checkout.head;
var _index = Checkout.index;
var _tree = Checkout.tree;

/**
* Patch head checkout to automatically coerce objects.
Expand All @@ -16,7 +17,22 @@ var tree = Checkout.tree;
Checkout.head = function(url, options) {
options = normalizeOptions(options, NodeGit.CheckoutOptions);

return head.call(this, url, options);
return _head.call(this, url, options);
};

/**
* Patch index checkout to automatically coerce objects.
*
* @async
* @param {Repository} repo The repo to checkout an index
* @param {Index} The index to checkout
* @param {CheckoutOptions} [options] Options for the checkout
* @return {Void} checkout complete
*/
Checkout.index = function(repo, index, options) {
options = normalizeOptions(options, NodeGit.CheckoutOptions);

return _index.call(this, repo, index, options);
};

/**
Expand All @@ -31,5 +47,5 @@ Checkout.head = function(url, options) {
Checkout.tree = function(repo, treeish, options) {
options = normalizeOptions(options, NodeGit.CheckoutOptions);

return tree.call(this, repo, treeish, options);
return _tree.call(this, repo, treeish, options);
};
75 changes: 75 additions & 0 deletions test/tests/checkout.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var assert = require("assert");
var path = require("path");
var Promise = require("nodegit-promise");
var fse = require("fs-extra");
var local = path.join.bind(path, __dirname);

Expand Down Expand Up @@ -107,4 +108,78 @@ describe("Checkout", function() {
assert.ok(~packageContent.indexOf("\"ejs\": \"~1.0.0\","));
});
});

it("can checkout an index with conflicts", function() {
var test = this;

var testBranchName = "test";
var ourCommit;

return test.repository.getBranchCommit(checkoutBranchName)
.then(function(commit) {
ourCommit = commit;

return test.repository.createBranch(testBranchName, commit.id());
})
.then(function() {
return test.repository.checkoutBranch(testBranchName);
})
.then(function(branch) {
fse.writeFileSync(packageJsonPath, "\n");

return test.repository.openIndex()
.then(function(index) {
index.read(1);
index.addByPath(packageJsonName);
index.write();

return index.writeTree();
});
})
.then(function(oid) {
assert.equal(oid.toString(),
"85135ab398976a4d5be6a8704297a45f2b1e7ab2");

var signature = test.repository.defaultSignature();

return test.repository.createCommit("refs/heads/" + testBranchName,
signature, signature, "we made breaking changes", oid, [ourCommit]);
})
.then(function(commit) {
return Promise.all([
test.repository.getBranchCommit(testBranchName),
test.repository.getBranchCommit("master")
]);
})
.then(function(commits) {
return NodeGit.Merge.commits(test.repository, commits[0], commits[1],
null);
})
.then(function(index) {
assert.ok(index);
assert.ok(index.hasConflicts && index.hasConflicts());

return NodeGit.Checkout.index(test.repository, index);
})
.then(function() {
// Verify that the conflict has been written to disk
var conflictedContent = fse.readFileSync(packageJsonPath, "utf-8");

assert.ok(~conflictedContent.indexOf("<<<<<<< ours"));
assert.ok(~conflictedContent.indexOf("======="));
assert.ok(~conflictedContent.indexOf(">>>>>>> theirs"));

// Cleanup
var opts = {
checkoutStrategy: Checkout.STRATEGY.FORCE,
paths: packageJsonName
};

return Checkout.head(test.repository, opts);
})
.then(function() {
var finalContent = fse.readFileSync(packageJsonPath, "utf-8");
assert.equal(finalContent, "\n");
});
});
});