Visitar URL original
adds support for gpg commit signing (fixes #1018) · nodegit/nodegit@a957def · GitHub
Skip to content

Commit a957def

Browse files
committed
adds support for gpg commit signing (fixes #1018)
1 parent 210d02a commit a957def

2 files changed

Lines changed: 89 additions & 2 deletions

File tree

‎lib/repository.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,34 @@ Repository.prototype.createCommit = function(
586586
}, callback);
587587
};
588588

589+
/**
590+
* Create a commit that is digitally signed
591+
*
592+
* @async
593+
* @param {String} commit_content
594+
* @param {String} signature
595+
* @param {String} signature_field
596+
* @return {Oid} The oid of the commit
597+
*/
598+
Repository.prototype.createCommitWithSignature = function(
599+
commit_content, signature, signature_field, callback) {
600+
601+
var repo = this;
602+
603+
return Commit.createWithSignature(
604+
repo,
605+
commit_content,
606+
signature,
607+
signature_field || "gpgsig"
608+
).then(function(commit) {
609+
if (typeof callback === "function") {
610+
callback(null, commit);
611+
}
612+
613+
return commit;
614+
}, callback);
615+
};
616+
589617
/**
590618
* Creates a new commit on HEAD from the list of passed in files
591619
*

‎test/tests/commit.js‎

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,6 @@ describe("Commit", function() {
199199
});
200200
});
201201

202-
203202
it("can amend commit", function(){
204203
var commitToAmendId = "315e77328ef596f3bc065d8ac6dd2c72c09de8a5";
205204
var expectedAmendedCommitId = "57836e96555243666ea74ea888310cc7c41d4613";
@@ -720,7 +719,67 @@ describe("Commit", function() {
720719
});
721720

722721
describe("Commit's Signature", function() {
723-
it.only("Can retrieve the gpg signature from a commit", function() {
722+
723+
it("Can create a signed commit", function() {
724+
var expectedCommitId = "b78570159824e6fc161418a626a646933a10ea89";
725+
726+
// https://github.com/libgit2/libgit2/blob/master/tests/commit/write.c#L362-L384
727+
var signature = "-----BEGIN PGP SIGNATURE-----\n" +
728+
"Version: GnuPG v1.4.12 (Darwin)\n" +
729+
"\n" +
730+
"iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al\n" +
731+
"o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8\n" +
732+
"JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq\n" +
733+
"AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq\n" +
734+
"SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW\n" +
735+
"who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok\n" +
736+
"6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG\n" +
737+
"cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu\n" +
738+
"c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9\n" +
739+
"ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J\n" +
740+
"7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc\n" +
741+
"cpxtDQQMGYFpXK/71stq\n" +
742+
"=ozeK\n" +
743+
"-----END PGP SIGNATURE-----";
744+
745+
var commit_content = "tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6\n" +
746+
"parent 34734e478d6cf50c27c9d69026d93974d052c454\n" +
747+
"author Ben Burkert <ben@benburkert.com> 1358451456 -0800\n" +
748+
"committer Ben Burkert <ben@benburkert.com> 1358451456 -0800\n" +
749+
"\n" +
750+
"a simple commit which works\n";
751+
752+
var repo;
753+
var commit;
754+
755+
return NodeGit.Repository.open(reposPath)
756+
.then(function(repoResult) {
757+
repo = repoResult;
758+
return repo.createCommitWithSignature(
759+
commit_content,
760+
signature);
761+
})
762+
.then(function(commitId) {
763+
assert.equal(expectedCommitId, commitId);
764+
return NodeGit.Commit.lookup(repo, commitId);
765+
})
766+
.then(function(commitResult) {
767+
commit = commitResult;
768+
return commit.getSignature();
769+
})
770+
.then(function(signatureInfoDefault) {
771+
assert.equal(signature, signatureInfoDefault.signature);
772+
assert.equal(commit_content, signatureInfoDefault.signedData);
773+
774+
return commit.getSignature("gpgsig");
775+
})
776+
.then(function(signatureInfo) {
777+
assert.equal(signature, signatureInfo.signature);
778+
assert.equal(commit_content, signatureInfo.signedData);
779+
});
780+
});
781+
782+
it("Can retrieve the gpg signature from a commit", function() {
724783
var expectedSignedData =
725784
"tree f4661419a6fbbe865f78644fec722c023ce4b65f\n" +
726785
"parent 32789a79e71fbc9e04d3eff7425e1771eb595150\n" +

0 commit comments

Comments
 (0)