Skip to content

Commit ef1e740

Browse files
committed
name-rev: favor describing with tags and use committer date to tiebreak
"git name-rev" assigned a phony "far in the future" date to tips of refs that are not pointing at tag objects, and favored names based on a ref with the oldest date. This made it almost impossible for an unannotated tags and branches to be counted as a viable base, which was especially problematic when the command is run with the "--tags" option. If an unannotated tag that points at an ancient commit and an annotated tag that points at a much newer commit reaches the commit that is being named, the old unannotated tag was ignored. Update the "taggerdate" field of the rev-name structure, which is initialized from the tip of ref, to have the committer date if the object at the tip of ref is a commit, not a tag, so that we can optionally take it into account when doing "is this name better?" comparison logic. When "name-rev" is run without the "--tags" option, the general expectation is still to name the commit based on a tag if possible, but use non-tag refs as fallback, and tiebreak among these non-tag refs by favoring names with shorter hops from the tip. The use of a phony "far in the future" date in the original code was an effective way to ensure this expectation is held: a non-tag tip gets the same "far in the future" timestamp, giving precedence to tags, and among non-tag tips, names with shorter hops are preferred over longer hops, without taking the "taggerdate" into account. As we are taking over the "taggerdate" field to store the committer date for tips with commits: (1) keep the original logic when comparing names based on two refs both of which are from refs/tags/; (2) favoring a name based on a ref in refs/tags/ hierarchy over a ref outside the hierarchy; (3) between two names based on a ref both outside refs/tags/, give precedence to a name with shorter hops and use "taggerdate" only to tie-break. A change to t4202 is a natural consequence. The test creates a commit on a branch "side" and points at it with an unannotated tag "refs/tags/side-2". The original code couldn't decide which one to favor at all, and gave a name based on a branch (simply because refs/heads/side sorts earlier than refs/tags/side-2). Because the updated logic is taught to favor refs in refs/tags/ hierarchy, the the test is updated to expect to see tags/side-2 instead. [mjg: open-coded the comparisons in is_better_name(), dropping a helper macro used in the original] Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Michael J Gruber <git@grubix.eu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 0041bf6 commit ef1e740

2 files changed

Lines changed: 45 additions & 10 deletions

File tree

builtin/name-rev.c

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ typedef struct rev_name {
1313
unsigned long taggerdate;
1414
int generation;
1515
int distance;
16+
int from_tag;
1617
} rev_name;
1718

1819
static long cutoff = LONG_MAX;
@@ -24,16 +25,43 @@ static int is_better_name(struct rev_name *name,
2425
const char *tip_name,
2526
unsigned long taggerdate,
2627
int generation,
27-
int distance)
28+
int distance,
29+
int from_tag)
2830
{
29-
return (name->taggerdate > taggerdate ||
30-
(name->taggerdate == taggerdate &&
31-
name->distance > distance));
31+
/*
32+
* When comparing names based on tags, prefer names
33+
* based on the older tag, even if it is farther away.
34+
*/
35+
if (from_tag && name->from_tag)
36+
return (name->taggerdate > taggerdate ||
37+
(name->taggerdate == taggerdate &&
38+
name->distance > distance));
39+
40+
/*
41+
* We know that at least one of them is a non-tag at this point.
42+
* favor a tag over a non-tag.
43+
*/
44+
if (name->from_tag != from_tag)
45+
return from_tag;
46+
47+
/*
48+
* We are now looking at two non-tags. Tiebreak to favor
49+
* shorter hops.
50+
*/
51+
if (name->distance != distance)
52+
return name->distance > distance;
53+
54+
/* ... or tiebreak to favor older date */
55+
if (name->taggerdate != taggerdate)
56+
return name->taggerdate > taggerdate;
57+
58+
/* keep the current one if we cannot decide */
59+
return 0;
3260
}
3361

3462
static void name_rev(struct commit *commit,
3563
const char *tip_name, unsigned long taggerdate,
36-
int generation, int distance,
64+
int generation, int distance, int from_tag,
3765
int deref)
3866
{
3967
struct rev_name *name = (struct rev_name *)commit->util;
@@ -57,12 +85,13 @@ static void name_rev(struct commit *commit,
5785
commit->util = name;
5886
goto copy_data;
5987
} else if (is_better_name(name, tip_name, taggerdate,
60-
generation, distance)) {
88+
generation, distance, from_tag)) {
6189
copy_data:
6290
name->tip_name = tip_name;
6391
name->taggerdate = taggerdate;
6492
name->generation = generation;
6593
name->distance = distance;
94+
name->from_tag = from_tag;
6695
} else
6796
return;
6897

@@ -82,10 +111,12 @@ static void name_rev(struct commit *commit,
82111
parent_number);
83112

84113
name_rev(parents->item, new_name, taggerdate, 0,
85-
distance + MERGE_TRAVERSAL_WEIGHT, 0);
114+
distance + MERGE_TRAVERSAL_WEIGHT,
115+
from_tag, 0);
86116
} else {
87117
name_rev(parents->item, tip_name, taggerdate,
88-
generation + 1, distance + 1, 0);
118+
generation + 1, distance + 1,
119+
from_tag, 0);
89120
}
90121
}
91122
}
@@ -216,9 +247,13 @@ static int name_ref(const char *path, const struct object_id *oid, int flags, vo
216247
}
217248
if (o && o->type == OBJ_COMMIT) {
218249
struct commit *commit = (struct commit *)o;
250+
int from_tag = starts_with(path, "refs/tags/");
219251

252+
if (taggerdate == ULONG_MAX)
253+
taggerdate = ((struct commit *)o)->date;
220254
path = name_ref_abbrev(path, can_abbreviate_output);
221-
name_rev(commit, xstrdup(path), taggerdate, 0, 0, deref);
255+
name_rev(commit, xstrdup(path), taggerdate, 0, 0,
256+
from_tag, deref);
222257
}
223258
return 0;
224259
}

t/t4202-log.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ cat > expect <<\EOF
398398
| |
399399
| | Merge branch 'side'
400400
| |
401-
| * commit side
401+
| * commit tags/side-2
402402
| | Author: A U Thor <author@example.com>
403403
| |
404404
| | side-2

0 commit comments

Comments
 (0)