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