]>
Commit | Line | Data |
---|---|---|
07047d68 | 1 | #define USE_THE_INDEX_VARIABLE |
908e5310 | 2 | #include "cache.h" |
b2141fc1 | 3 | #include "config.h" |
41771fa4 | 4 | #include "hex.h" |
697cc8ef | 5 | #include "lockfile.h" |
908e5310 | 6 | #include "commit.h" |
635d4134 | 7 | #include "tag.h" |
644eb60b | 8 | #include "blob.h" |
908e5310 | 9 | #include "refs.h" |
9a0eaf83 | 10 | #include "builtin.h" |
d807c4a0 | 11 | #include "exec-cmd.h" |
166185be | 12 | #include "parse-options.h" |
be26d2b2 | 13 | #include "revision.h" |
9f67d2e8 | 14 | #include "diff.h" |
29d8a834 | 15 | #include "hashmap.h" |
dbbcd44f | 16 | #include "strvec.h" |
b0176ce6 | 17 | #include "run-command.h" |
cbd53a21 | 18 | #include "object-store.h" |
644eb60b | 19 | #include "list-objects.h" |
c6b7206b | 20 | #include "commit-slab.h" |
908e5310 | 21 | |
8713ab30 SP |
22 | #define MAX_TAGS (FLAG_BITS - 1) |
23 | ||
c6b7206b NTND |
24 | define_commit_slab(commit_names, struct commit_name *); |
25 | ||
166185be | 26 | static const char * const describe_usage[] = { |
8c9e292d ÆAB |
27 | N_("git describe [--all] [--tags] [--contains] [--abbrev=<n>] [<commit-ish>...]"), |
28 | N_("git describe [--all] [--tags] [--contains] [--abbrev=<n>] --dirty[=<mark>]"), | |
29 | N_("git describe <blob>"), | |
166185be PH |
30 | NULL |
31 | }; | |
908e5310 | 32 | |
8713ab30 | 33 | static int debug; /* Display lots of verbose info */ |
7e425c4f SP |
34 | static int all; /* Any valid ref can be used */ |
35 | static int tags; /* Allow lightweight tags */ | |
518120e3 | 36 | static int longformat; |
e00dd1e9 | 37 | static int first_parent; |
dce96489 | 38 | static int abbrev = -1; /* unspecified */ |
8713ab30 | 39 | static int max_candidates = 10; |
29d8a834 | 40 | static struct hashmap names; |
d1645d02 | 41 | static int have_util; |
43f8080e | 42 | static struct string_list patterns = STRING_LIST_INIT_NODUP; |
77d21f29 | 43 | static struct string_list exclude_patterns = STRING_LIST_INIT_NODUP; |
da2478db | 44 | static int always; |
b0176ce6 | 45 | static const char *suffix, *dirty, *broken; |
c6b7206b | 46 | static struct commit_names commit_names; |
9f67d2e8 JP |
47 | |
48 | /* diff-index command arguments to check if working tree is dirty. */ | |
49 | static const char *diff_index_args[] = { | |
50 | "diff-index", "--quiet", "HEAD", "--", NULL | |
51 | }; | |
52 | ||
e7eb5034 | 53 | struct commit_name { |
29d8a834 | 54 | struct hashmap_entry entry; |
6439b5d9 | 55 | struct object_id peeled; |
212945d4 | 56 | struct tag *tag; |
03e8b541 SP |
57 | unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */ |
58 | unsigned name_checked:1; | |
ff165f00 | 59 | unsigned misnamed:1; |
6439b5d9 | 60 | struct object_id oid; |
219a0f33 | 61 | char *path; |
e7eb5034 | 62 | }; |
c4472643 | 63 | |
cf69fd49 | 64 | static const char *prio_names[] = { |
646c3bd1 | 65 | N_("head"), N_("lightweight"), N_("annotated"), |
cf69fd49 | 66 | }; |
908e5310 | 67 | |
5cf88fd8 | 68 | static int commit_name_neq(const void *cmp_data UNUSED, |
939af16e EW |
69 | const struct hashmap_entry *eptr, |
70 | const struct hashmap_entry *entry_or_key, | |
7663cdc8 | 71 | const void *peeled) |
29d8a834 | 72 | { |
939af16e EW |
73 | const struct commit_name *cn1, *cn2; |
74 | ||
75 | cn1 = container_of(eptr, const struct commit_name, entry); | |
76 | cn2 = container_of(entry_or_key, const struct commit_name, entry); | |
0068cede | 77 | |
cc00e5ce | 78 | return !oideq(&cn1->peeled, peeled ? peeled : &cn2->peeled); |
29d8a834 KB |
79 | } |
80 | ||
6439b5d9 | 81 | static inline struct commit_name *find_commit_name(const struct object_id *peeled) |
3cfa4db3 | 82 | { |
f23a4651 EW |
83 | return hashmap_get_entry_from_hash(&names, oidhash(peeled), peeled, |
84 | struct commit_name, entry); | |
d1645d02 AK |
85 | } |
86 | ||
03e8b541 SP |
87 | static int replace_name(struct commit_name *e, |
88 | int prio, | |
6439b5d9 | 89 | const struct object_id *oid, |
03e8b541 SP |
90 | struct tag **tag) |
91 | { | |
92 | if (!e || e->prio < prio) | |
93 | return 1; | |
94 | ||
95 | if (e->prio == 2 && prio == 2) { | |
96 | /* Multiple annotated tags point to the same commit. | |
97 | * Select one to keep based upon their tagger date. | |
98 | */ | |
99 | struct tag *t; | |
100 | ||
101 | if (!e->tag) { | |
ce71efb7 | 102 | t = lookup_tag(the_repository, &e->oid); |
03e8b541 SP |
103 | if (!t || parse_tag(t)) |
104 | return 1; | |
105 | e->tag = t; | |
106 | } | |
107 | ||
ce71efb7 | 108 | t = lookup_tag(the_repository, oid); |
03e8b541 SP |
109 | if (!t || parse_tag(t)) |
110 | return 0; | |
111 | *tag = t; | |
112 | ||
113 | if (e->tag->date < t->date) | |
114 | return 1; | |
115 | } | |
116 | ||
117 | return 0; | |
118 | } | |
119 | ||
64deb858 | 120 | static void add_to_known_names(const char *path, |
6439b5d9 | 121 | const struct object_id *peeled, |
212945d4 | 122 | int prio, |
6439b5d9 | 123 | const struct object_id *oid) |
908e5310 | 124 | { |
3cfa4db3 | 125 | struct commit_name *e = find_commit_name(peeled); |
03e8b541 | 126 | struct tag *tag = NULL; |
6439b5d9 | 127 | if (replace_name(e, prio, oid, &tag)) { |
1e1ade18 AK |
128 | if (!e) { |
129 | e = xmalloc(sizeof(struct commit_name)); | |
6439b5d9 | 130 | oidcpy(&e->peeled, peeled); |
d22245a2 | 131 | hashmap_entry_init(&e->entry, oidhash(peeled)); |
b94e5c1d | 132 | hashmap_add(&names, &e->entry); |
219a0f33 | 133 | e->path = NULL; |
1e1ade18 | 134 | } |
03e8b541 | 135 | e->tag = tag; |
e7eb5034 | 136 | e->prio = prio; |
03e8b541 | 137 | e->name_checked = 0; |
ff165f00 | 138 | e->misnamed = 0; |
6439b5d9 | 139 | oidcpy(&e->oid, oid); |
219a0f33 MH |
140 | free(e->path); |
141 | e->path = xstrdup(path); | |
908e5310 | 142 | } |
908e5310 LT |
143 | } |
144 | ||
63e14ee2 | 145 | static int get_name(const char *path, const struct object_id *oid, |
5cf88fd8 | 146 | int flag UNUSED, void *cb_data UNUSED) |
908e5310 | 147 | { |
6d68b2ab | 148 | int is_tag = 0; |
99a2cfbf | 149 | struct object_id peeled; |
46e1d6eb | 150 | int is_annotated, prio; |
6d68b2ab MK |
151 | const char *path_to_match = NULL; |
152 | ||
153 | if (skip_prefix(path, "refs/tags/", &path_to_match)) { | |
154 | is_tag = 1; | |
155 | } else if (all) { | |
156 | if ((exclude_patterns.nr || patterns.nr) && | |
157 | !skip_prefix(path, "refs/heads/", &path_to_match) && | |
158 | !skip_prefix(path, "refs/remotes/", &path_to_match)) { | |
159 | /* Only accept reference of known type if there are match/exclude patterns */ | |
160 | return 0; | |
161 | } | |
162 | } else { | |
163 | /* Reject anything outside refs/tags/ unless --all */ | |
8a5a1884 | 164 | return 0; |
6d68b2ab | 165 | } |
8a5a1884 | 166 | |
77d21f29 JK |
167 | /* |
168 | * If we're given exclude patterns, first exclude any tag which match | |
169 | * any of the exclude pattern. | |
170 | */ | |
171 | if (exclude_patterns.nr) { | |
172 | struct string_list_item *item; | |
173 | ||
77d21f29 | 174 | for_each_string_list_item(item, &exclude_patterns) { |
6d68b2ab | 175 | if (!wildmatch(item->string, path_to_match, 0)) |
77d21f29 JK |
176 | return 0; |
177 | } | |
178 | } | |
179 | ||
43f8080e JK |
180 | /* |
181 | * If we're given patterns, accept only tags which match at least one | |
182 | * pattern. | |
183 | */ | |
184 | if (patterns.nr) { | |
da769d29 | 185 | int found = 0; |
43f8080e JK |
186 | struct string_list_item *item; |
187 | ||
43f8080e | 188 | for_each_string_list_item(item, &patterns) { |
6d68b2ab | 189 | if (!wildmatch(item->string, path_to_match, 0)) { |
da769d29 | 190 | found = 1; |
43f8080e | 191 | break; |
da769d29 MK |
192 | } |
193 | } | |
43f8080e | 194 | |
da769d29 | 195 | if (!found) |
43f8080e | 196 | return 0; |
43f8080e | 197 | } |
46e1d6eb JH |
198 | |
199 | /* Is it annotated? */ | |
36a31792 | 200 | if (!peel_iterated_oid(oid, &peeled)) { |
4a7e27e9 | 201 | is_annotated = !oideq(oid, &peeled); |
feededd0 | 202 | } else { |
99a2cfbf | 203 | oidcpy(&peeled, oid); |
46e1d6eb | 204 | is_annotated = 0; |
feededd0 | 205 | } |
64deb858 | 206 | |
46e1d6eb JH |
207 | /* |
208 | * By default, we only use annotated tags, but with --tags | |
209 | * we fall back to lightweight ones (even without --tags, | |
210 | * we still remember lightweight ones, only to give hints | |
211 | * in an error message). --all allows any refs to be used. | |
2d9e7c9f | 212 | */ |
46e1d6eb JH |
213 | if (is_annotated) |
214 | prio = 2; | |
215 | else if (is_tag) | |
216 | prio = 1; | |
64deb858 JH |
217 | else |
218 | prio = 0; | |
219 | ||
6439b5d9 | 220 | add_to_known_names(all ? path + 5 : path + 10, &peeled, prio, oid); |
908e5310 LT |
221 | return 0; |
222 | } | |
223 | ||
80dbae03 | 224 | struct possible_tag { |
80dbae03 | 225 | struct commit_name *name; |
cf69fd49 SP |
226 | int depth; |
227 | int found_order; | |
8713ab30 | 228 | unsigned flag_within; |
80dbae03 SP |
229 | }; |
230 | ||
cf69fd49 SP |
231 | static int compare_pt(const void *a_, const void *b_) |
232 | { | |
233 | struct possible_tag *a = (struct possible_tag *)a_; | |
234 | struct possible_tag *b = (struct possible_tag *)b_; | |
cf69fd49 SP |
235 | if (a->depth != b->depth) |
236 | return a->depth - b->depth; | |
237 | if (a->found_order != b->found_order) | |
238 | return a->found_order - b->found_order; | |
239 | return 0; | |
240 | } | |
241 | ||
1b600e65 SP |
242 | static unsigned long finish_depth_computation( |
243 | struct commit_list **list, | |
244 | struct possible_tag *best) | |
245 | { | |
246 | unsigned long seen_commits = 0; | |
247 | while (*list) { | |
248 | struct commit *c = pop_commit(list); | |
249 | struct commit_list *parents = c->parents; | |
250 | seen_commits++; | |
251 | if (c->object.flags & best->flag_within) { | |
252 | struct commit_list *a = *list; | |
253 | while (a) { | |
254 | struct commit *i = a->item; | |
255 | if (!(i->object.flags & best->flag_within)) | |
256 | break; | |
257 | a = a->next; | |
258 | } | |
259 | if (!a) | |
260 | break; | |
261 | } else | |
262 | best->depth++; | |
263 | while (parents) { | |
264 | struct commit *p = parents->item; | |
265 | parse_commit(p); | |
266 | if (!(p->object.flags & SEEN)) | |
47e44ed1 | 267 | commit_list_insert_by_date(p, list); |
1b600e65 SP |
268 | p->object.flags |= c->object.flags; |
269 | parents = parents->next; | |
270 | } | |
271 | } | |
272 | return seen_commits; | |
273 | } | |
274 | ||
4dbc59a4 | 275 | static void append_name(struct commit_name *n, struct strbuf *dst) |
212945d4 SP |
276 | { |
277 | if (n->prio == 2 && !n->tag) { | |
ce71efb7 | 278 | n->tag = lookup_tag(the_repository, &n->oid); |
03e8b541 | 279 | if (!n->tag || parse_tag(n->tag)) |
e41f1cb3 | 280 | die(_("annotated tag %s not available"), n->path); |
03e8b541 SP |
281 | } |
282 | if (n->tag && !n->name_checked) { | |
ff165f00 JH |
283 | if (strcmp(n->tag->tag, all ? n->path + 5 : n->path)) { |
284 | warning(_("tag '%s' is externally known as '%s'"), | |
285 | n->path, n->tag->tag); | |
286 | n->misnamed = 1; | |
287 | } | |
03e8b541 | 288 | n->name_checked = 1; |
212945d4 SP |
289 | } |
290 | ||
1bba0013 DKF |
291 | if (n->tag) { |
292 | if (all) | |
fac64e01 | 293 | strbuf_addstr(dst, "tags/"); |
4dbc59a4 | 294 | strbuf_addstr(dst, n->tag->tag); |
1bba0013 | 295 | } else { |
4dbc59a4 | 296 | strbuf_addstr(dst, n->path); |
1bba0013 | 297 | } |
870cf7d6 JH |
298 | } |
299 | ||
4dbc59a4 | 300 | static void append_suffix(int depth, const struct object_id *oid, struct strbuf *dst) |
870cf7d6 | 301 | { |
aab9583f | 302 | strbuf_addf(dst, "-%d-g%s", depth, find_unique_abbrev(oid, abbrev)); |
212945d4 SP |
303 | } |
304 | ||
4dbc59a4 | 305 | static void describe_commit(struct object_id *oid, struct strbuf *dst) |
908e5310 | 306 | { |
8713ab30 | 307 | struct commit *cmit, *gave_up_on = NULL; |
908e5310 | 308 | struct commit_list *list; |
908e5310 | 309 | struct commit_name *n; |
cf69fd49 | 310 | struct possible_tag all_matches[MAX_TAGS]; |
8713ab30 SP |
311 | unsigned int match_cnt = 0, annotated_cnt = 0, cur_match; |
312 | unsigned long seen_commits = 0; | |
4d23660e | 313 | unsigned int unannotated_cnt = 0; |
908e5310 | 314 | |
2122f675 | 315 | cmit = lookup_commit_reference(the_repository, oid); |
4c34a2c5 | 316 | |
6439b5d9 | 317 | n = find_commit_name(&cmit->object.oid); |
7a0d61bb | 318 | if (n && (tags || all || n->prio == 2)) { |
870cf7d6 JH |
319 | /* |
320 | * Exact match to an existing ref. | |
321 | */ | |
4dbc59a4 | 322 | append_name(n, dst); |
ff165f00 | 323 | if (n->misnamed || longformat) |
c77722b3 | 324 | append_suffix(0, n->tag ? get_tagged_oid(n->tag) : oid, dst); |
b0176ce6 | 325 | if (suffix) |
4dbc59a4 | 326 | strbuf_addstr(dst, suffix); |
908e5310 LT |
327 | return; |
328 | } | |
329 | ||
2c33f757 | 330 | if (!max_candidates) |
f2fd0760 | 331 | die(_("no tag exactly matches '%s'"), oid_to_hex(&cmit->object.oid)); |
8713ab30 | 332 | if (debug) |
cdaed0cf | 333 | fprintf(stderr, _("No exact match on refs or tags, searching to describe\n")); |
8713ab30 | 334 | |
d1645d02 | 335 | if (!have_util) { |
29d8a834 KB |
336 | struct hashmap_iter iter; |
337 | struct commit *c; | |
c6b7206b NTND |
338 | struct commit_name *n; |
339 | ||
340 | init_commit_names(&commit_names); | |
23dee69f | 341 | hashmap_for_each_entry(&names, &iter, n, |
87571c3f | 342 | entry /* member name */) { |
21e1ee8f SB |
343 | c = lookup_commit_reference_gently(the_repository, |
344 | &n->peeled, 1); | |
29d8a834 | 345 | if (c) |
c6b7206b | 346 | *commit_names_at(&commit_names, c) = n; |
29d8a834 | 347 | } |
d1645d02 AK |
348 | have_util = 1; |
349 | } | |
350 | ||
908e5310 | 351 | list = NULL; |
8713ab30 | 352 | cmit->object.flags = SEEN; |
908e5310 LT |
353 | commit_list_insert(cmit, &list); |
354 | while (list) { | |
80dbae03 | 355 | struct commit *c = pop_commit(&list); |
dccd0c2a | 356 | struct commit_list *parents = c->parents; |
c6b7206b NTND |
357 | struct commit_name **slot; |
358 | ||
8713ab30 | 359 | seen_commits++; |
c6b7206b NTND |
360 | slot = commit_names_peek(&commit_names, c); |
361 | n = slot ? *slot : NULL; | |
908e5310 | 362 | if (n) { |
4d23660e TR |
363 | if (!tags && !all && n->prio < 2) { |
364 | unannotated_cnt++; | |
365 | } else if (match_cnt < max_candidates) { | |
8713ab30 SP |
366 | struct possible_tag *t = &all_matches[match_cnt++]; |
367 | t->name = n; | |
368 | t->depth = seen_commits - 1; | |
369 | t->flag_within = 1u << match_cnt; | |
8a8169c0 | 370 | t->found_order = match_cnt; |
8713ab30 SP |
371 | c->object.flags |= t->flag_within; |
372 | if (n->prio == 2) | |
373 | annotated_cnt++; | |
374 | } | |
375 | else { | |
376 | gave_up_on = c; | |
377 | break; | |
378 | } | |
379 | } | |
380 | for (cur_match = 0; cur_match < match_cnt; cur_match++) { | |
381 | struct possible_tag *t = &all_matches[cur_match]; | |
382 | if (!(c->object.flags & t->flag_within)) | |
383 | t->depth++; | |
384 | } | |
30b1c7ad | 385 | /* Stop if last remaining path already covered by best candidate(s) */ |
8713ab30 | 386 | if (annotated_cnt && !list) { |
30b1c7ad BE |
387 | int best_depth = INT_MAX; |
388 | unsigned best_within = 0; | |
389 | for (cur_match = 0; cur_match < match_cnt; cur_match++) { | |
390 | struct possible_tag *t = &all_matches[cur_match]; | |
391 | if (t->depth < best_depth) { | |
392 | best_depth = t->depth; | |
393 | best_within = t->flag_within; | |
394 | } else if (t->depth == best_depth) { | |
395 | best_within |= t->flag_within; | |
396 | } | |
397 | } | |
398 | if ((c->object.flags & best_within) == best_within) { | |
399 | if (debug) | |
400 | fprintf(stderr, _("finished search at %s\n"), | |
401 | oid_to_hex(&c->object.oid)); | |
402 | break; | |
403 | } | |
dccd0c2a SP |
404 | } |
405 | while (parents) { | |
406 | struct commit *p = parents->item; | |
407 | parse_commit(p); | |
8713ab30 | 408 | if (!(p->object.flags & SEEN)) |
47e44ed1 | 409 | commit_list_insert_by_date(p, &list); |
8713ab30 | 410 | p->object.flags |= c->object.flags; |
dccd0c2a | 411 | parents = parents->next; |
e00dd1e9 MC |
412 | |
413 | if (first_parent) | |
414 | break; | |
80dbae03 SP |
415 | } |
416 | } | |
417 | ||
da2478db | 418 | if (!match_cnt) { |
c87b653c | 419 | struct object_id *cmit_oid = &cmit->object.oid; |
da2478db | 420 | if (always) { |
aab9583f | 421 | strbuf_add_unique_abbrev(dst, cmit_oid, abbrev); |
b0176ce6 | 422 | if (suffix) |
4dbc59a4 | 423 | strbuf_addstr(dst, suffix); |
da2478db JH |
424 | return; |
425 | } | |
4d23660e | 426 | if (unannotated_cnt) |
e41f1cb3 ÆAB |
427 | die(_("No annotated tags can describe '%s'.\n" |
428 | "However, there were unannotated tags: try --tags."), | |
c87b653c | 429 | oid_to_hex(cmit_oid)); |
4d23660e | 430 | else |
e41f1cb3 ÆAB |
431 | die(_("No tags can describe '%s'.\n" |
432 | "Try --always, or create some tags."), | |
c87b653c | 433 | oid_to_hex(cmit_oid)); |
da2478db | 434 | } |
80dbae03 | 435 | |
9ed0d8d6 | 436 | QSORT(all_matches, match_cnt, compare_pt); |
1b600e65 SP |
437 | |
438 | if (gave_up_on) { | |
47e44ed1 | 439 | commit_list_insert_by_date(gave_up_on, &list); |
1b600e65 SP |
440 | seen_commits--; |
441 | } | |
442 | seen_commits += finish_depth_computation(&list, &all_matches[0]); | |
443 | free_commit_list(list); | |
444 | ||
8713ab30 | 445 | if (debug) { |
646c3bd1 MG |
446 | static int label_width = -1; |
447 | if (label_width < 0) { | |
448 | int i, w; | |
449 | for (i = 0; i < ARRAY_SIZE(prio_names); i++) { | |
450 | w = strlen(_(prio_names[i])); | |
451 | if (label_width < w) | |
452 | label_width = w; | |
453 | } | |
454 | } | |
8713ab30 SP |
455 | for (cur_match = 0; cur_match < match_cnt; cur_match++) { |
456 | struct possible_tag *t = &all_matches[cur_match]; | |
646c3bd1 MG |
457 | fprintf(stderr, " %-*s %8d %s\n", |
458 | label_width, _(prio_names[t->name->prio]), | |
8713ab30 SP |
459 | t->depth, t->name->path); |
460 | } | |
e41f1cb3 | 461 | fprintf(stderr, _("traversed %lu commits\n"), seen_commits); |
8713ab30 SP |
462 | if (gave_up_on) { |
463 | fprintf(stderr, | |
e41f1cb3 ÆAB |
464 | _("more than %i tags found; listed %i most recent\n" |
465 | "gave up search at %s\n"), | |
8713ab30 | 466 | max_candidates, max_candidates, |
f2fd0760 | 467 | oid_to_hex(&gave_up_on->object.oid)); |
8713ab30 | 468 | } |
80dbae03 | 469 | } |
212945d4 | 470 | |
4dbc59a4 | 471 | append_name(all_matches[0].name, dst); |
ff165f00 | 472 | if (all_matches[0].name->misnamed || abbrev) |
4dbc59a4 | 473 | append_suffix(all_matches[0].depth, &cmit->object.oid, dst); |
b0176ce6 | 474 | if (suffix) |
4dbc59a4 SB |
475 | strbuf_addstr(dst, suffix); |
476 | } | |
477 | ||
644eb60b SB |
478 | struct process_commit_data { |
479 | struct object_id current_commit; | |
480 | struct object_id looking_for; | |
481 | struct strbuf *dst; | |
482 | struct rev_info *revs; | |
483 | }; | |
484 | ||
485 | static void process_commit(struct commit *commit, void *data) | |
486 | { | |
487 | struct process_commit_data *pcd = data; | |
488 | pcd->current_commit = commit->object.oid; | |
489 | } | |
490 | ||
491 | static void process_object(struct object *obj, const char *path, void *data) | |
492 | { | |
493 | struct process_commit_data *pcd = data; | |
494 | ||
4a7e27e9 | 495 | if (oideq(&pcd->looking_for, &obj->oid) && !pcd->dst->len) { |
644eb60b SB |
496 | reset_revision_walk(); |
497 | describe_commit(&pcd->current_commit, pcd->dst); | |
498 | strbuf_addf(pcd->dst, ":%s", path); | |
499 | free_commit_list(pcd->revs->commits); | |
500 | pcd->revs->commits = NULL; | |
501 | } | |
502 | } | |
503 | ||
504 | static void describe_blob(struct object_id oid, struct strbuf *dst) | |
505 | { | |
506 | struct rev_info revs; | |
22f9b7f3 | 507 | struct strvec args = STRVEC_INIT; |
14228447 | 508 | struct process_commit_data pcd = { *null_oid(), oid, dst, &revs}; |
644eb60b | 509 | |
22f9b7f3 | 510 | strvec_pushl(&args, "internal: The first arg is not parsed", |
f6d8942b JK |
511 | "--objects", "--in-commit-order", "--reverse", "HEAD", |
512 | NULL); | |
644eb60b | 513 | |
2abf3503 | 514 | repo_init_revisions(the_repository, &revs, NULL); |
d70a9eb6 | 515 | if (setup_revisions(args.nr, args.v, &revs, NULL) > 1) |
644eb60b SB |
516 | BUG("setup_revisions could not handle all args?"); |
517 | ||
518 | if (prepare_revision_walk(&revs)) | |
519 | die("revision walk setup failed"); | |
520 | ||
521 | traverse_commit_list(&revs, process_commit, process_object, &pcd); | |
522 | reset_revision_walk(); | |
2108fe4a | 523 | release_revisions(&revs); |
644eb60b SB |
524 | } |
525 | ||
4dbc59a4 SB |
526 | static void describe(const char *arg, int last_one) |
527 | { | |
528 | struct object_id oid; | |
529 | struct commit *cmit; | |
530 | struct strbuf sb = STRBUF_INIT; | |
531 | ||
532 | if (debug) | |
533 | fprintf(stderr, _("describe %s\n"), arg); | |
534 | ||
535 | if (get_oid(arg, &oid)) | |
536 | die(_("Not a valid object name %s"), arg); | |
21e1ee8f | 537 | cmit = lookup_commit_reference_gently(the_repository, &oid, 1); |
4dbc59a4 | 538 | |
644eb60b SB |
539 | if (cmit) |
540 | describe_commit(&oid, &sb); | |
0df8e965 | 541 | else if (oid_object_info(the_repository, &oid, NULL) == OBJ_BLOB) |
644eb60b SB |
542 | describe_blob(oid, &sb); |
543 | else | |
544 | die(_("%s is neither a commit nor blob"), arg); | |
4dbc59a4 SB |
545 | |
546 | puts(sb.buf); | |
80dbae03 | 547 | |
8713ab30 SP |
548 | if (!last_one) |
549 | clear_commit_marks(cmit, -1); | |
4dbc59a4 SB |
550 | |
551 | strbuf_release(&sb); | |
908e5310 LT |
552 | } |
553 | ||
9a0eaf83 | 554 | int cmd_describe(int argc, const char **argv, const char *prefix) |
908e5310 | 555 | { |
23615708 | 556 | int contains = 0; |
166185be | 557 | struct option options[] = { |
d5d09d47 SB |
558 | OPT_BOOL(0, "contains", &contains, N_("find the tag that comes after the commit")), |
559 | OPT_BOOL(0, "debug", &debug, N_("debug search strategy on stderr")), | |
560 | OPT_BOOL(0, "all", &all, N_("use any ref")), | |
561 | OPT_BOOL(0, "tags", &tags, N_("use any tag, even unannotated")), | |
562 | OPT_BOOL(0, "long", &longformat, N_("always use long format")), | |
563 | OPT_BOOL(0, "first-parent", &first_parent, N_("only follow first parent")), | |
166185be | 564 | OPT__ABBREV(&abbrev), |
2c33f757 | 565 | OPT_SET_INT(0, "exact-match", &max_candidates, |
bfb0737d | 566 | N_("only output exact matches"), 0), |
166185be | 567 | OPT_INTEGER(0, "candidates", &max_candidates, |
bfb0737d | 568 | N_("consider <n> most recent tags (default: 10)")), |
43f8080e | 569 | OPT_STRING_LIST(0, "match", &patterns, N_("pattern"), |
bfb0737d | 570 | N_("only consider tags matching <pattern>")), |
77d21f29 JK |
571 | OPT_STRING_LIST(0, "exclude", &exclude_patterns, N_("pattern"), |
572 | N_("do not consider tags matching <pattern>")), | |
d5d09d47 SB |
573 | OPT_BOOL(0, "always", &always, |
574 | N_("show abbreviated commit object as fallback")), | |
bfb0737d | 575 | {OPTION_STRING, 0, "dirty", &dirty, N_("mark"), |
d5d09d47 SB |
576 | N_("append <mark> on dirty working tree (default: \"-dirty\")"), |
577 | PARSE_OPT_OPTARG, NULL, (intptr_t) "-dirty"}, | |
b0176ce6 SB |
578 | {OPTION_STRING, 0, "broken", &broken, N_("mark"), |
579 | N_("append <mark> on broken working tree (default: \"-broken\")"), | |
580 | PARSE_OPT_OPTARG, NULL, (intptr_t) "-broken"}, | |
166185be PH |
581 | OPT_END(), |
582 | }; | |
908e5310 | 583 | |
dce96489 | 584 | git_config(git_default_config, NULL); |
37782920 | 585 | argc = parse_options(argc, argv, prefix, options, describe_usage, 0); |
dce96489 LT |
586 | if (abbrev < 0) |
587 | abbrev = DEFAULT_ABBREV; | |
588 | ||
2c33f757 SP |
589 | if (max_candidates < 0) |
590 | max_candidates = 0; | |
166185be PH |
591 | else if (max_candidates > MAX_TAGS) |
592 | max_candidates = MAX_TAGS; | |
4c34a2c5 | 593 | |
8c599c74 | 594 | save_commit_buffer = 0; |
8112894d | 595 | |
518120e3 | 596 | if (longformat && abbrev == 0) |
12909b6b | 597 | die(_("options '%s' and '%s' cannot be used together"), "--long", "--abbrev=0"); |
518120e3 | 598 | |
23615708 | 599 | if (contains) { |
43f8080e | 600 | struct string_list_item *item; |
22f9b7f3 | 601 | struct strvec args; |
45bc950b | 602 | |
22f9b7f3 JK |
603 | strvec_init(&args); |
604 | strvec_pushl(&args, "name-rev", | |
f6d8942b JK |
605 | "--peel-tag", "--name-only", "--no-undefined", |
606 | NULL); | |
da2478db | 607 | if (always) |
22f9b7f3 | 608 | strvec_push(&args, "--always"); |
30ffa603 | 609 | if (!all) { |
22f9b7f3 | 610 | strvec_push(&args, "--tags"); |
43f8080e | 611 | for_each_string_list_item(item, &patterns) |
22f9b7f3 | 612 | strvec_pushf(&args, "--refs=refs/tags/%s", item->string); |
77d21f29 | 613 | for_each_string_list_item(item, &exclude_patterns) |
22f9b7f3 | 614 | strvec_pushf(&args, "--exclude=refs/tags/%s", item->string); |
45bc950b | 615 | } |
2bd07065 | 616 | if (argc) |
22f9b7f3 | 617 | strvec_pushv(&args, argv); |
2bd07065 | 618 | else |
22f9b7f3 | 619 | strvec_push(&args, "HEAD"); |
d70a9eb6 | 620 | return cmd_name_rev(args.nr, args.v, prefix); |
23615708 SP |
621 | } |
622 | ||
cc00e5ce | 623 | hashmap_init(&names, commit_name_neq, NULL, 0); |
99a2cfbf | 624 | for_each_rawref(get_name, NULL); |
8b604d19 | 625 | if (!hashmap_get_size(&names) && !always) |
e41f1cb3 | 626 | die(_("No names found, cannot describe anything.")); |
fb423da0 | 627 | |
166185be | 628 | if (argc == 0) { |
b0176ce6 SB |
629 | if (broken) { |
630 | struct child_process cp = CHILD_PROCESS_INIT; | |
22f9b7f3 | 631 | strvec_pushv(&cp.args, diff_index_args); |
b0176ce6 SB |
632 | cp.git_cmd = 1; |
633 | cp.no_stdin = 1; | |
634 | cp.no_stdout = 1; | |
635 | ||
636 | if (!dirty) | |
637 | dirty = "-dirty"; | |
638 | ||
639 | switch (run_command(&cp)) { | |
640 | case 0: | |
641 | suffix = NULL; | |
642 | break; | |
643 | case 1: | |
644 | suffix = dirty; | |
645 | break; | |
646 | default: | |
647 | /* diff-index aborted abnormally */ | |
648 | suffix = broken; | |
649 | } | |
650 | } else if (dirty) { | |
b2275868 | 651 | struct lock_file index_lock = LOCK_INIT; |
be26d2b2 | 652 | struct rev_info revs; |
22f9b7f3 | 653 | struct strvec args = STRVEC_INIT; |
be26d2b2 | 654 | int fd, result; |
bb571486 | 655 | |
2ed5c8e1 | 656 | setup_work_tree(); |
07047d68 | 657 | repo_read_index(the_repository); |
bb571486 AC |
658 | refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, |
659 | NULL, NULL, NULL); | |
07047d68 ÆAB |
660 | fd = repo_hold_locked_index(the_repository, |
661 | &index_lock, 0); | |
bb571486 | 662 | if (0 <= fd) |
1b0d968b | 663 | repo_update_index_if_able(the_repository, &index_lock); |
bb571486 | 664 | |
2abf3503 | 665 | repo_init_revisions(the_repository, &revs, prefix); |
22f9b7f3 | 666 | strvec_pushv(&args, diff_index_args); |
d70a9eb6 | 667 | if (setup_revisions(args.nr, args.v, &revs, NULL) != 1) |
be26d2b2 JH |
668 | BUG("malformed internal diff-index command line"); |
669 | result = run_diff_index(&revs, 0); | |
670 | ||
671 | if (!diff_result_code(&revs.diffopt, result)) | |
b0176ce6 SB |
672 | suffix = NULL; |
673 | else | |
674 | suffix = dirty; | |
2108fe4a | 675 | release_revisions(&revs); |
bb571486 | 676 | } |
fec9ebf1 | 677 | describe("HEAD", 1); |
9f67d2e8 | 678 | } else if (dirty) { |
246cac85 | 679 | die(_("option '%s' and commit-ishes cannot be used together"), "--dirty"); |
b0176ce6 | 680 | } else if (broken) { |
246cac85 | 681 | die(_("option '%s' and commit-ishes cannot be used together"), "--broken"); |
166185be | 682 | } else { |
c4472643 | 683 | while (argc-- > 0) |
166185be | 684 | describe(*argv++, argc == 0); |
166185be | 685 | } |
908e5310 LT |
686 | return 0; |
687 | } |