Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 488e2b8

Browse files
author
Vicent Marti
committed
Merge pull request libgit2#3599 from libgit2/gpgsign
Introduce git_commit_extract_signature
2 parents 534cc5a + a65afb7 commit 488e2b8

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

include/git2/commit.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,18 @@ GIT_EXTERN(int) git_commit_nth_gen_ancestor(
263263
*/
264264
GIT_EXTERN(int) git_commit_header_field(git_buf *out, const git_commit *commit, const char *field);
265265

266+
/**
267+
* Extract the signature from a commit
268+
*
269+
* @param signature the signature block
270+
* @param signed_data signed data; this is the commit contents minus the signature block
271+
* @param repo the repository in which the commit exists
272+
* @param commit_id the commit from which to extract the data
273+
* @param field the name of the header field containing the signature
274+
* block; pass `NULL` to extract the default 'gpgsig'
275+
*/
276+
GIT_EXTERN(int) git_commit_extract_signature(git_buf *signature, git_buf *signed_data, git_repository *repo, git_oid *commit_id, const char *field);
277+
266278
/**
267279
* Create new commit in the repository from a list of `git_object` pointers
268280
*

src/commit.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,3 +621,89 @@ int git_commit_header_field(git_buf *out, const git_commit *commit, const char *
621621
giterr_set_oom();
622622
return -1;
623623
}
624+
625+
int git_commit_extract_signature(git_buf *signature, git_buf *signed_data, git_repository *repo, git_oid *commit_id, const char *field)
626+
{
627+
git_odb_object *obj;
628+
git_odb *odb;
629+
const char *buf;
630+
const char *h, *eol;
631+
int error;
632+
633+
git_buf_sanitize(signature);
634+
git_buf_sanitize(signed_data);
635+
636+
if (!field)
637+
field = "gpgsig";
638+
639+
if ((error = git_repository_odb__weakptr(&odb, repo)) < 0)
640+
return error;
641+
642+
if ((error = git_odb_read(&obj, odb, commit_id)) < 0)
643+
return error;
644+
645+
buf = git_odb_object_data(obj);
646+
647+
while ((h = strchr(buf, '\n')) && h[1] != '\0' && h[1] != '\n') {
648+
h++;
649+
if (git__prefixcmp(buf, field)) {
650+
if (git_buf_put(signed_data, buf, h - buf) < 0)
651+
return -1;
652+
653+
buf = h;
654+
continue;
655+
}
656+
657+
h = buf;
658+
h += strlen(field);
659+
eol = strchr(h, '\n');
660+
if (h[0] != ' ') {
661+
buf = h;
662+
continue;
663+
}
664+
if (!eol)
665+
goto malformed;
666+
667+
h++; /* skip the SP */
668+
669+
git_buf_put(signature, h, eol - h);
670+
if (git_buf_oom(signature))
671+
goto oom;
672+
673+
/* If the next line starts with SP, it's multi-line, we must continue */
674+
while (eol[1] == ' ') {
675+
git_buf_putc(signature, '\n');
676+
h = eol + 2;
677+
eol = strchr(h, '\n');
678+
if (!eol)
679+
goto malformed;
680+
681+
git_buf_put(signature, h, eol - h);
682+
}
683+
684+
if (git_buf_oom(signature))
685+
goto oom;
686+
687+
git_odb_object_free(obj);
688+
return git_buf_puts(signed_data, eol+1);
689+
}
690+
691+
giterr_set(GITERR_INVALID, "this commit is not signed");
692+
error = GIT_ENOTFOUND;
693+
goto cleanup;
694+
695+
malformed:
696+
giterr_set(GITERR_OBJECT, "malformed header");
697+
error = -1;
698+
goto cleanup;
699+
oom:
700+
giterr_set_oom();
701+
error = -1;
702+
goto cleanup;
703+
704+
cleanup:
705+
git_odb_object_free(obj);
706+
git_buf_clear(signature);
707+
git_buf_clear(signed_data);
708+
return error;
709+
}

tests/commit/parse.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,3 +460,52 @@ cpxtDQQMGYFpXK/71stq\n\
460460
git_buf_free(&buf);
461461
git_commit__free(commit);
462462
}
463+
464+
void test_commit_parse__extract_signature(void)
465+
{
466+
git_odb *odb;
467+
git_oid commit_id;
468+
git_buf signature = GIT_BUF_INIT, signed_data = GIT_BUF_INIT;
469+
const char *gpgsig = "-----BEGIN PGP SIGNATURE-----\n\
470+
Version: GnuPG v1.4.12 (Darwin)\n\
471+
\n\
472+
iQIcBAABAgAGBQJQ+FMIAAoJEH+LfPdZDSs1e3EQAJMjhqjWF+WkGLHju7pTw2al\n\
473+
o6IoMAhv0Z/LHlWhzBd9e7JeCnanRt12bAU7yvYp9+Z+z+dbwqLwDoFp8LVuigl8\n\
474+
JGLcnwiUW3rSvhjdCp9irdb4+bhKUnKUzSdsR2CK4/hC0N2i/HOvMYX+BRsvqweq\n\
475+
AsAkA6dAWh+gAfedrBUkCTGhlNYoetjdakWqlGL1TiKAefEZrtA1TpPkGn92vbLq\n\
476+
SphFRUY9hVn1ZBWrT3hEpvAIcZag3rTOiRVT1X1flj8B2vGCEr3RrcwOIZikpdaW\n\
477+
who/X3xh/DGbI2RbuxmmJpxxP/8dsVchRJJzBwG+yhwU/iN3MlV2c5D69tls/Dok\n\
478+
6VbyU4lm/ae0y3yR83D9dUlkycOnmmlBAHKIZ9qUts9X7mWJf0+yy2QxJVpjaTGG\n\
479+
cmnQKKPeNIhGJk2ENnnnzjEve7L7YJQF6itbx5VCOcsGh3Ocb3YR7DMdWjt7f8pu\n\
480+
c6j+q1rP7EpE2afUN/geSlp5i3x8aXZPDj67jImbVCE/Q1X9voCtyzGJH7MXR0N9\n\
481+
ZpRF8yzveRfMH8bwAJjSOGAFF5XkcR/RNY95o+J+QcgBLdX48h+ZdNmUf6jqlu3J\n\
482+
7KmTXXQcOVpN6dD3CmRFsbjq+x6RHwa8u1iGn+oIkX908r97ckfB/kHKH7ZdXIJc\n\
483+
cpxtDQQMGYFpXK/71stq\n\
484+
=ozeK\n\
485+
-----END PGP SIGNATURE-----";
486+
487+
const char *data = "tree 6b79e22d69bf46e289df0345a14ca059dfc9bdf6\n\
488+
parent 34734e478d6cf50c27c9d69026d93974d052c454\n\
489+
author Ben Burkert <[email protected]> 1358451456 -0800\n\
490+
committer Ben Burkert <[email protected]> 1358451456 -0800\n\
491+
\n\
492+
a simple commit which works\n";
493+
494+
495+
cl_git_pass(git_repository_odb__weakptr(&odb, g_repo));
496+
cl_git_pass(git_odb_write(&commit_id, odb, passing_commit_cases[4], strlen(passing_commit_cases[4]), GIT_OBJ_COMMIT));
497+
498+
cl_git_pass(git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, NULL));
499+
cl_assert_equal_s(gpgsig, signature.ptr);
500+
cl_assert_equal_s(data, signed_data.ptr);
501+
502+
git_buf_clear(&signature);
503+
git_buf_clear(&signed_data);
504+
505+
cl_git_pass(git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, "gpgsig"));
506+
cl_assert_equal_s(gpgsig, signature.ptr);
507+
cl_assert_equal_s(data, signed_data.ptr);
508+
509+
git_buf_free(&signature);
510+
git_buf_free(&signed_data);
511+
}

0 commit comments

Comments
 (0)