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

Skip to content

Commit 474bd2c

Browse files
committed
Merge pull request libgit2#3617 from libgit2/cmn/extract-sig-errors
commit: expose the different kinds of errors
2 parents 1aa1492 + eadd0f0 commit 474bd2c

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

include/git2/commit.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,18 @@ GIT_EXTERN(int) git_commit_header_field(git_buf *out, const git_commit *commit,
266266
/**
267267
* Extract the signature from a commit
268268
*
269+
* If the id is not for a commit, the error class will be
270+
* `GITERR_INVALID`. If the commit does not have a signature, the
271+
* error class will be `GITERR_OBJECT`.
272+
*
269273
* @param signature the signature block
270274
* @param signed_data signed data; this is the commit contents minus the signature block
271275
* @param repo the repository in which the commit exists
272276
* @param commit_id the commit from which to extract the data
273277
* @param field the name of the header field containing the signature
274278
* block; pass `NULL` to extract the default 'gpgsig'
279+
* @return 0 on success, GIT_ENOTFOUND if the id is not for a commit
280+
* or the commit does not have a signature.
275281
*/
276282
GIT_EXTERN(int) git_commit_extract_signature(git_buf *signature, git_buf *signed_data, git_repository *repo, git_oid *commit_id, const char *field);
277283

src/commit.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,12 @@ int git_commit_extract_signature(git_buf *signature, git_buf *signed_data, git_r
642642
if ((error = git_odb_read(&obj, odb, commit_id)) < 0)
643643
return error;
644644

645+
if (obj->cached.type != GIT_OBJ_COMMIT) {
646+
giterr_set(GITERR_INVALID, "the requested type does not match the type in ODB");
647+
error = GIT_ENOTFOUND;
648+
goto cleanup;
649+
}
650+
645651
buf = git_odb_object_data(obj);
646652

647653
while ((h = strchr(buf, '\n')) && h[1] != '\0' && h[1] != '\n') {
@@ -688,7 +694,7 @@ int git_commit_extract_signature(git_buf *signature, git_buf *signed_data, git_r
688694
return git_buf_puts(signed_data, eol+1);
689695
}
690696

691-
giterr_set(GITERR_INVALID, "this commit is not signed");
697+
giterr_set(GITERR_OBJECT, "this commit is not signed");
692698
error = GIT_ENOTFOUND;
693699
goto cleanup;
694700

tests/commit/parse.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,17 @@ a simple commit which works\n";
513513
cl_assert_equal_s(gpgsig, signature.ptr);
514514
cl_assert_equal_s(data, signed_data.ptr);
515515

516+
/* Try to parse a tree */
517+
cl_git_pass(git_oid_fromstr(&commit_id, "45dd856fdd4d89b884c340ba0e047752d9b085d6"));
518+
cl_git_fail_with(GIT_ENOTFOUND, git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, NULL));
519+
cl_assert_equal_i(GITERR_INVALID, giterr_last()->klass);
520+
521+
/* Try to parse an unsigned commit */
522+
cl_git_pass(git_odb_write(&commit_id, odb, passing_commit_cases[1], strlen(passing_commit_cases[1]), GIT_OBJ_COMMIT));
523+
cl_git_fail_with(GIT_ENOTFOUND, git_commit_extract_signature(&signature, &signed_data, g_repo, &commit_id, NULL));
524+
cl_assert_equal_i(GITERR_OBJECT, giterr_last()->klass);
525+
516526
git_buf_free(&signature);
517527
git_buf_free(&signed_data);
528+
518529
}

0 commit comments

Comments
 (0)