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/rev-parse.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 / rev-parse.c` 1203 lines ยท 29443 bytes Raw Blame History 1 /* 2 * rev-parse.c 3 * 4 * Copyright (C) Linus Torvalds, 2005 5 */ 6 7 #define USE_THE_REPOSITORY_VARIABLE 8 #define DISABLE_SIGN_COMPARE_WARNINGS 9 10 #include "builtin.h" 11 12 #include "abspath.h" 13 #include "bisect.h" 14 #include "config.h" 15 #include "commit.h" 16 #include "environment.h" 17 #include "gettext.h" 18 #include "hash.h" 19 #include "hex.h" 20 #include "odb.h" 21 #include "refs.h" 22 #include "quote.h" 23 #include "object-name.h" 24 #include "parse-options.h" 25 #include "path.h" 26 #include "diff.h" 27 #include "read-cache-ll.h" 28 #include "repo-settings.h" 29 #include "repository.h" 30 #include "revision.h" 31 #include "setup.h" 32 #include "split-index.h" 33 #include "submodule.h" 34 #include "commit-reach.h" 35 #include "shallow.h" 36 #include "object-file-convert.h" 37 38 #define DO_REVS 1 39 #define DO_NOREV 2 40 #define DO_FLAGS 4 41 #define DO_NONFLAGS 8 42 static int filter = ~0; 43 44 static const char *def; 45 46 #define NORMAL 0 47 #define REVERSED 1 48 static int show_type = NORMAL; 49 50 #define SHOW_SYMBOLIC_ASIS 1 51 #define SHOW_SYMBOLIC_FULL 2 52 static int symbolic; 53 static int abbrev; 54 static int abbrev_ref; 55 static int abbrev_ref_strict; 56 static int output_sq; 57 58 static int stuck_long; 59 static struct ref_exclusions ref_excludes = REF_EXCLUSIONS_INIT; 60 61 /* 62 * Some arguments are relevant "revision" arguments, 63 * others are about output format or other details. 64 * This sorts it all out. 65 */ 66 static int is_rev_argument(const char *arg) 67 { 68 static const char *rev_args[] = { 69 "--all", 70 "--bisect", 71 "--dense", 72 "--branches=", 73 "--branches", 74 "--header", 75 "--ignore-missing", 76 "--max-age=", 77 "--max-count=", 78 "--min-age=", 79 "--no-merges", 80 "--min-parents=", 81 "--no-min-parents", 82 "--max-parents=", 83 "--no-max-parents", 84 "--objects", 85 "--objects-edge", 86 "--parents", 87 "--pretty", 88 "--remotes=", 89 "--remotes", 90 "--glob=", 91 "--sparse", 92 "--tags=", 93 "--tags", 94 "--topo-order", 95 "--date-order", 96 "--unpacked", 97 NULL 98 }; 99 const char **p = rev_args; 100 101 /* accept -, like traditional "head" */ 102 if ((*arg == '-') && isdigit(arg[1])) 103 return 1; 104 105 for (;;) { 106 const char *str = *p++; 107 int len; 108 if (!str) 109 return 0; 110 len = strlen(str); 111 if (!strcmp(arg, str) || 112 (str[len-1] == '=' && !strncmp(arg, str, len))) 113 return 1; 114 } 115 } 116 117 /* Output argument as a string, either SQ or normal */ 118 static void show(const char *arg) 119 { 120 if (output_sq) { 121 int sq = '\'', ch; 122 123 putchar(sq); 124 while ((ch = *arg++)) { 125 if (ch == sq) 126 fputs("'\\'", stdout); 127 putchar(ch); 128 } 129 putchar(sq); 130 putchar(' '); 131 } 132 else 133 puts(arg); 134 } 135 136 /* Like show(), but with a negation prefix according to type */ 137 static void show_with_type(int type, const char *arg) 138 { 139 if (type != show_type) 140 putchar('^'); 141 show(arg); 142 } 143 144 /* Output a revision, only if filter allows it */ 145 static void show_rev(int type, const struct object_id *oid, const char *name) 146 { 147 if (!(filter & DO_REVS)) 148 return; 149 def = NULL; 150 151 if ((symbolic || abbrev_ref) && name) { 152 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) { 153 struct object_id discard; 154 char *full; 155 156 switch (repo_dwim_ref(the_repository, name, 157 strlen(name), &discard, &full, 158 0)) { 159 case 0: 160 /* 161 * Not found -- not a ref. We could 162 * emit "name" here, but symbolic-full 163 * users are interested in finding the 164 * refs spelled in full, and they would 165 * need to filter non-refs if we did so. 166 */ 167 break; 168 case 1: /* happy */ 169 if (abbrev_ref) { 170 char *old = full; 171 full = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), 172 full, 173 abbrev_ref_strict); 174 free(old); 175 } 176 show_with_type(type, full); 177 break; 178 default: /* ambiguous */ 179 error("refname '%s' is ambiguous", name); 180 break; 181 } 182 free(full); 183 } else { 184 show_with_type(type, name); 185 } 186 } 187 else if (abbrev) 188 show_with_type(type, 189 repo_find_unique_abbrev(the_repository, oid, abbrev)); 190 else 191 show_with_type(type, oid_to_hex(oid)); 192 } 193 194 /* Output a flag, only if filter allows it. */ 195 static int show_flag(const char *arg) 196 { 197 if (!(filter & DO_FLAGS)) 198 return 0; 199 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) { 200 show(arg); 201 return 1; 202 } 203 return 0; 204 } 205 206 static int show_default(void) 207 { 208 const char *s = def; 209 210 if (s) { 211 struct object_id oid; 212 213 def = NULL; 214 if (!repo_get_oid(the_repository, s, &oid)) { 215 show_rev(NORMAL, &oid, s); 216 return 1; 217 } 218 } 219 return 0; 220 } 221 222 static int show_reference(const struct reference *ref, void *cb_data UNUSED) 223 { 224 if (ref_excluded(&ref_excludes, ref->name)) 225 return 0; 226 show_rev(NORMAL, ref->oid, ref->name); 227 return 0; 228 } 229 230 static int anti_reference(const struct reference *ref, void *cb_data UNUSED) 231 { 232 show_rev(REVERSED, ref->oid, ref->name); 233 return 0; 234 } 235 236 static int show_abbrev(const struct object_id *oid, void *cb_data UNUSED) 237 { 238 show_rev(NORMAL, oid, NULL); 239 return 0; 240 } 241 242 static void show_datestring(const char *flag, const char *datestr) 243 { 244 char *buffer; 245 246 /* date handling requires both flags and revs */ 247 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS)) 248 return; 249 buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr)); 250 show(buffer); 251 free(buffer); 252 } 253 254 static int show_file(const char *arg, int output_prefix) 255 { 256 show_default(); 257 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) { 258 if (output_prefix) { 259 const char *prefix = the_repository->prefix; 260 char *fname = prefix_filename(prefix, arg); 261 show(fname); 262 free(fname); 263 } else 264 show(arg); 265 return 1; 266 } 267 return 0; 268 } 269 270 static int try_difference(const char *arg) 271 { 272 const char *dotdot; 273 struct object_id start_oid; 274 struct object_id end_oid; 275 const char *end; 276 const char *start; 277 char *to_free; 278 int symmetric; 279 static const char head_by_default[] = "HEAD"; 280 281 if (!(dotdot = strstr(arg, ".."))) 282 return 0; 283 start = to_free = xmemdupz(arg, dotdot - arg); 284 end = dotdot + 2; 285 symmetric = (*end == '.'); 286 end += symmetric; 287 288 if (!*end) 289 end = head_by_default; 290 if (dotdot == arg) 291 start = head_by_default; 292 293 if (start == head_by_default && end == head_by_default && 294 !symmetric) { 295 /* 296 * Just ".."? That is not a range but the 297 * pathspec for the parent directory. 298 */ 299 free(to_free); 300 return 0; 301 } 302 303 if (!repo_get_oid_committish(the_repository, start, &start_oid) && !repo_get_oid_committish(the_repository, end, &end_oid)) { 304 show_rev(NORMAL, &end_oid, end); 305 show_rev(symmetric ? NORMAL : REVERSED, &start_oid, start); 306 if (symmetric) { 307 struct commit_list *exclude = NULL; 308 struct commit *a, *b; 309 a = lookup_commit_reference(the_repository, &start_oid); 310 b = lookup_commit_reference(the_repository, &end_oid); 311 if (!a || !b) { 312 free(to_free); 313 return 0; 314 } 315 if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0) 316 exit(128); 317 while (exclude) { 318 struct commit *commit = pop_commit(&exclude); 319 show_rev(REVERSED, &commit->object.oid, NULL); 320 } 321 } 322 free(to_free); 323 return 1; 324 } 325 free(to_free); 326 return 0; 327 } 328 329 static int try_parent_shorthands(const char *arg) 330 { 331 const char *mark; 332 struct object_id oid; 333 struct commit *commit; 334 struct commit_list *parents; 335 int parent_number; 336 int include_rev = 0; 337 int include_parents = 0; 338 int exclude_parent = 0; 339 char *to_free; 340 341 if ((mark = strstr(arg, "^!"))) { 342 include_rev = 1; 343 if (mark[2]) 344 return 0; 345 } else if ((mark = strstr(arg, "^@"))) { 346 include_parents = 1; 347 if (mark[2]) 348 return 0; 349 } else if ((mark = strstr(arg, "^-"))) { 350 include_rev = 1; 351 exclude_parent = 1; 352 353 if (mark[2]) { 354 char *end; 355 exclude_parent = strtoul(mark + 2, &end, 10); 356 if (*end != '\0' || !exclude_parent) 357 return 0; 358 } 359 } else 360 return 0; 361 362 arg = to_free = xmemdupz(arg, mark - arg); 363 if (repo_get_oid_committish(the_repository, arg, &oid) || 364 !(commit = lookup_commit_reference(the_repository, &oid))) { 365 free(to_free); 366 return 0; 367 } 368 369 if (exclude_parent && 370 exclude_parent > commit_list_count(commit->parents)) { 371 free(to_free); 372 return 0; 373 } 374 375 if (include_rev) 376 show_rev(NORMAL, &oid, arg); 377 for (parents = commit->parents, parent_number = 1; 378 parents; 379 parents = parents->next, parent_number++) { 380 char *name = NULL; 381 382 if (exclude_parent && parent_number != exclude_parent) 383 continue; 384 385 if (symbolic) 386 name = xstrfmt("%s^%d", arg, parent_number); 387 show_rev(include_parents ? NORMAL : REVERSED, 388 &parents->item->object.oid, name); 389 free(name); 390 } 391 392 free(to_free); 393 return 1; 394 } 395 396 static int parseopt_dump(const struct option *o, const char *arg, int unset) 397 { 398 struct strbuf *parsed = o->value; 399 if (unset) 400 strbuf_addf(parsed, " --no-%s", o->long_name); 401 else if (o->short_name && (o->long_name == NULL || !stuck_long)) 402 strbuf_addf(parsed, " -%c", o->short_name); 403 else 404 strbuf_addf(parsed, " --%s", o->long_name); 405 if (arg) { 406 if (!stuck_long) 407 strbuf_addch(parsed, ' '); 408 else if (o->long_name) 409 strbuf_addch(parsed, '='); 410 sq_quote_buf(parsed, arg); 411 } 412 return 0; 413 } 414 415 static const char *skipspaces(const char *s) 416 { 417 while (isspace(*s)) 418 s++; 419 return s; 420 } 421 422 static char *findspace(const char *s) 423 { 424 for (; *s; s++) 425 if (isspace(*s)) 426 return (char*)s; 427 return NULL; 428 } 429 430 static int cmd_parseopt(int argc, const char **argv, const char *prefix) 431 { 432 int keep_dashdash = 0, stop_at_non_option = 0; 433 char const * const parseopt_usage[] = { 434 N_("git rev-parse --parseopt [] -- [...]"), 435 NULL 436 }; 437 struct option parseopt_opts[] = { 438 OPT_BOOL(0, "keep-dashdash", &keep_dashdash, 439 N_("keep the `--` passed as an arg")), 440 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option, 441 N_("stop parsing after the " 442 "first non-option argument")), 443 OPT_BOOL(0, "stuck-long", &stuck_long, 444 N_("output in stuck long form")), 445 OPT_END(), 446 }; 447 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT; 448 struct strvec longnames = STRVEC_INIT; 449 struct strvec usage = STRVEC_INIT; 450 struct option *opts = NULL; 451 size_t opts_nr = 0, opts_alloc = 0; 452 453 strbuf_addstr(&parsed, "set --"); 454 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage, 455 PARSE_OPT_KEEP_DASHDASH); 456 if (argc < 1 || strcmp(argv[0], "--")) 457 usage_with_options(parseopt_usage, parseopt_opts); 458 459 /* get the usage up to the first line with a -- on it */ 460 for (;;) { 461 strbuf_reset(&sb); 462 if (strbuf_getline(&sb, stdin) == EOF) 463 die(_("premature end of input")); 464 if (!strcmp("--", sb.buf)) { 465 if (!usage.nr) 466 die(_("no usage string given before the `--' separator")); 467 break; 468 } 469 470 strvec_push(&usage, sb.buf); 471 } 472 473 /* parse: (|,|)[*=?!]*? SP+ */ 474 while (strbuf_getline(&sb, stdin) != EOF) { 475 const char *s; 476 char *help; 477 struct option *o; 478 479 if (!sb.len) 480 continue; 481 482 ALLOC_GROW(opts, opts_nr + 1, opts_alloc); 483 memset(opts + opts_nr, 0, sizeof(*opts)); 484 485 o = &opts[opts_nr++]; 486 help = findspace(sb.buf); 487 if (!help || sb.buf == help) { 488 o->type = OPTION_GROUP; 489 o->help = xstrdup(skipspaces(sb.buf)); 490 continue; 491 } 492 493 *help = '\0'; 494 495 o->type = OPTION_CALLBACK; 496 o->help = xstrdup(skipspaces(help+1)); 497 o->value = &parsed; 498 o->flags = PARSE_OPT_NOARG; 499 o->callback = &parseopt_dump; 500 501 /* name(s) */ 502 s = strpbrk(sb.buf, "*=?!"); 503 if (!s) 504 s = help; 505 506 if (s == sb.buf) 507 die(_("missing opt-spec before option flags")); 508 509 if (s - sb.buf == 1) { /* short option only */ 510 o->short_name = *sb.buf; 511 } else if (sb.buf[1] != ',') { /* long option only */ 512 o->long_name = strvec_pushf(&longnames, "%.*s", 513 (int)(s - sb.buf), sb.buf); 514 } else { 515 o->short_name = *sb.buf; 516 o->long_name = strvec_pushf(&longnames, "%.*s", 517 (int)(s - sb.buf - 2), sb.buf + 2); 518 } 519 520 /* flags */ 521 while (s < help) { 522 switch (*s++) { 523 case '=': 524 o->flags &= ~PARSE_OPT_NOARG; 525 continue; 526 case '?': 527 o->flags &= ~PARSE_OPT_NOARG; 528 o->flags |= PARSE_OPT_OPTARG; 529 continue; 530 case '!': 531 o->flags |= PARSE_OPT_NONEG; 532 continue; 533 case '*': 534 o->flags |= PARSE_OPT_HIDDEN; 535 continue; 536 } 537 s--; 538 break; 539 } 540 541 if (s < help) 542 o->argh = xmemdupz(s, help - s); 543 } 544 strbuf_release(&sb); 545 546 /* put an OPT_END() */ 547 ALLOC_GROW(opts, opts_nr + 1, opts_alloc); 548 memset(opts + opts_nr, 0, sizeof(*opts)); 549 argc = parse_options(argc, argv, prefix, opts, usage.v, 550 (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) | 551 (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) | 552 PARSE_OPT_SHELL_EVAL); 553 554 strbuf_addstr(&parsed, " --"); 555 sq_quote_argv(&parsed, argv); 556 puts(parsed.buf); 557 558 strbuf_release(&parsed); 559 strbuf_release(&sb); 560 strvec_clear(&longnames); 561 strvec_clear(&usage); 562 for (size_t i = 0; i < opts_nr; i++) { 563 free((char *) opts[i].help); 564 free((char *) opts[i].argh); 565 } 566 free(opts); 567 return 0; 568 } 569 570 static int cmd_sq_quote(int argc, const char **argv) 571 { 572 struct strbuf buf = STRBUF_INIT; 573 574 if (argc) 575 sq_quote_argv(&buf, argv); 576 printf("%s\n", buf.buf); 577 strbuf_release(&buf); 578 579 return 0; 580 } 581 582 static void die_no_single_rev(int quiet) 583 { 584 if (quiet) 585 exit(1); 586 else 587 die(_("Needed a single revision")); 588 } 589 590 static const char builtin_rev_parse_usage[] = 591 N_("git rev-parse --parseopt [] -- [...]\n" 592 " or: git rev-parse --sq-quote [...]\n" 593 " or: git rev-parse [] [...]\n" 594 "\n" 595 "Run \"git rev-parse --parseopt -h\" for more information on the first usage."); 596 597 /* 598 * Parse "opt" or "opt=", setting value respectively to either 599 * NULL or the string after "=". 600 */ 601 static int opt_with_value(const char *arg, const char *opt, const char **value) 602 { 603 if (skip_prefix(arg, opt, &arg)) { 604 if (!*arg) { 605 *value = NULL; 606 return 1; 607 } 608 if (*arg++ == '=') { 609 *value = arg; 610 return 1; 611 } 612 } 613 return 0; 614 } 615 616 static void handle_ref_opt(const char *pattern, const char *prefix) 617 { 618 if (pattern) { 619 struct refs_for_each_ref_options opts = { 620 .pattern = pattern, 621 .prefix = prefix, 622 .trim_prefix = prefix ? strlen(prefix) : 0, 623 }; 624 refs_for_each_ref_ext(get_main_ref_store(the_repository), 625 show_reference, NULL, &opts); 626 } else { 627 struct refs_for_each_ref_options opts = { 628 .prefix = prefix, 629 .trim_prefix = strlen(prefix), 630 }; 631 refs_for_each_ref_ext(get_main_ref_store(the_repository), 632 show_reference, NULL, &opts); 633 } 634 clear_ref_exclusions(&ref_excludes); 635 } 636 637 enum format_type { 638 /* We would like a relative path. */ 639 FORMAT_RELATIVE, 640 /* We would like a canonical absolute path. */ 641 FORMAT_CANONICAL, 642 /* We would like the default behavior. */ 643 FORMAT_DEFAULT, 644 }; 645 646 enum default_type { 647 /* Our default is a relative path. */ 648 DEFAULT_RELATIVE, 649 /* Our default is a relative path if there's a shared root. */ 650 DEFAULT_RELATIVE_IF_SHARED, 651 /* Our default is a canonical absolute path. */ 652 DEFAULT_CANONICAL, 653 /* Our default is not to modify the item. */ 654 DEFAULT_UNMODIFIED, 655 }; 656 657 static void print_path(const char *path, const char *prefix, 658 enum format_type format, enum default_type def) 659 { 660 struct strbuf sb = STRBUF_INIT; 661 enum path_format fmt; 662 663 if (format == FORMAT_DEFAULT) { 664 switch (def) { 665 case DEFAULT_RELATIVE: 666 fmt = PATH_FORMAT_RELATIVE; 667 break; 668 case DEFAULT_RELATIVE_IF_SHARED: 669 fmt = PATH_FORMAT_RELATIVE_IF_SHARED; 670 break; 671 case DEFAULT_CANONICAL: 672 fmt = PATH_FORMAT_CANONICAL; 673 break; 674 case DEFAULT_UNMODIFIED: 675 default: 676 fmt = PATH_FORMAT_UNMODIFIED; 677 break; 678 } 679 } else { 680 switch (format) { 681 case FORMAT_RELATIVE: 682 fmt = PATH_FORMAT_RELATIVE; 683 break; 684 case FORMAT_CANONICAL: 685 fmt = PATH_FORMAT_CANONICAL; 686 break; 687 default: 688 fmt = PATH_FORMAT_UNMODIFIED; 689 break; 690 } 691 } 692 693 format_path(&sb, path, prefix, fmt); 694 puts(sb.buf); 695 696 strbuf_release(&sb); 697 } 698 699 int cmd_rev_parse(int argc, 700 const char **argv, 701 const char *prefix, 702 struct repository *repo UNUSED) 703 { 704 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0; 705 const struct git_hash_algo *output_algo = NULL; 706 const struct git_hash_algo *compat = NULL; 707 int did_repo_setup = 0; 708 int has_dashdash = 0; 709 int output_prefix = 0; 710 struct object_id oid; 711 unsigned int flags = 0; 712 const char *name = NULL; 713 struct strbuf buf = STRBUF_INIT; 714 int seen_end_of_options = 0; 715 enum format_type format = FORMAT_DEFAULT; 716 717 show_usage_if_asked(argc, argv, builtin_rev_parse_usage); 718 719 if (argc > 1 && !strcmp("--parseopt", argv[1])) 720 return cmd_parseopt(argc - 1, argv + 1, prefix); 721 722 if (argc > 1 && !strcmp("--sq-quote", argv[1])) 723 return cmd_sq_quote(argc - 2, argv + 2); 724 725 if (argc > 1 && !strcmp("-h", argv[1])) 726 usage(builtin_rev_parse_usage); 727 728 for (i = 1; i < argc; i++) { 729 if (!strcmp(argv[i], "--")) { 730 has_dashdash = 1; 731 break; 732 } 733 } 734 735 /* No options; just report on whether we're in a git repo or not. */ 736 if (argc == 1) { 737 setup_git_directory(the_repository); 738 repo_config(the_repository, git_default_config, NULL); 739 return 0; 740 } 741 742 for (i = 1; i < argc; i++) { 743 const char *arg = argv[i]; 744 745 if (as_is) { 746 if (show_file(arg, output_prefix) && as_is < 2) 747 verify_filename(the_repository, prefix, arg, 0); 748 continue; 749 } 750 751 if (!seen_end_of_options) { 752 if (!strcmp(arg, "--local-env-vars")) { 753 int i; 754 for (i = 0; local_repo_env[i]; i++) 755 printf("%s\n", local_repo_env[i]); 756 continue; 757 } 758 if (!strcmp(arg, "--resolve-git-dir")) { 759 const char *gitdir = argv[++i]; 760 if (!gitdir) 761 die(_("--resolve-git-dir requires an argument")); 762 gitdir = resolve_gitdir(gitdir); 763 if (!gitdir) 764 die(_("not a gitdir '%s'"), argv[i]); 765 puts(gitdir); 766 continue; 767 } 768 } 769 770 /* The rest of the options require a git repository. */ 771 if (!did_repo_setup) { 772 prefix = setup_git_directory(the_repository); 773 repo_config(the_repository, git_default_config, NULL); 774 did_repo_setup = 1; 775 776 prepare_repo_settings(the_repository); 777 the_repository->settings.command_requires_full_index = 0; 778 compat = the_repository->compat_hash_algo; 779 } 780 781 if (!strcmp(arg, "--")) { 782 as_is = 2; 783 /* Pass on the "--" if we show anything but files.. */ 784 if (filter & (DO_FLAGS | DO_REVS)) 785 show_file(arg, 0); 786 continue; 787 } 788 789 if (!seen_end_of_options && *arg == '-') { 790 if (!strcmp(arg, "--git-path")) { 791 if (!argv[i + 1]) 792 die(_("--git-path requires an argument")); 793 print_path(repo_git_path_replace(the_repository, &buf, 794 "%s", argv[i + 1]), prefix, 795 format, 796 DEFAULT_RELATIVE_IF_SHARED); 797 i++; 798 continue; 799 } 800 if (!strcmp(arg,"-n")) { 801 if (++i >= argc) 802 die(_("-n requires an argument")); 803 if ((filter & DO_FLAGS) && (filter & DO_REVS)) { 804 show(arg); 805 show(argv[i]); 806 } 807 continue; 808 } 809 if (starts_with(arg, "-n")) { 810 if ((filter & DO_FLAGS) && (filter & DO_REVS)) 811 show(arg); 812 continue; 813 } 814 if (opt_with_value(arg, "--path-format", &arg)) { 815 if (!arg) 816 die(_("--path-format requires an argument")); 817 if (!strcmp(arg, "absolute")) { 818 format = FORMAT_CANONICAL; 819 } else if (!strcmp(arg, "relative")) { 820 format = FORMAT_RELATIVE; 821 } else { 822 die(_("unknown argument to --path-format: %s"), arg); 823 } 824 continue; 825 } 826 if (!strcmp(arg, "--default")) { 827 def = argv[++i]; 828 if (!def) 829 die(_("--default requires an argument")); 830 continue; 831 } 832 if (!strcmp(arg, "--prefix")) { 833 prefix = argv[++i]; 834 if (!prefix) 835 die(_("--prefix requires an argument")); 836 FREE_AND_NULL(the_repository->prefix); 837 the_repository->prefix = xstrdup(prefix); 838 output_prefix = 1; 839 continue; 840 } 841 if (!strcmp(arg, "--revs-only")) { 842 filter &= ~DO_NOREV; 843 continue; 844 } 845 if (!strcmp(arg, "--no-revs")) { 846 filter &= ~DO_REVS; 847 continue; 848 } 849 if (!strcmp(arg, "--flags")) { 850 filter &= ~DO_NONFLAGS; 851 continue; 852 } 853 if (!strcmp(arg, "--no-flags")) { 854 filter &= ~DO_FLAGS; 855 continue; 856 } 857 if (!strcmp(arg, "--verify")) { 858 filter &= ~(DO_FLAGS|DO_NOREV); 859 verify = 1; 860 continue; 861 } 862 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) { 863 quiet = 1; 864 flags |= GET_OID_QUIETLY; 865 continue; 866 } 867 if (opt_with_value(arg, "--output-object-format", &arg)) { 868 if (!arg) 869 die(_("no object format specified")); 870 if (!strcmp(arg, the_hash_algo->name) || 871 !strcmp(arg, "storage")) { 872 flags |= GET_OID_HASH_ANY; 873 output_algo = the_hash_algo; 874 continue; 875 } 876 else if (compat && !strcmp(arg, compat->name)) { 877 flags |= GET_OID_HASH_ANY; 878 output_algo = compat; 879 continue; 880 } 881 else die(_("unsupported object format: %s"), arg); 882 } 883 if (opt_with_value(arg, "--short", &arg)) { 884 filter &= ~(DO_FLAGS|DO_NOREV); 885 verify = 1; 886 abbrev = DEFAULT_ABBREV; 887 if (!arg) 888 continue; 889 abbrev = strtoul(arg, NULL, 10); 890 if (abbrev < MINIMUM_ABBREV) 891 abbrev = MINIMUM_ABBREV; 892 else if ((int)the_hash_algo->hexsz <= abbrev) 893 abbrev = the_hash_algo->hexsz; 894 continue; 895 } 896 if (!strcmp(arg, "--sq")) { 897 output_sq = 1; 898 continue; 899 } 900 if (!strcmp(arg, "--not")) { 901 show_type ^= REVERSED; 902 continue; 903 } 904 if (!strcmp(arg, "--symbolic")) { 905 symbolic = SHOW_SYMBOLIC_ASIS; 906 continue; 907 } 908 if (!strcmp(arg, "--symbolic-full-name")) { 909 symbolic = SHOW_SYMBOLIC_FULL; 910 continue; 911 } 912 if (opt_with_value(arg, "--abbrev-ref", &arg)) { 913 abbrev_ref = 1; 914 abbrev_ref_strict = 915 repo_settings_get_warn_ambiguous_refs(the_repository); 916 if (arg) { 917 if (!strcmp(arg, "strict")) 918 abbrev_ref_strict = 1; 919 else if (!strcmp(arg, "loose")) 920 abbrev_ref_strict = 0; 921 else 922 die(_("unknown mode for --abbrev-ref: %s"), 923 arg); 924 } 925 continue; 926 } 927 if (!strcmp(arg, "--all")) { 928 refs_for_each_ref(get_main_ref_store(the_repository), 929 show_reference, NULL); 930 clear_ref_exclusions(&ref_excludes); 931 continue; 932 } 933 if (skip_prefix(arg, "--disambiguate=", &arg)) { 934 repo_for_each_abbrev(the_repository, arg, the_hash_algo, 935 show_abbrev, NULL); 936 continue; 937 } 938 if (!strcmp(arg, "--bisect")) { 939 char *prefix; 940 char *term_bad = NULL; 941 char *term_good = NULL; 942 struct refs_for_each_ref_options opts = { 0 }; 943 read_bisect_terms(&term_bad, &term_good); 944 prefix = xstrfmt("refs/bisect/%s", term_bad); 945 opts.prefix = prefix; 946 refs_for_each_ref_ext(get_main_ref_store(the_repository), 947 show_reference, NULL, &opts); 948 free(prefix); 949 prefix = xstrfmt("refs/bisect/%s", term_good); 950 opts.prefix = prefix; 951 refs_for_each_ref_ext(get_main_ref_store(the_repository), 952 anti_reference, NULL, &opts); 953 free(prefix); 954 free(term_good); 955 free(term_bad); 956 continue; 957 } 958 if (opt_with_value(arg, "--branches", &arg)) { 959 if (ref_excludes.hidden_refs_configured) 960 return error(_("options '%s' and '%s' cannot be used together"), 961 "--exclude-hidden", "--branches"); 962 handle_ref_opt(arg, "refs/heads/"); 963 continue; 964 } 965 if (opt_with_value(arg, "--tags", &arg)) { 966 if (ref_excludes.hidden_refs_configured) 967 return error(_("options '%s' and '%s' cannot be used together"), 968 "--exclude-hidden", "--tags"); 969 handle_ref_opt(arg, "refs/tags/"); 970 continue; 971 } 972 if (skip_prefix(arg, "--glob=", &arg)) { 973 handle_ref_opt(arg, NULL); 974 continue; 975 } 976 if (opt_with_value(arg, "--remotes", &arg)) { 977 if (ref_excludes.hidden_refs_configured) 978 return error(_("options '%s' and '%s' cannot be used together"), 979 "--exclude-hidden", "--remotes"); 980 handle_ref_opt(arg, "refs/remotes/"); 981 continue; 982 } 983 if (skip_prefix(arg, "--exclude=", &arg)) { 984 add_ref_exclusion(&ref_excludes, arg); 985 continue; 986 } 987 if (skip_prefix(arg, "--exclude-hidden=", &arg)) { 988 exclude_hidden_refs(&ref_excludes, arg); 989 continue; 990 } 991 if (!strcmp(arg, "--show-toplevel")) { 992 const char *work_tree = repo_get_work_tree(the_repository); 993 if (work_tree) 994 print_path(work_tree, prefix, format, DEFAULT_UNMODIFIED); 995 else 996 die(_("this operation must be run in a work tree")); 997 continue; 998 } 999 if (!strcmp(arg, "--show-superproject-working-tree")) { 1000 struct strbuf superproject = STRBUF_INIT; 1001 if (get_superproject_working_tree(&superproject)) 1002 print_path(superproject.buf, prefix, format, DEFAULT_UNMODIFIED); 1003 strbuf_release(&superproject); 1004 continue; 1005 } 1006 if (!strcmp(arg, "--show-prefix")) { 1007 if (prefix) 1008 puts(prefix); 1009 else 1010 putchar('\n'); 1011 continue; 1012 } 1013 if (!strcmp(arg, "--show-cdup")) { 1014 const char *pfx = prefix; 1015 if (!is_inside_work_tree(the_repository)) { 1016 const char *work_tree = 1017 repo_get_work_tree(the_repository); 1018 if (work_tree) 1019 printf("%s\n", work_tree); 1020 continue; 1021 } 1022 while (pfx) { 1023 pfx = strchr(pfx, '/'); 1024 if (pfx) { 1025 pfx++; 1026 printf("../"); 1027 } 1028 } 1029 putchar('\n'); 1030 continue; 1031 } 1032 if (!strcmp(arg, "--git-dir") || 1033 !strcmp(arg, "--absolute-git-dir")) { 1034 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT); 1035 char *cwd; 1036 int len; 1037 enum format_type wanted = format; 1038 if (arg[2] == 'g') { /* --git-dir */ 1039 if (gitdir) { 1040 print_path(gitdir, prefix, format, DEFAULT_UNMODIFIED); 1041 continue; 1042 } 1043 if (!prefix) { 1044 print_path(".git", prefix, format, DEFAULT_UNMODIFIED); 1045 continue; 1046 } 1047 } else { /* --absolute-git-dir */ 1048 wanted = FORMAT_CANONICAL; 1049 if (!gitdir && !prefix) 1050 gitdir = ".git"; 1051 if (gitdir) { 1052 struct strbuf realpath = STRBUF_INIT; 1053 strbuf_realpath(&realpath, gitdir, 1); 1054 puts(realpath.buf); 1055 strbuf_release(&realpath); 1056 continue; 1057 } 1058 } 1059 cwd = xgetcwd(); 1060 len = strlen(cwd); 1061 strbuf_reset(&buf); 1062 strbuf_addf(&buf, "%s%s.git", cwd, len && cwd[len-1] != '/' ? "/" : ""); 1063 free(cwd); 1064 print_path(buf.buf, prefix, wanted, DEFAULT_CANONICAL); 1065 continue; 1066 } 1067 if (!strcmp(arg, "--git-common-dir")) { 1068 print_path(repo_get_common_dir(the_repository), prefix, format, DEFAULT_RELATIVE_IF_SHARED); 1069 continue; 1070 } 1071 if (!strcmp(arg, "--is-inside-git-dir")) { 1072 printf("%s\n", is_inside_git_dir(the_repository) ? "true" 1073 : "false"); 1074 continue; 1075 } 1076 if (!strcmp(arg, "--is-inside-work-tree")) { 1077 printf("%s\n", is_inside_work_tree(the_repository) ? "true" 1078 : "false"); 1079 continue; 1080 } 1081 if (!strcmp(arg, "--is-bare-repository")) { 1082 printf("%s\n", is_bare_repository(the_repository) ? "true" 1083 : "false"); 1084 continue; 1085 } 1086 if (!strcmp(arg, "--is-shallow-repository")) { 1087 printf("%s\n", 1088 is_repository_shallow(the_repository) ? "true" 1089 : "false"); 1090 continue; 1091 } 1092 if (!strcmp(arg, "--shared-index-path")) { 1093 if (repo_read_index(the_repository) < 0) 1094 die(_("Could not read the index")); 1095 if (the_repository->index->split_index) { 1096 const struct object_id *oid = &the_repository->index->split_index->base_oid; 1097 const char *path = repo_git_path_replace(the_repository, &buf, "sharedindex.%s", oid_to_hex(oid)); 1098 print_path(path, prefix, format, DEFAULT_RELATIVE); 1099 } 1100 continue; 1101 } 1102 if (skip_prefix(arg, "--since=", &arg)) { 1103 show_datestring("--max-age=", arg); 1104 continue; 1105 } 1106 if (skip_prefix(arg, "--after=", &arg)) { 1107 show_datestring("--max-age=", arg); 1108 continue; 1109 } 1110 if (skip_prefix(arg, "--before=", &arg)) { 1111 show_datestring("--min-age=", arg); 1112 continue; 1113 } 1114 if (skip_prefix(arg, "--until=", &arg)) { 1115 show_datestring("--min-age=", arg); 1116 continue; 1117 } 1118 if (opt_with_value(arg, "--show-object-format", &arg)) { 1119 const char *val = arg ? arg : "storage"; 1120 1121 if (strcmp(val, "storage") && 1122 strcmp(val, "compat") && 1123 strcmp(val, "input") && 1124 strcmp(val, "output")) 1125 die(_("unknown mode for --show-object-format: %s"), 1126 arg); 1127 1128 if (!strcmp(val, "compat")) { 1129 if (the_repository->compat_hash_algo) 1130 puts(the_repository->compat_hash_algo->name); 1131 else 1132 putchar('\n'); 1133 } else { 1134 puts(the_hash_algo->name); 1135 } 1136 continue; 1137 } 1138 if (!strcmp(arg, "--show-ref-format") || !strcmp(arg, "--show-ref-storage-format")) { 1139 puts(ref_storage_format_to_name(the_repository->ref_storage_format)); 1140 continue; 1141 } 1142 if (!strcmp(arg, "--show-object-storage-format")) { 1143 const char *format = object_storage_format_to_name(the_repository->object_storage_format); 1144 if (the_repository->object_storage_payload) 1145 printf("%s://%s\n", format, the_repository->object_storage_payload); 1146 else 1147 puts(format); 1148 continue; 1149 } 1150 if (!strcmp(arg, "--end-of-options")) { 1151 seen_end_of_options = 1; 1152 if (filter & (DO_FLAGS | DO_REVS)) 1153 show_file(arg, 0); 1154 continue; 1155 } 1156 if (show_flag(arg) && verify) 1157 die_no_single_rev(quiet); 1158 continue; 1159 } 1160 1161 /* Not a flag argument */ 1162 if (try_difference(arg)) 1163 continue; 1164 if (try_parent_shorthands(arg)) 1165 continue; 1166 name = arg; 1167 type = NORMAL; 1168 if (*arg == '^') { 1169 name++; 1170 type = REVERSED; 1171 } 1172 if (!repo_get_oid_with_flags(the_repository, name, &oid, 1173 flags)) { 1174 if (output_algo) 1175 repo_oid_to_algop(the_repository, &oid, 1176 output_algo, &oid); 1177 if (verify) 1178 revs_count++; 1179 else 1180 show_rev(type, &oid, name); 1181 continue; 1182 } 1183 if (verify) 1184 die_no_single_rev(quiet); 1185 if (has_dashdash) 1186 die(_("bad revision '%s'"), arg); 1187 as_is = 1; 1188 if (!show_file(arg, output_prefix)) 1189 continue; 1190 verify_filename(the_repository, prefix, arg, 1); 1191 } 1192 strbuf_release(&buf); 1193 if (verify) { 1194 if (revs_count == 1) { 1195 show_rev(type, &oid, name); 1196 return 0; 1197 } else if (revs_count == 0 && show_default()) 1198 return 0; 1199 die_no_single_rev(quiet); 1200 } else 1201 show_default(); 1202 return 0; 1203 }