Code

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