builtin/tag: add --format argument for tag -v

Adding --format to git tag -v mutes the default output of the GPG verification and instead prints the formatted tag object. This allows callers to cross-check the tagname from refs/tags with the tagname from the tag object header upon GPG verification. The callback function for for_each_tag_name() didn't allow callers to pass custom data to their callback functions. Add a new opaque pointer to each_tag_name_fn's parameter to allow this. Signed-off-by: Lukas Puehringer <luk.puehringer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lukas Puehringer committed Jan 17, 2017 at 18:37 UTC 07d347cf9ac13c3d1746ae368f6e72afbe82dc15
2 files changed +28 -11
Documentation/git-tag.txt
+1 -1
@@ -15,7 +15,7 @@ SYNOPSIS
15 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]
16 [--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>]
17 [--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]
18 -'git tag' -v <tagname>...
18 +'git tag' -v [--format=<format>] <tagname>...
19
20 DESCRIPTION
21 -----------
builtin/tag.c
+27 -10
@@ -24,7 +24,7 @@ static const char * const git_tag_usage[] = {
24 N_("git tag -d <tagname>..."),
25 N_("git tag -l [-n[<num>]] [--contains <commit>] [--points-at <object>]"
26 "\n\t\t[--format=<format>] [--[no-]merged [<commit>]] [<pattern>...]"),
27 - N_("git tag -v <tagname>..."),
27 + N_("git tag -v [--format=<format>] <tagname>..."),
28 NULL
29 };
30
@@ -66,9 +66,10 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, con
66 }
67
68 typedef int (*each_tag_name_fn)(const char *name, const char *ref,
69 - const unsigned char *sha1);
69 + const unsigned char *sha1, const void *cb_data);
70
71 -static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
71 +static int for_each_tag_name(const char **argv, each_tag_name_fn fn,
72 + const void *cb_data)
73 {
74 const char **p;
75 char ref[PATH_MAX];
@@ -87,14 +88,14 @@ static int for_each_tag_name(const char **argv, each_tag_name_fn fn)
88 had_error = 1;
89 continue;
90 }
90 - if (fn(*p, ref, sha1))
91 + if (fn(*p, ref, sha1, cb_data))
92 had_error = 1;
93 }
94 return had_error;
95 }
96
97 static int delete_tag(const char *name, const char *ref,
97 - const unsigned char *sha1)
98 + const unsigned char *sha1, const void *cb_data)
99 {
100 if (delete_ref(ref, sha1, 0))
101 return 1;
@@ -103,9 +104,22 @@ static int delete_tag(const char *name, const char *ref,
104 }
105
106 static int verify_tag(const char *name, const char *ref,
106 - const unsigned char *sha1)
107 + const unsigned char *sha1, const void *cb_data)
108 {
108 - return gpg_verify_tag(sha1, name, GPG_VERIFY_VERBOSE);
109 + int flags;
110 + const char *fmt_pretty = cb_data;
111 + flags = GPG_VERIFY_VERBOSE;
112 +
113 + if (fmt_pretty)
114 + flags = GPG_VERIFY_OMIT_STATUS;
115 +
116 + if (gpg_verify_tag(sha1, name, flags))
117 + return -1;
118 +
119 + if (fmt_pretty)
120 + pretty_print_ref(name, sha1, fmt_pretty);
121 +
122 + return 0;
123 }
124
125 static int do_sign(struct strbuf *buffer)
@@ -424,9 +438,12 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
438 if (filter.merge_commit)
439 die(_("--merged and --no-merged option are only allowed with -l"));
440 if (cmdmode == 'd')
427 - return for_each_tag_name(argv, delete_tag);
428 - if (cmdmode == 'v')
429 - return for_each_tag_name(argv, verify_tag);
441 + return for_each_tag_name(argv, delete_tag, NULL);
442 + if (cmdmode == 'v') {
443 + if (format)
444 + verify_ref_format(format);
445 + return for_each_tag_name(argv, verify_tag, format);
446 + }
447
448 if (msg.given || msgfile) {
449 if (msg.given && msgfile)