Merge branch 'maint'
[git/git.git] / builtin-describe.c
CommitLineData
908e5310
LT
1#include "cache.h"
2#include "commit.h"
635d4134 3#include "tag.h"
908e5310 4#include "refs.h"
9a0eaf83 5#include "builtin.h"
23615708 6#include "exec_cmd.h"
166185be 7#include "parse-options.h"
908e5310 8
8713ab30
SP
9#define SEEN (1u<<0)
10#define MAX_TAGS (FLAG_BITS - 1)
11
166185be
PH
12static const char * const describe_usage[] = {
13 "git-describe [options] <committish>*",
14 NULL
15};
908e5310 16
8713ab30 17static int debug; /* Display lots of verbose info */
96f1e58f
DR
18static int all; /* Default to annotated tags only */
19static int tags; /* But allow any tags if --tags is specified */
2d9e7c9f 20static int abbrev = DEFAULT_ABBREV;
8713ab30 21static int max_candidates = 10;
30ffa603 22const char *pattern = NULL;
908e5310 23
e7eb5034 24struct commit_name {
64deb858 25 int prio; /* annotated tag = 2, tag = 1, head = 0 */
5a2282de 26 char path[FLEX_ARRAY]; /* more */
e7eb5034 27};
cf69fd49
SP
28static const char *prio_names[] = {
29 "head", "lightweight", "annotated",
30};
908e5310 31
64deb858 32static void add_to_known_names(const char *path,
80dbae03 33 struct commit *commit,
64deb858 34 int prio)
908e5310 35{
e7eb5034
SP
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;
908e5310 44 }
908e5310
LT
45}
46
8da19775 47static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
908e5310 48{
8a5a1884 49 int might_be_tag = !prefixcmp(path, "refs/tags/");
feededd0 50 struct commit *commit;
64deb858 51 struct object *object;
feededd0
SP
52 unsigned char peeled[20];
53 int is_tag, prio;
54
8a5a1884
SP
55 if (!all && !might_be_tag)
56 return 0;
57
feededd0
SP
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 }
64deb858 70
2d9e7c9f
JH
71 /* If --all, then any refs are used.
72 * If --tags, then any tags are used.
73 * Otherwise only annotated tags are used.
74 */
8a5a1884 75 if (might_be_tag) {
feededd0 76 if (is_tag) {
64deb858 77 prio = 2;
30ffa603
PH
78 if (pattern && fnmatch(pattern, path + 10, 0))
79 prio = 0;
80 } else
64deb858
JH
81 prio = 1;
82 }
83 else
84 prio = 0;
85
635d4134 86 if (!all) {
64deb858
JH
87 if (!prio)
88 return 0;
89 if (!tags && prio < 2)
635d4134 90 return 0;
635d4134 91 }
64deb858 92 add_to_known_names(all ? path + 5 : path + 10, commit, prio);
908e5310
LT
93 return 0;
94}
95
80dbae03 96struct possible_tag {
80dbae03 97 struct commit_name *name;
cf69fd49
SP
98 int depth;
99 int found_order;
8713ab30 100 unsigned flag_within;
80dbae03
SP
101};
102
cf69fd49
SP
103static int compare_pt(const void *a_, const void *b_)
104{
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;
114}
115
1b600e65
SP
116static unsigned long finish_depth_computation(
117 struct commit_list **list,
118 struct possible_tag *best)
119{
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;
147}
148
554fe20d 149static void describe(const char *arg, int last_one)
908e5310 150{
4c34a2c5 151 unsigned char sha1[20];
8713ab30 152 struct commit *cmit, *gave_up_on = NULL;
908e5310
LT
153 struct commit_list *list;
154 static int initialized = 0;
155 struct commit_name *n;
cf69fd49 156 struct possible_tag all_matches[MAX_TAGS];
8713ab30
SP
157 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
158 unsigned long seen_commits = 0;
908e5310 159
31fff305
DL
160 if (get_sha1(arg, sha1))
161 die("Not a valid object name %s", arg);
4c34a2c5
JH
162 cmit = lookup_commit_reference(sha1);
163 if (!cmit)
31fff305 164 die("%s is not a valid '%s' object", arg, commit_type);
4c34a2c5 165
908e5310
LT
166 if (!initialized) {
167 initialized = 1;
cb5d709f 168 for_each_ref(get_name, NULL);
908e5310
LT
169 }
170
e7eb5034 171 n = cmit->util;
908e5310
LT
172 if (n) {
173 printf("%s\n", n->path);
174 return;
175 }
176
2c33f757
SP
177 if (!max_candidates)
178 die("no tag exactly matches '%s'", sha1_to_hex(cmit->object.sha1));
8713ab30
SP
179 if (debug)
180 fprintf(stderr, "searching to describe %s\n", arg);
181
908e5310 182 list = NULL;
8713ab30 183 cmit->object.flags = SEEN;
908e5310
LT
184 commit_list_insert(cmit, &list);
185 while (list) {
80dbae03 186 struct commit *c = pop_commit(&list);
dccd0c2a 187 struct commit_list *parents = c->parents;
8713ab30 188 seen_commits++;
e7eb5034 189 n = c->util;
908e5310 190 if (n) {
8713ab30
SP
191 if (match_cnt < max_candidates) {
192 struct possible_tag *t = &all_matches[match_cnt++];
193 t->name = n;
194 t->depth = seen_commits - 1;
195 t->flag_within = 1u << match_cnt;
8a8169c0 196 t->found_order = match_cnt;
8713ab30
SP
197 c->object.flags |= t->flag_within;
198 if (n->prio == 2)
199 annotated_cnt++;
200 }
201 else {
202 gave_up_on = c;
203 break;
204 }
205 }
206 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
207 struct possible_tag *t = &all_matches[cur_match];
208 if (!(c->object.flags & t->flag_within))
209 t->depth++;
210 }
211 if (annotated_cnt && !list) {
212 if (debug)
213 fprintf(stderr, "finished search at %s\n",
214 sha1_to_hex(c->object.sha1));
215 break;
dccd0c2a
SP
216 }
217 while (parents) {
218 struct commit *p = parents->item;
219 parse_commit(p);
8713ab30 220 if (!(p->object.flags & SEEN))
dccd0c2a 221 insert_by_date(p, &list);
8713ab30 222 p->object.flags |= c->object.flags;
dccd0c2a 223 parents = parents->next;
80dbae03
SP
224 }
225 }
226
8713ab30 227 if (!match_cnt)
80dbae03
SP
228 die("cannot describe '%s'", sha1_to_hex(cmit->object.sha1));
229
cf69fd49 230 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
1b600e65
SP
231
232 if (gave_up_on) {
233 insert_by_date(gave_up_on, &list);
234 seen_commits--;
235 }
236 seen_commits += finish_depth_computation(&list, &all_matches[0]);
237 free_commit_list(list);
238
8713ab30
SP
239 if (debug) {
240 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
241 struct possible_tag *t = &all_matches[cur_match];
cf69fd49
SP
242 fprintf(stderr, " %-11s %8d %s\n",
243 prio_names[t->name->prio],
8713ab30
SP
244 t->depth, t->name->path);
245 }
246 fprintf(stderr, "traversed %lu commits\n", seen_commits);
247 if (gave_up_on) {
248 fprintf(stderr,
249 "more than %i tags found; listed %i most recent\n"
250 "gave up search at %s\n",
251 max_candidates, max_candidates,
252 sha1_to_hex(gave_up_on->object.sha1));
253 }
80dbae03 254 }
f127404c
AP
255 if (abbrev == 0)
256 printf("%s\n", all_matches[0].name->path );
257 else
237fb6ca
SP
258 printf("%s-%d-g%s\n", all_matches[0].name->path,
259 all_matches[0].depth,
260 find_unique_abbrev(cmit->object.sha1, abbrev));
80dbae03 261
8713ab30
SP
262 if (!last_one)
263 clear_commit_marks(cmit, -1);
908e5310
LT
264}
265
9a0eaf83 266int cmd_describe(int argc, const char **argv, const char *prefix)
908e5310 267{
23615708 268 int contains = 0;
166185be
PH
269 struct option options[] = {
270 OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"),
271 OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"),
272 OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),
273 OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),
274 OPT__ABBREV(&abbrev),
2c33f757
SP
275 OPT_SET_INT(0, "exact-match", &max_candidates,
276 "only output exact matches", 0),
166185be 277 OPT_INTEGER(0, "candidates", &max_candidates,
30ffa603
PH
278 "consider <n> most recent tags (default: 10)"),
279 OPT_STRING(0, "match", &pattern, "pattern",
280 "only consider tags matching <pattern>"),
166185be
PH
281 OPT_END(),
282 };
908e5310 283
166185be 284 argc = parse_options(argc, argv, options, describe_usage, 0);
2c33f757
SP
285 if (max_candidates < 0)
286 max_candidates = 0;
166185be
PH
287 else if (max_candidates > MAX_TAGS)
288 max_candidates = MAX_TAGS;
4c34a2c5 289
8c599c74 290 save_commit_buffer = 0;
8112894d 291
23615708 292 if (contains) {
a2cf9f44 293 const char **args = xmalloc((6 + argc) * sizeof(char*));
3f7701a4
NP
294 int i = 0;
295 args[i++] = "name-rev";
296 args[i++] = "--name-only";
a2cf9f44 297 args[i++] = "--no-undefined";
30ffa603 298 if (!all) {
3f7701a4 299 args[i++] = "--tags";
30ffa603
PH
300 if (pattern) {
301 char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
302 sprintf(s, "--refs=refs/tags/%s", pattern);
303 args[i++] = s;
304 }
305 }
3f7701a4
NP
306 memcpy(args + i, argv, argc * sizeof(char*));
307 args[i + argc] = NULL;
308 return cmd_name_rev(i + argc, args, prefix);
23615708
SP
309 }
310
166185be 311 if (argc == 0) {
fec9ebf1 312 describe("HEAD", 1);
166185be
PH
313 } else {
314 while (argc-- > 0) {
315 describe(*argv++, argc == 0);
fec9ebf1 316 }
166185be 317 }
908e5310
LT
318 return 0;
319}