Merge branch 'tm/fetch-prune'
[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 #include "diff.h"
9 #include "hash.h"
10 #include "argv-array.h"
11
12 #define SEEN            (1u << 0)
13 #define MAX_TAGS        (FLAG_BITS - 1)
14
15 static const char * const describe_usage[] = {
16         N_("git describe [options] <commit-ish>*"),
17         N_("git describe [options] --dirty"),
18         NULL
19 };
20
21 static int debug;       /* Display lots of verbose info */
22 static int all; /* Any valid ref can be used */
23 static int tags;        /* Allow lightweight tags */
24 static int longformat;
25 static int first_parent;
26 static int abbrev = -1; /* unspecified */
27 static int max_candidates = 10;
28 static struct hash_table names;
29 static int have_util;
30 static const char *pattern;
31 static int always;
32 static const char *dirty;
33
34 /* diff-index command arguments to check if working tree is dirty. */
35 static const char *diff_index_args[] = {
36         "diff-index", "--quiet", "HEAD", "--", NULL
37 };
38
39 struct commit_name {
40         struct commit_name *next;
41         unsigned char peeled[20];
42         struct tag *tag;
43         unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
44         unsigned name_checked:1;
45         unsigned char sha1[20];
46         char *path;
47 };
48
49 static const char *prio_names[] = {
50         "head", "lightweight", "annotated",
51 };
52
53 static inline unsigned int hash_sha1(const unsigned char *sha1)
54 {
55         unsigned int hash;
56         memcpy(&hash, sha1, sizeof(hash));
57         return hash;
58 }
59
60 static inline struct commit_name *find_commit_name(const unsigned char *peeled)
61 {
62         struct commit_name *n = lookup_hash(hash_sha1(peeled), &names);
63         while (n && !!hashcmp(peeled, n->peeled))
64                 n = n->next;
65         return n;
66 }
67
68 static int set_util(void *chain, void *data)
69 {
70         struct commit_name *n;
71         for (n = chain; n; n = n->next) {
72                 struct commit *c = lookup_commit_reference_gently(n->peeled, 1);
73                 if (c)
74                         c->util = n;
75         }
76         return 0;
77 }
78
79 static int replace_name(struct commit_name *e,
80                                int prio,
81                                const unsigned char *sha1,
82                                struct tag **tag)
83 {
84         if (!e || e->prio < prio)
85                 return 1;
86
87         if (e->prio == 2 && prio == 2) {
88                 /* Multiple annotated tags point to the same commit.
89                  * Select one to keep based upon their tagger date.
90                  */
91                 struct tag *t;
92
93                 if (!e->tag) {
94                         t = lookup_tag(e->sha1);
95                         if (!t || parse_tag(t))
96                                 return 1;
97                         e->tag = t;
98                 }
99
100                 t = lookup_tag(sha1);
101                 if (!t || parse_tag(t))
102                         return 0;
103                 *tag = t;
104
105                 if (e->tag->date < t->date)
106                         return 1;
107         }
108
109         return 0;
110 }
111
112 static void add_to_known_names(const char *path,
113                                const unsigned char *peeled,
114                                int prio,
115                                const unsigned char *sha1)
116 {
117         struct commit_name *e = find_commit_name(peeled);
118         struct tag *tag = NULL;
119         if (replace_name(e, prio, sha1, &tag)) {
120                 if (!e) {
121                         void **pos;
122                         e = xmalloc(sizeof(struct commit_name));
123                         hashcpy(e->peeled, peeled);
124                         pos = insert_hash(hash_sha1(peeled), e, &names);
125                         if (pos) {
126                                 e->next = *pos;
127                                 *pos = e;
128                         } else {
129                                 e->next = NULL;
130                         }
131                         e->path = NULL;
132                 }
133                 e->tag = tag;
134                 e->prio = prio;
135                 e->name_checked = 0;
136                 hashcpy(e->sha1, sha1);
137                 free(e->path);
138                 e->path = xstrdup(path);
139         }
140 }
141
142 static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
143 {
144         int is_tag = starts_with(path, "refs/tags/");
145         unsigned char peeled[20];
146         int is_annotated, prio;
147
148         /* Reject anything outside refs/tags/ unless --all */
149         if (!all && !is_tag)
150                 return 0;
151
152         /* Accept only tags that match the pattern, if given */
153         if (pattern && (!is_tag || fnmatch(pattern, path + 10, 0)))
154                 return 0;
155
156         /* Is it annotated? */
157         if (!peel_ref(path, peeled)) {
158                 is_annotated = !!hashcmp(sha1, peeled);
159         } else {
160                 hashcpy(peeled, sha1);
161                 is_annotated = 0;
162         }
163
164         /*
165          * By default, we only use annotated tags, but with --tags
166          * we fall back to lightweight ones (even without --tags,
167          * we still remember lightweight ones, only to give hints
168          * in an error message).  --all allows any refs to be used.
169          */
170         if (is_annotated)
171                 prio = 2;
172         else if (is_tag)
173                 prio = 1;
174         else
175                 prio = 0;
176
177         add_to_known_names(all ? path + 5 : path + 10, peeled, prio, sha1);
178         return 0;
179 }
180
181 struct possible_tag {
182         struct commit_name *name;
183         int depth;
184         int found_order;
185         unsigned flag_within;
186 };
187
188 static int compare_pt(const void *a_, const void *b_)
189 {
190         struct possible_tag *a = (struct possible_tag *)a_;
191         struct possible_tag *b = (struct possible_tag *)b_;
192         if (a->depth != b->depth)
193                 return a->depth - b->depth;
194         if (a->found_order != b->found_order)
195                 return a->found_order - b->found_order;
196         return 0;
197 }
198
199 static unsigned long finish_depth_computation(
200         struct commit_list **list,
201         struct possible_tag *best)
202 {
203         unsigned long seen_commits = 0;
204         while (*list) {
205                 struct commit *c = pop_commit(list);
206                 struct commit_list *parents = c->parents;
207                 seen_commits++;
208                 if (c->object.flags & best->flag_within) {
209                         struct commit_list *a = *list;
210                         while (a) {
211                                 struct commit *i = a->item;
212                                 if (!(i->object.flags & best->flag_within))
213                                         break;
214                                 a = a->next;
215                         }
216                         if (!a)
217                                 break;
218                 } else
219                         best->depth++;
220                 while (parents) {
221                         struct commit *p = parents->item;
222                         parse_commit(p);
223                         if (!(p->object.flags & SEEN))
224                                 commit_list_insert_by_date(p, list);
225                         p->object.flags |= c->object.flags;
226                         parents = parents->next;
227                 }
228         }
229         return seen_commits;
230 }
231
232 static void display_name(struct commit_name *n)
233 {
234         if (n->prio == 2 && !n->tag) {
235                 n->tag = lookup_tag(n->sha1);
236                 if (!n->tag || parse_tag(n->tag))
237                         die(_("annotated tag %s not available"), n->path);
238         }
239         if (n->tag && !n->name_checked) {
240                 if (!n->tag->tag)
241                         die(_("annotated tag %s has no embedded name"), n->path);
242                 if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
243                         warning(_("tag '%s' is really '%s' here"), n->tag->tag, n->path);
244                 n->name_checked = 1;
245         }
246
247         if (n->tag)
248                 printf("%s", n->tag->tag);
249         else
250                 printf("%s", n->path);
251 }
252
253 static void show_suffix(int depth, const unsigned char *sha1)
254 {
255         printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev));
256 }
257
258 static void describe(const char *arg, int last_one)
259 {
260         unsigned char sha1[20];
261         struct commit *cmit, *gave_up_on = NULL;
262         struct commit_list *list;
263         struct commit_name *n;
264         struct possible_tag all_matches[MAX_TAGS];
265         unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
266         unsigned long seen_commits = 0;
267         unsigned int unannotated_cnt = 0;
268
269         if (get_sha1(arg, sha1))
270                 die(_("Not a valid object name %s"), arg);
271         cmit = lookup_commit_reference(sha1);
272         if (!cmit)
273                 die(_("%s is not a valid '%s' object"), arg, commit_type);
274
275         n = find_commit_name(cmit->object.sha1);
276         if (n && (tags || all || n->prio == 2)) {
277                 /*
278                  * Exact match to an existing ref.
279                  */
280                 display_name(n);
281                 if (longformat)
282                         show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1);
283                 if (dirty)
284                         printf("%s", dirty);
285                 printf("\n");
286                 return;
287         }
288
289         if (!max_candidates)
290                 die(_("no tag exactly matches '%s'"), sha1_to_hex(cmit->object.sha1));
291         if (debug)
292                 fprintf(stderr, _("searching to describe %s\n"), arg);
293
294         if (!have_util) {
295                 for_each_hash(&names, set_util, NULL);
296                 have_util = 1;
297         }
298
299         list = NULL;
300         cmit->object.flags = SEEN;
301         commit_list_insert(cmit, &list);
302         while (list) {
303                 struct commit *c = pop_commit(&list);
304                 struct commit_list *parents = c->parents;
305                 seen_commits++;
306                 n = c->util;
307                 if (n) {
308                         if (!tags && !all && n->prio < 2) {
309                                 unannotated_cnt++;
310                         } else if (match_cnt < max_candidates) {
311                                 struct possible_tag *t = &all_matches[match_cnt++];
312                                 t->name = n;
313                                 t->depth = seen_commits - 1;
314                                 t->flag_within = 1u << match_cnt;
315                                 t->found_order = match_cnt;
316                                 c->object.flags |= t->flag_within;
317                                 if (n->prio == 2)
318                                         annotated_cnt++;
319                         }
320                         else {
321                                 gave_up_on = c;
322                                 break;
323                         }
324                 }
325                 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
326                         struct possible_tag *t = &all_matches[cur_match];
327                         if (!(c->object.flags & t->flag_within))
328                                 t->depth++;
329                 }
330                 if (annotated_cnt && !list) {
331                         if (debug)
332                                 fprintf(stderr, _("finished search at %s\n"),
333                                         sha1_to_hex(c->object.sha1));
334                         break;
335                 }
336                 while (parents) {
337                         struct commit *p = parents->item;
338                         parse_commit(p);
339                         if (!(p->object.flags & SEEN))
340                                 commit_list_insert_by_date(p, &list);
341                         p->object.flags |= c->object.flags;
342                         parents = parents->next;
343
344                         if (first_parent)
345                                 break;
346                 }
347         }
348
349         if (!match_cnt) {
350                 const unsigned char *sha1 = cmit->object.sha1;
351                 if (always) {
352                         printf("%s", find_unique_abbrev(sha1, abbrev));
353                         if (dirty)
354                                 printf("%s", dirty);
355                         printf("\n");
356                         return;
357                 }
358                 if (unannotated_cnt)
359                         die(_("No annotated tags can describe '%s'.\n"
360                             "However, there were unannotated tags: try --tags."),
361                             sha1_to_hex(sha1));
362                 else
363                         die(_("No tags can describe '%s'.\n"
364                             "Try --always, or create some tags."),
365                             sha1_to_hex(sha1));
366         }
367
368         qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
369
370         if (gave_up_on) {
371                 commit_list_insert_by_date(gave_up_on, &list);
372                 seen_commits--;
373         }
374         seen_commits += finish_depth_computation(&list, &all_matches[0]);
375         free_commit_list(list);
376
377         if (debug) {
378                 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
379                         struct possible_tag *t = &all_matches[cur_match];
380                         fprintf(stderr, " %-11s %8d %s\n",
381                                 prio_names[t->name->prio],
382                                 t->depth, t->name->path);
383                 }
384                 fprintf(stderr, _("traversed %lu commits\n"), seen_commits);
385                 if (gave_up_on) {
386                         fprintf(stderr,
387                                 _("more than %i tags found; listed %i most recent\n"
388                                 "gave up search at %s\n"),
389                                 max_candidates, max_candidates,
390                                 sha1_to_hex(gave_up_on->object.sha1));
391                 }
392         }
393
394         display_name(all_matches[0].name);
395         if (abbrev)
396                 show_suffix(all_matches[0].depth, cmit->object.sha1);
397         if (dirty)
398                 printf("%s", dirty);
399         printf("\n");
400
401         if (!last_one)
402                 clear_commit_marks(cmit, -1);
403 }
404
405 int cmd_describe(int argc, const char **argv, const char *prefix)
406 {
407         int contains = 0;
408         struct option options[] = {
409                 OPT_BOOL(0, "contains",   &contains, N_("find the tag that comes after the commit")),
410                 OPT_BOOL(0, "debug",      &debug, N_("debug search strategy on stderr")),
411                 OPT_BOOL(0, "all",        &all, N_("use any ref")),
412                 OPT_BOOL(0, "tags",       &tags, N_("use any tag, even unannotated")),
413                 OPT_BOOL(0, "long",       &longformat, N_("always use long format")),
414                 OPT_BOOL(0, "first-parent", &first_parent, N_("only follow first parent")),
415                 OPT__ABBREV(&abbrev),
416                 OPT_SET_INT(0, "exact-match", &max_candidates,
417                             N_("only output exact matches"), 0),
418                 OPT_INTEGER(0, "candidates", &max_candidates,
419                             N_("consider <n> most recent tags (default: 10)")),
420                 OPT_STRING(0, "match",       &pattern, N_("pattern"),
421                            N_("only consider tags matching <pattern>")),
422                 OPT_BOOL(0, "always",        &always,
423                         N_("show abbreviated commit object as fallback")),
424                 {OPTION_STRING, 0, "dirty",  &dirty, N_("mark"),
425                         N_("append <mark> on dirty working tree (default: \"-dirty\")"),
426                         PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
427                 OPT_END(),
428         };
429
430         git_config(git_default_config, NULL);
431         argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
432         if (abbrev < 0)
433                 abbrev = DEFAULT_ABBREV;
434
435         if (max_candidates < 0)
436                 max_candidates = 0;
437         else if (max_candidates > MAX_TAGS)
438                 max_candidates = MAX_TAGS;
439
440         save_commit_buffer = 0;
441
442         if (longformat && abbrev == 0)
443                 die(_("--long is incompatible with --abbrev=0"));
444
445         if (contains) {
446                 struct argv_array args;
447
448                 argv_array_init(&args);
449                 argv_array_pushl(&args, "name-rev",
450                                  "--peel-tag", "--name-only", "--no-undefined",
451                                  NULL);
452                 if (always)
453                         argv_array_push(&args, "--always");
454                 if (!all) {
455                         argv_array_push(&args, "--tags");
456                         if (pattern)
457                                 argv_array_pushf(&args, "--refs=refs/tags/%s", pattern);
458                 }
459                 while (*argv) {
460                         argv_array_push(&args, *argv);
461                         argv++;
462                 }
463                 return cmd_name_rev(args.argc, args.argv, prefix);
464         }
465
466         init_hash(&names);
467         for_each_rawref(get_name, NULL);
468         if (!names.nr && !always)
469                 die(_("No names found, cannot describe anything."));
470
471         if (argc == 0) {
472                 if (dirty) {
473                         static struct lock_file index_lock;
474                         int fd;
475
476                         read_cache_preload(NULL);
477                         refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED,
478                                       NULL, NULL, NULL);
479                         fd = hold_locked_index(&index_lock, 0);
480                         if (0 <= fd)
481                                 update_index_if_able(&the_index, &index_lock);
482
483                         if (!cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1,
484                                             diff_index_args, prefix))
485                                 dirty = NULL;
486                 }
487                 describe("HEAD", 1);
488         } else if (dirty) {
489                 die(_("--dirty is incompatible with commit-ishes"));
490         } else {
491                 while (argc-- > 0)
492                         describe(*argv++, argc == 0);
493         }
494         return 0;
495 }