Skip to content

Commit aa78384

Browse files
committed
Merge branch 'mg/maint-tag-rfc1991' into maint
* mg/maint-tag-rfc1991: tag: recognize rfc1991 signatures tag: factor out sig detection for tag display tag: factor out sig detection for body edits verify-tag: factor out signature detection t/t7004-tag: test handling of rfc1991 signatures
2 parents af41867 + 3d5854e commit aa78384

File tree

5 files changed

+88
-20
lines changed

5 files changed

+88
-20
lines changed

builtin/tag.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ struct tag_filter {
2929
struct commit_list *with_commit;
3030
};
3131

32-
#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
33-
3432
static int show_reference(const char *refname, const unsigned char *sha1,
3533
int flag, void *cb_data)
3634
{
@@ -70,9 +68,9 @@ static int show_reference(const char *refname, const unsigned char *sha1,
7068
return 0;
7169
}
7270
/* only take up to "lines" lines, and strip the signature */
71+
size = parse_signature(buf, size);
7372
for (i = 0, sp += 2;
74-
i < filter->lines && sp < buf + size &&
75-
prefixcmp(sp, PGP_SIGNATURE "\n");
73+
i < filter->lines && sp < buf + size;
7674
i++) {
7775
if (i)
7876
printf("\n ");
@@ -242,8 +240,7 @@ static void write_tag_body(int fd, const unsigned char *sha1)
242240
{
243241
unsigned long size;
244242
enum object_type type;
245-
char *buf, *sp, *eob;
246-
size_t len;
243+
char *buf, *sp;
247244

248245
buf = read_sha1_file(sha1, &type, &size);
249246
if (!buf)
@@ -256,12 +253,7 @@ static void write_tag_body(int fd, const unsigned char *sha1)
256253
return;
257254
}
258255
sp += 2; /* skip the 2 LFs */
259-
eob = strstr(sp, "\n" PGP_SIGNATURE "\n");
260-
if (eob)
261-
len = eob - sp;
262-
else
263-
len = buf + size - sp;
264-
write_or_die(fd, sp, len);
256+
write_or_die(fd, sp, parse_signature(sp, buf + size - sp));
265257

266258
free(buf);
267259
}

builtin/verify-tag.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ static const char * const verify_tag_usage[] = {
1717
NULL
1818
};
1919

20-
#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
21-
2220
static int run_gpg_verify(const char *buf, unsigned long size, int verbose)
2321
{
2422
struct child_process gpg;
2523
const char *args_gpg[] = {"gpg", "--verify", "FILE", "-", NULL};
26-
char path[PATH_MAX], *eol;
24+
char path[PATH_MAX];
2725
size_t len;
2826
int fd, ret;
2927

@@ -37,11 +35,7 @@ static int run_gpg_verify(const char *buf, unsigned long size, int verbose)
3735
close(fd);
3836

3937
/* find the length without signature */
40-
len = 0;
41-
while (len < size && prefixcmp(buf + len, PGP_SIGNATURE)) {
42-
eol = memchr(buf + len, '\n', size - len);
43-
len += eol ? eol - (buf + len) + 1 : size - len;
44-
}
38+
len = parse_signature(buf, size);
4539
if (verbose)
4640
write_in_full(1, buf, len);
4741

t/t7004-tag.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,72 @@ test_expect_success GPG \
10301030
test_cmp expect actual
10311031
'
10321032

1033+
# usage with rfc1991 signatures
1034+
echo "rfc1991" > gpghome/gpg.conf
1035+
get_tag_header rfc1991-signed-tag $commit commit $time >expect
1036+
echo "RFC1991 signed tag" >>expect
1037+
echo '-----BEGIN PGP MESSAGE-----' >>expect
1038+
test_expect_success GPG \
1039+
'creating a signed tag with rfc1991' '
1040+
git tag -s -m "RFC1991 signed tag" rfc1991-signed-tag $commit &&
1041+
get_tag_msg rfc1991-signed-tag >actual &&
1042+
test_cmp expect actual
1043+
'
1044+
1045+
cat >fakeeditor <<'EOF'
1046+
#!/bin/sh
1047+
cp "$1" actual
1048+
EOF
1049+
chmod +x fakeeditor
1050+
1051+
test_expect_success GPG \
1052+
'reediting a signed tag body omits signature' '
1053+
echo "RFC1991 signed tag" >expect &&
1054+
GIT_EDITOR=./fakeeditor git tag -f -s rfc1991-signed-tag $commit &&
1055+
test_cmp expect actual
1056+
'
1057+
1058+
test_expect_success GPG \
1059+
'verifying rfc1991 signature' '
1060+
git tag -v rfc1991-signed-tag
1061+
'
1062+
1063+
test_expect_success GPG \
1064+
'list tag with rfc1991 signature' '
1065+
echo "rfc1991-signed-tag RFC1991 signed tag" >expect &&
1066+
git tag -l -n1 rfc1991-signed-tag >actual &&
1067+
test_cmp expect actual &&
1068+
git tag -l -n2 rfc1991-signed-tag >actual &&
1069+
test_cmp expect actual &&
1070+
git tag -l -n999 rfc1991-signed-tag >actual &&
1071+
test_cmp expect actual
1072+
'
1073+
1074+
rm -f gpghome/gpg.conf
1075+
1076+
test_expect_success GPG \
1077+
'verifying rfc1991 signature without --rfc1991' '
1078+
git tag -v rfc1991-signed-tag
1079+
'
1080+
1081+
test_expect_success GPG \
1082+
'list tag with rfc1991 signature without --rfc1991' '
1083+
echo "rfc1991-signed-tag RFC1991 signed tag" >expect &&
1084+
git tag -l -n1 rfc1991-signed-tag >actual &&
1085+
test_cmp expect actual &&
1086+
git tag -l -n2 rfc1991-signed-tag >actual &&
1087+
test_cmp expect actual &&
1088+
git tag -l -n999 rfc1991-signed-tag >actual &&
1089+
test_cmp expect actual
1090+
'
1091+
1092+
test_expect_success GPG \
1093+
'reediting a signed tag body omits signature' '
1094+
echo "RFC1991 signed tag" >expect &&
1095+
GIT_EDITOR=./fakeeditor git tag -f -s rfc1991-signed-tag $commit &&
1096+
test_cmp expect actual
1097+
'
1098+
10331099
# try to sign with bad user.signingkey
10341100
git config user.signingkey BobTheMouse
10351101
test_expect_success GPG \

tag.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include "tree.h"
55
#include "blob.h"
66

7+
#define PGP_SIGNATURE "-----BEGIN PGP SIGNATURE-----"
8+
#define PGP_MESSAGE "-----BEGIN PGP MESSAGE-----"
9+
710
const char *tag_type = "tag";
811

912
struct object *deref_tag(struct object *o, const char *warn, int warnlen)
@@ -133,3 +136,15 @@ int parse_tag(struct tag *item)
133136
free(data);
134137
return ret;
135138
}
139+
140+
size_t parse_signature(const char *buf, unsigned long size)
141+
{
142+
char *eol;
143+
size_t len = 0;
144+
while (len < size && prefixcmp(buf + len, PGP_SIGNATURE) &&
145+
prefixcmp(buf + len, PGP_MESSAGE)) {
146+
eol = memchr(buf + len, '\n', size - len);
147+
len += eol ? eol - (buf + len) + 1 : size - len;
148+
}
149+
return len;
150+
}

tag.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ extern struct tag *lookup_tag(const unsigned char *sha1);
1616
extern int parse_tag_buffer(struct tag *item, void *data, unsigned long size);
1717
extern int parse_tag(struct tag *item);
1818
extern struct object *deref_tag(struct object *, const char *, int);
19+
extern size_t parse_signature(const char *buf, unsigned long size);
1920

2021
#endif /* TAG_H */

0 commit comments

Comments
 (0)