merge: extract verify_merge_signature() helper

The logic to implement "merge --verify-signatures" is inline in cmd_merge(), but this site misses some cases. Let's extract the logic into a function so we can call it from more places. We'll move it to commit.[ch], since one of the callers (git-pull) is outside our source file. This function isn't all that general (after all, its main function is to exit the program) but it's not worth trying to fix that. The heavy lifting is done by check_commit_signature(), and our purpose here is just sharing the die() logic. We'll mark it with a comment to make that clear. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Nov 6, 2018 at 02:50 UTC edc4d47d54cb1c0ec1550aa50bba86b23720a72f
3 files changed +34 -25
builtin/merge.c
+1 -25
@@ -1355,31 +1355,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1355
1356 if (verify_signatures) {
1357 for (p = remoteheads; p; p = p->next) {
1358 - struct commit *commit = p->item;
1359 - char hex[GIT_MAX_HEXSZ + 1];
1360 - struct signature_check signature_check;
1361 - memset(&signature_check, 0, sizeof(signature_check));
1362 -
1363 - check_commit_signature(commit, &signature_check);
1364 -
1365 - find_unique_abbrev_r(hex, &commit->object.oid, DEFAULT_ABBREV);
1366 - switch (signature_check.result) {
1367 - case 'G':
1368 - break;
1369 - case 'U':
1370 - die(_("Commit %s has an untrusted GPG signature, "
1371 - "allegedly by %s."), hex, signature_check.signer);
1372 - case 'B':
1373 - die(_("Commit %s has a bad GPG signature "
1374 - "allegedly by %s."), hex, signature_check.signer);
1375 - default: /* 'N' */
1376 - die(_("Commit %s does not have a GPG signature."), hex);
1377 - }
1378 - if (verbosity >= 0 && signature_check.result == 'G')
1379 - printf(_("Commit %s has a good GPG signature by %s\n"),
1380 - hex, signature_check.signer);
1381 -
1382 - signature_check_clear(&signature_check);
1358 + verify_merge_signature(p->item, verbosity);
1359 }
1360 }
1361
commit.c
+26
@@ -1379,7 +1379,33 @@ int check_commit_signature(const struct commit *commit, struct signature_check *
1379 return ret;
1380 }
1381
1382 +void verify_merge_signature(struct commit *commit, int verbosity)
1383 +{
1384 + char hex[GIT_MAX_HEXSZ + 1];
1385 + struct signature_check signature_check;
1386 + memset(&signature_check, 0, sizeof(signature_check));
1387 +
1388 + check_commit_signature(commit, &signature_check);
1389 +
1390 + find_unique_abbrev_r(hex, &commit->object.oid, DEFAULT_ABBREV);
1391 + switch (signature_check.result) {
1392 + case 'G':
1393 + break;
1394 + case 'U':
1395 + die(_("Commit %s has an untrusted GPG signature, "
1396 + "allegedly by %s."), hex, signature_check.signer);
1397 + case 'B':
1398 + die(_("Commit %s has a bad GPG signature "
1399 + "allegedly by %s."), hex, signature_check.signer);
1400 + default: /* 'N' */
1401 + die(_("Commit %s does not have a GPG signature."), hex);
1402 + }
1403 + if (verbosity >= 0 && signature_check.result == 'G')
1404 + printf(_("Commit %s has a good GPG signature by %s\n"),
1405 + hex, signature_check.signer);
1406
1407 + signature_check_clear(&signature_check);
1408 +}
1409
1410 void append_merge_tag_headers(struct commit_list *parents,
1411 struct commit_extra_header ***tail)
commit.h
+7
@@ -357,6 +357,13 @@ extern int remove_signature(struct strbuf *buf);
357 */
358 extern int check_commit_signature(const struct commit *commit, struct signature_check *sigc);
359
360 +/*
361 + * Verify a single commit with check_commit_signature() and die() if it is not
362 + * a good signature. This isn't really suitable for general use, but is a
363 + * helper to implement consistent logic for pull/merge --verify-signatures.
364 + */
365 +void verify_merge_signature(struct commit *commit, int verbose);
366 +
367 int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused);
368 int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void *unused);
369