@matheusbsbs / matheus-bagliano / commits / da2478dbb0
Fork 0

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>

Junio C Hamano committed Mar 2, 2008 at 08:51 UTC da2478dbb000436b79e813ba7f243d6042f26e66
2 files changed +44 -32
builtin-describe.c
+14 -3
@@ -21,6 +21,7 @@ static int longformat;
21 static int abbrev = DEFAULT_ABBREV;
22 static int max_candidates = 10;
23 const char *pattern = NULL;
24 +static int always;
25
26 struct commit_name {
27 struct tag *tag;
@@ -250,8 +251,14 @@ static void describe(const char *arg, int last_one)
251 }
252 }
253
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 + }
262
263 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
264
@@ -305,6 +312,8 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
312 "consider <n> most recent tags (default: 10)"),
313 OPT_STRING(0, "match", &pattern, "pattern",
314 "only consider tags matching <pattern>"),
315 + OPT_BOOLEAN(0, "always", &always,
316 + "show abbreviated commit object as fallback"),
317 OPT_END(),
318 };
319
@@ -320,11 +329,13 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
329 die("--long is incompatible with --abbrev=0");
330
331 if (contains) {
323 - const char **args = xmalloc((6 + argc) * sizeof(char*));
332 + const char **args = xmalloc((7 + argc) * sizeof(char*));
333 int i = 0;
334 args[i++] = "name-rev";
335 args[i++] = "--name-only";
336 args[i++] = "--no-undefined";
337 + if (always)
338 + args[i++] = "--always";
339 if (!all) {
340 args[i++] = "--tags";
341 if (pattern) {
builtin-name-rev.c
+30 -29
@@ -125,7 +125,7 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
125 }
126
127 /* 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)
129 {
130 static char buffer[1024];
131 struct rev_name *n;
@@ -151,6 +151,26 @@ static const char *get_rev_name(struct object *o)
151 }
152 }
153
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 +
174 static char const * const name_rev_usage[] = {
175 "git-name-rev [options] ( --all | --stdin | <commit>... )",
176 NULL
@@ -159,7 +179,7 @@ static char const * const name_rev_usage[] = {
179 int cmd_name_rev(int argc, const char **argv, const char *prefix)
180 {
181 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;
183 struct name_ref_data data = { 0, 0, NULL };
184 struct option opts[] = {
185 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)
190 OPT_BOOLEAN(0, "all", &all, "list all commits reachable from all refs"),
191 OPT_BOOLEAN(0, "stdin", &transform_stdin, "read from stdin"),
192 OPT_BOOLEAN(0, "undefined", &allow_undefined, "allow to print `undefined` names"),
193 + OPT_BOOLEAN(0, "always", &always,
194 + "show abbreviated commit object as fallback"),
195 OPT_END(),
196 };
197
@@ -258,35 +280,14 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
280 int i, max;
281
282 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);
286 } else {
287 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);
291 }
292
293 return 0;