lockfile.h: extract new header file for the functions in lockfile.c
[git/git.git] / builtin / describe.c
CommitLineData
908e5310 1#include "cache.h"
697cc8ef 2#include "lockfile.h"
908e5310 3#include "commit.h"
635d4134 4#include "tag.h"
908e5310 5#include "refs.h"
9a0eaf83 6#include "builtin.h"
23615708 7#include "exec_cmd.h"
166185be 8#include "parse-options.h"
9f67d2e8 9#include "diff.h"
29d8a834 10#include "hashmap.h"
45bc950b 11#include "argv-array.h"
908e5310 12
c4472643 13#define SEEN (1u << 0)
8713ab30
SP
14#define MAX_TAGS (FLAG_BITS - 1)
15
166185be 16static const char * const describe_usage[] = {
a8a5406a 17 N_("git describe [options] <commit-ish>*"),
bfb0737d 18 N_("git describe [options] --dirty"),
166185be
PH
19 NULL
20};
908e5310 21
8713ab30 22static int debug; /* Display lots of verbose info */
7e425c4f
SP
23static int all; /* Any valid ref can be used */
24static int tags; /* Allow lightweight tags */
518120e3 25static int longformat;
e00dd1e9 26static int first_parent;
dce96489 27static int abbrev = -1; /* unspecified */
8713ab30 28static int max_candidates = 10;
29d8a834 29static struct hashmap names;
d1645d02 30static int have_util;
cb2a1cc2 31static const char *pattern;
da2478db 32static int always;
9f67d2e8
JP
33static const char *dirty;
34
35/* diff-index command arguments to check if working tree is dirty. */
36static const char *diff_index_args[] = {
37 "diff-index", "--quiet", "HEAD", "--", NULL
38};
39
e7eb5034 40struct commit_name {
29d8a834 41 struct hashmap_entry entry;
3cfa4db3 42 unsigned char peeled[20];
212945d4 43 struct tag *tag;
03e8b541
SP
44 unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
45 unsigned name_checked:1;
212945d4 46 unsigned char sha1[20];
219a0f33 47 char *path;
e7eb5034 48};
c4472643 49
cf69fd49
SP
50static const char *prio_names[] = {
51 "head", "lightweight", "annotated",
52};
908e5310 53
29d8a834
KB
54static int commit_name_cmp(const struct commit_name *cn1,
55 const struct commit_name *cn2, const void *peeled)
56{
57 return hashcmp(cn1->peeled, peeled ? peeled : cn2->peeled);
58}
59
3cfa4db3
AK
60static inline struct commit_name *find_commit_name(const unsigned char *peeled)
61{
ab73a9d1 62 return hashmap_get_from_hash(&names, sha1hash(peeled), peeled);
d1645d02
AK
63}
64
03e8b541
SP
65static int replace_name(struct commit_name *e,
66 int prio,
67 const unsigned char *sha1,
68 struct tag **tag)
69{
70 if (!e || e->prio < prio)
71 return 1;
72
73 if (e->prio == 2 && prio == 2) {
74 /* Multiple annotated tags point to the same commit.
75 * Select one to keep based upon their tagger date.
76 */
77 struct tag *t;
78
79 if (!e->tag) {
80 t = lookup_tag(e->sha1);
81 if (!t || parse_tag(t))
82 return 1;
83 e->tag = t;
84 }
85
86 t = lookup_tag(sha1);
87 if (!t || parse_tag(t))
88 return 0;
89 *tag = t;
90
91 if (e->tag->date < t->date)
92 return 1;
93 }
94
95 return 0;
96}
97
64deb858 98static void add_to_known_names(const char *path,
d1645d02 99 const unsigned char *peeled,
212945d4
SP
100 int prio,
101 const unsigned char *sha1)
908e5310 102{
3cfa4db3 103 struct commit_name *e = find_commit_name(peeled);
03e8b541
SP
104 struct tag *tag = NULL;
105 if (replace_name(e, prio, sha1, &tag)) {
1e1ade18
AK
106 if (!e) {
107 e = xmalloc(sizeof(struct commit_name));
3cfa4db3 108 hashcpy(e->peeled, peeled);
039dc71a 109 hashmap_entry_init(e, sha1hash(peeled));
29d8a834 110 hashmap_add(&names, e);
219a0f33 111 e->path = NULL;
1e1ade18 112 }
03e8b541 113 e->tag = tag;
e7eb5034 114 e->prio = prio;
03e8b541 115 e->name_checked = 0;
212945d4 116 hashcpy(e->sha1, sha1);
219a0f33
MH
117 free(e->path);
118 e->path = xstrdup(path);
908e5310 119 }
908e5310
LT
120}
121
8da19775 122static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
908e5310 123{
59556548 124 int is_tag = starts_with(path, "refs/tags/");
feededd0 125 unsigned char peeled[20];
46e1d6eb 126 int is_annotated, prio;
feededd0 127
46e1d6eb
JH
128 /* Reject anything outside refs/tags/ unless --all */
129 if (!all && !is_tag)
8a5a1884
SP
130 return 0;
131
46e1d6eb 132 /* Accept only tags that match the pattern, if given */
eb07894f 133 if (pattern && (!is_tag || wildmatch(pattern, path + 10, 0, NULL)))
46e1d6eb
JH
134 return 0;
135
136 /* Is it annotated? */
e6dbffa6 137 if (!peel_ref(path, peeled)) {
46e1d6eb 138 is_annotated = !!hashcmp(sha1, peeled);
feededd0 139 } else {
d1645d02 140 hashcpy(peeled, sha1);
46e1d6eb 141 is_annotated = 0;
feededd0 142 }
64deb858 143
46e1d6eb
JH
144 /*
145 * By default, we only use annotated tags, but with --tags
146 * we fall back to lightweight ones (even without --tags,
147 * we still remember lightweight ones, only to give hints
148 * in an error message). --all allows any refs to be used.
2d9e7c9f 149 */
46e1d6eb
JH
150 if (is_annotated)
151 prio = 2;
152 else if (is_tag)
153 prio = 1;
64deb858
JH
154 else
155 prio = 0;
156
d1645d02 157 add_to_known_names(all ? path + 5 : path + 10, peeled, prio, sha1);
908e5310
LT
158 return 0;
159}
160
80dbae03 161struct possible_tag {
80dbae03 162 struct commit_name *name;
cf69fd49
SP
163 int depth;
164 int found_order;
8713ab30 165 unsigned flag_within;
80dbae03
SP
166};
167
cf69fd49
SP
168static int compare_pt(const void *a_, const void *b_)
169{
170 struct possible_tag *a = (struct possible_tag *)a_;
171 struct possible_tag *b = (struct possible_tag *)b_;
cf69fd49
SP
172 if (a->depth != b->depth)
173 return a->depth - b->depth;
174 if (a->found_order != b->found_order)
175 return a->found_order - b->found_order;
176 return 0;
177}
178
1b600e65
SP
179static unsigned long finish_depth_computation(
180 struct commit_list **list,
181 struct possible_tag *best)
182{
183 unsigned long seen_commits = 0;
184 while (*list) {
185 struct commit *c = pop_commit(list);
186 struct commit_list *parents = c->parents;
187 seen_commits++;
188 if (c->object.flags & best->flag_within) {
189 struct commit_list *a = *list;
190 while (a) {
191 struct commit *i = a->item;
192 if (!(i->object.flags & best->flag_within))
193 break;
194 a = a->next;
195 }
196 if (!a)
197 break;
198 } else
199 best->depth++;
200 while (parents) {
201 struct commit *p = parents->item;
202 parse_commit(p);
203 if (!(p->object.flags & SEEN))
47e44ed1 204 commit_list_insert_by_date(p, list);
1b600e65
SP
205 p->object.flags |= c->object.flags;
206 parents = parents->next;
207 }
208 }
209 return seen_commits;
210}
211
212945d4
SP
212static void display_name(struct commit_name *n)
213{
214 if (n->prio == 2 && !n->tag) {
215 n->tag = lookup_tag(n->sha1);
03e8b541 216 if (!n->tag || parse_tag(n->tag))
e41f1cb3 217 die(_("annotated tag %s not available"), n->path);
03e8b541
SP
218 }
219 if (n->tag && !n->name_checked) {
220 if (!n->tag->tag)
e41f1cb3 221 die(_("annotated tag %s has no embedded name"), n->path);
81dc223d 222 if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
e41f1cb3 223 warning(_("tag '%s' is really '%s' here"), n->tag->tag, n->path);
03e8b541 224 n->name_checked = 1;
212945d4
SP
225 }
226
227 if (n->tag)
228 printf("%s", n->tag->tag);
229 else
230 printf("%s", n->path);
870cf7d6
JH
231}
232
233static void show_suffix(int depth, const unsigned char *sha1)
234{
235 printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev));
212945d4
SP
236}
237
554fe20d 238static void describe(const char *arg, int last_one)
908e5310 239{
4c34a2c5 240 unsigned char sha1[20];
8713ab30 241 struct commit *cmit, *gave_up_on = NULL;
908e5310 242 struct commit_list *list;
908e5310 243 struct commit_name *n;
cf69fd49 244 struct possible_tag all_matches[MAX_TAGS];
8713ab30
SP
245 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
246 unsigned long seen_commits = 0;
4d23660e 247 unsigned int unannotated_cnt = 0;
908e5310 248
31fff305 249 if (get_sha1(arg, sha1))
e41f1cb3 250 die(_("Not a valid object name %s"), arg);
4c34a2c5
JH
251 cmit = lookup_commit_reference(sha1);
252 if (!cmit)
e41f1cb3 253 die(_("%s is not a valid '%s' object"), arg, commit_type);
4c34a2c5 254
3cfa4db3 255 n = find_commit_name(cmit->object.sha1);
7a0d61bb 256 if (n && (tags || all || n->prio == 2)) {
870cf7d6
JH
257 /*
258 * Exact match to an existing ref.
259 */
212945d4 260 display_name(n);
870cf7d6 261 if (longformat)
14d4642e 262 show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1);
9f67d2e8
JP
263 if (dirty)
264 printf("%s", dirty);
212945d4 265 printf("\n");
908e5310
LT
266 return;
267 }
268
2c33f757 269 if (!max_candidates)
e41f1cb3 270 die(_("no tag exactly matches '%s'"), sha1_to_hex(cmit->object.sha1));
8713ab30 271 if (debug)
e41f1cb3 272 fprintf(stderr, _("searching to describe %s\n"), arg);
8713ab30 273
d1645d02 274 if (!have_util) {
29d8a834
KB
275 struct hashmap_iter iter;
276 struct commit *c;
277 struct commit_name *n = hashmap_iter_first(&names, &iter);
278 for (; n; n = hashmap_iter_next(&iter)) {
279 c = lookup_commit_reference_gently(n->peeled, 1);
280 if (c)
281 c->util = n;
282 }
d1645d02
AK
283 have_util = 1;
284 }
285
908e5310 286 list = NULL;
8713ab30 287 cmit->object.flags = SEEN;
908e5310
LT
288 commit_list_insert(cmit, &list);
289 while (list) {
80dbae03 290 struct commit *c = pop_commit(&list);
dccd0c2a 291 struct commit_list *parents = c->parents;
8713ab30 292 seen_commits++;
e7eb5034 293 n = c->util;
908e5310 294 if (n) {
4d23660e
TR
295 if (!tags && !all && n->prio < 2) {
296 unannotated_cnt++;
297 } else if (match_cnt < max_candidates) {
8713ab30
SP
298 struct possible_tag *t = &all_matches[match_cnt++];
299 t->name = n;
300 t->depth = seen_commits - 1;
301 t->flag_within = 1u << match_cnt;
8a8169c0 302 t->found_order = match_cnt;
8713ab30
SP
303 c->object.flags |= t->flag_within;
304 if (n->prio == 2)
305 annotated_cnt++;
306 }
307 else {
308 gave_up_on = c;
309 break;
310 }
311 }
312 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
313 struct possible_tag *t = &all_matches[cur_match];
314 if (!(c->object.flags & t->flag_within))
315 t->depth++;
316 }
317 if (annotated_cnt && !list) {
318 if (debug)
e41f1cb3 319 fprintf(stderr, _("finished search at %s\n"),
8713ab30
SP
320 sha1_to_hex(c->object.sha1));
321 break;
dccd0c2a
SP
322 }
323 while (parents) {
324 struct commit *p = parents->item;
325 parse_commit(p);
8713ab30 326 if (!(p->object.flags & SEEN))
47e44ed1 327 commit_list_insert_by_date(p, &list);
8713ab30 328 p->object.flags |= c->object.flags;
dccd0c2a 329 parents = parents->next;
e00dd1e9
MC
330
331 if (first_parent)
332 break;
80dbae03
SP
333 }
334 }
335
da2478db
JH
336 if (!match_cnt) {
337 const unsigned char *sha1 = cmit->object.sha1;
338 if (always) {
9f67d2e8
JP
339 printf("%s", find_unique_abbrev(sha1, abbrev));
340 if (dirty)
341 printf("%s", dirty);
342 printf("\n");
da2478db
JH
343 return;
344 }
4d23660e 345 if (unannotated_cnt)
e41f1cb3
ÆAB
346 die(_("No annotated tags can describe '%s'.\n"
347 "However, there were unannotated tags: try --tags."),
4d23660e
TR
348 sha1_to_hex(sha1));
349 else
e41f1cb3
ÆAB
350 die(_("No tags can describe '%s'.\n"
351 "Try --always, or create some tags."),
4d23660e 352 sha1_to_hex(sha1));
da2478db 353 }
80dbae03 354
cf69fd49 355 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
1b600e65
SP
356
357 if (gave_up_on) {
47e44ed1 358 commit_list_insert_by_date(gave_up_on, &list);
1b600e65
SP
359 seen_commits--;
360 }
361 seen_commits += finish_depth_computation(&list, &all_matches[0]);
362 free_commit_list(list);
363
8713ab30
SP
364 if (debug) {
365 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
366 struct possible_tag *t = &all_matches[cur_match];
cf69fd49
SP
367 fprintf(stderr, " %-11s %8d %s\n",
368 prio_names[t->name->prio],
8713ab30
SP
369 t->depth, t->name->path);
370 }
e41f1cb3 371 fprintf(stderr, _("traversed %lu commits\n"), seen_commits);
8713ab30
SP
372 if (gave_up_on) {
373 fprintf(stderr,
e41f1cb3
ÆAB
374 _("more than %i tags found; listed %i most recent\n"
375 "gave up search at %s\n"),
8713ab30
SP
376 max_candidates, max_candidates,
377 sha1_to_hex(gave_up_on->object.sha1));
378 }
80dbae03 379 }
212945d4
SP
380
381 display_name(all_matches[0].name);
382 if (abbrev)
870cf7d6 383 show_suffix(all_matches[0].depth, cmit->object.sha1);
9f67d2e8
JP
384 if (dirty)
385 printf("%s", dirty);
212945d4 386 printf("\n");
80dbae03 387
8713ab30
SP
388 if (!last_one)
389 clear_commit_marks(cmit, -1);
908e5310
LT
390}
391
9a0eaf83 392int cmd_describe(int argc, const char **argv, const char *prefix)
908e5310 393{
23615708 394 int contains = 0;
166185be 395 struct option options[] = {
d5d09d47
SB
396 OPT_BOOL(0, "contains", &contains, N_("find the tag that comes after the commit")),
397 OPT_BOOL(0, "debug", &debug, N_("debug search strategy on stderr")),
398 OPT_BOOL(0, "all", &all, N_("use any ref")),
399 OPT_BOOL(0, "tags", &tags, N_("use any tag, even unannotated")),
400 OPT_BOOL(0, "long", &longformat, N_("always use long format")),
401 OPT_BOOL(0, "first-parent", &first_parent, N_("only follow first parent")),
166185be 402 OPT__ABBREV(&abbrev),
2c33f757 403 OPT_SET_INT(0, "exact-match", &max_candidates,
bfb0737d 404 N_("only output exact matches"), 0),
166185be 405 OPT_INTEGER(0, "candidates", &max_candidates,
bfb0737d
NTND
406 N_("consider <n> most recent tags (default: 10)")),
407 OPT_STRING(0, "match", &pattern, N_("pattern"),
408 N_("only consider tags matching <pattern>")),
d5d09d47
SB
409 OPT_BOOL(0, "always", &always,
410 N_("show abbreviated commit object as fallback")),
bfb0737d 411 {OPTION_STRING, 0, "dirty", &dirty, N_("mark"),
d5d09d47
SB
412 N_("append <mark> on dirty working tree (default: \"-dirty\")"),
413 PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
166185be
PH
414 OPT_END(),
415 };
908e5310 416
dce96489 417 git_config(git_default_config, NULL);
37782920 418 argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
dce96489
LT
419 if (abbrev < 0)
420 abbrev = DEFAULT_ABBREV;
421
2c33f757
SP
422 if (max_candidates < 0)
423 max_candidates = 0;
166185be
PH
424 else if (max_candidates > MAX_TAGS)
425 max_candidates = MAX_TAGS;
4c34a2c5 426
8c599c74 427 save_commit_buffer = 0;
8112894d 428
518120e3 429 if (longformat && abbrev == 0)
e41f1cb3 430 die(_("--long is incompatible with --abbrev=0"));
518120e3 431
23615708 432 if (contains) {
45bc950b
JH
433 struct argv_array args;
434
435 argv_array_init(&args);
adfc1857
JH
436 argv_array_pushl(&args, "name-rev",
437 "--peel-tag", "--name-only", "--no-undefined",
45bc950b 438 NULL);
da2478db 439 if (always)
45bc950b 440 argv_array_push(&args, "--always");
30ffa603 441 if (!all) {
45bc950b
JH
442 argv_array_push(&args, "--tags");
443 if (pattern)
444 argv_array_pushf(&args, "--refs=refs/tags/%s", pattern);
445 }
446 while (*argv) {
447 argv_array_push(&args, *argv);
448 argv++;
30ffa603 449 }
45bc950b 450 return cmd_name_rev(args.argc, args.argv, prefix);
23615708
SP
451 }
452
29d8a834 453 hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, 0);
56a5f3af 454 for_each_rawref(get_name, NULL);
29d8a834 455 if (!names.size && !always)
e41f1cb3 456 die(_("No names found, cannot describe anything."));
fb423da0 457
166185be 458 if (argc == 0) {
bb571486
AC
459 if (dirty) {
460 static struct lock_file index_lock;
461 int fd;
462
463 read_cache_preload(NULL);
464 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED,
465 NULL, NULL, NULL);
466 fd = hold_locked_index(&index_lock, 0);
467 if (0 <= fd)
468 update_index_if_able(&the_index, &index_lock);
469
470 if (!cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1,
471 diff_index_args, prefix))
472 dirty = NULL;
473 }
fec9ebf1 474 describe("HEAD", 1);
9f67d2e8 475 } else if (dirty) {
a8a5406a 476 die(_("--dirty is incompatible with commit-ishes"));
166185be 477 } else {
c4472643 478 while (argc-- > 0)
166185be 479 describe(*argv++, argc == 0);
166185be 480 }
908e5310
LT
481 return 0;
482}