Skip to content

Commit 7550424

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7654286 commit 7550424

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

builtin/name-rev.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
typedef struct rev_name {
1212
const char *tip_name;
13+
unsigned long taggerdate;
1314
int generation;
1415
int distance;
1516
} rev_name;
@@ -20,7 +21,8 @@ static long cutoff = LONG_MAX;
2021
#define MERGE_TRAVERSAL_WEIGHT 65535
2122

2223
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,
2426
int deref)
2527
{
2628
struct rev_name *name = (struct rev_name *)commit->util;
@@ -43,9 +45,12 @@ static void name_rev(struct commit *commit,
4345
name = xmalloc(sizeof(rev_name));
4446
commit->util = name;
4547
goto copy_data;
46-
} else if (name->distance > distance) {
48+
} else if (name->taggerdate > taggerdate ||
49+
(name->taggerdate == taggerdate &&
50+
name->distance > distance)) {
4751
copy_data:
4852
name->tip_name = tip_name;
53+
name->taggerdate = taggerdate;
4954
name->generation = generation;
5055
name->distance = distance;
5156
} else
@@ -70,11 +75,11 @@ static void name_rev(struct commit *commit,
7075
sprintf(new_name, "%.*s^%d", len, tip_name,
7176
parent_number);
7277

73-
name_rev(parents->item, new_name, 0,
78+
name_rev(parents->item, new_name, taggerdate, 0,
7479
distance + MERGE_TRAVERSAL_WEIGHT, 0);
7580
} 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);
7883
}
7984
}
8085
}
@@ -144,6 +149,7 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
144149
struct name_ref_data *data = cb_data;
145150
int can_abbreviate_output = data->tags_only && data->name_only;
146151
int deref = 0;
152+
unsigned long taggerdate = ULONG_MAX;
147153

148154
if (data->tags_only && !starts_with(path, "refs/tags/"))
149155
return 0;
@@ -168,12 +174,13 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
168174
break; /* broken repository */
169175
o = parse_object(t->tagged->sha1);
170176
deref = 1;
177+
taggerdate = t->date;
171178
}
172179
if (o && o->type == OBJ_COMMIT) {
173180
struct commit *commit = (struct commit *)o;
174181

175182
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);
177184
}
178185
return 0;
179186
}

t/t9903-bash-prompt.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ test_expect_success 'prompt - describe detached head - contains' '
107107
'
108108

109109
test_expect_success 'prompt - describe detached head - branch' '
110-
printf " ((b1~1))" >expected &&
110+
printf " ((tags/t2~1))" >expected &&
111111
git checkout b1^ &&
112112
test_when_finished "git checkout master" &&
113113
(

0 commit comments

Comments
 (0)