GIT 1.6.5
[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 12static const char * const describe_usage[] = {
1b1dd23f 13 "git describe [options] <committish>*",
166185be
PH
14 NULL
15};
908e5310 16
8713ab30 17static int debug; /* Display lots of verbose info */
7e425c4f
SP
18static int all; /* Any valid ref can be used */
19static int tags; /* Allow lightweight tags */
518120e3 20static int longformat;
2d9e7c9f 21static int abbrev = DEFAULT_ABBREV;
8713ab30 22static int max_candidates = 10;
d68dc34c 23static int found_names;
cb2a1cc2 24static const char *pattern;
da2478db 25static int always;
908e5310 26
e7eb5034 27struct commit_name {
212945d4 28 struct tag *tag;
64deb858 29 int prio; /* annotated tag = 2, tag = 1, head = 0 */
212945d4 30 unsigned char sha1[20];
5a2282de 31 char path[FLEX_ARRAY]; /* more */
e7eb5034 32};
cf69fd49
SP
33static const char *prio_names[] = {
34 "head", "lightweight", "annotated",
35};
908e5310 36
64deb858 37static void add_to_known_names(const char *path,
80dbae03 38 struct commit *commit,
212945d4
SP
39 int prio,
40 const unsigned char *sha1)
908e5310 41{
e7eb5034
SP
42 struct commit_name *e = commit->util;
43 if (!e || e->prio < prio) {
44 size_t len = strlen(path)+1;
45 free(e);
46 e = xmalloc(sizeof(struct commit_name) + len);
212945d4 47 e->tag = NULL;
e7eb5034 48 e->prio = prio;
212945d4 49 hashcpy(e->sha1, sha1);
e7eb5034
SP
50 memcpy(e->path, path, len);
51 commit->util = e;
908e5310 52 }
d68dc34c 53 found_names = 1;
908e5310
LT
54}
55
8da19775 56static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
908e5310 57{
8a5a1884 58 int might_be_tag = !prefixcmp(path, "refs/tags/");
feededd0 59 struct commit *commit;
64deb858 60 struct object *object;
feededd0
SP
61 unsigned char peeled[20];
62 int is_tag, prio;
63
8a5a1884
SP
64 if (!all && !might_be_tag)
65 return 0;
66
feededd0
SP
67 if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) {
68 commit = lookup_commit_reference_gently(peeled, 1);
69 if (!commit)
70 return 0;
71 is_tag = !!hashcmp(sha1, commit->object.sha1);
72 } else {
73 commit = lookup_commit_reference_gently(sha1, 1);
74 object = parse_object(sha1);
75 if (!commit || !object)
76 return 0;
77 is_tag = object->type == OBJ_TAG;
78 }
64deb858 79
2d9e7c9f
JH
80 /* If --all, then any refs are used.
81 * If --tags, then any tags are used.
82 * Otherwise only annotated tags are used.
83 */
8a5a1884 84 if (might_be_tag) {
4ed19a3c 85 if (is_tag)
64deb858 86 prio = 2;
4ed19a3c 87 else
64deb858 88 prio = 1;
4ed19a3c
MD
89
90 if (pattern && fnmatch(pattern, path + 10, 0))
91 prio = 0;
64deb858
JH
92 }
93 else
94 prio = 0;
95
635d4134 96 if (!all) {
64deb858
JH
97 if (!prio)
98 return 0;
99 if (!tags && prio < 2)
635d4134 100 return 0;
635d4134 101 }
212945d4 102 add_to_known_names(all ? path + 5 : path + 10, commit, prio, sha1);
908e5310
LT
103 return 0;
104}
105
80dbae03 106struct possible_tag {
80dbae03 107 struct commit_name *name;
cf69fd49
SP
108 int depth;
109 int found_order;
8713ab30 110 unsigned flag_within;
80dbae03
SP
111};
112
cf69fd49
SP
113static int compare_pt(const void *a_, const void *b_)
114{
115 struct possible_tag *a = (struct possible_tag *)a_;
116 struct possible_tag *b = (struct possible_tag *)b_;
cf69fd49
SP
117 if (a->depth != b->depth)
118 return a->depth - b->depth;
119 if (a->found_order != b->found_order)
120 return a->found_order - b->found_order;
121 return 0;
122}
123
1b600e65
SP
124static unsigned long finish_depth_computation(
125 struct commit_list **list,
126 struct possible_tag *best)
127{
128 unsigned long seen_commits = 0;
129 while (*list) {
130 struct commit *c = pop_commit(list);
131 struct commit_list *parents = c->parents;
132 seen_commits++;
133 if (c->object.flags & best->flag_within) {
134 struct commit_list *a = *list;
135 while (a) {
136 struct commit *i = a->item;
137 if (!(i->object.flags & best->flag_within))
138 break;
139 a = a->next;
140 }
141 if (!a)
142 break;
143 } else
144 best->depth++;
145 while (parents) {
146 struct commit *p = parents->item;
147 parse_commit(p);
148 if (!(p->object.flags & SEEN))
149 insert_by_date(p, list);
150 p->object.flags |= c->object.flags;
151 parents = parents->next;
152 }
153 }
154 return seen_commits;
155}
156
212945d4
SP
157static void display_name(struct commit_name *n)
158{
159 if (n->prio == 2 && !n->tag) {
160 n->tag = lookup_tag(n->sha1);
3167d725 161 if (!n->tag || parse_tag(n->tag) || !n->tag->tag)
212945d4 162 die("annotated tag %s not available", n->path);
81dc223d 163 if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
212945d4
SP
164 warning("tag '%s' is really '%s' here", n->tag->tag, n->path);
165 }
166
167 if (n->tag)
168 printf("%s", n->tag->tag);
169 else
170 printf("%s", n->path);
870cf7d6
JH
171}
172
173static void show_suffix(int depth, const unsigned char *sha1)
174{
175 printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev));
212945d4
SP
176}
177
554fe20d 178static void describe(const char *arg, int last_one)
908e5310 179{
4c34a2c5 180 unsigned char sha1[20];
8713ab30 181 struct commit *cmit, *gave_up_on = NULL;
908e5310
LT
182 struct commit_list *list;
183 static int initialized = 0;
184 struct commit_name *n;
cf69fd49 185 struct possible_tag all_matches[MAX_TAGS];
8713ab30
SP
186 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
187 unsigned long seen_commits = 0;
908e5310 188
31fff305
DL
189 if (get_sha1(arg, sha1))
190 die("Not a valid object name %s", arg);
4c34a2c5
JH
191 cmit = lookup_commit_reference(sha1);
192 if (!cmit)
31fff305 193 die("%s is not a valid '%s' object", arg, commit_type);
4c34a2c5 194
908e5310
LT
195 if (!initialized) {
196 initialized = 1;
cb5d709f 197 for_each_ref(get_name, NULL);
908e5310
LT
198 }
199
d68dc34c
BS
200 if (!found_names)
201 die("cannot describe '%s'", sha1_to_hex(sha1));
202
e7eb5034 203 n = cmit->util;
908e5310 204 if (n) {
870cf7d6
JH
205 /*
206 * Exact match to an existing ref.
207 */
212945d4 208 display_name(n);
870cf7d6 209 if (longformat)
14d4642e 210 show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1);
212945d4 211 printf("\n");
908e5310
LT
212 return;
213 }
214
2c33f757
SP
215 if (!max_candidates)
216 die("no tag exactly matches '%s'", sha1_to_hex(cmit->object.sha1));
8713ab30
SP
217 if (debug)
218 fprintf(stderr, "searching to describe %s\n", arg);
219
908e5310 220 list = NULL;
8713ab30 221 cmit->object.flags = SEEN;
908e5310
LT
222 commit_list_insert(cmit, &list);
223 while (list) {
80dbae03 224 struct commit *c = pop_commit(&list);
dccd0c2a 225 struct commit_list *parents = c->parents;
8713ab30 226 seen_commits++;
e7eb5034 227 n = c->util;
908e5310 228 if (n) {
8713ab30
SP
229 if (match_cnt < max_candidates) {
230 struct possible_tag *t = &all_matches[match_cnt++];
231 t->name = n;
232 t->depth = seen_commits - 1;
233 t->flag_within = 1u << match_cnt;
8a8169c0 234 t->found_order = match_cnt;
8713ab30
SP
235 c->object.flags |= t->flag_within;
236 if (n->prio == 2)
237 annotated_cnt++;
238 }
239 else {
240 gave_up_on = c;
241 break;
242 }
243 }
244 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
245 struct possible_tag *t = &all_matches[cur_match];
246 if (!(c->object.flags & t->flag_within))
247 t->depth++;
248 }
249 if (annotated_cnt && !list) {
250 if (debug)
251 fprintf(stderr, "finished search at %s\n",
252 sha1_to_hex(c->object.sha1));
253 break;
dccd0c2a
SP
254 }
255 while (parents) {
256 struct commit *p = parents->item;
257 parse_commit(p);
8713ab30 258 if (!(p->object.flags & SEEN))
dccd0c2a 259 insert_by_date(p, &list);
8713ab30 260 p->object.flags |= c->object.flags;
dccd0c2a 261 parents = parents->next;
80dbae03
SP
262 }
263 }
264
da2478db
JH
265 if (!match_cnt) {
266 const unsigned char *sha1 = cmit->object.sha1;
267 if (always) {
268 printf("%s\n", find_unique_abbrev(sha1, abbrev));
269 return;
270 }
271 die("cannot describe '%s'", sha1_to_hex(sha1));
272 }
80dbae03 273
cf69fd49 274 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
1b600e65
SP
275
276 if (gave_up_on) {
277 insert_by_date(gave_up_on, &list);
278 seen_commits--;
279 }
280 seen_commits += finish_depth_computation(&list, &all_matches[0]);
281 free_commit_list(list);
282
8713ab30
SP
283 if (debug) {
284 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
285 struct possible_tag *t = &all_matches[cur_match];
cf69fd49
SP
286 fprintf(stderr, " %-11s %8d %s\n",
287 prio_names[t->name->prio],
8713ab30
SP
288 t->depth, t->name->path);
289 }
290 fprintf(stderr, "traversed %lu commits\n", seen_commits);
291 if (gave_up_on) {
292 fprintf(stderr,
293 "more than %i tags found; listed %i most recent\n"
294 "gave up search at %s\n",
295 max_candidates, max_candidates,
296 sha1_to_hex(gave_up_on->object.sha1));
297 }
80dbae03 298 }
212945d4
SP
299
300 display_name(all_matches[0].name);
301 if (abbrev)
870cf7d6 302 show_suffix(all_matches[0].depth, cmit->object.sha1);
212945d4 303 printf("\n");
80dbae03 304
8713ab30
SP
305 if (!last_one)
306 clear_commit_marks(cmit, -1);
908e5310
LT
307}
308
9a0eaf83 309int cmd_describe(int argc, const char **argv, const char *prefix)
908e5310 310{
23615708 311 int contains = 0;
166185be
PH
312 struct option options[] = {
313 OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"),
314 OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"),
315 OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),
316 OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),
518120e3 317 OPT_BOOLEAN(0, "long", &longformat, "always use long format"),
166185be 318 OPT__ABBREV(&abbrev),
2c33f757
SP
319 OPT_SET_INT(0, "exact-match", &max_candidates,
320 "only output exact matches", 0),
166185be 321 OPT_INTEGER(0, "candidates", &max_candidates,
30ffa603
PH
322 "consider <n> most recent tags (default: 10)"),
323 OPT_STRING(0, "match", &pattern, "pattern",
324 "only consider tags matching <pattern>"),
da2478db
JH
325 OPT_BOOLEAN(0, "always", &always,
326 "show abbreviated commit object as fallback"),
166185be
PH
327 OPT_END(),
328 };
908e5310 329
37782920 330 argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
2c33f757
SP
331 if (max_candidates < 0)
332 max_candidates = 0;
166185be
PH
333 else if (max_candidates > MAX_TAGS)
334 max_candidates = MAX_TAGS;
4c34a2c5 335
8c599c74 336 save_commit_buffer = 0;
8112894d 337
518120e3
SB
338 if (longformat && abbrev == 0)
339 die("--long is incompatible with --abbrev=0");
340
23615708 341 if (contains) {
4b25d091 342 const char **args = xmalloc((7 + argc) * sizeof(char *));
3f7701a4
NP
343 int i = 0;
344 args[i++] = "name-rev";
345 args[i++] = "--name-only";
a2cf9f44 346 args[i++] = "--no-undefined";
da2478db
JH
347 if (always)
348 args[i++] = "--always";
30ffa603 349 if (!all) {
3f7701a4 350 args[i++] = "--tags";
30ffa603
PH
351 if (pattern) {
352 char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
353 sprintf(s, "--refs=refs/tags/%s", pattern);
354 args[i++] = s;
355 }
356 }
4b25d091 357 memcpy(args + i, argv, argc * sizeof(char *));
3f7701a4
NP
358 args[i + argc] = NULL;
359 return cmd_name_rev(i + argc, args, prefix);
23615708
SP
360 }
361
166185be 362 if (argc == 0) {
fec9ebf1 363 describe("HEAD", 1);
166185be
PH
364 } else {
365 while (argc-- > 0) {
366 describe(*argv++, argc == 0);
fec9ebf1 367 }
166185be 368 }
908e5310
LT
369 return 0;
370}