Skip to content

Commit edc4d47

Browse files
peffgitster
authored andcommitted
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>
1 parent cae598d commit edc4d47

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

builtin/merge.c

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,31 +1355,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
13551355

13561356
if (verify_signatures) {
13571357
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);
13831359
}
13841360
}
13851361

commit.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,33 @@ int check_commit_signature(const struct commit *commit, struct signature_check *
13791379
return ret;
13801380
}
13811381

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);
13821406

1407+
signature_check_clear(&signature_check);
1408+
}
13831409

13841410
void append_merge_tag_headers(struct commit_list *parents,
13851411
struct commit_extra_header ***tail)

commit.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,13 @@ extern int remove_signature(struct strbuf *buf);
357357
*/
358358
extern int check_commit_signature(const struct commit *commit, struct signature_check *sigc);
359359

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+
360367
int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused);
361368
int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void *unused);
362369

0 commit comments

Comments
 (0)