Skip to content

Commit 9cfa512

Browse files
peffgitster
authored andcommitted
cat-file: print tags raw for "cat-file -p"
When "cat-file -p" prints commits, it shows them in their raw format, since git's format is already human-readable. For tags, however, we print the whole thing raw except for one thing: we convert the timestamp on the tagger line into a human-readable date. This dates all the way back to a0f15fa (Pretty-print tagger dates, 2006-03-01). At that time there was no other way to pretty-print a tag. These days, however, neither of those matters much. The normal way to pretty-print a tag is with "git show", which is much more flexible than "cat-file -p". Commit a0f15fa also built "verify-tag --verbose" (and subsequently "tag -v") around the "cat-file -p" output. However, that behavior was lost in commit 62e09ce (Make git tag a builtin, 2007-07-20), and we went back to printing the raw tag contents. Nobody seems to have noticed the bug since then (and it is arguably a saner behavior anyway, as it shows the actual bytes for which we verified the signature). Let's drop the tagger-date formatting for "cat-file -p". It makes us more consistent with cat-file's commit pretty-printer, and as a bonus, we can drop the hand-rolled tag parsing code in cat-file (which happened to behave inconsistently with the tag pretty-printing code elsewhere). This is a change of output format, so it's possible that some callers could considered this a regression. However, the original behavior was arguably a bug (due to the inconsistency with commits), likely nobody was relying on it (even we do not use it ourselves these days), and anyone relying on the "-p" pretty-printer should be able to expect a change in the output format (i.e., while "cat-file" is plumbing, the output format of "-p" was never guaranteed to be stable). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f2de0b9 commit 9cfa512

File tree

2 files changed

+1
-75
lines changed

2 files changed

+1
-75
lines changed

builtin/cat-file.c

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,73 +16,6 @@
1616
#define BATCH 1
1717
#define BATCH_CHECK 2
1818

19-
static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long size)
20-
{
21-
/* the parser in tag.c is useless here. */
22-
const char *endp = buf + size;
23-
const char *cp = buf;
24-
25-
while (cp < endp) {
26-
char c = *cp++;
27-
if (c != '\n')
28-
continue;
29-
if (7 <= endp - cp && !memcmp("tagger ", cp, 7)) {
30-
const char *tagger = cp;
31-
32-
/* Found the tagger line. Copy out the contents
33-
* of the buffer so far.
34-
*/
35-
write_or_die(1, buf, cp - buf);
36-
37-
/*
38-
* Do something intelligent, like pretty-printing
39-
* the date.
40-
*/
41-
while (cp < endp) {
42-
if (*cp++ == '\n') {
43-
/* tagger to cp is a line
44-
* that has ident and time.
45-
*/
46-
const char *sp = tagger;
47-
char *ep;
48-
unsigned long date;
49-
long tz;
50-
while (sp < cp && *sp != '>')
51-
sp++;
52-
if (sp == cp) {
53-
/* give up */
54-
write_or_die(1, tagger,
55-
cp - tagger);
56-
break;
57-
}
58-
while (sp < cp &&
59-
!('0' <= *sp && *sp <= '9'))
60-
sp++;
61-
write_or_die(1, tagger, sp - tagger);
62-
date = strtoul(sp, &ep, 10);
63-
tz = strtol(ep, NULL, 10);
64-
sp = show_date(date, tz, 0);
65-
write_or_die(1, sp, strlen(sp));
66-
xwrite(1, "\n", 1);
67-
break;
68-
}
69-
}
70-
break;
71-
}
72-
if (cp < endp && *cp == '\n')
73-
/* end of header */
74-
break;
75-
}
76-
/* At this point, we have copied out the header up to the end of
77-
* the tagger line and cp points at one past \n. It could be the
78-
* next header line after the tagger line, or it could be another
79-
* \n that marks the end of the headers. We need to copy out the
80-
* remainder as is.
81-
*/
82-
if (cp < endp)
83-
write_or_die(1, cp, endp - cp);
84-
}
85-
8619
static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
8720
{
8821
unsigned char sha1[20];
@@ -133,10 +66,6 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
13366
buf = read_sha1_file(sha1, &type, &size);
13467
if (!buf)
13568
die("Cannot read object %s", obj_name);
136-
if (type == OBJ_TAG) {
137-
pprint_tag(sha1, buf, size);
138-
return 0;
139-
}
14069

14170
/* otherwise just spit out the data */
14271
break;

t/t1006-cat-file.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,12 @@ tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
134134
tag_description="This is a tag"
135135
tag_content="$tag_header_without_timestamp 0000000000 +0000
136136
137-
$tag_description"
138-
tag_pretty_content="$tag_header_without_timestamp Thu Jan 1 00:00:00 1970 +0000
139-
140137
$tag_description"
141138

142139
tag_sha1=$(echo_without_newline "$tag_content" | git mktag)
143140
tag_size=$(strlen "$tag_content")
144141

145-
run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_pretty_content" 1
142+
run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content" 1
146143

147144
test_expect_success \
148145
"Reach a blob from a tag pointing to it" \

0 commit comments

Comments
 (0)