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