Skip to content

Commit a95750c

Browse files
committed
Merge branch 'jk/maint-tag-show-fixes' into maint
* jk/maint-tag-show-fixes: tag: do not show non-tag contents with "-n" tag: die when listing missing or corrupt objects tag: fix output of "tag -n" when errors occur Conflicts: t/t7004-tag.sh
2 parents 801b28a + 31fd8d7 commit a95750c

File tree

2 files changed

+53
-32
lines changed

2 files changed

+53
-32
lines changed

builtin/tag.c

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,51 @@ static int contains(struct commit *candidate, const struct commit_list *want)
8383
return contains_recurse(candidate, want);
8484
}
8585

86+
static void show_tag_lines(const unsigned char *sha1, int lines)
87+
{
88+
int i;
89+
unsigned long size;
90+
enum object_type type;
91+
char *buf, *sp, *eol;
92+
size_t len;
93+
94+
buf = read_sha1_file(sha1, &type, &size);
95+
if (!buf)
96+
die_errno("unable to read object %s", sha1_to_hex(sha1));
97+
if (type != OBJ_COMMIT && type != OBJ_TAG)
98+
goto free_return;
99+
if (!size)
100+
die("an empty %s object %s?",
101+
typename(type), sha1_to_hex(sha1));
102+
103+
/* skip header */
104+
sp = strstr(buf, "\n\n");
105+
if (!sp)
106+
goto free_return;
107+
108+
/* only take up to "lines" lines, and strip the signature from a tag */
109+
if (type == OBJ_TAG)
110+
size = parse_signature(buf, size);
111+
for (i = 0, sp += 2; i < lines && sp < buf + size; i++) {
112+
if (i)
113+
printf("\n ");
114+
eol = memchr(sp, '\n', size - (sp - buf));
115+
len = eol ? eol - sp : size - (sp - buf);
116+
fwrite(sp, len, 1, stdout);
117+
if (!eol)
118+
break;
119+
sp = eol + 1;
120+
}
121+
free_return:
122+
free(buf);
123+
}
124+
86125
static int show_reference(const char *refname, const unsigned char *sha1,
87126
int flag, void *cb_data)
88127
{
89128
struct tag_filter *filter = cb_data;
90129

91130
if (match_pattern(filter->patterns, refname)) {
92-
int i;
93-
unsigned long size;
94-
enum object_type type;
95-
char *buf, *sp, *eol;
96-
size_t len;
97-
98131
if (filter->with_commit) {
99132
struct commit *commit;
100133

@@ -110,33 +143,8 @@ static int show_reference(const char *refname, const unsigned char *sha1,
110143
return 0;
111144
}
112145
printf("%-15s ", refname);
113-
114-
buf = read_sha1_file(sha1, &type, &size);
115-
if (!buf || !size)
116-
return 0;
117-
118-
/* skip header */
119-
sp = strstr(buf, "\n\n");
120-
if (!sp) {
121-
free(buf);
122-
return 0;
123-
}
124-
/* only take up to "lines" lines, and strip the signature */
125-
size = parse_signature(buf, size);
126-
for (i = 0, sp += 2;
127-
i < filter->lines && sp < buf + size;
128-
i++) {
129-
if (i)
130-
printf("\n ");
131-
eol = memchr(sp, '\n', size - (sp - buf));
132-
len = eol ? eol - sp : size - (sp - buf);
133-
fwrite(sp, len, 1, stdout);
134-
if (!eol)
135-
break;
136-
sp = eol + 1;
137-
}
146+
show_tag_lines(sha1, filter->lines);
138147
putchar('\n');
139-
free(buf);
140148
}
141149

142150
return 0;

t/t7004-tag.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,19 @@ test_expect_success \
586586
test_cmp expect actual
587587
'
588588

589+
test_expect_success 'annotations for blobs are empty' '
590+
blob=$(git hash-object -w --stdin <<-\EOF
591+
Blob paragraph 1.
592+
593+
Blob paragraph 2.
594+
EOF
595+
) &&
596+
git tag tag-blob $blob &&
597+
echo "tag-blob " >expect &&
598+
git tag -n1 -l tag-blob >actual &&
599+
test_cmp expect actual
600+
'
601+
589602
# trying to verify annotated non-signed tags:
590603

591604
test_expect_success GPG \

0 commit comments

Comments
 (0)