Code

describe --always: fall back to showing an abbreviated object name
[git.git] / builtin-describe.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "refs.h"
5 #include "builtin.h"
6 #include "exec_cmd.h"
7 #include "parse-options.h"
9 #define SEEN            (1u<<0)
10 #define MAX_TAGS        (FLAG_BITS - 1)
12 static const char * const describe_usage[] = {
13         "git-describe [options] <committish>*",
14         NULL
15 };
17 static int debug;       /* Display lots of verbose info */
18 static int all; /* Default to annotated tags only */
19 static int tags;        /* But allow any tags if --tags is specified */
20 static int longformat;
21 static int abbrev = DEFAULT_ABBREV;
22 static int max_candidates = 10;
23 const char *pattern = NULL;
24 static int always;
26 struct commit_name {
27         struct tag *tag;
28         int prio; /* annotated tag = 2, tag = 1, head = 0 */
29         unsigned char sha1[20];
30         char path[FLEX_ARRAY]; /* more */
31 };
32 static const char *prio_names[] = {
33         "head", "lightweight", "annotated",
34 };
36 static void add_to_known_names(const char *path,
37                                struct commit *commit,
38                                int prio,
39                                const unsigned char *sha1)
40 {
41         struct commit_name *e = commit->util;
42         if (!e || e->prio < prio) {
43                 size_t len = strlen(path)+1;
44                 free(e);
45                 e = xmalloc(sizeof(struct commit_name) + len);
46                 e->tag = NULL;
47                 e->prio = prio;
48                 hashcpy(e->sha1, sha1);
49                 memcpy(e->path, path, len);
50                 commit->util = e;
51         }
52 }
54 static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
55 {
56         int might_be_tag = !prefixcmp(path, "refs/tags/");
57         struct commit *commit;
58         struct object *object;
59         unsigned char peeled[20];
60         int is_tag, prio;
62         if (!all && !might_be_tag)
63                 return 0;
65         if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) {
66                 commit = lookup_commit_reference_gently(peeled, 1);
67                 if (!commit)
68                         return 0;
69                 is_tag = !!hashcmp(sha1, commit->object.sha1);
70         } else {
71                 commit = lookup_commit_reference_gently(sha1, 1);
72                 object = parse_object(sha1);
73                 if (!commit || !object)
74                         return 0;
75                 is_tag = object->type == OBJ_TAG;
76         }
78         /* If --all, then any refs are used.
79          * If --tags, then any tags are used.
80          * Otherwise only annotated tags are used.
81          */
82         if (might_be_tag) {
83                 if (is_tag) {
84                         prio = 2;
85                         if (pattern && fnmatch(pattern, path + 10, 0))
86                                 prio = 0;
87                 } else
88                         prio = 1;
89         }
90         else
91                 prio = 0;
93         if (!all) {
94                 if (!prio)
95                         return 0;
96                 if (!tags && prio < 2)
97                         return 0;
98         }
99         add_to_known_names(all ? path + 5 : path + 10, commit, prio, sha1);
100         return 0;
103 struct possible_tag {
104         struct commit_name *name;
105         int depth;
106         int found_order;
107         unsigned flag_within;
108 };
110 static int compare_pt(const void *a_, const void *b_)
112         struct possible_tag *a = (struct possible_tag *)a_;
113         struct possible_tag *b = (struct possible_tag *)b_;
114         if (a->name->prio != b->name->prio)
115                 return b->name->prio - a->name->prio;
116         if (a->depth != b->depth)
117                 return a->depth - b->depth;
118         if (a->found_order != b->found_order)
119                 return a->found_order - b->found_order;
120         return 0;
123 static unsigned long finish_depth_computation(
124         struct commit_list **list,
125         struct possible_tag *best)
127         unsigned long seen_commits = 0;
128         while (*list) {
129                 struct commit *c = pop_commit(list);
130                 struct commit_list *parents = c->parents;
131                 seen_commits++;
132                 if (c->object.flags & best->flag_within) {
133                         struct commit_list *a = *list;
134                         while (a) {
135                                 struct commit *i = a->item;
136                                 if (!(i->object.flags & best->flag_within))
137                                         break;
138                                 a = a->next;
139                         }
140                         if (!a)
141                                 break;
142                 } else
143                         best->depth++;
144                 while (parents) {
145                         struct commit *p = parents->item;
146                         parse_commit(p);
147                         if (!(p->object.flags & SEEN))
148                                 insert_by_date(p, list);
149                         p->object.flags |= c->object.flags;
150                         parents = parents->next;
151                 }
152         }
153         return seen_commits;
156 static void display_name(struct commit_name *n)
158         if (n->prio == 2 && !n->tag) {
159                 n->tag = lookup_tag(n->sha1);
160                 if (!n->tag || !n->tag->tag)
161                         die("annotated tag %s not available", n->path);
162                 if (strcmp(n->tag->tag, n->path))
163                         warning("tag '%s' is really '%s' here", n->tag->tag, n->path);
164         }
166         if (n->tag)
167                 printf("%s", n->tag->tag);
168         else
169                 printf("%s", n->path);
170         if (longformat)
171                 printf("-0-g%s",
172                        find_unique_abbrev(n->tag->tagged->sha1, abbrev));
175 static void describe(const char *arg, int last_one)
177         unsigned char sha1[20];
178         struct commit *cmit, *gave_up_on = NULL;
179         struct commit_list *list;
180         static int initialized = 0;
181         struct commit_name *n;
182         struct possible_tag all_matches[MAX_TAGS];
183         unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
184         unsigned long seen_commits = 0;
186         if (get_sha1(arg, sha1))
187                 die("Not a valid object name %s", arg);
188         cmit = lookup_commit_reference(sha1);
189         if (!cmit)
190                 die("%s is not a valid '%s' object", arg, commit_type);
192         if (!initialized) {
193                 initialized = 1;
194                 for_each_ref(get_name, NULL);
195         }
197         n = cmit->util;
198         if (n) {
199                 display_name(n);
200                 printf("\n");
201                 return;
202         }
204         if (!max_candidates)
205                 die("no tag exactly matches '%s'", sha1_to_hex(cmit->object.sha1));
206         if (debug)
207                 fprintf(stderr, "searching to describe %s\n", arg);
209         list = NULL;
210         cmit->object.flags = SEEN;
211         commit_list_insert(cmit, &list);
212         while (list) {
213                 struct commit *c = pop_commit(&list);
214                 struct commit_list *parents = c->parents;
215                 seen_commits++;
216                 n = c->util;
217                 if (n) {
218                         if (match_cnt < max_candidates) {
219                                 struct possible_tag *t = &all_matches[match_cnt++];
220                                 t->name = n;
221                                 t->depth = seen_commits - 1;
222                                 t->flag_within = 1u << match_cnt;
223                                 t->found_order = match_cnt;
224                                 c->object.flags |= t->flag_within;
225                                 if (n->prio == 2)
226                                         annotated_cnt++;
227                         }
228                         else {
229                                 gave_up_on = c;
230                                 break;
231                         }
232                 }
233                 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
234                         struct possible_tag *t = &all_matches[cur_match];
235                         if (!(c->object.flags & t->flag_within))
236                                 t->depth++;
237                 }
238                 if (annotated_cnt && !list) {
239                         if (debug)
240                                 fprintf(stderr, "finished search at %s\n",
241                                         sha1_to_hex(c->object.sha1));
242                         break;
243                 }
244                 while (parents) {
245                         struct commit *p = parents->item;
246                         parse_commit(p);
247                         if (!(p->object.flags & SEEN))
248                                 insert_by_date(p, &list);
249                         p->object.flags |= c->object.flags;
250                         parents = parents->next;
251                 }
252         }
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         }
263         qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
265         if (gave_up_on) {
266                 insert_by_date(gave_up_on, &list);
267                 seen_commits--;
268         }
269         seen_commits += finish_depth_computation(&list, &all_matches[0]);
270         free_commit_list(list);
272         if (debug) {
273                 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
274                         struct possible_tag *t = &all_matches[cur_match];
275                         fprintf(stderr, " %-11s %8d %s\n",
276                                 prio_names[t->name->prio],
277                                 t->depth, t->name->path);
278                 }
279                 fprintf(stderr, "traversed %lu commits\n", seen_commits);
280                 if (gave_up_on) {
281                         fprintf(stderr,
282                                 "more than %i tags found; listed %i most recent\n"
283                                 "gave up search at %s\n",
284                                 max_candidates, max_candidates,
285                                 sha1_to_hex(gave_up_on->object.sha1));
286                 }
287         }
289         display_name(all_matches[0].name);
290         if (abbrev)
291                 printf("-%d-g%s", all_matches[0].depth,
292                        find_unique_abbrev(cmit->object.sha1, abbrev));
293         printf("\n");
295         if (!last_one)
296                 clear_commit_marks(cmit, -1);
299 int cmd_describe(int argc, const char **argv, const char *prefix)
301         int contains = 0;
302         struct option options[] = {
303                 OPT_BOOLEAN(0, "contains",   &contains, "find the tag that comes after the commit"),
304                 OPT_BOOLEAN(0, "debug",      &debug, "debug search strategy on stderr"),
305                 OPT_BOOLEAN(0, "all",        &all, "use any ref in .git/refs"),
306                 OPT_BOOLEAN(0, "tags",       &tags, "use any tag in .git/refs/tags"),
307                 OPT_BOOLEAN(0, "long",       &longformat, "always use long format"),
308                 OPT__ABBREV(&abbrev),
309                 OPT_SET_INT(0, "exact-match", &max_candidates,
310                             "only output exact matches", 0),
311                 OPT_INTEGER(0, "candidates", &max_candidates,
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         };
320         argc = parse_options(argc, argv, options, describe_usage, 0);
321         if (max_candidates < 0)
322                 max_candidates = 0;
323         else if (max_candidates > MAX_TAGS)
324                 max_candidates = MAX_TAGS;
326         save_commit_buffer = 0;
328         if (longformat && abbrev == 0)
329                 die("--long is incompatible with --abbrev=0");
331         if (contains) {
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) {
342                                 char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
343                                 sprintf(s, "--refs=refs/tags/%s", pattern);
344                                 args[i++] = s;
345                         }
346                 }
347                 memcpy(args + i, argv, argc * sizeof(char*));
348                 args[i + argc] = NULL;
349                 return cmd_name_rev(i + argc, args, prefix);
350         }
352         if (argc == 0) {
353                 describe("HEAD", 1);
354         } else {
355                 while (argc-- > 0) {
356                         describe(*argv++, argc == 0);
357                 }
358         }
359         return 0;