name-rev: include taggerdate in considering the best name

We most likely want the oldest tag that contained the commit to be reported. So let's remember the taggerdate, and make it more important than anything else when choosing the best name for a given commit. Suggested by Linus Torvalds. Note that we need to update t9903 because it tested for the old behavior (which preferred the description "b1~1" over "tags/t2~1"). We might want to introduce a --heed-taggerdate option, and make the new behavior dependent on that, if it turns out that some scripts rely on the old name-rev method. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Apr 22, 2016 at 15:39 UTC 755042480429fbadbb1953416d2c04c035a78e4c
2 files changed +14 -7
builtin/name-rev.c
+13 -6
@@ -10,6 +10,7 @@
10
11 typedef struct rev_name {
12 const char *tip_name;
13 + unsigned long taggerdate;
14 int generation;
15 int distance;
16 } rev_name;
@@ -20,7 +21,8 @@ static long cutoff = LONG_MAX;
21 #define MERGE_TRAVERSAL_WEIGHT 65535
22
23 static void name_rev(struct commit *commit,
23 - const char *tip_name, int generation, int distance,
24 + const char *tip_name, unsigned long taggerdate,
25 + int generation, int distance,
26 int deref)
27 {
28 struct rev_name *name = (struct rev_name *)commit->util;
@@ -43,9 +45,12 @@ static void name_rev(struct commit *commit,
45 name = xmalloc(sizeof(rev_name));
46 commit->util = name;
47 goto copy_data;
46 - } else if (name->distance > distance) {
48 + } else if (name->taggerdate > taggerdate ||
49 + (name->taggerdate == taggerdate &&
50 + name->distance > distance)) {
51 copy_data:
52 name->tip_name = tip_name;
53 + name->taggerdate = taggerdate;
54 name->generation = generation;
55 name->distance = distance;
56 } else
@@ -70,11 +75,11 @@ copy_data:
75 sprintf(new_name, "%.*s^%d", len, tip_name,
76 parent_number);
77
73 - name_rev(parents->item, new_name, 0,
78 + name_rev(parents->item, new_name, taggerdate, 0,
79 distance + MERGE_TRAVERSAL_WEIGHT, 0);
80 } else {
76 - name_rev(parents->item, tip_name, generation + 1,
77 - distance + 1, 0);
81 + name_rev(parents->item, tip_name, taggerdate,
82 + generation + 1, distance + 1, 0);
83 }
84 }
85 }
@@ -144,6 +149,7 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
149 struct name_ref_data *data = cb_data;
150 int can_abbreviate_output = data->tags_only && data->name_only;
151 int deref = 0;
152 + unsigned long taggerdate = ULONG_MAX;
153
154 if (data->tags_only && !starts_with(path, "refs/tags/"))
155 return 0;
@@ -168,12 +174,13 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
174 break; /* broken repository */
175 o = parse_object(t->tagged->sha1);
176 deref = 1;
177 + taggerdate = t->date;
178 }
179 if (o && o->type == OBJ_COMMIT) {
180 struct commit *commit = (struct commit *)o;
181
182 path = name_ref_abbrev(path, can_abbreviate_output);
176 - name_rev(commit, xstrdup(path), 0, 0, deref);
183 + name_rev(commit, xstrdup(path), taggerdate, 0, 0, deref);
184 }
185 return 0;
186 }
t/t9903-bash-prompt.sh
+1 -1
@@ -107,7 +107,7 @@ test_expect_success 'prompt - describe detached head - contains' '
107 '
108
109 test_expect_success 'prompt - describe detached head - branch' '
110 - printf " ((b1~1))" >expected &&
110 + printf " ((tags/t2~1))" >expected &&
111 git checkout b1^ &&
112 test_when_finished "git checkout master" &&
113 (