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