Skip to content

Commit 2b01f49

Browse files
mjggitster
authored andcommitted
name-rev: provide debug output
Currently, `git describe --contains --debug` does not create any debug output because it does not pass the flag down to `git name-rev`, which does not know that flag. Teach the latter that flag, so that the former can pass it down (in the following commit). The output is patterned after that of `git describe --debug`, with the following differences: describe loops over all args to describe, then over all possible descriptions; name-rev does it the other way round. Therefore, we need to amend each possible description by the arg that it is for (and we leave out the "searching to describe" header). The date cut-off for name-rev kicks in way more often than the candidate number cut-off of describe, so we do not clutter the output with the cut-off. Signed-off-by: Michael J Gruber <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ef1e740 commit 2b01f49

File tree

2 files changed

+56
-13
lines changed

2 files changed

+56
-13
lines changed

Documentation/git-name-rev.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ OPTIONS
4242
--all::
4343
List all commits reachable from all refs
4444

45+
--debug::
46+
Verbosely display information about the searching strategy
47+
being employed to standard error. The symbolic name will still
48+
be printed to standard out.
49+
4550
--stdin::
4651
Transform stdin by substituting all the 40-character SHA-1
4752
hexes (say $hex) with "$hex ($rev_name)". When used with

builtin/name-rev.c

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ typedef struct rev_name {
1818

1919
static long cutoff = LONG_MAX;
2020

21+
static const char *prio_names[] = {
22+
N_("head"), N_("lightweight"), N_("annotated"),
23+
};
24+
2125
/* How many generations are maximally preferred over _one_ merge traversal? */
2226
#define MERGE_TRAVERSAL_WEIGHT 65535
2327

@@ -59,10 +63,19 @@ static int is_better_name(struct rev_name *name,
5963
return 0;
6064
}
6165

66+
struct name_ref_data {
67+
int tags_only;
68+
int name_only;
69+
int debug;
70+
struct string_list ref_filters;
71+
struct string_list exclude_filters;
72+
struct object_array *revs;
73+
};
74+
6275
static void name_rev(struct commit *commit,
6376
const char *tip_name, unsigned long taggerdate,
6477
int generation, int distance, int from_tag,
65-
int deref)
78+
int deref, struct name_ref_data *data)
6679
{
6780
struct rev_name *name = (struct rev_name *)commit->util;
6881
struct commit_list *parents;
@@ -75,6 +88,7 @@ static void name_rev(struct commit *commit,
7588

7689
if (deref) {
7790
tip_name = xstrfmt("%s^0", tip_name);
91+
from_tag += 1;
7892

7993
if (generation)
8094
die("generation: %d, but deref?", generation);
@@ -87,6 +101,36 @@ static void name_rev(struct commit *commit,
87101
} else if (is_better_name(name, tip_name, taggerdate,
88102
generation, distance, from_tag)) {
89103
copy_data:
104+
if (data->debug) {
105+
int i;
106+
static int label_width = -1;
107+
static int name_width = -1;
108+
if (label_width < 0) {
109+
int w;
110+
for (i = 0; i < ARRAY_SIZE(prio_names); i++) {
111+
w = strlen(_(prio_names[i]));
112+
if (label_width < w)
113+
label_width = w;
114+
}
115+
}
116+
if (name_width < 0) {
117+
int w;
118+
for (i = 0; i < data->revs->nr; i++) {
119+
w = strlen(data->revs->objects[i].name);
120+
if (name_width < w)
121+
name_width = w;
122+
}
123+
}
124+
for (i = 0; i < data->revs->nr; i++)
125+
if (!oidcmp(&commit->object.oid,
126+
&data->revs->objects[i].item->oid))
127+
fprintf(stderr, " %-*s %8d %-*s %s~%d\n",
128+
label_width,
129+
_(prio_names[from_tag]),
130+
distance, name_width,
131+
data->revs->objects[i].name,
132+
tip_name, generation);
133+
}
90134
name->tip_name = tip_name;
91135
name->taggerdate = taggerdate;
92136
name->generation = generation;
@@ -112,11 +156,11 @@ static void name_rev(struct commit *commit,
112156

113157
name_rev(parents->item, new_name, taggerdate, 0,
114158
distance + MERGE_TRAVERSAL_WEIGHT,
115-
from_tag, 0);
159+
from_tag, 0, data);
116160
} else {
117161
name_rev(parents->item, tip_name, taggerdate,
118162
generation + 1, distance + 1,
119-
from_tag, 0);
163+
from_tag, 0, data);
120164
}
121165
}
122166
}
@@ -146,13 +190,6 @@ static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous)
146190
return refname;
147191
}
148192

149-
struct name_ref_data {
150-
int tags_only;
151-
int name_only;
152-
struct string_list ref_filters;
153-
struct string_list exclude_filters;
154-
};
155-
156193
static struct tip_table {
157194
struct tip_table_entry {
158195
unsigned char sha1[20];
@@ -236,7 +273,6 @@ static int name_ref(const char *path, const struct object_id *oid, int flags, vo
236273
}
237274

238275
add_to_tip_table(oid->hash, path, can_abbreviate_output);
239-
240276
while (o && o->type == OBJ_TAG) {
241277
struct tag *t = (struct tag *) o;
242278
if (!t->tagged)
@@ -253,7 +289,7 @@ static int name_ref(const char *path, const struct object_id *oid, int flags, vo
253289
taggerdate = ((struct commit *)o)->date;
254290
path = name_ref_abbrev(path, can_abbreviate_output);
255291
name_rev(commit, xstrdup(path), taggerdate, 0, 0,
256-
from_tag, deref);
292+
from_tag, deref, data);
257293
}
258294
return 0;
259295
}
@@ -383,7 +419,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
383419
{
384420
struct object_array revs = OBJECT_ARRAY_INIT;
385421
int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
386-
struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
422+
struct name_ref_data data = { 0, 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
387423
struct option opts[] = {
388424
OPT_BOOL(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
389425
OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
@@ -393,6 +429,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
393429
N_("ignore refs matching <pattern>")),
394430
OPT_GROUP(""),
395431
OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
432+
OPT_BOOL(0, "debug", &data.debug, N_("debug search strategy on stderr")),
396433
OPT_BOOL(0, "stdin", &transform_stdin, N_("read from stdin")),
397434
OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")),
398435
OPT_BOOL(0, "always", &always,
@@ -458,6 +495,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
458495

459496
if (cutoff)
460497
cutoff = cutoff - CUTOFF_DATE_SLOP;
498+
data.revs = &revs;
461499
for_each_ref(name_ref, &data);
462500

463501
if (transform_stdin) {

0 commit comments

Comments
 (0)