Merge branch 'db/send-email-omit-cc'
[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 abbrev = DEFAULT_ABBREV;
21 static int max_candidates = 10;
22 const char *pattern = NULL;
23
24 struct commit_name {
25         int prio; /* annotated tag = 2, tag = 1, head = 0 */
26         char path[FLEX_ARRAY]; /* more */
27 };
28 static const char *prio_names[] = {
29         "head", "lightweight", "annotated",
30 };
31
32 static void add_to_known_names(const char *path,
33                                struct commit *commit,
34                                int prio)
35 {
36         struct commit_name *e = commit->util;
37         if (!e || e->prio < prio) {
38                 size_t len = strlen(path)+1;
39                 free(e);
40                 e = xmalloc(sizeof(struct commit_name) + len);
41                 e->prio = prio;
42                 memcpy(e->path, path, len);
43                 commit->util = e;
44         }
45 }
46
47 static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
48 {
49         struct commit *commit = lookup_commit_reference_gently(sha1, 1);
50         struct object *object;
51         int prio;
52
53         if (!commit)
54                 return 0;
55         object = parse_object(sha1);
56         /* If --all, then any refs are used.
57          * If --tags, then any tags are used.
58          * Otherwise only annotated tags are used.
59          */
60         if (!prefixcmp(path, "refs/tags/")) {
61                 if (object->type == OBJ_TAG) {
62                         prio = 2;
63                         if (pattern && fnmatch(pattern, path + 10, 0))
64                                 prio = 0;
65                 } else
66                         prio = 1;
67         }
68         else
69                 prio = 0;
70
71         if (!all) {
72                 if (!prio)
73                         return 0;
74                 if (!tags && prio < 2)
75                         return 0;
76         }
77         add_to_known_names(all ? path + 5 : path + 10, commit, prio);
78         return 0;
79 }
80
81 struct possible_tag {
82         struct commit_name *name;
83         int depth;
84         int found_order;
85         unsigned flag_within;
86 };
87
88 static int compare_pt(const void *a_, const void *b_)
89 {
90         struct possible_tag *a = (struct possible_tag *)a_;
91         struct possible_tag *b = (struct possible_tag *)b_;
92         if (a->name->prio != b->name->prio)
93                 return b->name->prio - a->name->prio;
94         if (a->depth != b->depth)
95                 return a->depth - b->depth;
96         if (a->found_order != b->found_order)
97                 return a->found_order - b->found_order;
98         return 0;
99 }
100
101 static unsigned long finish_depth_computation(
102         struct commit_list **list,
103         struct possible_tag *best)
104 {
105         unsigned long seen_commits = 0;
106         while (*list) {
107                 struct commit *c = pop_commit(list);
108                 struct commit_list *parents = c->parents;
109                 seen_commits++;
110                 if (c->object.flags & best->flag_within) {
111                         struct commit_list *a = *list;
112                         while (a) {
113                                 struct commit *i = a->item;
114                                 if (!(i->object.flags & best->flag_within))
115                                         break;
116                                 a = a->next;
117                         }
118                         if (!a)
119                                 break;
120                 } else
121                         best->depth++;
122                 while (parents) {
123                         struct commit *p = parents->item;
124                         parse_commit(p);
125                         if (!(p->object.flags & SEEN))
126                                 insert_by_date(p, list);
127                         p->object.flags |= c->object.flags;
128                         parents = parents->next;
129                 }
130         }
131         return seen_commits;
132 }
133
134 static void describe(const char *arg, int last_one)
135 {
136         unsigned char sha1[20];
137         struct commit *cmit, *gave_up_on = NULL;
138         struct commit_list *list;
139         static int initialized = 0;
140         struct commit_name *n;
141         struct possible_tag all_matches[MAX_TAGS];
142         unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
143         unsigned long seen_commits = 0;
144
145         if (get_sha1(arg, sha1))
146                 die("Not a valid object name %s", arg);
147         cmit = lookup_commit_reference(sha1);
148         if (!cmit)
149                 die("%s is not a valid '%s' object", arg, commit_type);
150
151         if (!initialized) {
152                 initialized = 1;
153                 for_each_ref(get_name, NULL);
154         }
155
156         n = cmit->util;
157         if (n) {
158                 printf("%s\n", n->path);
159                 return;
160         }
161
162         if (debug)
163                 fprintf(stderr, "searching to describe %s\n", arg);
164
165         list = NULL;
166         cmit->object.flags = SEEN;
167         commit_list_insert(cmit, &list);
168         while (list) {
169                 struct commit *c = pop_commit(&list);
170                 struct commit_list *parents = c->parents;
171                 seen_commits++;
172                 n = c->util;
173                 if (n) {
174                         if (match_cnt < max_candidates) {
175                                 struct possible_tag *t = &all_matches[match_cnt++];
176                                 t->name = n;
177                                 t->depth = seen_commits - 1;
178                                 t->flag_within = 1u << match_cnt;
179                                 t->found_order = match_cnt;
180                                 c->object.flags |= t->flag_within;
181                                 if (n->prio == 2)
182                                         annotated_cnt++;
183                         }
184                         else {
185                                 gave_up_on = c;
186                                 break;
187                         }
188                 }
189                 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
190                         struct possible_tag *t = &all_matches[cur_match];
191                         if (!(c->object.flags & t->flag_within))
192                                 t->depth++;
193                 }
194                 if (annotated_cnt && !list) {
195                         if (debug)
196                                 fprintf(stderr, "finished search at %s\n",
197                                         sha1_to_hex(c->object.sha1));
198                         break;
199                 }
200                 while (parents) {
201                         struct commit *p = parents->item;
202                         parse_commit(p);
203                         if (!(p->object.flags & SEEN))
204                                 insert_by_date(p, &list);
205                         p->object.flags |= c->object.flags;
206                         parents = parents->next;
207                 }
208         }
209
210         if (!match_cnt)
211                 die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
212
213         qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
214
215         if (gave_up_on) {
216                 insert_by_date(gave_up_on, &list);
217                 seen_commits--;
218         }
219         seen_commits += finish_depth_computation(&list, &all_matches[0]);
220         free_commit_list(list);
221
222         if (debug) {
223                 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
224                         struct possible_tag *t = &all_matches[cur_match];
225                         fprintf(stderr, " %-11s %8d %s\n",
226                                 prio_names[t->name->prio],
227                                 t->depth, t->name->path);
228                 }
229                 fprintf(stderr, "traversed %lu commits\n", seen_commits);
230                 if (gave_up_on) {
231                         fprintf(stderr,
232                                 "more than %i tags found; listed %i most recent\n"
233                                 "gave up search at %s\n",
234                                 max_candidates, max_candidates,
235                                 sha1_to_hex(gave_up_on->object.sha1));
236                 }
237         }
238         if (abbrev == 0)
239                 printf("%s\n", all_matches[0].name->path );
240         else
241                 printf("%s-%d-g%s\n", all_matches[0].name->path,
242                        all_matches[0].depth,
243                        find_unique_abbrev(cmit->object.sha1, abbrev));
244
245         if (!last_one)
246                 clear_commit_marks(cmit, -1);
247 }
248
249 int cmd_describe(int argc, const char **argv, const char *prefix)
250 {
251         int contains = 0;
252         struct option options[] = {
253                 OPT_BOOLEAN(0, "contains",   &contains, "find the tag that comes after the commit"),
254                 OPT_BOOLEAN(0, "debug",      &debug, "debug search strategy on stderr"),
255                 OPT_BOOLEAN(0, "all",        &all, "use any ref in .git/refs"),
256                 OPT_BOOLEAN(0, "tags",       &tags, "use any tag in .git/refs/tags"),
257                 OPT__ABBREV(&abbrev),
258                 OPT_INTEGER(0, "candidates", &max_candidates,
259                             "consider <n> most recent tags (default: 10)"),
260                 OPT_STRING(0, "match",       &pattern, "pattern",
261                            "only consider tags matching <pattern>"),
262                 OPT_END(),
263         };
264
265         argc = parse_options(argc, argv, options, describe_usage, 0);
266         if (max_candidates < 1)
267                 max_candidates = 1;
268         else if (max_candidates > MAX_TAGS)
269                 max_candidates = MAX_TAGS;
270
271         save_commit_buffer = 0;
272
273         if (contains) {
274                 const char **args = xmalloc((6 + argc) * sizeof(char*));
275                 int i = 0;
276                 args[i++] = "name-rev";
277                 args[i++] = "--name-only";
278                 args[i++] = "--no-undefined";
279                 if (!all) {
280                         args[i++] = "--tags";
281                         if (pattern) {
282                                 char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
283                                 sprintf(s, "--refs=refs/tags/%s", pattern);
284                                 args[i++] = s;
285                         }
286                 }
287                 memcpy(args + i, argv, argc * sizeof(char*));
288                 args[i + argc] = NULL;
289                 return cmd_name_rev(i + argc, args, prefix);
290         }
291
292         if (argc == 0) {
293                 describe("HEAD", 1);
294         } else {
295                 while (argc-- > 0) {
296                         describe(*argv++, argc == 0);
297                 }
298         }
299         return 0;
300 }