describe: Store commit_names in a hash table by commit SHA1
[git/jnareb-git.git] / builtin / describe.c
blob8149233059d84155cb3d2385e1bc19042f539b6c
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 "hash.h"
11 #define SEEN (1u<<0)
12 #define MAX_TAGS (FLAG_BITS - 1)
14 static const char * const describe_usage[] = {
15 "git describe [options] <committish>*",
16 "git describe [options] --dirty",
17 NULL
20 static int debug; /* Display lots of verbose info */
21 static int all; /* Any valid ref can be used */
22 static int tags; /* Allow lightweight tags */
23 static int longformat;
24 static int abbrev = DEFAULT_ABBREV;
25 static int max_candidates = 10;
26 static struct hash_table names;
27 static const char *pattern;
28 static int always;
29 static const char *dirty;
31 /* diff-index command arguments to check if working tree is dirty. */
32 static const char *diff_index_args[] = {
33 "diff-index", "--quiet", "HEAD", "--", NULL
37 struct commit_name {
38 struct commit_name *next;
39 unsigned char peeled[20];
40 struct tag *tag;
41 unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
42 unsigned name_checked:1;
43 unsigned char sha1[20];
44 const char *path;
46 static const char *prio_names[] = {
47 "head", "lightweight", "annotated",
50 static inline unsigned int hash_sha1(const unsigned char *sha1)
52 unsigned int hash;
53 memcpy(&hash, sha1, sizeof(hash));
54 return hash;
57 static inline struct commit_name *find_commit_name(const unsigned char *peeled)
59 struct commit_name *n = lookup_hash(hash_sha1(peeled), &names);
60 while (n && !!hashcmp(peeled, n->peeled))
61 n = n->next;
62 return n;
65 static int replace_name(struct commit_name *e,
66 int prio,
67 const unsigned char *sha1,
68 struct tag **tag)
70 if (!e || e->prio < prio)
71 return 1;
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.
77 struct tag *t;
79 if (!e->tag) {
80 t = lookup_tag(e->sha1);
81 if (!t || parse_tag(t))
82 return 1;
83 e->tag = t;
86 t = lookup_tag(sha1);
87 if (!t || parse_tag(t))
88 return 0;
89 *tag = t;
91 if (e->tag->date < t->date)
92 return 1;
95 return 0;
98 static void add_to_known_names(const char *path,
99 struct commit *commit,
100 int prio,
101 const unsigned char *sha1)
103 const unsigned char *peeled = commit->object.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 void **pos;
109 e = xmalloc(sizeof(struct commit_name));
110 commit->util = e;
111 hashcpy(e->peeled, peeled);
112 pos = insert_hash(hash_sha1(peeled), e, &names);
113 if (pos) {
114 e->next = *pos;
115 *pos = e;
116 } else {
117 e->next = NULL;
120 e->tag = tag;
121 e->prio = prio;
122 e->name_checked = 0;
123 hashcpy(e->sha1, sha1);
124 e->path = path;
128 static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
130 int might_be_tag = !prefixcmp(path, "refs/tags/");
131 struct commit *commit;
132 struct object *object;
133 unsigned char peeled[20];
134 int is_tag, prio;
136 if (!all && !might_be_tag)
137 return 0;
139 if (!peel_ref(path, peeled) && !is_null_sha1(peeled)) {
140 commit = lookup_commit_reference_gently(peeled, 1);
141 if (!commit)
142 return 0;
143 is_tag = !!hashcmp(sha1, commit->object.sha1);
144 } else {
145 commit = lookup_commit_reference_gently(sha1, 1);
146 object = parse_object(sha1);
147 if (!commit || !object)
148 return 0;
149 is_tag = object->type == OBJ_TAG;
152 /* If --all, then any refs are used.
153 * If --tags, then any tags are used.
154 * Otherwise only annotated tags are used.
156 if (might_be_tag) {
157 if (is_tag)
158 prio = 2;
159 else
160 prio = 1;
162 if (pattern && fnmatch(pattern, path + 10, 0))
163 prio = 0;
165 else
166 prio = 0;
168 if (!all) {
169 if (!prio)
170 return 0;
172 add_to_known_names(all ? path + 5 : path + 10, commit, prio, sha1);
173 return 0;
176 struct possible_tag {
177 struct commit_name *name;
178 int depth;
179 int found_order;
180 unsigned flag_within;
183 static int compare_pt(const void *a_, const void *b_)
185 struct possible_tag *a = (struct possible_tag *)a_;
186 struct possible_tag *b = (struct possible_tag *)b_;
187 if (a->depth != b->depth)
188 return a->depth - b->depth;
189 if (a->found_order != b->found_order)
190 return a->found_order - b->found_order;
191 return 0;
194 static unsigned long finish_depth_computation(
195 struct commit_list **list,
196 struct possible_tag *best)
198 unsigned long seen_commits = 0;
199 while (*list) {
200 struct commit *c = pop_commit(list);
201 struct commit_list *parents = c->parents;
202 seen_commits++;
203 if (c->object.flags & best->flag_within) {
204 struct commit_list *a = *list;
205 while (a) {
206 struct commit *i = a->item;
207 if (!(i->object.flags & best->flag_within))
208 break;
209 a = a->next;
211 if (!a)
212 break;
213 } else
214 best->depth++;
215 while (parents) {
216 struct commit *p = parents->item;
217 parse_commit(p);
218 if (!(p->object.flags & SEEN))
219 insert_by_date(p, list);
220 p->object.flags |= c->object.flags;
221 parents = parents->next;
224 return seen_commits;
227 static void display_name(struct commit_name *n)
229 if (n->prio == 2 && !n->tag) {
230 n->tag = lookup_tag(n->sha1);
231 if (!n->tag || parse_tag(n->tag))
232 die("annotated tag %s not available", n->path);
234 if (n->tag && !n->name_checked) {
235 if (!n->tag->tag)
236 die("annotated tag %s has no embedded name", n->path);
237 if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
238 warning("tag '%s' is really '%s' here", n->tag->tag, n->path);
239 n->name_checked = 1;
242 if (n->tag)
243 printf("%s", n->tag->tag);
244 else
245 printf("%s", n->path);
248 static void show_suffix(int depth, const unsigned char *sha1)
250 printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev));
253 static void describe(const char *arg, int last_one)
255 unsigned char sha1[20];
256 struct commit *cmit, *gave_up_on = NULL;
257 struct commit_list *list;
258 struct commit_name *n;
259 struct possible_tag all_matches[MAX_TAGS];
260 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
261 unsigned long seen_commits = 0;
262 unsigned int unannotated_cnt = 0;
264 if (get_sha1(arg, sha1))
265 die("Not a valid object name %s", arg);
266 cmit = lookup_commit_reference(sha1);
267 if (!cmit)
268 die("%s is not a valid '%s' object", arg, commit_type);
270 n = find_commit_name(cmit->object.sha1);
271 if (n && (tags || all || n->prio == 2)) {
273 * Exact match to an existing ref.
275 display_name(n);
276 if (longformat)
277 show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1);
278 if (dirty)
279 printf("%s", dirty);
280 printf("\n");
281 return;
284 if (!max_candidates)
285 die("no tag exactly matches '%s'", sha1_to_hex(cmit->object.sha1));
286 if (debug)
287 fprintf(stderr, "searching to describe %s\n", arg);
289 list = NULL;
290 cmit->object.flags = SEEN;
291 commit_list_insert(cmit, &list);
292 while (list) {
293 struct commit *c = pop_commit(&list);
294 struct commit_list *parents = c->parents;
295 seen_commits++;
296 n = c->util;
297 if (n) {
298 if (!tags && !all && n->prio < 2) {
299 unannotated_cnt++;
300 } else if (match_cnt < max_candidates) {
301 struct possible_tag *t = &all_matches[match_cnt++];
302 t->name = n;
303 t->depth = seen_commits - 1;
304 t->flag_within = 1u << match_cnt;
305 t->found_order = match_cnt;
306 c->object.flags |= t->flag_within;
307 if (n->prio == 2)
308 annotated_cnt++;
310 else {
311 gave_up_on = c;
312 break;
315 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
316 struct possible_tag *t = &all_matches[cur_match];
317 if (!(c->object.flags & t->flag_within))
318 t->depth++;
320 if (annotated_cnt && !list) {
321 if (debug)
322 fprintf(stderr, "finished search at %s\n",
323 sha1_to_hex(c->object.sha1));
324 break;
326 while (parents) {
327 struct commit *p = parents->item;
328 parse_commit(p);
329 if (!(p->object.flags & SEEN))
330 insert_by_date(p, &list);
331 p->object.flags |= c->object.flags;
332 parents = parents->next;
336 if (!match_cnt) {
337 const unsigned char *sha1 = cmit->object.sha1;
338 if (always) {
339 printf("%s", find_unique_abbrev(sha1, abbrev));
340 if (dirty)
341 printf("%s", dirty);
342 printf("\n");
343 return;
345 if (unannotated_cnt)
346 die("No annotated tags can describe '%s'.\n"
347 "However, there were unannotated tags: try --tags.",
348 sha1_to_hex(sha1));
349 else
350 die("No tags can describe '%s'.\n"
351 "Try --always, or create some tags.",
352 sha1_to_hex(sha1));
355 qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
357 if (gave_up_on) {
358 insert_by_date(gave_up_on, &list);
359 seen_commits--;
361 seen_commits += finish_depth_computation(&list, &all_matches[0]);
362 free_commit_list(list);
364 if (debug) {
365 for (cur_match = 0; cur_match < match_cnt; cur_match++) {
366 struct possible_tag *t = &all_matches[cur_match];
367 fprintf(stderr, " %-11s %8d %s\n",
368 prio_names[t->name->prio],
369 t->depth, t->name->path);
371 fprintf(stderr, "traversed %lu commits\n", seen_commits);
372 if (gave_up_on) {
373 fprintf(stderr,
374 "more than %i tags found; listed %i most recent\n"
375 "gave up search at %s\n",
376 max_candidates, max_candidates,
377 sha1_to_hex(gave_up_on->object.sha1));
381 display_name(all_matches[0].name);
382 if (abbrev)
383 show_suffix(all_matches[0].depth, cmit->object.sha1);
384 if (dirty)
385 printf("%s", dirty);
386 printf("\n");
388 if (!last_one)
389 clear_commit_marks(cmit, -1);
392 int cmd_describe(int argc, const char **argv, const char *prefix)
394 int contains = 0;
395 struct option options[] = {
396 OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"),
397 OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"),
398 OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),
399 OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),
400 OPT_BOOLEAN(0, "long", &longformat, "always use long format"),
401 OPT__ABBREV(&abbrev),
402 OPT_SET_INT(0, "exact-match", &max_candidates,
403 "only output exact matches", 0),
404 OPT_INTEGER(0, "candidates", &max_candidates,
405 "consider <n> most recent tags (default: 10)"),
406 OPT_STRING(0, "match", &pattern, "pattern",
407 "only consider tags matching <pattern>"),
408 OPT_BOOLEAN(0, "always", &always,
409 "show abbreviated commit object as fallback"),
410 {OPTION_STRING, 0, "dirty", &dirty, "mark",
411 "append <mark> on dirty working tree (default: \"-dirty\")",
412 PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
413 OPT_END(),
416 argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
417 if (max_candidates < 0)
418 max_candidates = 0;
419 else if (max_candidates > MAX_TAGS)
420 max_candidates = MAX_TAGS;
422 save_commit_buffer = 0;
424 if (longformat && abbrev == 0)
425 die("--long is incompatible with --abbrev=0");
427 if (contains) {
428 const char **args = xmalloc((7 + argc) * sizeof(char *));
429 int i = 0;
430 args[i++] = "name-rev";
431 args[i++] = "--name-only";
432 args[i++] = "--no-undefined";
433 if (always)
434 args[i++] = "--always";
435 if (!all) {
436 args[i++] = "--tags";
437 if (pattern) {
438 char *s = xmalloc(strlen("--refs=refs/tags/") + strlen(pattern) + 1);
439 sprintf(s, "--refs=refs/tags/%s", pattern);
440 args[i++] = s;
443 memcpy(args + i, argv, argc * sizeof(char *));
444 args[i + argc] = NULL;
445 return cmd_name_rev(i + argc, args, prefix);
448 init_hash(&names);
449 for_each_rawref(get_name, NULL);
450 if (!names.nr && !always)
451 die("No names found, cannot describe anything.");
453 if (argc == 0) {
454 if (dirty && !cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1, diff_index_args, prefix))
455 dirty = NULL;
456 describe("HEAD", 1);
457 } else if (dirty) {
458 die("--dirty is incompatible with committishes");
459 } else {
460 while (argc-- > 0) {
461 describe(*argv++, argc == 0);
464 return 0;