Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/builtin/describe.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / builtin / describe.c` 795 lines ยท 21421 bytes Raw Blame History 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=] [...]"), 36 N_("git describe [--all] [--tags] [--contains] [--abbrev=] --dirty[=]"), 37 N_("git describe "), 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 static unsigned long finish_depth_computation(struct prio_queue *queue, 255 struct possible_tag *best) 256 { 257 unsigned long seen_commits = 0; 258 struct oidset unflagged = OIDSET_INIT; 259 struct commit *c; 260 261 prio_queue_for_each(queue, c) { 262 if (!(c->object.flags & best->flag_within)) 263 oidset_insert(&unflagged, &c->object.oid); 264 } 265 266 while ((c = prio_queue_get(queue))) { 267 struct commit_list *parents = c->parents; 268 seen_commits++; 269 if (c->object.flags & best->flag_within) { 270 if (!oidset_size(&unflagged)) 271 break; 272 } else { 273 oidset_remove(&unflagged, &c->object.oid); 274 best->depth++; 275 } 276 while (parents) { 277 unsigned seen, flag_before, flag_after; 278 struct commit *p = parents->item; 279 repo_parse_commit(the_repository, p); 280 seen = p->object.flags & SEEN; 281 if (!seen) 282 prio_queue_put(queue, p); 283 flag_before = p->object.flags & best->flag_within; 284 p->object.flags |= c->object.flags; 285 flag_after = p->object.flags & best->flag_within; 286 if (!seen && !flag_after) 287 oidset_insert(&unflagged, &p->object.oid); 288 if (seen && !flag_before && flag_after) 289 oidset_remove(&unflagged, &p->object.oid); 290 parents = parents->next; 291 } 292 } 293 oidset_clear(&unflagged); 294 return seen_commits; 295 } 296 297 static void append_name(struct commit_name *n, struct strbuf *dst) 298 { 299 if (n->prio == 2 && !n->tag) { 300 n->tag = lookup_tag(the_repository, &n->oid); 301 if (!n->tag || parse_tag(the_repository, n->tag)) 302 die(_("annotated tag %s not available"), n->path); 303 } 304 if (n->tag && !n->name_checked) { 305 if (strcmp(n->tag->tag, all ? n->path + 5 : n->path)) { 306 warning(_("tag '%s' is externally known as '%s'"), 307 n->path, n->tag->tag); 308 n->misnamed = 1; 309 } 310 n->name_checked = 1; 311 } 312 313 if (n->tag) { 314 if (all) 315 strbuf_addstr(dst, "tags/"); 316 strbuf_addstr(dst, n->tag->tag); 317 } else { 318 strbuf_addstr(dst, n->path); 319 } 320 } 321 322 static void append_suffix(int depth, const struct object_id *oid, struct strbuf *dst) 323 { 324 strbuf_addf(dst, "-%d-g%s", depth, 325 repo_find_unique_abbrev(the_repository, oid, abbrev)); 326 } 327 328 static void describe_commit(struct commit *cmit, struct strbuf *dst) 329 { 330 struct commit *c, *gave_up_on = NULL; 331 struct prio_queue queue = { compare_commits_by_commit_date }; 332 struct commit_name *n; 333 struct possible_tag all_matches[MAX_TAGS]; 334 unsigned int match_cnt = 0, annotated_cnt = 0, cur_match; 335 unsigned long seen_commits = 0; 336 unsigned int unannotated_cnt = 0; 337 338 n = find_commit_name(&cmit->object.oid); 339 if (n && (tags || all || n->prio == 2)) { 340 /* 341 * Exact match to an existing ref. 342 */ 343 append_name(n, dst); 344 if (n->misnamed || longformat) 345 append_suffix(0, n->tag ? get_tagged_oid(n->tag) : &cmit->object.oid, dst); 346 if (suffix) 347 strbuf_addstr(dst, suffix); 348 return; 349 } 350 351 if (!max_candidates) 352 die(_("no tag exactly matches '%s'"), oid_to_hex(&cmit->object.oid)); 353 if (debug) 354 fprintf(stderr, _("No exact match on refs or tags, searching to describe\n")); 355 356 if (!have_util) { 357 struct hashmap_iter iter; 358 struct commit *c; 359 struct commit_name *n; 360 361 init_commit_names(&commit_names); 362 hashmap_for_each_entry(&names, &iter, n, 363 entry /* member name */) { 364 c = lookup_commit_reference_gently(the_repository, 365 &n->peeled, 1); 366 if (c) 367 *commit_names_at(&commit_names, c) = n; 368 } 369 have_util = 1; 370 } 371 372 cmit->object.flags = SEEN; 373 prio_queue_put(&queue, cmit); 374 while ((c = prio_queue_get(&queue))) { 375 struct commit_list *parents = c->parents; 376 struct commit_name **slot; 377 378 seen_commits++; 379 380 if (match_cnt == max_candidates || 381 match_cnt == hashmap_get_size(&names)) { 382 gave_up_on = c; 383 break; 384 } 385 386 slot = commit_names_peek(&commit_names, c); 387 n = slot ? *slot : NULL; 388 if (n) { 389 if (!tags && !all && n->prio < 2) { 390 unannotated_cnt++; 391 } else if (match_cnt < max_candidates) { 392 struct possible_tag *t = &all_matches[match_cnt++]; 393 t->name = n; 394 t->depth = seen_commits - 1; 395 t->flag_within = 1u << match_cnt; 396 t->found_order = match_cnt; 397 c->object.flags |= t->flag_within; 398 if (n->prio == 2) 399 annotated_cnt++; 400 } 401 } 402 for (cur_match = 0; cur_match < match_cnt; cur_match++) { 403 struct possible_tag *t = &all_matches[cur_match]; 404 if (!(c->object.flags & t->flag_within)) 405 t->depth++; 406 } 407 /* Stop if last remaining path already covered by best candidate(s) */ 408 if (annotated_cnt && !prio_queue_size(&queue)) { 409 int best_depth = INT_MAX; 410 unsigned best_within = 0; 411 for (cur_match = 0; cur_match < match_cnt; cur_match++) { 412 struct possible_tag *t = &all_matches[cur_match]; 413 if (t->depth < best_depth) { 414 best_depth = t->depth; 415 best_within = t->flag_within; 416 } else if (t->depth == best_depth) { 417 best_within |= t->flag_within; 418 } 419 } 420 if ((c->object.flags & best_within) == best_within) { 421 if (debug) 422 fprintf(stderr, _("finished search at %s\n"), 423 oid_to_hex(&c->object.oid)); 424 break; 425 } 426 } 427 while (parents) { 428 struct commit *p = parents->item; 429 repo_parse_commit(the_repository, p); 430 if (!(p->object.flags & SEEN)) 431 prio_queue_put(&queue, p); 432 p->object.flags |= c->object.flags; 433 parents = parents->next; 434 435 if (first_parent) 436 break; 437 } 438 } 439 440 if (!match_cnt) { 441 struct object_id *cmit_oid = &cmit->object.oid; 442 if (always) { 443 strbuf_add_unique_abbrev(dst, cmit_oid, abbrev); 444 if (suffix) 445 strbuf_addstr(dst, suffix); 446 clear_prio_queue(&queue); 447 return; 448 } 449 if (unannotated_cnt) 450 die(_("No annotated tags can describe '%s'.\n" 451 "However, there were unannotated tags: try --tags."), 452 oid_to_hex(cmit_oid)); 453 else 454 die(_("No tags can describe '%s'.\n" 455 "Try --always, or create some tags."), 456 oid_to_hex(cmit_oid)); 457 } 458 459 QSORT(all_matches, match_cnt, compare_pt); 460 461 if (gave_up_on) { 462 prio_queue_put(&queue, gave_up_on); 463 seen_commits--; 464 } 465 seen_commits += finish_depth_computation(&queue, &all_matches[0]); 466 clear_prio_queue(&queue); 467 468 if (debug) { 469 static int label_width = -1; 470 if (label_width < 0) { 471 int i, w; 472 for (i = 0; i < ARRAY_SIZE(prio_names); i++) { 473 w = strlen(_(prio_names[i])); 474 if (label_width < w) 475 label_width = w; 476 } 477 } 478 for (cur_match = 0; cur_match < match_cnt; cur_match++) { 479 struct possible_tag *t = &all_matches[cur_match]; 480 fprintf(stderr, " %-*s %8d %s\n", 481 label_width, _(prio_names[t->name->prio]), 482 t->depth, t->name->path); 483 } 484 fprintf(stderr, _("traversed %lu commits\n"), seen_commits); 485 if (gave_up_on) { 486 fprintf(stderr, 487 _("found %i tags; gave up search at %s\n"), 488 max_candidates, 489 oid_to_hex(&gave_up_on->object.oid)); 490 } 491 } 492 493 append_name(all_matches[0].name, dst); 494 if (all_matches[0].name->misnamed || abbrev) 495 append_suffix(all_matches[0].depth, &cmit->object.oid, dst); 496 if (suffix) 497 strbuf_addstr(dst, suffix); 498 } 499 500 struct process_commit_data { 501 struct commit *current_commit; 502 const struct object_id *looking_for; 503 struct strbuf *dst; 504 struct rev_info *revs; 505 }; 506 507 static void process_commit(struct commit *commit, void *data) 508 { 509 struct process_commit_data *pcd = data; 510 pcd->current_commit = commit; 511 } 512 513 static void process_object(struct object *obj, const char *path, void *data) 514 { 515 struct process_commit_data *pcd = data; 516 517 if (oideq(pcd->looking_for, &obj->oid) && !pcd->dst->len) { 518 reset_revision_walk(); 519 if (pcd->current_commit) { 520 describe_commit(pcd->current_commit, pcd->dst); 521 strbuf_addf(pcd->dst, ":%s", path); 522 } 523 commit_list_free(pcd->revs->commits); 524 pcd->revs->commits = NULL; 525 } 526 } 527 528 static void describe_blob(const struct object_id *oid, struct strbuf *dst) 529 { 530 struct rev_info revs; 531 struct strvec args = STRVEC_INIT; 532 struct object_id head_oid; 533 struct process_commit_data pcd = { NULL, oid, dst, &revs}; 534 535 if (repo_get_oid(the_repository, "HEAD", &head_oid)) 536 die(_("cannot search for blob '%s' on an unborn branch"), 537 oid_to_hex(oid)); 538 539 strvec_pushl(&args, "internal: The first arg is not parsed", 540 "--objects", "--in-commit-order", "--reverse", 541 oid_to_hex(&head_oid), 542 NULL); 543 544 repo_init_revisions(the_repository, &revs, NULL); 545 setup_revisions_from_strvec(&args, &revs, NULL); 546 if (args.nr > 1) 547 BUG("setup_revisions could not handle all args?"); 548 549 if (prepare_revision_walk(&revs)) 550 die("revision walk setup failed"); 551 552 traverse_commit_list(&revs, process_commit, process_object, &pcd); 553 reset_revision_walk(); 554 release_revisions(&revs); 555 strvec_clear(&args); 556 557 if (!dst->len) 558 die(_("blob '%s' not reachable from HEAD"), oid_to_hex(oid)); 559 } 560 561 static void describe(const char *arg, int last_one) 562 { 563 struct object_id oid; 564 struct commit *cmit; 565 struct strbuf sb = STRBUF_INIT; 566 567 if (debug) 568 fprintf(stderr, _("describe %s\n"), arg); 569 570 if (repo_get_oid(the_repository, arg, &oid)) 571 die(_("Not a valid object name %s"), arg); 572 cmit = lookup_commit_reference_gently(the_repository, &oid, 1); 573 574 if (cmit) 575 describe_commit(cmit, &sb); 576 else if (odb_read_object_info(the_repository->objects, 577 &oid, NULL) == OBJ_BLOB) 578 describe_blob(&oid, &sb); 579 else 580 die(_("%s is neither a commit nor blob"), arg); 581 582 puts(sb.buf); 583 584 if (!last_one) 585 clear_commit_marks(cmit, -1); 586 587 strbuf_release(&sb); 588 } 589 590 static int option_parse_exact_match(const struct option *opt, const char *arg, 591 int unset) 592 { 593 int *val = opt->value; 594 595 BUG_ON_OPT_ARG(arg); 596 597 *val = unset ? DEFAULT_CANDIDATES : 0; 598 return 0; 599 } 600 601 int cmd_describe(int argc, 602 const char **argv, 603 const char *prefix, 604 struct repository *repo UNUSED ) 605 { 606 struct refs_for_each_ref_options for_each_ref_opts = { 607 .flags = REFS_FOR_EACH_INCLUDE_BROKEN, 608 }; 609 int contains = 0; 610 struct option options[] = { 611 OPT_BOOL(0, "contains", &contains, N_("find the tag that comes after the commit")), 612 OPT_BOOL(0, "debug", &debug, N_("debug search strategy on stderr")), 613 OPT_BOOL(0, "all", &all, N_("use any ref")), 614 OPT_BOOL(0, "tags", &tags, N_("use any tag, even unannotated")), 615 OPT_BOOL(0, "long", &longformat, N_("always use long format")), 616 OPT_BOOL(0, "first-parent", &first_parent, N_("only follow first parent")), 617 OPT__ABBREV(&abbrev), 618 OPT_CALLBACK_F(0, "exact-match", &max_candidates, NULL, 619 N_("only output exact matches"), 620 PARSE_OPT_NOARG, option_parse_exact_match), 621 OPT_INTEGER(0, "candidates", &max_candidates, 622 N_("consider most recent tags (default: 10)")), 623 OPT_STRING_LIST(0, "match", &patterns, N_("pattern"), 624 N_("only consider tags matching ")), 625 OPT_STRING_LIST(0, "exclude", &exclude_patterns, N_("pattern"), 626 N_("do not consider tags matching ")), 627 OPT_BOOL(0, "always", &always, 628 N_("show abbreviated commit object as fallback")), 629 { 630 .type = OPTION_STRING, 631 .long_name = "dirty", 632 .value = &dirty, 633 .argh = N_("mark"), 634 .help = N_("append on dirty working tree (default: \"-dirty\")"), 635 .flags = PARSE_OPT_OPTARG, 636 .defval = (intptr_t) "-dirty", 637 }, 638 { 639 .type = OPTION_STRING, 640 .long_name = "broken", 641 .value = &broken, 642 .argh = N_("mark"), 643 .help = N_("append on broken working tree (default: \"-broken\")"), 644 .flags = PARSE_OPT_OPTARG, 645 .defval = (intptr_t) "-broken", 646 }, 647 OPT_END(), 648 }; 649 650 repo_config(the_repository, git_default_config, NULL); 651 argc = parse_options(argc, argv, prefix, options, describe_usage, 0); 652 if (abbrev < 0) 653 abbrev = DEFAULT_ABBREV; 654 655 if (max_candidates < 0) 656 max_candidates = 0; 657 else if (max_candidates > MAX_TAGS) 658 max_candidates = MAX_TAGS; 659 660 save_commit_buffer = 0; 661 662 if (longformat && abbrev == 0) 663 die(_("options '%s' and '%s' cannot be used together"), "--long", "--abbrev=0"); 664 665 if (contains) { 666 struct string_list_item *item; 667 struct strvec args; 668 const char **argv_copy; 669 int ret; 670 671 strvec_init(&args); 672 strvec_pushl(&args, "name-rev", 673 "--peel-tag", "--name-only", "--no-undefined", 674 NULL); 675 if (always) 676 strvec_push(&args, "--always"); 677 if (!all) 678 strvec_push(&args, "--tags"); 679 680 for_each_string_list_item(item, &patterns) 681 strvec_pushf(&args, "--refs=refs/tags/%s", item->string); 682 for_each_string_list_item(item, &exclude_patterns) 683 strvec_pushf(&args, "--exclude=refs/tags/%s", item->string); 684 685 if (all) { 686 for_each_string_list_item(item, &patterns) 687 strvec_pushf(&args, "--refs=refs/heads/%s", item->string); 688 for_each_string_list_item(item, &exclude_patterns) 689 strvec_pushf(&args, "--exclude=refs/heads/%s", item->string); 690 for_each_string_list_item(item, &patterns) 691 strvec_pushf(&args, "--refs=refs/remotes/%s", item->string); 692 for_each_string_list_item(item, &exclude_patterns) 693 strvec_pushf(&args, "--exclude=refs/remotes/%s", item->string); 694 } 695 696 if (argc) 697 strvec_pushv(&args, argv); 698 else 699 strvec_push(&args, "HEAD"); 700 701 /* 702 * `cmd_name_rev()` modifies the array, so we'd leak its 703 * contained strings if we didn't do a copy here. 704 */ 705 ALLOC_ARRAY(argv_copy, args.nr + 1); 706 for (size_t i = 0; i < args.nr; i++) 707 argv_copy[i] = args.v[i]; 708 argv_copy[args.nr] = NULL; 709 710 ret = cmd_name_rev(args.nr, argv_copy, prefix, the_repository); 711 712 strvec_clear(&args); 713 free(argv_copy); 714 return ret; 715 } 716 717 if (!all) 718 for_each_ref_opts.prefix = "refs/tags/"; 719 720 hashmap_init(&names, commit_name_neq, NULL, 0); 721 refs_for_each_ref_ext(get_main_ref_store(the_repository), 722 get_name, NULL, &for_each_ref_opts); 723 if (!hashmap_get_size(&names) && !always) 724 die(_("No names found, cannot describe anything.")); 725 726 if (argc == 0) { 727 if (broken) { 728 struct child_process cp = CHILD_PROCESS_INIT; 729 730 strvec_pushv(&cp.args, update_index_args); 731 cp.git_cmd = 1; 732 cp.no_stdin = 1; 733 cp.no_stdout = 1; 734 run_command(&cp); 735 736 child_process_init(&cp); 737 strvec_pushv(&cp.args, diff_index_args); 738 cp.git_cmd = 1; 739 cp.no_stdin = 1; 740 cp.no_stdout = 1; 741 742 if (!dirty) 743 dirty = "-dirty"; 744 745 switch (run_command(&cp)) { 746 case 0: 747 suffix = NULL; 748 break; 749 case 1: 750 suffix = dirty; 751 break; 752 default: 753 /* diff-index aborted abnormally */ 754 suffix = broken; 755 } 756 } else if (dirty) { 757 struct lock_file index_lock = LOCK_INIT; 758 struct rev_info revs; 759 int fd; 760 761 setup_work_tree(the_repository); 762 prepare_repo_settings(the_repository); 763 the_repository->settings.command_requires_full_index = 0; 764 repo_read_index(the_repository); 765 refresh_index(the_repository->index, REFRESH_QUIET|REFRESH_UNMERGED, 766 NULL, NULL, NULL); 767 fd = repo_hold_locked_index(the_repository, 768 &index_lock, 0); 769 if (0 <= fd) 770 repo_update_index_if_able(the_repository, &index_lock); 771 772 repo_init_revisions(the_repository, &revs, prefix); 773 774 if (setup_revisions(ARRAY_SIZE(diff_index_args) - 1, 775 diff_index_args, &revs, NULL) != 1) 776 BUG("malformed internal diff-index command line"); 777 run_diff_index(&revs, 0); 778 779 if (!diff_result_code(&revs)) 780 suffix = NULL; 781 else 782 suffix = dirty; 783 release_revisions(&revs); 784 } 785 describe("HEAD", 1); 786 } else if (dirty) { 787 die(_("option '%s' and commit-ishes cannot be used together"), "--dirty"); 788 } else if (broken) { 789 die(_("option '%s' and commit-ishes cannot be used together"), "--broken"); 790 } else { 791 while (argc-- > 0) 792 describe(*argv++, argc == 0); 793 } 794 return 0; 795 }