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>

Christian Couder committed Nov 17, 2025 at 05:34 UTC cb034c020aba54360e7c19faf82021399bf131e7
2 files changed +22 -2
commit.c
+15 -2
@@ -1315,7 +1315,8 @@ free_return:
1315 free(buf);
1316 }
1317
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)
1320 {
1321 struct strbuf payload = STRBUF_INIT;
1322 struct strbuf signature = STRBUF_INIT;
@@ -1323,7 +1324,8 @@ int check_commit_signature(const struct commit *commit, struct signature_check *
1324
1325 sigc->result = 'N';
1326
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)
1329 goto out;
1330
1331 sigc->payload_type = SIGNATURE_PAYLOAD_COMMIT;
@@ -1337,6 +1339,17 @@ int check_commit_signature(const struct commit *commit, struct signature_check *
1339 return ret;
1340 }
1341
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 +
1353 void verify_merge_signature(struct commit *commit, int verbosity,
1354 int check_trust)
1355 {
commit.h
+7
@@ -333,6 +333,13 @@ int remove_signature(struct strbuf *buf);
333 */
334 int check_commit_signature(const struct commit *commit, struct signature_check *sigc);
335
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 +
343 /* record author-date for each commit object */
344 struct author_date_slab;
345 void record_author_date(struct author_date_slab *author_date,