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
22 changes: 21 additions & 1 deletion generate/input/descriptor.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 38 additions & 1 deletion lib/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*
Expand Down
72 changes: 72 additions & 0 deletions test/tests/commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <foo@bar.com> 123456789 +0100\n" +
"committer Foo A Bar <foo@bar.com> 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";
Expand Down