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