Skip to content

Commit 11cc180

Browse files
committed
Merge branch 'mg/gpg-fingerprint'
New "--pretty=format:" placeholders %GF and %GP that show the GPG key fingerprints have been invented. * mg/gpg-fingerprint: gpg-interface.c: obtain primary key fingerprint as well gpg-interface.c: support getting key fingerprint via %GF format gpg-interface.c: use flags to determine key/signer info presence
2 parents 0256189 + 4de9394 commit 11cc180

File tree

5 files changed

+70
-18
lines changed

5 files changed

+70
-18
lines changed

Documentation/pretty-formats.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ endif::git-rev-list[]
153153
and "N" for no signature
154154
- '%GS': show the name of the signer for a signed commit
155155
- '%GK': show the key used to sign a signed commit
156+
- '%GF': show the fingerprint of the key used to sign a signed commit
157+
- '%GP': show the fingerprint of the primary key whose subkey was used
158+
to sign a signed commit
156159
- '%gD': reflog selector, e.g., `refs/stash@{1}` or
157160
`refs/stash@{2 minutes ago`}; the format follows the rules described
158161
for the `-g` option. The portion before the `@` is the refname as

gpg-interface.c

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,31 +73,43 @@ void signature_check_clear(struct signature_check *sigc)
7373
FREE_AND_NULL(sigc->gpg_status);
7474
FREE_AND_NULL(sigc->signer);
7575
FREE_AND_NULL(sigc->key);
76+
FREE_AND_NULL(sigc->fingerprint);
77+
FREE_AND_NULL(sigc->primary_key_fingerprint);
7678
}
7779

7880
/* An exclusive status -- only one of them can appear in output */
7981
#define GPG_STATUS_EXCLUSIVE (1<<0)
82+
/* The status includes key identifier */
83+
#define GPG_STATUS_KEYID (1<<1)
84+
/* The status includes user identifier */
85+
#define GPG_STATUS_UID (1<<2)
86+
/* The status includes key fingerprints */
87+
#define GPG_STATUS_FINGERPRINT (1<<3)
88+
89+
/* Short-hand for standard exclusive *SIG status with keyid & UID */
90+
#define GPG_STATUS_STDSIG (GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID|GPG_STATUS_UID)
8091

8192
static struct {
8293
char result;
8394
const char *check;
8495
unsigned int flags;
8596
} sigcheck_gpg_status[] = {
86-
{ 'G', "GOODSIG ", GPG_STATUS_EXCLUSIVE },
87-
{ 'B', "BADSIG ", GPG_STATUS_EXCLUSIVE },
97+
{ 'G', "GOODSIG ", GPG_STATUS_STDSIG },
98+
{ 'B', "BADSIG ", GPG_STATUS_STDSIG },
8899
{ 'U', "TRUST_NEVER", 0 },
89100
{ 'U', "TRUST_UNDEFINED", 0 },
90-
{ 'E', "ERRSIG ", GPG_STATUS_EXCLUSIVE },
91-
{ 'X', "EXPSIG ", GPG_STATUS_EXCLUSIVE },
92-
{ 'Y', "EXPKEYSIG ", GPG_STATUS_EXCLUSIVE },
93-
{ 'R', "REVKEYSIG ", GPG_STATUS_EXCLUSIVE },
101+
{ 'E', "ERRSIG ", GPG_STATUS_EXCLUSIVE|GPG_STATUS_KEYID },
102+
{ 'X', "EXPSIG ", GPG_STATUS_STDSIG },
103+
{ 'Y', "EXPKEYSIG ", GPG_STATUS_STDSIG },
104+
{ 'R', "REVKEYSIG ", GPG_STATUS_STDSIG },
105+
{ 0, "VALIDSIG ", GPG_STATUS_FINGERPRINT },
94106
};
95107

96108
static void parse_gpg_output(struct signature_check *sigc)
97109
{
98110
const char *buf = sigc->gpg_status;
99111
const char *line, *next;
100-
int i;
112+
int i, j;
101113
int seen_exclusive_status = 0;
102114

103115
/* Iterate over all lines */
@@ -116,20 +128,39 @@ static void parse_gpg_output(struct signature_check *sigc)
116128
goto found_duplicate_status;
117129
}
118130

119-
sigc->result = sigcheck_gpg_status[i].result;
120-
/* The trust messages are not followed by key/signer information */
121-
if (sigc->result != 'U') {
131+
if (sigcheck_gpg_status[i].result)
132+
sigc->result = sigcheck_gpg_status[i].result;
133+
/* Do we have key information? */
134+
if (sigcheck_gpg_status[i].flags & GPG_STATUS_KEYID) {
122135
next = strchrnul(line, ' ');
123136
free(sigc->key);
124137
sigc->key = xmemdupz(line, next - line);
125-
/* The ERRSIG message is not followed by signer information */
126-
if (*next && sigc->result != 'E') {
138+
/* Do we have signer information? */
139+
if (*next && (sigcheck_gpg_status[i].flags & GPG_STATUS_UID)) {
127140
line = next + 1;
128141
next = strchrnul(line, '\n');
129142
free(sigc->signer);
130143
sigc->signer = xmemdupz(line, next - line);
131144
}
132145
}
146+
/* Do we have fingerprint? */
147+
if (sigcheck_gpg_status[i].flags & GPG_STATUS_FINGERPRINT) {
148+
next = strchrnul(line, ' ');
149+
free(sigc->fingerprint);
150+
sigc->fingerprint = xmemdupz(line, next - line);
151+
152+
/* Skip interim fields */
153+
for (j = 9; j > 0; j--) {
154+
if (!*next)
155+
break;
156+
line = next + 1;
157+
next = strchrnul(line, ' ');
158+
}
159+
160+
next = strchrnul(line, '\n');
161+
free(sigc->primary_key_fingerprint);
162+
sigc->primary_key_fingerprint = xmemdupz(line, next - line);
163+
}
133164

134165
break;
135166
}
@@ -147,6 +178,8 @@ static void parse_gpg_output(struct signature_check *sigc)
147178
*/
148179
sigc->result = 'E';
149180
/* Clear partial data to avoid confusion */
181+
FREE_AND_NULL(sigc->primary_key_fingerprint);
182+
FREE_AND_NULL(sigc->fingerprint);
150183
FREE_AND_NULL(sigc->signer);
151184
FREE_AND_NULL(sigc->key);
152185
}

gpg-interface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ struct signature_check {
2323
char result;
2424
char *signer;
2525
char *key;
26+
char *fingerprint;
27+
char *primary_key_fingerprint;
2628
};
2729

2830
void signature_check_clear(struct signature_check *sigc);

pretty.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,14 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
12561256
if (c->signature_check.key)
12571257
strbuf_addstr(sb, c->signature_check.key);
12581258
break;
1259+
case 'F':
1260+
if (c->signature_check.fingerprint)
1261+
strbuf_addstr(sb, c->signature_check.fingerprint);
1262+
break;
1263+
case 'P':
1264+
if (c->signature_check.primary_key_fingerprint)
1265+
strbuf_addstr(sb, c->signature_check.primary_key_fingerprint);
1266+
break;
12591267
default:
12601268
return 0;
12611269
}

t/t7510-signed-commit.sh

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,9 @@ test_expect_success GPG 'show good signature with custom format' '
175175
G
176176
13B6F51ECDDE430D
177177
C O Mitter <[email protected]>
178+
73D758744BE721698EC54E8713B6F51ECDDE430D
178179
EOF
179-
git log -1 --format="%G?%n%GK%n%GS" sixth-signed >actual &&
180+
git log -1 --format="%G?%n%GK%n%GS%n%GF" sixth-signed >actual &&
180181
test_cmp expect actual
181182
'
182183

@@ -185,8 +186,9 @@ test_expect_success GPG 'show bad signature with custom format' '
185186
B
186187
13B6F51ECDDE430D
187188
C O Mitter <[email protected]>
189+
188190
EOF
189-
git log -1 --format="%G?%n%GK%n%GS" $(cat forged1.commit) >actual &&
191+
git log -1 --format="%G?%n%GK%n%GS%n%GF" $(cat forged1.commit) >actual &&
190192
test_cmp expect actual
191193
'
192194

@@ -195,8 +197,9 @@ test_expect_success GPG 'show untrusted signature with custom format' '
195197
U
196198
61092E85B7227189
197199
Eris Discordia <[email protected]>
200+
D4BE22311AD3131E5EDA29A461092E85B7227189
198201
EOF
199-
git log -1 --format="%G?%n%GK%n%GS" eighth-signed-alt >actual &&
202+
git log -1 --format="%G?%n%GK%n%GS%n%GF" eighth-signed-alt >actual &&
200203
test_cmp expect actual
201204
'
202205

@@ -205,8 +208,9 @@ test_expect_success GPG 'show unknown signature with custom format' '
205208
E
206209
61092E85B7227189
207210
211+
208212
EOF
209-
GNUPGHOME="$GNUPGHOME_NOT_USED" git log -1 --format="%G?%n%GK%n%GS" eighth-signed-alt >actual &&
213+
GNUPGHOME="$GNUPGHOME_NOT_USED" git log -1 --format="%G?%n%GK%n%GS%n%GF" eighth-signed-alt >actual &&
210214
test_cmp expect actual
211215
'
212216

@@ -215,8 +219,9 @@ test_expect_success GPG 'show lack of signature with custom format' '
215219
N
216220
217221
222+
218223
EOF
219-
git log -1 --format="%G?%n%GK%n%GS" seventh-unsigned >actual &&
224+
git log -1 --format="%G?%n%GK%n%GS%n%GF" seventh-unsigned >actual &&
220225
test_cmp expect actual
221226
'
222227

@@ -255,8 +260,9 @@ test_expect_success GPG 'show double signature with custom format' '
255260
E
256261
257262
263+
258264
EOF
259-
git log -1 --format="%G?%n%GK%n%GS" $(cat double-commit.commit) >actual &&
265+
git log -1 --format="%G?%n%GK%n%GS%n%GF" $(cat double-commit.commit) >actual &&
260266
test_cmp expect actual
261267
'
262268

0 commit comments

Comments
 (0)