run-command.c: remove run_command_v_opt_cd()
[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"
8
9 #define SEEN            (1u<<0)
10 #define MAX_TAGS        (FLAG_BITS - 1)
11
12 static const char * const describe_usage[] = {
13         "git describe [options] <committish>*",
14         NULL
15 };
16
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 static const char *pattern;
24 static int always;
25
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 };
35
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 }
53
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;
61
62         if (!all && !might_be_tag)
63                 return 0;
64
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         }
77
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                 else
86                         prio = 1;
87
88                 if (pattern && fnmatch(pattern, path + 10, 0))
89                         prio = 0;
90         }
91         else
92                 prio = 0;
93
94         if (!all) {
95                 if (!prio)
96                         return 0;
97                 if (!tags && prio < 2)
98                         return 0;
99         }
100         add_to_known_names(all ? path + 5 : path + 10, commit, prio, sha1);
101         return 0;
102 }
103
104 struct possible_tag {
105         struct commit_name *name;
106         int depth;
107         int found_order;
108         unsigned flag_within;
109 };
110
111 static int compare_pt(const void *a_, const void *b_)
112 {
113         struct possible_tag *a = (struct possible_tag *)a_;
114         struct possible_tag *b = (struct possible_tag *)b_;
115         if (a->name->prio != b->name->prio)
116                 return b->name->prio - a->name->prio;
117         if (a->depth != b->depth)
118                 return a->depth - b->depth;
119         if (a->found_order != b->found_order)
120                 return a->found_order - b->found_order;
121         return 0;
122 }
123
124 static unsigned long finish_depth_computation(
125         struct commit_list **list,
126         struct possible_tag *best)
127 {
128         unsigned long seen_commits = 0;
129         while (*list) {
130                 struct commit *c = pop_commit(list);
131                 struct commit_list *parents = c->parents;
132                 seen_commits++;
133                 if (c->object.flags & best->flag_within) {
134                         struct commit_list *a = *list;
135                         while (a) {
136                                 struct commit *i = a->item;
137                                 if (!(i->object.flags & best->flag_within))
138                                         break;
139                                 a = a->next;
140                         }
141                         if (!a)
142                                 break;
143                 } else
144                         best->depth++;
145                 while (parents) {
146                         struct commit *p = parents->item;
147                         parse_commit(p);
148                         if (!(p->object.flags & SEEN))
149                                 insert_by_date(p, list);
150                         p->object.flags |= c->object.flags;
151                         parents = parents->next;
152                 }
153         }
154         return seen_commits;
155 }
156
157 static void display_name(struct commit_name *n)
158 {
159         if (n->prio == 2 && !n->tag) {
160                 n->tag = lookup_tag(n->sha1);
161                 if (!n->tag || parse_tag(n->tag) || !n->tag->tag)
162                         die("annotated tag %s not available", n->path);
163                 if (strcmp(n->tag->tag, n->path))
164                         warning("tag '%s' is really '%s' here", n->tag->tag, n->path);
165         }
166
167         if (n->tag)
168                 printf("%s", n->tag->tag);
169         else
170                 printf("%s", n->path);
171 }
172
173 static void show_suffix(int depth, const unsigned char *sha1)
174 {
175         printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev));
176 }
177
178 static void describe(const char *arg, int last_one)
179 {
180         unsigned char sha1[20];
181         struct commit *cmit, *gave_up_on = NULL;
182         struct commit_list *list;
183         static int initialized = 0;
184         struct commit_name *n;
185         struct possible_tag all_matches[MAX_TAGS];
186         unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
187         unsigned long seen_commits = 0;
188
189         if (get_sha1(arg, sha1))
190                 die("Not a valid object name %s", arg);
191         cmit = lookup_commit_reference(sha1);
192         if (!cmit)
193                 die("%s is not a valid '%s' object", arg, commit_type);
194
195         if (!initialized) {
196                 initialized = 1;
197                 for_each_ref(get_name, NULL);
198         }
199
200         n = cmit->util;
201         if (n) {
202                 /*
203                  * Exact match to an existing ref.
204                  */
205                 display_name(n);
206                 if (longformat)
207                         show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1);
208                 printf("\n");
209                 return;
210         }
211
212         if (!max_candidates)
213                 die("no tag exactly matches '%s'", sha1_to_hex(cmit->object.sha1));
214         if (debug)
215                 fprintf(stderr, "searching to describe %s\n", arg);
216
217         list = NULL;
218         cmit->object.flags = SEEN;
219         commit_list_insert(cmit, &list);
220         while (list) {
221                 struct commit *c = pop_commit(&list);
222                 struct commit_list *parents = c->parents;
223                 seen_commits++;
224                 n = c->util;
225                 if (n) {
226                         if (match_cnt < max_candidates) {
227                                 struct possible_tag *t = &all_matches[match_cnt++];
228                                 t->name = n;
229                                 t->depth = seen_commits - 1;
230                                 t->flag_within = 1u << match_cnt;
231                                 t->found_order = match_cnt;
232                                 c->object.flags |= t->flag_within;
233                                 if (n->prio == 2)
234                                         annotated_cnt++;
235                         }
236                         else {
237                                 gave_up_on = c;
238                                 break;
239                         }
240                 }
241                 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
242                         struct possible_tag *t = &all_matches[cur_match];
243                         if (!(c->object.flags & t->flag_within))
244                                 t->depth++;
245                 }
246                 if (annotated_cnt && !list) {
247                         if (debug)
248                                 fprintf(stderr, "finished search at %s\n",
249                                         sha1_to_hex(c->object.sha1));
250                         break;
251                 }
252                 while (parents) {
253                         struct commit *p = parents->item;
254                         parse_commit(p);
255                         if (!(p->object.flags & SEEN))
256                                 insert_by_date(p, &list);
257                         p->object.flags |= c->object.flags;
258                         parents = parents->next;
259                 }
260         }
261
262         if (!match_cnt) {
263                 const unsigned char *sha1 = cmit->object.sha1;
264                 if (always) {
265                         printf("%s\n", find_unique_abbrev(sha1, abbrev));
266                         return;
267                 }
268                 die("cannot describe '%s'", sha1_to_hex(sha1));
269         }
270
271         qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
272
273         if (gave_up_on) {
274                 insert_by_date(gave_up_on, &list);
275                 seen_commits--;
276         }
277         seen_commits += finish_depth_computation(&list, &all_matches[0]);
278         free_commit_list(list);
279
280         if (debug) {
281                 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
282                         struct possible_tag *t = &all_matches[cur_match];
283                         fprintf(stderr, " %-11s %8d %s\n",
284                                 prio_names[t->name->prio],
285                                 t->depth, t->name->path);
286                 }
287                 fprintf(stderr, "traversed %lu commits\n", seen_commits);
288                 if (gave_up_on) {
289                         fprintf(stderr,
290                                 "more than %i tags found; listed %i most recent\n"
291                                 "gave up search at %s\n",
292                                 max_candidates, max_candidates,
293                                 sha1_to_hex(gave_up_on->object.sha1));
294                 }
295         }
296
297         display_name(all_matches[0].name);
298         if (abbrev)
299                 show_suffix(all_matches[0].depth, cmit->object.sha1);
300         printf("\n");
301
302         if (!last_one)
303                 clear_commit_marks(cmit, -1);
304 }
305
306 int cmd_describe(int argc, const char **argv, const char *prefix)
307 {
308         int contains = 0;
309         struct option options[] = {
310                 OPT_BOOLEAN(0, "contains",   &contains, "find the tag that comes after the commit"),
311                 OPT_BOOLEAN(0, "debug",      &debug, "debug search strategy on stderr"),
312                 OPT_BOOLEAN(0, "all",        &all, "use any ref in .git/refs"),
313                 OPT_BOOLEAN(0, "tags",       &tags, "use any tag in .git/refs/tags"),
314                 OPT_BOOLEAN(0, "long",       &longformat, "always use long format"),
315                 OPT__ABBREV(&abbrev),
316                 OPT_SET_INT(0, "exact-match", &max_candidates,
317                             "only output exact matches", 0),
318                 OPT_INTEGER(0, "candidates", &max_candidates,
319                             "consider <n> most recent tags (default: 10)"),
320                 OPT_STRING(0, "match",       &pattern, "pattern",
321                            "only consider tags matching <pattern>"),
322                 OPT_BOOLEAN(0, "always",     &always,
323                            "show abbreviated commit object as fallback"),
324                 OPT_END(),
325         };
326
327         argc = parse_options(argc, argv, options, describe_usage, 0);
328         if (max_candidates < 0)
329                 max_candidates = 0;
330         else if (max_candidates > MAX_TAGS)
331                 max_candidates = MAX_TAGS;
332
333         save_commit_buffer = 0;
334
335         if (longformat && abbrev == 0)
336                 die("--long is incompatible with --abbrev=0");
337
338         if (contains) {
339                 const char **args = xmalloc((7 + argc) * sizeof(char*));
340                 int i = 0;
341                 args[i++] = "name-rev";
342                 args[i++] = "--name-only";
343                 args[i++] = "--no-undefined";
344                 if (always)
345                         args[i++] = "--always";
346                 if (!all) {
347                         args[i++] = "--tags";
348                         if (pattern) {
349                                 char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
350                                 sprintf(s, "--refs=refs/tags/%s", pattern);
351                                 args[i++] = s;
352                         }
353                 }
354                 memcpy(args + i, argv, argc * sizeof(char*));
355                 args[i + argc] = NULL;
356                 return cmd_name_rev(i + argc, args, prefix);
357         }
358
359         if (argc == 0) {
360                 describe("HEAD", 1);
361         } else {
362                 while (argc-- > 0) {
363                         describe(*argv++, argc == 0);
364                 }
365         }
366         return 0;
367 }