Skip to content

Commit f171e04

Browse files
committed
Add Commit::create_with_signature
This lets you pass in a buffer (e.g from Commit::create_to_s) and the contents of a signature and the resulting commit will have the signature attached at the correct place in the header.
1 parent 0bb5564 commit f171e04

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

ext/rugged/rugged_commit.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,12 +758,51 @@ static VALUE rb_git_commit_create_to_s(VALUE self, VALUE rb_repo, VALUE rb_data)
758758
return ret;
759759
}
760760

761+
/*
762+
* call-seq:
763+
* Rugged::Commit.create_with_signature(repo, content, signature, field_name = "gpgsig") -> oid
764+
*
765+
* Create a commit from the +content+ string and the +signature+,
766+
* adding this data to the +field_name+ header field in the resulting
767+
* commit.
768+
*
769+
* Returns the new commit's object id.
770+
*
771+
*/
772+
static VALUE rb_git_commit_create_with_signature(int argc, VALUE *argv, VALUE self)
773+
{
774+
int error;
775+
git_oid id;
776+
const char *field = NULL;
777+
git_repository *repo;
778+
VALUE rb_repo, rb_content, rb_signature, rb_field = Qnil;
779+
780+
rb_scan_args(argc, argv, "31", &rb_repo, &rb_content, &rb_signature, &rb_field);
781+
782+
rugged_check_repo(rb_repo);
783+
Data_Get_Struct(rb_repo, git_repository, repo);
784+
785+
Check_Type(rb_content, T_STRING);
786+
Check_Type(rb_signature, T_STRING);
787+
788+
if (!NIL_P(rb_field)) {
789+
Check_Type(rb_field, T_STRING);
790+
field = StringValueCStr(rb_field);
791+
}
792+
793+
error = git_commit_create_with_signature(&id, repo, StringValueCStr(rb_content), StringValueCStr(rb_signature), field);
794+
rugged_exception_check(error);
795+
796+
return rugged_create_oid(&id);
797+
}
798+
761799
void Init_rugged_commit(void)
762800
{
763801
rb_cRuggedCommit = rb_define_class_under(rb_mRugged, "Commit", rb_cRuggedObject);
764802

765803
rb_define_singleton_method(rb_cRuggedCommit, "create", rb_git_commit_create, 2);
766804
rb_define_singleton_method(rb_cRuggedCommit, "create_to_s", rb_git_commit_create_to_s, 2);
805+
rb_define_singleton_method(rb_cRuggedCommit, "create_with_signature", rb_git_commit_create_with_signature, -1);
767806
rb_define_singleton_method(rb_cRuggedCommit, "extract_signature", rb_git_commit_extract_signature, -1);
768807

769808
rb_define_method(rb_cRuggedCommit, "message", rb_git_commit_message_GET, 0);

test/commit_test.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,74 @@ def test_extract_signature
280280
# Ask for a non-existent object
281281
assert_raises(Rugged::OdbError) { Rugged::Commit.extract_signature(@repo, "181037049a54a1eb5fab404658a3a250b44335d8") }
282282
end
283+
284+
def test_create_with_signature
285+
signed_commit = <<-COMMIT
286+
tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6
287+
parent 34734e478d6cf50c27c9d69026d93974d052c454
288+
author Ben Burkert <[email protected]> 1358451456 -0800
289+
committer Ben Burkert <[email protected]> 1358451456 -0800
290+
gpgsig -----BEGIN PGP SIGNATURE-----
291+
Version: GnuPG v1.4.12 (Darwin)
292+
293+
iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al
294+
o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8
295+
JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq
296+
AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq
297+
SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW
298+
who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok
299+
6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG
300+
cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu
301+
c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9
302+
ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J
303+
7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc
304+
cpxtDQQMGYFpXK/71stq
305+
=ozeK
306+
-----END PGP SIGNATURE-----
307+
308+
a simple commit which works
309+
COMMIT
310+
311+
signature = <<-SIGNATURE.strip
312+
-----BEGIN PGP SIGNATURE-----
313+
Version: GnuPG v1.4.12 (Darwin)
314+
315+
iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al
316+
o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8
317+
JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq
318+
AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq
319+
SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW
320+
who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok
321+
6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG
322+
cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu
323+
c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9
324+
ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J
325+
7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc
326+
cpxtDQQMGYFpXK/71stq
327+
=ozeK
328+
-----END PGP SIGNATURE-----
329+
SIGNATURE
330+
331+
base_data = <<-SIGNEDDATA
332+
tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6
333+
parent 34734e478d6cf50c27c9d69026d93974d052c454
334+
author Ben Burkert <[email protected]> 1358451456 -0800
335+
committer Ben Burkert <[email protected]> 1358451456 -0800
336+
337+
a simple commit which works
338+
SIGNEDDATA
339+
340+
id1 = Rugged::Commit::create_with_signature(@repo, base_data, signature, "gpgsig")
341+
id2 = Rugged::Commit::create_with_signature(@repo, base_data, signature)
342+
sig, data = Rugged::Commit::extract_signature(@repo, id1)
343+
344+
assert_equal id1, id2
345+
assert_equal base_data, data
346+
assert_equal signature, sig
347+
348+
raw_commit = Rugged::Commit::lookup(@repo, id1).read_raw.data
349+
assert_equal signed_commit, raw_commit
350+
end
283351
end
284352

285353
class CommitWriteTest < Rugged::TestCase

0 commit comments

Comments
 (0)