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 <gitster@pobox.com>
Junio C Hamano committed
Feb 6, 2012 at 10:13 UTC
31fd8d72f2eb82c2b6d8d450ee4e13a3925291c3
2 files changed
+25
-10
builtin/tag.c
+12
-10
@@ -95,19 +95,20 @@ static void show_tag_lines(const unsigned char *sha1, int lines)
95
buf = read_sha1_file(sha1, &type, &size);
96
if (!buf)
97
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));
103
104
/* skip header */
105
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);
112
for (i = 0, sp += 2; i < lines && sp < buf + size; i++) {
113
if (i)
114
printf("\n ");
@@ -118,6 +119,7 @@ static void show_tag_lines(const unsigned char *sha1, int lines)
119
break;
120
sp = eol + 1;
121
}
122
+free_return:
123
free(buf);
124
}
125
t/t7004-tag.sh
+13
@@ -585,6 +585,19 @@ test_expect_success \
585
test_cmp expect actual
586
'
587
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
+
601
# subsequent tests require gpg; check if it is available
602
gpg --version >/dev/null 2>/dev/null
603
if [ $? -eq 127 ]; then