Skip to content

Instantly share code, notes, and snippets.

@tjklemz
Last active April 19, 2016 22:51
Show Gist options
  • Save tjklemz/18a018f1ba34fe557456b363aff0d21b to your computer and use it in GitHub Desktop.
Save tjklemz/18a018f1ba34fe557456b363aff0d21b to your computer and use it in GitHub Desktop.
function merge (repo, remotePath) {
var remoteName = 'superman';
var remoteBranch = 'master'
var remoteHeadRef = 'refs/remotes/' + remoteName + '/HEAD';
var remoteMasterRef = 'refs/remotes/' + remoteName + '/master';
var ourBranchName = 'master';
var newBranchName = 'wonderwoman';
NodeGit.Remote.create(repo, remoteName, remotePath);
return repo.fetch(remoteName).then(function () {
return NodeGit.Reference.symbolicCreate(repo, remoteHeadRef, remoteMasterRef, 1, 'symbolic-ref');
}).then(function () {
return repo.getReference(remoteMasterRef);
}).then(function (ref) {
return repo.createBranch(newBranchName, ref.target());
}).then(function () {
return repo.mergeBranches(newBranchName, remoteName + '/' + remoteBranch, null, NodeGit.Merge.PREFERENCE.NO_FASTFORWARD);
}).then(function () {
var ours = repo.getBranchCommit(ourBranchName);
var theirs = repo.getBranchCommit(newBranchName);
return Promise.all([ours, theirs]);
}).then(function (commits) {
return {
ourCommit: commits[0],
theirCommit: commits[1]
};
}).then(function (commits) {
return NodeGit.Merge.commits(repo, commits.ourCommit, commits.theirCommit, {
fileFavor: NodeGit.Merge.FILE_FAVOR.THEIRS
}).then(function (index) {
commits.index = index;
return commits;
});
}).then(function (commits) {
if (commits.index.hasConflicts()) {
commits.index.conflictCleanup();
}
commits.index.write();
return index.writeTreeTo(repo).then(function (oid) {
commits.mergeCommit = oid;
return commits;
});
}).then(function (commits) {
var sig = repo.defaultSignature();
var parents = [commits.ourCommit, commits.theirCommit];
return repo.createCommit('refs/heads/' + ourBranchName, sig, sig, 'Merging...', commits.mergeCommit, parents);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment