From f2d9e3f0db90566d0f37759a851d71cbab1d5adc Mon Sep 17 00:00:00 2001 From: Tyler Ang-Wanek Date: Mon, 9 Apr 2018 09:28:37 -0700 Subject: [PATCH] Add repo.createCommitBuffer --- generate/input/descriptor.json | 22 ++++++++++- lib/repository.js | 39 +++++++++++++++++- test/tests/commit.js | 72 ++++++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 2 deletions(-) diff --git a/generate/input/descriptor.json b/generate/input/descriptor.json index 7ef3cb57d..d309a5fd9 100644 --- a/generate/input/descriptor.json +++ b/generate/input/descriptor.json @@ -524,7 +524,27 @@ } }, "git_commit_create_buffer": { - "ignore": true + "args": { + "out": { + "isReturn": true, + "cppClassName": "GitBuf", + "jsClassName": "Buffer", + "shouldAlloc": true + }, + "message_encoding": { + "isOptional": true + }, + "parents": { + "cType": "const git_commit **", + "cppClassName": "Array", + "jsClassName": "Array", + "arrayElementCppClassName": "GitCommit" + } + }, + "isAsync": true, + "return": { + "isErrorCode": true + } }, "git_commit_create_from_callback": { "ignore": true diff --git a/lib/repository.js b/lib/repository.js index d876b1241..01dbfff59 100644 --- a/lib/repository.js +++ b/lib/repository.js @@ -537,7 +537,7 @@ Repository.prototype.createBlobFromBuffer = function(buffer) { * @param {Signature} author * @param {Signature} committer * @param {String} message - * @param {Tree|Oid|String} Tree + * @param {Oid|String} Tree * @param {Array} parents * @return {Oid} The oid of the commit */ @@ -586,6 +586,43 @@ Repository.prototype.createCommit = function( }, callback); }; +/** + * Create a commit + * + * @async + * @param {Signature} author + * @param {Signature} committer + * @param {String} message + * @param {Oid|String} treeOid + * @param {Array} parents + * @return {String} The content of the commit object + * as a string + */ +Repository.prototype.createCommitBuffer = function( + author, committer, message, treeOid, parents) { + + const repo = this; + const promises = (parents || []) + .reduce(function(acc, parent) { + acc.push(repo.getCommit(parent)); + return acc; + }, [repo.getTree(treeOid)]); + + return Promise.all(promises) + .then(function([tree, ...parentCommits]) { + return Commit.createBuffer( + repo, + author, + committer, + null /* use default message encoding */, + message, + tree, + parentCommits.length, + parentCommits + ); + }); +}; + /** * Creates a new commit on HEAD from the list of passed in files * diff --git a/test/tests/commit.js b/test/tests/commit.js index 181a1461b..2a109f07a 100644 --- a/test/tests/commit.js +++ b/test/tests/commit.js @@ -199,6 +199,78 @@ describe("Commit", function() { }); }); + it("can create a commit as a buffer", function() { + var test = this; + var fileName = "newfile.txt"; + var fileContent = "hello world"; + + const expectedCommitContent = + "tree 11c8685af551550e73e5ab89fa554576bd92ef3f\n" + + "parent 32789a79e71fbc9e04d3eff7425e1771eb595150\n" + + "author Foo Bar 123456789 +0100\n" + + "committer Foo A Bar 987654321 +0130\n\n" + + "message"; + + var repo; + var index; + var treeOid; + var parent; + + return NodeGit.Repository.open(reposPath) + .then(function(repoResult) { + repo = repoResult; + return fse.writeFile(path.join(repo.workdir(), fileName), fileContent); + }) + .then(function() { + return repo.refreshIndex(); + }) + .then(function(indexResult) { + index = indexResult; + }) + .then(function() { + return index.addByPath(fileName); + }) + .then(function() { + return index.write(); + }) + .then(function() { + return index.writeTree(); + }) + .then(function(oidResult) { + treeOid = oidResult; + return NodeGit.Reference.nameToId(repo, "HEAD"); + }) + .then(function(head) { + return repo.getCommit(head); + }) + .then(function(parentResult) { + parent = parentResult; + return Promise.all([ + NodeGit.Signature.create("Foo Bar", "foo@bar.com", 123456789, 60), + NodeGit.Signature.create("Foo A Bar", "foo@bar.com", 987654321, 90) + ]); + }) + .then(function(signatures) { + var author = signatures[0]; + var committer = signatures[1]; + + return repo.createCommitBuffer( + author, + committer, + "message", + treeOid, + [parent]); + }) + .then(function(commitContent) { + assert.equal(expectedCommitContent, commitContent); + return reinitialize(test); + }, function(reason) { + return reinitialize(test) + .then(function() { + return Promise.reject(reason); + }); + }); + }); it("can amend commit", function(){ var commitToAmendId = "315e77328ef596f3bc065d8ac6dd2c72c09de8a5";