]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/describe.c
describe: Store commit_names in a hash table by commit SHA1
[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"
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[] = {
1b1dd23f 15 "git describe [options] <committish>*",
9f67d2e8 16 "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;
2d9e7c9f 24static int abbrev = DEFAULT_ABBREV;
8713ab30 25static int max_candidates = 10;
3cfa4db3 26static struct hash_table names;
cb2a1cc2 27static const char *pattern;
da2478db 28static int always;
9f67d2e8
JP
29static const char *dirty;
30
31/* diff-index command arguments to check if working tree is dirty. */
32static const char *diff_index_args[] = {
33 "diff-index", "--quiet", "HEAD", "--", NULL
34};
35
908e5310 36
e7eb5034 37struct commit_name {
3cfa4db3
AK
38 struct commit_name *next;
39 unsigned char peeled[20];
212945d4 40 struct tag *tag;
03e8b541
SP
41 unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
42 unsigned name_checked:1;
212945d4 43 unsigned char sha1[20];
1e1ade18 44 const char *path;
e7eb5034 45};
cf69fd49
SP
46static const char *prio_names[] = {
47 "head", "lightweight", "annotated",
48};
908e5310 49
3cfa4db3
AK
50static inline unsigned int hash_sha1(const unsigned char *sha1)
51{
52 unsigned int hash;
53 memcpy(&hash, sha1, sizeof(hash));
54 return hash;
55}
56
57static inline struct commit_name *find_commit_name(const unsigned char *peeled)
58{
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;
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,
80dbae03 99 struct commit *commit,
212945d4
SP
100 int prio,
101 const unsigned char *sha1)
908e5310 102{
3cfa4db3
AK
103 const unsigned char *peeled = commit->object.sha1;
104 struct commit_name *e = find_commit_name(peeled);
03e8b541
SP
105 struct tag *tag = NULL;
106 if (replace_name(e, prio, sha1, &tag)) {
1e1ade18 107 if (!e) {
3cfa4db3 108 void **pos;
1e1ade18
AK
109 e = xmalloc(sizeof(struct commit_name));
110 commit->util = e;
3cfa4db3
AK
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;
118 }
1e1ade18 119 }
03e8b541 120 e->tag = tag;
e7eb5034 121 e->prio = prio;
03e8b541 122 e->name_checked = 0;
212945d4 123 hashcpy(e->sha1, sha1);
1e1ade18 124 e->path = path;
908e5310 125 }
908e5310
LT
126}
127
8da19775 128static int get_name(const char *path, const unsigned char *sha1, int flag, void *cb_data)
908e5310 129{
8a5a1884 130 int might_be_tag = !prefixcmp(path, "refs/tags/");
feededd0 131 struct commit *commit;
64deb858 132 struct object *object;
feededd0
SP
133 unsigned char peeled[20];
134 int is_tag, prio;
135
8a5a1884
SP
136 if (!all && !might_be_tag)
137 return 0;
138
feededd0
SP
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;
150 }
64deb858 151
2d9e7c9f
JH
152 /* If --all, then any refs are used.
153 * If --tags, then any tags are used.
154 * Otherwise only annotated tags are used.
155 */
8a5a1884 156 if (might_be_tag) {
4ed19a3c 157 if (is_tag)
64deb858 158 prio = 2;
4ed19a3c 159 else
64deb858 160 prio = 1;
4ed19a3c
MD
161
162 if (pattern && fnmatch(pattern, path + 10, 0))
163 prio = 0;
64deb858
JH
164 }
165 else
166 prio = 0;
167
635d4134 168 if (!all) {
64deb858
JH
169 if (!prio)
170 return 0;
635d4134 171 }
212945d4 172 add_to_known_names(all ? path + 5 : path + 10, commit, prio, sha1);
908e5310
LT
173 return 0;
174}
175
80dbae03 176struct possible_tag {
80dbae03 177 struct commit_name *name;
cf69fd49
SP
178 int depth;
179 int found_order;
8713ab30 180 unsigned flag_within;
80dbae03
SP
181};
182
cf69fd49
SP
183static int compare_pt(const void *a_, const void *b_)
184{
185 struct possible_tag *a = (struct possible_tag *)a_;
186 struct possible_tag *b = (struct possible_tag *)b_;
cf69fd49
SP
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;
192}
193
1b600e65
SP
194static unsigned long finish_depth_computation(
195 struct commit_list **list,
196 struct possible_tag *best)
197{
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;
210 }
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;
222 }
223 }
224 return seen_commits;
225}
226
212945d4
SP
227static void display_name(struct commit_name *n)
228{
229 if (n->prio == 2 && !n->tag) {
230 n->tag = lookup_tag(n->sha1);
03e8b541 231 if (!n->tag || parse_tag(n->tag))
212945d4 232 die("annotated tag %s not available", n->path);
03e8b541
SP
233 }
234 if (n->tag && !n->name_checked) {
235 if (!n->tag->tag)
236 die("annotated tag %s has no embedded name", n->path);
81dc223d 237 if (strcmp(n->tag->tag, all ? n->path + 5 : n->path))
212945d4 238 warning("tag '%s' is really '%s' here", n->tag->tag, n->path);
03e8b541 239 n->name_checked = 1;
212945d4
SP
240 }
241
242 if (n->tag)
243 printf("%s", n->tag->tag);
244 else
245 printf("%s", n->path);
870cf7d6
JH
246}
247
248static void show_suffix(int depth, const unsigned char *sha1)
249{
250 printf("-%d-g%s", depth, find_unique_abbrev(sha1, abbrev));
212945d4
SP
251}
252
554fe20d 253static void describe(const char *arg, int last_one)
908e5310 254{
4c34a2c5 255 unsigned char sha1[20];
8713ab30 256 struct commit *cmit, *gave_up_on = NULL;
908e5310 257 struct commit_list *list;
908e5310 258 struct commit_name *n;
cf69fd49 259 struct possible_tag all_matches[MAX_TAGS];
8713ab30
SP
260 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match;
261 unsigned long seen_commits = 0;
4d23660e 262 unsigned int unannotated_cnt = 0;
908e5310 263
31fff305
DL
264 if (get_sha1(arg, sha1))
265 die("Not a valid object name %s", arg);
4c34a2c5
JH
266 cmit = lookup_commit_reference(sha1);
267 if (!cmit)
31fff305 268 die("%s is not a valid '%s' object", arg, commit_type);
4c34a2c5 269
3cfa4db3 270 n = find_commit_name(cmit->object.sha1);
7a0d61bb 271 if (n && (tags || all || n->prio == 2)) {
870cf7d6
JH
272 /*
273 * Exact match to an existing ref.
274 */
212945d4 275 display_name(n);
870cf7d6 276 if (longformat)
14d4642e 277 show_suffix(0, n->tag ? n->tag->tagged->sha1 : sha1);
9f67d2e8
JP
278 if (dirty)
279 printf("%s", dirty);
212945d4 280 printf("\n");
908e5310
LT
281 return;
282 }
283
2c33f757
SP
284 if (!max_candidates)
285 die("no tag exactly matches '%s'", sha1_to_hex(cmit->object.sha1));
8713ab30
SP
286 if (debug)
287 fprintf(stderr, "searching to describe %s\n", arg);
288
908e5310 289 list = NULL;
8713ab30 290 cmit->object.flags = SEEN;
908e5310
LT
291 commit_list_insert(cmit, &list);
292 while (list) {
80dbae03 293 struct commit *c = pop_commit(&list);
dccd0c2a 294 struct commit_list *parents = c->parents;
8713ab30 295 seen_commits++;
e7eb5034 296 n = c->util;
908e5310 297 if (n) {
4d23660e
TR
298 if (!tags && !all && n->prio < 2) {
299 unannotated_cnt++;
300 } else if (match_cnt < max_candidates) {
8713ab30
SP
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;
8a8169c0 305 t->found_order = match_cnt;
8713ab30
SP
306 c->object.flags |= t->flag_within;
307 if (n->prio == 2)
308 annotated_cnt++;
309 }
310 else {
311 gave_up_on = c;
312 break;
313 }
314 }
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++;
319 }
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;
dccd0c2a
SP
325 }
326 while (parents) {
327 struct commit *p = parents->item;
328 parse_commit(p);
8713ab30 329 if (!(p->object.flags & SEEN))
dccd0c2a 330 insert_by_date(p, &list);
8713ab30 331 p->object.flags |= c->object.flags;
dccd0c2a 332 parents = parents->next;
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
TR
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));
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) {
358 insert_by_date(gave_up_on, &list);
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 }
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));
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
PH
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"),
518120e3 400 OPT_BOOLEAN(0, "long", &longformat, "always use long format"),
166185be 401 OPT__ABBREV(&abbrev),
2c33f757
SP
402 OPT_SET_INT(0, "exact-match", &max_candidates,
403 "only output exact matches", 0),
166185be 404 OPT_INTEGER(0, "candidates", &max_candidates,
30ffa603
PH
405 "consider <n> most recent tags (default: 10)"),
406 OPT_STRING(0, "match", &pattern, "pattern",
407 "only consider tags matching <pattern>"),
da2478db
JH
408 OPT_BOOLEAN(0, "always", &always,
409 "show abbreviated commit object as fallback"),
9f67d2e8
JP
410 {OPTION_STRING, 0, "dirty", &dirty, "mark",
411 "append <mark> on dirty working tree (default: \"-dirty\")",
412 PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"},
166185be
PH
413 OPT_END(),
414 };
908e5310 415
37782920 416 argc = parse_options(argc, argv, prefix, options, describe_usage, 0);
2c33f757
SP
417 if (max_candidates < 0)
418 max_candidates = 0;
166185be
PH
419 else if (max_candidates > MAX_TAGS)
420 max_candidates = MAX_TAGS;
4c34a2c5 421
8c599c74 422 save_commit_buffer = 0;
8112894d 423
518120e3
SB
424 if (longformat && abbrev == 0)
425 die("--long is incompatible with --abbrev=0");
426
23615708 427 if (contains) {
4b25d091 428 const char **args = xmalloc((7 + argc) * sizeof(char *));
3f7701a4
NP
429 int i = 0;
430 args[i++] = "name-rev";
431 args[i++] = "--name-only";
a2cf9f44 432 args[i++] = "--no-undefined";
da2478db
JH
433 if (always)
434 args[i++] = "--always";
30ffa603 435 if (!all) {
3f7701a4 436 args[i++] = "--tags";
30ffa603
PH
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;
441 }
442 }
4b25d091 443 memcpy(args + i, argv, argc * sizeof(char *));
3f7701a4
NP
444 args[i + argc] = NULL;
445 return cmd_name_rev(i + argc, args, prefix);
23615708
SP
446 }
447
3cfa4db3 448 init_hash(&names);
56a5f3af 449 for_each_rawref(get_name, NULL);
3cfa4db3 450 if (!names.nr && !always)
fb423da0
RS
451 die("No names found, cannot describe anything.");
452
166185be 453 if (argc == 0) {
9f67d2e8
JP
454 if (dirty && !cmd_diff_index(ARRAY_SIZE(diff_index_args) - 1, diff_index_args, prefix))
455 dirty = NULL;
fec9ebf1 456 describe("HEAD", 1);
9f67d2e8
JP
457 } else if (dirty) {
458 die("--dirty is incompatible with committishes");
166185be
PH
459 } else {
460 while (argc-- > 0) {
461 describe(*argv++, argc == 0);
fec9ebf1 462 }
166185be 463 }
908e5310
LT
464 return 0;
465}