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