hashmap: factor out getting a hash code from a SHA1
[git/raj.git] / builtin / describe.c
blob57e84c8ae68c8172b3cbc4136f40c459743427da
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "refs.h"
5 #include "builtin.h"
6 #include "exec_cmd.h"
7 #include "parse-options.h"
8 #include "diff.h"
9 #include "hashmap.h"
10 #include "argv-array.h"
12 #define SEEN (1u << 0)
13 #define MAX_TAGS (FLAG_BITS - 1)
15 static const char * const describe_usage[] = {
16 N_("git describe [options] <commit-ish>*"),
17 N_("git describe [options] --dirty"),
18 NULL
21 static int debug; /* Display lots of verbose info */
22 static int all; /* Any valid ref can be used */
23 static int tags; /* Allow lightweight tags */
24 static int longformat;
25 static int first_parent;
26 static int abbrev = -1; /* unspecified */
27 static int max_candidates = 10;
28 static struct hashmap names;
29 static int have_util;
30 static const char *pattern;
31 static int always;
32 static const char *dirty;
34 /* diff-index command arguments to check if working tree is dirty. */
35 static const char *diff_index_args[] = {
36 "diff-index", "--quiet", "HEAD", "--", NULL
39 struct commit_name {
40 struct hashmap_entry entry;
41 unsigned char peeled[20];
42 struct tag *tag;
43 unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
44 unsigned name_checked:1;
45 unsigned char sha1[20];
46 char *path;
49 static const char *prio_names[] = {
50 "head", "lightweight", "annotated",
53 static int commit_name_cmp(const struct commit_name *cn1,
54 const struct commit_name *cn2, const void *peeled)
56 return hashcmp(cn1->peeled, peeled ? peeled : cn2->peeled);
59 static inline struct commit_name *find_commit_name(const unsigned char *peeled)
61 struct commit_name key;
62 hashmap_entry_init(&key, sha1hash(peeled));
63 return hashmap_get(&names, &key, peeled);
66 static int replace_name(struct commit_name *e,
67 int prio,
68 const unsigned char *sha1,
69 struct tag **tag)
71 if (!e || e->prio < prio)
72 return 1;
74 if (e->prio == 2 && prio == 2) {
75 /* Multiple annotated tags point to the same commit.
76 * Select one to keep based upon their tagger date.
78 struct tag *t;
80 if (!e->tag) {
81 t = lookup_tag(e->sha1);
82 if (!t || parse_tag(t))
83 return 1;
84 e->tag = t;
87 t = lookup_tag(sha1);
88 if (!t || parse_tag(t))
89 return 0;
90 *tag = t;
92 if (e->tag->date < t->date)
93 return 1;
96 return 0;
99 static void add_to_known_names(const char *path,
100 const unsigned char *peeled,
101 int prio,
102 const unsigned char *sha1)
104 struct commit_name *e = find_commit_name(peeled);
105 struct tag *tag = NULL;
106 if (replace_name(e, prio, sha1, &tag)) {
107 if (!e) {
108 e = xmalloc(sizeof(struct commit_name));
109 hashcpy(e->peeled, peeled);
110 hashmap_entry_init(e, sha1hash(peeled));
111 hashmap_add(&names, e);
112 e->path = NULL;
114 e->tag = tag;
115 e->prio = prio;
116 e->name_checked = 0;
117 hashcpy(e->sha1, sha1);
118 free(e->path);
119 e->path = xstrdup(path);
123 static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
125 int is_tag = starts_with(path, "refs/tags/");
126 unsigned char peeled[20];
127 int is_annotated, prio;
129 /* Reject anything outside refs/tags/ unless --all */
130 if (!all && !is_tag)
131 return 0;
133 /* Accept only tags that match the pattern, if given */
134 if (pattern && (!is_tag || wildmatch(pattern, path + 10, 0, NULL)))
135 return 0;
137 /* Is it annotated? */
138 if (!peel_ref(path, peeled)) {
139 is_annotated = !!hashcmp(sha1, peeled);
140 } else {
141 hashcpy(peeled, sha1);
142 is_annotated = 0;
146 * By default, we only use annotated tags, but with --tags
147 * we fall back to lightweight ones (even without --tags,
148 * we still remember lightweight ones, only to give hints
149 * in an error message). --all allows any refs to be used.
151 if (is_annotated)
152 prio = 2;
153 else if (is_tag)
154 prio = 1;
155 else
156 prio = 0;
158 add_to_known_names(all ? path + 5 : path + 10, peeled, prio, sha1);
159 return 0;
162 struct possible_tag {
163 struct commit_name *name;
164 int depth;
165 int found_order;
166 unsigned flag_within;
169 static int compare_pt(const void *a_, const void *b_)
171 struct possible_tag *a = (struct possible_tag *)a_;
172 struct possible_tag *b = (struct possible_tag *)b_;
173 if (a->depth != b->depth)
174 return a->depth - b->depth;
175 if (a->found_order != b->found_order)
176 return a->found_order - b->found_order;
177 return 0;
180 static unsigned long finish_depth_computation(
181 struct commit_list **list,
182 struct possible_tag *best)
184 unsigned long seen_commits = 0;
185 while (*list) {
186 struct commit *c = pop_commit(list);
187 struct commit_list *parents = c->parents;
188 seen_commits++;
189 if (c->object.flags & best->flag_within) {
190 struct commit_list *a = *list;
191 while (a) {
192 struct commit *i = a->item;
193 if (!(i->object.flags & best->flag_within))
194 break;
195 a = a->next;
197 if (!a)
198 break;
199 } else
200 best->depth++;
201 while (parents) {
202 struct commit *p = parents->item;
203 parse_commit(p);
204 if (!(p->object.flags & SEEN))
205 commit_list_insert_by_date(p, list);
206 p->object.flags |= c->object.flags;
207 parents = parents->next;
210 return seen_commits;
213 static void display_name(struct commit_name *n)
215 if (n->prio == 2 && !n->tag) {
216 n->tag = lookup_tag(n->sha1);
217 if (!n->tag || parse_tag(n->tag))
218 die(_("annotated tag %s not available"), n->path);
220 if (n->tag && !n->name_checked) {
221 if (!n->tag->tag)
222 die(_("annotated tag %s has no embedded name"), n->path);
223 if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
224 warning(_("tag '%s' is really '%s' here"), n->tag->tag, n->path);
225 n->name_checked = 1;
228 if (n->tag)
229 printf("%s", n->tag->tag);
230 else
231 printf("%s", n->path);
234 static void show_suffix(int depth, const unsigned char *sha1)
236 printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev));
239 static void describe(const char *arg, int last_one)
241 unsigned char sha1[20];
242 struct commit *cmit, *gave_up_on = NULL;
243 struct commit_list *list;
244 struct commit_name *n;
245 struct possible_tag all_matches[MAX_TAGS];
246 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
247 unsigned long seen_commits = 0;
248 unsigned int unannotated_cnt = 0;
250 if (get_sha1(arg, sha1))
251 die(_("Not a valid object name %s"), arg);
252 cmit = lookup_commit_reference(sha1);
253 if (!cmit)
254 die(_("%s is not a valid '%s' object"), arg, commit_type);
256 n = find_commit_name(cmit->object.sha1);
257 if (n && (tags || all || n->prio == 2)) {
259 * Exact match to an existing ref.
261 display_name(n);
262 if (longformat)
263 show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1);
264 if (dirty)
265 printf("%s", dirty);
266 printf("\n");
267 return;
270 if (!max_candidates)
271 die(_("no tag exactly matches '%s'"), sha1_to_hex(cmit->object.sha1));
272 if (debug)
273 fprintf(stderr, _("searching to describe %s\n"), arg);
275 if (!have_util) {
276 struct hashmap_iter iter;
277 struct commit *c;
278 struct commit_name *n = hashmap_iter_first(&names, &iter);
279 for (; n; n = hashmap_iter_next(&iter)) {
280 c = lookup_commit_reference_gently(n->peeled, 1);
281 if (c)
282 c->util = n;
284 have_util = 1;
287 list = NULL;
288 cmit->object.flags = SEEN;
289 commit_list_insert(cmit, &list);
290 while (list) {
291 struct commit *c = pop_commit(&list);
292 struct commit_list *parents = c->parents;
293 seen_commits++;
294 n = c->util;
295 if (n) {
296 if (!tags && !all && n->prio < 2) {
297 unannotated_cnt++;
298 } else if (match_cnt < max_candidates) {
299 struct possible_tag *t = &all_matches[match_cnt++];
300 t->name = n;
301 t->depth = seen_commits - 1;
302 t->flag_within = 1u << match_cnt;
303 t->found_order = match_cnt;
304 c->object.flags |= t->flag_within;
305 if (n->prio == 2)
306 annotated_cnt++;
308 else {
309 gave_up_on = c;
310 break;
313 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
314 struct possible_tag *t = &all_matches[cur_match];
315 if (!(c->object.flags & t->flag_within))
316 t->depth++;
318 if (annotated_cnt && !list) {
319 if (debug)
320 fprintf(stderr, _("finished search at %s\n"),
321 sha1_to_hex(c->object.sha1));
322 break;
324 while (parents) {
325 struct commit *p = parents->item;
326 parse_commit(p);
327 if (!(p->object.flags & SEEN))
328 commit_list_insert_by_date(p, &list);
329 p->object.flags |= c->object.flags;
330 parents = parents->next;
332 if (first_parent)
333 break;
337 if (!match_cnt) {
338 const unsigned char *sha1 = cmit->object.sha1;
339 if (always) {
340 printf("%s", find_unique_abbrev(sha1, abbrev));
341 if (dirty)
342 printf("%s", dirty);
343 printf("\n");
344 return;
346 if (unannotated_cnt)
347 die(_("No annotated tags can describe '%s'.\n"
348 "However, there were unannotated tags: try --tags."),
349 sha1_to_hex(sha1));
350 else
351 die(_("No tags can describe '%s'.\n"
352 "Try --always, or create some tags."),
353 sha1_to_hex(sha1));
356 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
358 if (gave_up_on) {
359 commit_list_insert_by_date(gave_up_on, &list);
360 seen_commits--;
362 seen_commits += finish_depth_computation(&list, &all_matches[0]);
363 free_commit_list(list);
365 if (debug) {
366 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
367 struct possible_tag *t = &all_matches[cur_match];
368 fprintf(stderr, " %-11s %8d %s\n",
369 prio_names[t->name->prio],
370 t->depth, t->name->path);
372 fprintf(stderr, _("traversed %lu commits\n"), seen_commits);
373 if (gave_up_on) {
374 fprintf(stderr,
375 _("more than %i tags found; listed %i most recent\n"
376 "gave up search at %s\n"),
377 max_candidates, max_candidates,
378 sha1_to_hex(gave_up_on->object.sha1));
382 display_name(all_matches[0].name);
383 if (abbrev)
384 show_suffix(all_matches[0].depth, cmit->object.sha1);
385 if (dirty)
386 printf("%s", dirty);
387 printf("\n");
389 if (!last_one)
390 clear_commit_marks(cmit, -1);
393 int cmd_describe(int argc, const char **argv, const char *prefix)
395 int contains = 0;
396 struct option options[] = {
397 OPT_BOOL(0, "contains", &contains, N_("find the tag that comes after the commit")),
398 OPT_BOOL(0, "debug", &debug, N_("debug search strategy on stderr")),
399 OPT_BOOL(0, "all", &all, N_("use any ref")),
400 OPT_BOOL(0, "tags", &tags, N_("use any tag, even unannotated")),
401 OPT_BOOL(0, "long", &longformat, N_("always use long format")),
402 OPT_BOOL(0, "first-parent", &first_parent, N_("only follow first parent")),
403 OPT__ABBREV(&abbrev),
404 OPT_SET_INT(0, "exact-match", &max_candidates,
405 N_("only output exact matches"), 0),
406 OPT_INTEGER(0, "candidates", &max_candidates,
407 N_("consider <n> most recent tags (default: 10)")),
408 OPT_STRING(0, "match", &pattern, N_("pattern"),
409 N_("only consider tags matching <pattern>")),
410 OPT_BOOL(0, "always", &always,
411 N_("show abbreviated commit object as fallback")),
412 {OPTION_STRING, 0, "dirty", &dirty, N_("mark"),
413 N_("append <mark> on dirty working tree (default: \"-dirty\")"),
414 PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
415 OPT_END(),
418 git_config(git_default_config, NULL);
419 argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
420 if (abbrev < 0)
421 abbrev = DEFAULT_ABBREV;
423 if (max_candidates < 0)
424 max_candidates = 0;
425 else if (max_candidates > MAX_TAGS)
426 max_candidates = MAX_TAGS;
428 save_commit_buffer = 0;
430 if (longformat && abbrev == 0)
431 die(_("--long is incompatible with --abbrev=0"));
433 if (contains) {
434 struct argv_array args;
436 argv_array_init(&args);
437 argv_array_pushl(&args, "name-rev",
438 "--peel-tag", "--name-only", "--no-undefined",
439 NULL);
440 if (always)
441 argv_array_push(&args, "--always");
442 if (!all) {
443 argv_array_push(&args, "--tags");
444 if (pattern)
445 argv_array_pushf(&args, "--refs=refs/tags/%s", pattern);
447 while (*argv) {
448 argv_array_push(&args, *argv);
449 argv++;
451 return cmd_name_rev(args.argc, args.argv, prefix);
454 hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, 0);
455 for_each_rawref(get_name, NULL);
456 if (!names.size && !always)
457 die(_("No names found, cannot describe anything."));
459 if (argc == 0) {
460 if (dirty) {
461 static struct lock_file index_lock;
462 int fd;
464 read_cache_preload(NULL);
465 refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED,
466 NULL, NULL, NULL);
467 fd = hold_locked_index(&index_lock, 0);
468 if (0 <= fd)
469 update_index_if_able(&the_index, &index_lock);
471 if (!cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1,
472 diff_index_args, prefix))
473 dirty = NULL;
475 describe("HEAD", 1);
476 } else if (dirty) {
477 die(_("--dirty is incompatible with commit-ishes"));
478 } else {
479 while (argc-- > 0)
480 describe(*argv++, argc == 0);
482 return 0;