Skip to content

Commit 500bafb

Browse files
chriscoolgitster
authored andcommitted
commit: refactor verify_commit_buffer()
In a following commit, we are going to check commit signatures, but we won't have a commit yet, only a commit buffer, and we are going to discard this commit buffer if the signature is invalid. So it would be wasteful to create a commit that we might discard, just to be able to check a commit signature. It would be simpler instead to be able to check commit signatures using only a commit buffer instead of a commit. To be able to do that, let's extract some code from the check_commit_signature() function into a new verify_commit_buffer() function, and then let's make check_commit_signature() call verify_commit_buffer(). Note that this doesn't fundamentally change how check_commit_signature() works. It used to call parse_signed_commit() which calls repo_get_commit_buffer(), parse_buffer_signed_by_header() and repo_unuse_commit_buffer(). Now these 3 functions are called directly by verify_commit_buffer(). Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 25f87d7 commit 500bafb

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

commit.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,15 +1315,17 @@ static void handle_signed_tag(const struct commit *parent, struct commit_extra_h
13151315
free(buf);
13161316
}
13171317

1318-
int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
1318+
int verify_commit_buffer(const char *buffer, size_t size,
1319+
struct signature_check *sigc)
13191320
{
13201321
struct strbuf payload = STRBUF_INIT;
13211322
struct strbuf signature = STRBUF_INIT;
13221323
int ret = 1;
13231324

13241325
sigc->result = 'N';
13251326

1326-
if (parse_signed_commit(commit, &payload, &signature, the_hash_algo) <= 0)
1327+
if (parse_buffer_signed_by_header(buffer, size, &payload,
1328+
&signature, the_hash_algo) <= 0)
13271329
goto out;
13281330

13291331
sigc->payload_type = SIGNATURE_PAYLOAD_COMMIT;
@@ -1337,6 +1339,17 @@ int check_commit_signature(const struct commit *commit, struct signature_check *
13371339
return ret;
13381340
}
13391341

1342+
int check_commit_signature(const struct commit *commit, struct signature_check *sigc)
1343+
{
1344+
unsigned long size;
1345+
const char *buffer = repo_get_commit_buffer(the_repository, commit, &size);
1346+
int ret = verify_commit_buffer(buffer, size, sigc);
1347+
1348+
repo_unuse_commit_buffer(the_repository, commit, buffer);
1349+
1350+
return ret;
1351+
}
1352+
13401353
void verify_merge_signature(struct commit *commit, int verbosity,
13411354
int check_trust)
13421355
{

commit.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,13 @@ int remove_signature(struct strbuf *buf);
333333
*/
334334
int check_commit_signature(const struct commit *commit, struct signature_check *sigc);
335335

336+
/*
337+
* Same as check_commit_signature() but accepts a commit buffer and
338+
* its size, instead of a `struct commit *`.
339+
*/
340+
int verify_commit_buffer(const char *buffer, size_t size,
341+
struct signature_check *sigc);
342+
336343
/* record author-date for each commit object */
337344
struct author_date_slab;
338345
void record_author_date(struct author_date_slab *author_date,

0 commit comments

Comments
 (0)