Skip to content

Commit 0f0625a

Browse files
committed
Merge branch 'jc/describe-misnamed-annotated-tag'
When "git describe C" finds an annotated tag with tagname A to be the best name to explain commit C, and the tag is stored in a "wrong" place in the refs/tags hierarchy, e.g. refs/tags/B, the command gave a warning message but used A (not B) to describe C. If C is exactly at the tag, the describe output would be "A", but "git rev-parse A^0" would not be equal as "git rev-parse C^0". The behavior of the command has been changed to use the "long" form i.e. A-0-gOBJECTNAME, which is correctly interpreted by rev-parse. * jc/describe-misnamed-annotated-tag: describe: force long format for a name based on a mislocated tag
2 parents fb4175b + ff165f0 commit 0f0625a

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);
@@ -463,7 +466,7 @@ static void describe_commit(struct object_id *oid, struct strbuf *dst)
463466
}
464467

465468
append_name(all_matches[0].name, dst);
466-
if (abbrev)
469+
if (all_matches[0].name->misnamed || abbrev)
467470
append_suffix(all_matches[0].depth, &cmit->object.oid, dst);
468471
if (suffix)
469472
strbuf_addstr(dst, suffix);

t/t6120-describe.sh

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

0 commit comments

Comments
 (0)