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