Skip to content

Commit ff165f0

Browse files
committed
describe: force long format for a name based on a mislocated tag
An annotated tag has two names: where it sits in the refs/tags hierarchy and the tagname recorded in the "tag" field in the object itself. They usually should match. Since 212945d ("Teach git-describe to verify annotated tag names before output", 2008-02-28), a commit described using an annotated tag bases its name on the tagname from the object. While this was a deliberate design decision to make it easier to converse about tags with others, even if the tags happen to be fetched to a different name than it was given by its creator, it had one downside. The output from "git describe", at least in the modern Git, should be usable as an object name to name the exact commit given to the "git describe" command. Using the tagname, when two names differ, breaks this property, when describing a commit that is directly pointed at by such a tag. An annotated tag Bob made as "v1.0" may sit at "refs/tags/v1.0-bob" in the ref hierarchy, and output from "git describe v1.0-bob^0" would say "v1.0", but there may not be any tag at "refs/tags/v1.0" locally or there may be another tag that points at a different object. Note that this won't be a problem if a commit being described is not directly pointed at by such a mislocated tag. In the example in the previous paragraph, describing a commit whose parent is v1.0-bob would result in "v1.0" (i.e. the tagname taken from the tag object) followed by "-1-gXXXXX" where XXXXX is the abbreviated object name, and a string that ends with "-g" followed by a hexadecimal string is an object name for the object whose name begins with hexadecimal string (as long as it is unique), so it does not matter if the leading part is "v1.0" or "v1.0-bob". Show the name in the long format, i.e. with "-0-gXXXXX" suffix, when the name we give is based on a mislocated annotated tag to ensure that the output can be used as the object name for the object originally given to the command to fix the issue. While at it, remove an overly cautious dead code to protect against an annotated tag object without the tagname. Such a tag is filtered out much earlier in the codeflow, and will not reach this part of the code. Helped-by: Matheus Tavares <[email protected]> Helped-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b6d4d82 commit ff165f0

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

builtin/describe.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct commit_name {
5454
struct tag *tag;
5555
unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
5656
unsigned name_checked:1;
57+
unsigned misnamed:1;
5758
struct object_id oid;
5859
char *path;
5960
};
@@ -132,6 +133,7 @@ static void add_to_known_names(const char *path,
132133
e->tag = tag;
133134
e->prio = prio;
134135
e->name_checked = 0;
136+
e->misnamed = 0;
135137
oidcpy(&e->oid, oid);
136138
free(e->path);
137139
e->path = xstrdup(path);
@@ -275,10 +277,11 @@ static void append_name(struct commit_name *n, struct strbuf *dst)
275277
die(_("annotated tag %s not available"), n->path);
276278
}
277279
if (n->tag && !n->name_checked) {
278-
if (!n->tag->tag)
279-
die(_("annotated tag %s has no embedded name"), n->path);
280-
if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
281-
warning(_("tag '%s' is really '%s' here"), n->tag->tag, n->path);
280+
if (strcmp(n->tag->tag, all ? n->path + 5 : n->path)) {
281+
warning(_("tag '%s' is externally known as '%s'"),
282+
n->path, n->tag->tag);
283+
n->misnamed = 1;
284+
}
282285
n->name_checked = 1;
283286
}
284287

@@ -314,7 +317,7 @@ static void describe_commit(struct object_id *oid, struct strbuf *dst)
314317
* Exact match to an existing ref.
315318
*/
316319
append_name(n, dst);
317-
if (longformat)
320+
if (n->misnamed || longformat)
318321
append_suffix(0, n->tag ? get_tagged_oid(n->tag) : oid, dst);
319322
if (suffix)
320323
strbuf_addstr(dst, suffix);
@@ -449,7 +452,7 @@ static void describe_commit(struct object_id *oid, struct strbuf *dst)
449452
}
450453

451454
append_name(all_matches[0].name, dst);
452-
if (abbrev)
455+
if (all_matches[0].name->misnamed || abbrev)
453456
append_suffix(all_matches[0].depth, &cmit->object.oid, dst);
454457
if (suffix)
455458
strbuf_addstr(dst, suffix);

t/t6120-describe.sh

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,30 @@ test_expect_success 'rename tag A to Q locally' '
130130
mv .git/refs/tags/A .git/refs/tags/Q
131131
'
132132
cat - >err.expect <<EOF
133-
warning: tag 'A' is really 'Q' here
133+
warning: tag 'Q' is externally known as 'A'
134134
EOF
135135
check_describe A-* HEAD
136136
test_expect_success 'warning was displayed for Q' '
137137
test_i18ncmp err.expect err.actual
138138
'
139+
test_expect_success 'misnamed annotated tag forces long output' '
140+
description=$(git describe --no-long Q^0) &&
141+
expr "$description" : "A-0-g[0-9a-f]*$" &&
142+
git rev-parse --verify "$description" >actual &&
143+
git rev-parse --verify Q^0 >expect &&
144+
test_cmp expect actual
145+
'
146+
147+
test_expect_success 'abbrev=0 will not break misplaced tag (1)' '
148+
description=$(git describe --abbrev=0 Q^0) &&
149+
expr "$description" : "A-0-g[0-9a-f]*$"
150+
'
151+
152+
test_expect_success 'abbrev=0 will not break misplaced tag (2)' '
153+
description=$(git describe --abbrev=0 c^0) &&
154+
expr "$description" : "A-1-g[0-9a-f]*$"
155+
'
156+
139157
test_expect_success 'rename tag Q back to A' '
140158
mv .git/refs/tags/Q .git/refs/tags/A
141159
'

0 commit comments

Comments
 (0)