Skip to content

Commit da2478d

Browse files
committed
describe --always: fall back to showing an abbreviated object name
Some callers may find it useful if "git describe" always gave back a string that can be used as a shorter name for a commit object, rather than checking its exit status (while squelching its error message, which could potentially talk about more grave errors that should not be squelched) and implementing a fallback themselves. This teaches describe/name-rev a new option, --always, to use an abbreviated object name when no tags or refs to use is found. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 34cd62e commit da2478d

File tree

2 files changed

+44
-32
lines changed

2 files changed

+44
-32
lines changed

builtin-describe.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ static int longformat;
2121
static int abbrev = DEFAULT_ABBREV;
2222
static int max_candidates = 10;
2323
const char *pattern = NULL;
24+
static int always;
2425

2526
struct commit_name {
2627
struct tag *tag;
@@ -250,8 +251,14 @@ static void describe(const char *arg, int last_one)
250251
}
251252
}
252253

253-
if (!match_cnt)
254-
die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
254+
if (!match_cnt) {
255+
const unsigned char *sha1 = cmit->object.sha1;
256+
if (always) {
257+
printf("%s\n", find_unique_abbrev(sha1, abbrev));
258+
return;
259+
}
260+
die("cannot describe '%s'", sha1_to_hex(sha1));
261+
}
255262

256263
qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
257264

@@ -305,6 +312,8 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
305312
"consider <n> most recent tags (default: 10)"),
306313
OPT_STRING(0, "match", &pattern, "pattern",
307314
"only consider tags matching <pattern>"),
315+
OPT_BOOLEAN(0, "always", &always,
316+
"show abbreviated commit object as fallback"),
308317
OPT_END(),
309318
};
310319

@@ -320,11 +329,13 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
320329
die("--long is incompatible with --abbrev=0");
321330

322331
if (contains) {
323-
const char **args = xmalloc((6 + argc) * sizeof(char*));
332+
const char **args = xmalloc((7 + argc) * sizeof(char*));
324333
int i = 0;
325334
args[i++] = "name-rev";
326335
args[i++] = "--name-only";
327336
args[i++] = "--no-undefined";
337+
if (always)
338+
args[i++] = "--always";
328339
if (!all) {
329340
args[i++] = "--tags";
330341
if (pattern) {

builtin-name-rev.c

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
125125
}
126126

127127
/* returns a static buffer */
128-
static const char *get_rev_name(struct object *o)
128+
static const char *get_rev_name(const struct object *o)
129129
{
130130
static char buffer[1024];
131131
struct rev_name *n;
@@ -151,6 +151,26 @@ static const char *get_rev_name(struct object *o)
151151
}
152152
}
153153

154+
static void show_name(const struct object *obj,
155+
const char *caller_name,
156+
int always, int allow_undefined, int name_only)
157+
{
158+
const char *name;
159+
const unsigned char *sha1 = obj->sha1;
160+
161+
if (!name_only)
162+
printf("%s ", caller_name ? caller_name : sha1_to_hex(sha1));
163+
name = get_rev_name(obj);
164+
if (name)
165+
printf("%s\n", name);
166+
else if (allow_undefined)
167+
printf("undefined\n");
168+
else if (always)
169+
printf("%s\n", find_unique_abbrev(sha1, DEFAULT_ABBREV));
170+
else
171+
die("cannot describe '%s'", sha1_to_hex(sha1));
172+
}
173+
154174
static char const * const name_rev_usage[] = {
155175
"git-name-rev [options] ( --all | --stdin | <commit>... )",
156176
NULL
@@ -159,7 +179,7 @@ static char const * const name_rev_usage[] = {
159179
int cmd_name_rev(int argc, const char **argv, const char *prefix)
160180
{
161181
struct object_array revs = { 0, 0, NULL };
162-
int all = 0, transform_stdin = 0, allow_undefined = 1;
182+
int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0;
163183
struct name_ref_data data = { 0, 0, NULL };
164184
struct option opts[] = {
165185
OPT_BOOLEAN(0, "name-only", &data.name_only, "print only names (no SHA-1)"),
@@ -170,6 +190,8 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
170190
OPT_BOOLEAN(0, "all", &all, "list all commits reachable from all refs"),
171191
OPT_BOOLEAN(0, "stdin", &transform_stdin, "read from stdin"),
172192
OPT_BOOLEAN(0, "undefined", &allow_undefined, "allow to print `undefined` names"),
193+
OPT_BOOLEAN(0, "always", &always,
194+
"show abbreviated commit object as fallback"),
173195
OPT_END(),
174196
};
175197

@@ -258,35 +280,14 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
258280
int i, max;
259281

260282
max = get_max_object_index();
261-
for (i = 0; i < max; i++) {
262-
struct object * obj = get_indexed_object(i);
263-
const char *name;
264-
if (!obj)
265-
continue;
266-
if (!data.name_only)
267-
printf("%s ", sha1_to_hex(obj->sha1));
268-
name = get_rev_name(obj);
269-
if (name)
270-
printf("%s\n", name);
271-
else if (allow_undefined)
272-
printf("undefined\n");
273-
else
274-
die("cannot describe '%s'", sha1_to_hex(obj->sha1));
275-
}
283+
for (i = 0; i < max; i++)
284+
show_name(get_indexed_object(i), NULL,
285+
always, allow_undefined, data.name_only);
276286
} else {
277287
int i;
278-
for (i = 0; i < revs.nr; i++) {
279-
const char *name;
280-
if (!data.name_only)
281-
printf("%s ", revs.objects[i].name);
282-
name = get_rev_name(revs.objects[i].item);
283-
if (name)
284-
printf("%s\n", name);
285-
else if (allow_undefined)
286-
printf("undefined\n");
287-
else
288-
die("cannot describe '%s'", sha1_to_hex(revs.objects[i].item->sha1));
289-
}
288+
for (i = 0; i < revs.nr; i++)
289+
show_name(revs.objects[i].item, revs.objects[i].name,
290+
always, allow_undefined, data.name_only);
290291
}
291292

292293
return 0;

0 commit comments

Comments
 (0)