Skip to content

Commit

Permalink
tag: do not show non-tag contents with "-n"
Browse files Browse the repository at this point in the history
"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>
  • Loading branch information
Junio C Hamano committed Feb 9, 2012
1 parent fb630e0 commit 31fd8d7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
22 changes: 12 additions & 10 deletions builtin/tag.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,20 @@ static void show_tag_lines(const unsigned char *sha1, int lines)
buf = read_sha1_file(sha1, &type, &size);
if (!buf)
die_errno("unable to read object %s", sha1_to_hex(sha1));
if (!size) {
free(buf);
return;
}
if (type != OBJ_COMMIT && type != OBJ_TAG)
goto free_return;
if (!size)
die("an empty %s object %s?",
typename(type), sha1_to_hex(sha1));

/* skip header */
sp = strstr(buf, "\n\n");
if (!sp) {
free(buf);
return;
}
/* only take up to "lines" lines, and strip the signature */
size = parse_signature(buf, size);
if (!sp)
goto free_return;

/* only take up to "lines" lines, and strip the signature from a tag */
if (type == OBJ_TAG)
size = parse_signature(buf, size);
for (i = 0, sp += 2; i < lines && sp < buf + size; i++) {
if (i)
printf("\n ");
Expand All @@ -118,6 +119,7 @@ static void show_tag_lines(const unsigned char *sha1, int lines)
break;
sp = eol + 1;
}
free_return:
free(buf);
}

Expand Down
13 changes: 13 additions & 0 deletions t/t7004-tag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,19 @@ test_expect_success \
test_cmp expect actual
'

test_expect_success 'annotations for blobs are empty' '
blob=$(git hash-object -w --stdin <<-\EOF
Blob paragraph 1.
Blob paragraph 2.
EOF
) &&
git tag tag-blob $blob &&
echo "tag-blob " >expect &&
git tag -n1 -l tag-blob >actual &&
test_cmp expect actual
'

# subsequent tests require gpg; check if it is available
gpg --version >/dev/null 2>/dev/null
if [ $? -eq 127 ]; then
Expand Down

0 comments on commit 31fd8d7

Please sign in to comment.