Git fork
at reftables-rust 815 lines 22 kB view raw
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 32define_commit_slab(commit_names, struct commit_name *); 33 34static 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 41static int debug; /* Display lots of verbose info */ 42static int all; /* Any valid ref can be used */ 43static int tags; /* Allow lightweight tags */ 44static int longformat; 45static int first_parent; 46static int abbrev = -1; /* unspecified */ 47static int max_candidates = DEFAULT_CANDIDATES; 48static struct hashmap names; 49static int have_util; 50static struct string_list patterns = STRING_LIST_INIT_NODUP; 51static struct string_list exclude_patterns = STRING_LIST_INIT_NODUP; 52static int always; 53static const char *suffix, *dirty, *broken; 54static struct commit_names commit_names; 55 56/* diff-index command arguments to check if working tree is dirty. */ 57static const char *diff_index_args[] = { 58 "diff-index", "--quiet", "HEAD", "--", NULL 59}; 60 61static const char *update_index_args[] = { 62 "update-index", "--unmerged", "-q", "--refresh", NULL 63}; 64 65struct 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 76static const char *prio_names[] = { 77 N_("head"), N_("lightweight"), N_("annotated"), 78}; 79 80static 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 93static 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 99static 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(t)) 116 return 1; 117 e->tag = t; 118 } 119 120 t = lookup_tag(the_repository, oid); 121 if (!t || parse_tag(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 132static 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 157static int get_name(const char *path, const char *referent UNUSED, const struct object_id *oid, 158 int flag UNUSED, void *cb_data UNUSED) 159{ 160 int is_tag = 0; 161 struct object_id peeled; 162 int is_annotated, prio; 163 const char *path_to_match = NULL; 164 165 if (skip_prefix(path, "refs/tags/", &path_to_match)) { 166 is_tag = 1; 167 } else if (all) { 168 if ((exclude_patterns.nr || patterns.nr) && 169 !skip_prefix(path, "refs/heads/", &path_to_match) && 170 !skip_prefix(path, "refs/remotes/", &path_to_match)) { 171 /* Only accept reference of known type if there are match/exclude patterns */ 172 return 0; 173 } 174 } else { 175 /* Reject anything outside refs/tags/ unless --all */ 176 return 0; 177 } 178 179 /* 180 * If we're given exclude patterns, first exclude any tag which match 181 * any of the exclude pattern. 182 */ 183 if (exclude_patterns.nr) { 184 struct string_list_item *item; 185 186 for_each_string_list_item(item, &exclude_patterns) { 187 if (!wildmatch(item->string, path_to_match, 0)) 188 return 0; 189 } 190 } 191 192 /* 193 * If we're given patterns, accept only tags which match at least one 194 * pattern. 195 */ 196 if (patterns.nr) { 197 int found = 0; 198 struct string_list_item *item; 199 200 for_each_string_list_item(item, &patterns) { 201 if (!wildmatch(item->string, path_to_match, 0)) { 202 found = 1; 203 break; 204 } 205 } 206 207 if (!found) 208 return 0; 209 } 210 211 /* Is it annotated? */ 212 if (!peel_iterated_oid(the_repository, oid, &peeled)) { 213 is_annotated = !oideq(oid, &peeled); 214 } else { 215 oidcpy(&peeled, oid); 216 is_annotated = 0; 217 } 218 219 /* 220 * By default, we only use annotated tags, but with --tags 221 * we fall back to lightweight ones (even without --tags, 222 * we still remember lightweight ones, only to give hints 223 * in an error message). --all allows any refs to be used. 224 */ 225 if (is_annotated) 226 prio = 2; 227 else if (is_tag) 228 prio = 1; 229 else 230 prio = 0; 231 232 add_to_known_names(all ? path + 5 : path + 10, &peeled, prio, oid); 233 return 0; 234} 235 236struct possible_tag { 237 struct commit_name *name; 238 int depth; 239 int found_order; 240 unsigned flag_within; 241}; 242 243static 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 254struct 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 261static 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 270static 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 279static bool lazy_queue_empty(const struct lazy_queue *queue) 280{ 281 return queue->queue.nr == (queue->get_pending ? 1 : 0); 282} 283 284static void lazy_queue_clear(struct lazy_queue *queue) 285{ 286 clear_prio_queue(&queue->queue); 287 queue->get_pending = false; 288} 289 290static 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 334static 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(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 359static 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 365static 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 538struct 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 545static void process_commit(struct commit *commit, void *data) 546{ 547 struct process_commit_data *pcd = data; 548 pcd->current_commit = commit; 549} 550 551static 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 free_commit_list(pcd->revs->commits); 562 pcd->revs->commits = NULL; 563 } 564} 565 566static 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 599static 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 628static 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 639int cmd_describe(int argc, 640 const char **argv, 641 const char *prefix, 642 struct repository *repo UNUSED ) 643{ 644 int contains = 0; 645 struct option options[] = { 646 OPT_BOOL(0, "contains", &contains, N_("find the tag that comes after the commit")), 647 OPT_BOOL(0, "debug", &debug, N_("debug search strategy on stderr")), 648 OPT_BOOL(0, "all", &all, N_("use any ref")), 649 OPT_BOOL(0, "tags", &tags, N_("use any tag, even unannotated")), 650 OPT_BOOL(0, "long", &longformat, N_("always use long format")), 651 OPT_BOOL(0, "first-parent", &first_parent, N_("only follow first parent")), 652 OPT__ABBREV(&abbrev), 653 OPT_CALLBACK_F(0, "exact-match", &max_candidates, NULL, 654 N_("only output exact matches"), 655 PARSE_OPT_NOARG, option_parse_exact_match), 656 OPT_INTEGER(0, "candidates", &max_candidates, 657 N_("consider <n> most recent tags (default: 10)")), 658 OPT_STRING_LIST(0, "match", &patterns, N_("pattern"), 659 N_("only consider tags matching <pattern>")), 660 OPT_STRING_LIST(0, "exclude", &exclude_patterns, N_("pattern"), 661 N_("do not consider tags matching <pattern>")), 662 OPT_BOOL(0, "always", &always, 663 N_("show abbreviated commit object as fallback")), 664 { 665 .type = OPTION_STRING, 666 .long_name = "dirty", 667 .value = &dirty, 668 .argh = N_("mark"), 669 .help = N_("append <mark> on dirty working tree (default: \"-dirty\")"), 670 .flags = PARSE_OPT_OPTARG, 671 .defval = (intptr_t) "-dirty", 672 }, 673 { 674 .type = OPTION_STRING, 675 .long_name = "broken", 676 .value = &broken, 677 .argh = N_("mark"), 678 .help = N_("append <mark> on broken working tree (default: \"-broken\")"), 679 .flags = PARSE_OPT_OPTARG, 680 .defval = (intptr_t) "-broken", 681 }, 682 OPT_END(), 683 }; 684 685 repo_config(the_repository, git_default_config, NULL); 686 argc = parse_options(argc, argv, prefix, options, describe_usage, 0); 687 if (abbrev < 0) 688 abbrev = DEFAULT_ABBREV; 689 690 if (max_candidates < 0) 691 max_candidates = 0; 692 else if (max_candidates > MAX_TAGS) 693 max_candidates = MAX_TAGS; 694 695 save_commit_buffer = 0; 696 697 if (longformat && abbrev == 0) 698 die(_("options '%s' and '%s' cannot be used together"), "--long", "--abbrev=0"); 699 700 if (contains) { 701 struct string_list_item *item; 702 struct strvec args; 703 const char **argv_copy; 704 int ret; 705 706 strvec_init(&args); 707 strvec_pushl(&args, "name-rev", 708 "--peel-tag", "--name-only", "--no-undefined", 709 NULL); 710 if (always) 711 strvec_push(&args, "--always"); 712 if (!all) { 713 strvec_push(&args, "--tags"); 714 for_each_string_list_item(item, &patterns) 715 strvec_pushf(&args, "--refs=refs/tags/%s", item->string); 716 for_each_string_list_item(item, &exclude_patterns) 717 strvec_pushf(&args, "--exclude=refs/tags/%s", item->string); 718 } 719 if (argc) 720 strvec_pushv(&args, argv); 721 else 722 strvec_push(&args, "HEAD"); 723 724 /* 725 * `cmd_name_rev()` modifies the array, so we'd leak its 726 * contained strings if we didn't do a copy here. 727 */ 728 ALLOC_ARRAY(argv_copy, args.nr + 1); 729 for (size_t i = 0; i < args.nr; i++) 730 argv_copy[i] = args.v[i]; 731 argv_copy[args.nr] = NULL; 732 733 ret = cmd_name_rev(args.nr, argv_copy, prefix, the_repository); 734 735 strvec_clear(&args); 736 free(argv_copy); 737 return ret; 738 } 739 740 hashmap_init(&names, commit_name_neq, NULL, 0); 741 refs_for_each_rawref(get_main_ref_store(the_repository), get_name, 742 NULL); 743 if (!hashmap_get_size(&names) && !always) 744 die(_("No names found, cannot describe anything.")); 745 746 if (argc == 0) { 747 if (broken) { 748 struct child_process cp = CHILD_PROCESS_INIT; 749 750 strvec_pushv(&cp.args, update_index_args); 751 cp.git_cmd = 1; 752 cp.no_stdin = 1; 753 cp.no_stdout = 1; 754 run_command(&cp); 755 756 child_process_init(&cp); 757 strvec_pushv(&cp.args, diff_index_args); 758 cp.git_cmd = 1; 759 cp.no_stdin = 1; 760 cp.no_stdout = 1; 761 762 if (!dirty) 763 dirty = "-dirty"; 764 765 switch (run_command(&cp)) { 766 case 0: 767 suffix = NULL; 768 break; 769 case 1: 770 suffix = dirty; 771 break; 772 default: 773 /* diff-index aborted abnormally */ 774 suffix = broken; 775 } 776 } else if (dirty) { 777 struct lock_file index_lock = LOCK_INIT; 778 struct rev_info revs; 779 int fd; 780 781 setup_work_tree(); 782 prepare_repo_settings(the_repository); 783 the_repository->settings.command_requires_full_index = 0; 784 repo_read_index(the_repository); 785 refresh_index(the_repository->index, REFRESH_QUIET|REFRESH_UNMERGED, 786 NULL, NULL, NULL); 787 fd = repo_hold_locked_index(the_repository, 788 &index_lock, 0); 789 if (0 <= fd) 790 repo_update_index_if_able(the_repository, &index_lock); 791 792 repo_init_revisions(the_repository, &revs, prefix); 793 794 if (setup_revisions(ARRAY_SIZE(diff_index_args) - 1, 795 diff_index_args, &revs, NULL) != 1) 796 BUG("malformed internal diff-index command line"); 797 run_diff_index(&revs, 0); 798 799 if (!diff_result_code(&revs)) 800 suffix = NULL; 801 else 802 suffix = dirty; 803 release_revisions(&revs); 804 } 805 describe("HEAD", 1); 806 } else if (dirty) { 807 die(_("option '%s' and commit-ishes cannot be used together"), "--dirty"); 808 } else if (broken) { 809 die(_("option '%s' and commit-ishes cannot be used together"), "--broken"); 810 } else { 811 while (argc-- > 0) 812 describe(*argv++, argc == 0); 813 } 814 return 0; 815}