Code

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