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