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