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