Skip to content

Commit 31fd8d7

Browse files
committed
tag: do not show non-tag contents with "-n"
"git tag -n" did not check the type of the object it is reading the top n lines from. At least, avoid showing the beginning of trees and blobs when dealing with lightweight tags that point at them. As the payload of a tag and a commit look similar in that they both start with a header block, which is skipped for the purpose of "-n" output, followed by human readable text, allow the message of commit objects to be shown just like the contents of tag objects. This avoids regression for people who have been using "tag -n" to show the log messages of commits that are pointed at by lightweight tags. Test script is from Jeff King. Signed-off-by: Junio C Hamano <[email protected]>
1 parent fb630e0 commit 31fd8d7

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

builtin/tag.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,20 @@ static void show_tag_lines(const unsigned char *sha1, int lines)
9595
buf = read_sha1_file(sha1, &type, &size);
9696
if (!buf)
9797
die_errno("unable to read object %s", sha1_to_hex(sha1));
98-
if (!size) {
99-
free(buf);
100-
return;
101-
}
98+
if (type != OBJ_COMMIT && type != OBJ_TAG)
99+
goto free_return;
100+
if (!size)
101+
die("an empty %s object %s?",
102+
typename(type), sha1_to_hex(sha1));
102103

103104
/* skip header */
104105
sp = strstr(buf, "\n\n");
105-
if (!sp) {
106-
free(buf);
107-
return;
108-
}
109-
/* only take up to "lines" lines, and strip the signature */
110-
size = parse_signature(buf, size);
106+
if (!sp)
107+
goto free_return;
108+
109+
/* only take up to "lines" lines, and strip the signature from a tag */
110+
if (type == OBJ_TAG)
111+
size = parse_signature(buf, size);
111112
for (i = 0, sp += 2; i < lines && sp < buf + size; i++) {
112113
if (i)
113114
printf("\n ");
@@ -118,6 +119,7 @@ static void show_tag_lines(const unsigned char *sha1, int lines)
118119
break;
119120
sp = eol + 1;
120121
}
122+
free_return:
121123
free(buf);
122124
}
123125

t/t7004-tag.sh

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

588+
test_expect_success 'annotations for blobs are empty' '
589+
blob=$(git hash-object -w --stdin <<-\EOF
590+
Blob paragraph 1.
591+
592+
Blob paragraph 2.
593+
EOF
594+
) &&
595+
git tag tag-blob $blob &&
596+
echo "tag-blob " >expect &&
597+
git tag -n1 -l tag-blob >actual &&
598+
test_cmp expect actual
599+
'
600+
588601
# subsequent tests require gpg; check if it is available
589602
gpg --version >/dev/null 2>/dev/null
590603
if [ $? -eq 127 ]; then

0 commit comments

Comments
 (0)