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