c8035331e21579df39c6a76aca8a8299c6b9b5df
   1/*
   2 * rev-parse.c
   3 *
   4 * Copyright (C) Linus Torvalds, 2005
   5 */
   6#include "cache.h"
   7#include "commit.h"
   8#include "refs.h"
   9#include "quote.h"
  10#include "builtin.h"
  11#include "parse-options.h"
  12#include "diff.h"
  13#include "revision.h"
  14#include "split-index.h"
  15#include "submodule.h"
  16
  17#define DO_REVS         1
  18#define DO_NOREV        2
  19#define DO_FLAGS        4
  20#define DO_NONFLAGS     8
  21static int filter = ~0;
  22
  23static const char *def;
  24
  25#define NORMAL 0
  26#define REVERSED 1
  27static int show_type = NORMAL;
  28
  29#define SHOW_SYMBOLIC_ASIS 1
  30#define SHOW_SYMBOLIC_FULL 2
  31static int symbolic;
  32static int abbrev;
  33static int abbrev_ref;
  34static int abbrev_ref_strict;
  35static int output_sq;
  36
  37static int stuck_long;
  38static struct string_list *ref_excludes;
  39
  40/*
  41 * Some arguments are relevant "revision" arguments,
  42 * others are about output format or other details.
  43 * This sorts it all out.
  44 */
  45static int is_rev_argument(const char *arg)
  46{
  47        static const char *rev_args[] = {
  48                "--all",
  49                "--bisect",
  50                "--dense",
  51                "--branches=",
  52                "--branches",
  53                "--header",
  54                "--ignore-missing",
  55                "--max-age=",
  56                "--max-count=",
  57                "--min-age=",
  58                "--no-merges",
  59                "--min-parents=",
  60                "--no-min-parents",
  61                "--max-parents=",
  62                "--no-max-parents",
  63                "--objects",
  64                "--objects-edge",
  65                "--parents",
  66                "--pretty",
  67                "--remotes=",
  68                "--remotes",
  69                "--glob=",
  70                "--sparse",
  71                "--tags=",
  72                "--tags",
  73                "--topo-order",
  74                "--date-order",
  75                "--unpacked",
  76                NULL
  77        };
  78        const char **p = rev_args;
  79
  80        /* accept -<digit>, like traditional "head" */
  81        if ((*arg == '-') && isdigit(arg[1]))
  82                return 1;
  83
  84        for (;;) {
  85                const char *str = *p++;
  86                int len;
  87                if (!str)
  88                        return 0;
  89                len = strlen(str);
  90                if (!strcmp(arg, str) ||
  91                    (str[len-1] == '=' && !strncmp(arg, str, len)))
  92                        return 1;
  93        }
  94}
  95
  96/* Output argument as a string, either SQ or normal */
  97static void show(const char *arg)
  98{
  99        if (output_sq) {
 100                int sq = '\'', ch;
 101
 102                putchar(sq);
 103                while ((ch = *arg++)) {
 104                        if (ch == sq)
 105                                fputs("'\\'", stdout);
 106                        putchar(ch);
 107                }
 108                putchar(sq);
 109                putchar(' ');
 110        }
 111        else
 112                puts(arg);
 113}
 114
 115/* Like show(), but with a negation prefix according to type */
 116static void show_with_type(int type, const char *arg)
 117{
 118        if (type != show_type)
 119                putchar('^');
 120        show(arg);
 121}
 122
 123/* Output a revision, only if filter allows it */
 124static void show_rev(int type, const unsigned char *sha1, const char *name)
 125{
 126        if (!(filter & DO_REVS))
 127                return;
 128        def = NULL;
 129
 130        if ((symbolic || abbrev_ref) && name) {
 131                if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
 132                        unsigned char discard[20];
 133                        char *full;
 134
 135                        switch (dwim_ref(name, strlen(name), discard, &full)) {
 136                        case 0:
 137                                /*
 138                                 * Not found -- not a ref.  We could
 139                                 * emit "name" here, but symbolic-full
 140                                 * users are interested in finding the
 141                                 * refs spelled in full, and they would
 142                                 * need to filter non-refs if we did so.
 143                                 */
 144                                break;
 145                        case 1: /* happy */
 146                                if (abbrev_ref)
 147                                        full = shorten_unambiguous_ref(full,
 148                                                abbrev_ref_strict);
 149                                show_with_type(type, full);
 150                                break;
 151                        default: /* ambiguous */
 152                                error("refname '%s' is ambiguous", name);
 153                                break;
 154                        }
 155                        free(full);
 156                } else {
 157                        show_with_type(type, name);
 158                }
 159        }
 160        else if (abbrev)
 161                show_with_type(type, find_unique_abbrev(sha1, abbrev));
 162        else
 163                show_with_type(type, sha1_to_hex(sha1));
 164}
 165
 166/* Output a flag, only if filter allows it. */
 167static int show_flag(const char *arg)
 168{
 169        if (!(filter & DO_FLAGS))
 170                return 0;
 171        if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
 172                show(arg);
 173                return 1;
 174        }
 175        return 0;
 176}
 177
 178static int show_default(void)
 179{
 180        const char *s = def;
 181
 182        if (s) {
 183                unsigned char sha1[20];
 184
 185                def = NULL;
 186                if (!get_sha1(s, sha1)) {
 187                        show_rev(NORMAL, sha1, s);
 188                        return 1;
 189                }
 190        }
 191        return 0;
 192}
 193
 194static int show_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
 195{
 196        if (ref_excluded(ref_excludes, refname))
 197                return 0;
 198        show_rev(NORMAL, oid->hash, refname);
 199        return 0;
 200}
 201
 202static int anti_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
 203{
 204        show_rev(REVERSED, oid->hash, refname);
 205        return 0;
 206}
 207
 208static int show_abbrev(const unsigned char *sha1, void *cb_data)
 209{
 210        show_rev(NORMAL, sha1, NULL);
 211        return 0;
 212}
 213
 214static void show_datestring(const char *flag, const char *datestr)
 215{
 216        static char buffer[100];
 217
 218        /* date handling requires both flags and revs */
 219        if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
 220                return;
 221        snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
 222        show(buffer);
 223}
 224
 225static int show_file(const char *arg, int output_prefix)
 226{
 227        show_default();
 228        if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
 229                if (output_prefix) {
 230                        const char *prefix = startup_info->prefix;
 231                        show(prefix_filename(prefix, arg));
 232                } else
 233                        show(arg);
 234                return 1;
 235        }
 236        return 0;
 237}
 238
 239static int try_difference(const char *arg)
 240{
 241        char *dotdot;
 242        unsigned char sha1[20];
 243        unsigned char end[20];
 244        const char *next;
 245        const char *this;
 246        int symmetric;
 247        static const char head_by_default[] = "HEAD";
 248
 249        if (!(dotdot = strstr(arg, "..")))
 250                return 0;
 251        next = dotdot + 2;
 252        this = arg;
 253        symmetric = (*next == '.');
 254
 255        *dotdot = 0;
 256        next += symmetric;
 257
 258        if (!*next)
 259                next = head_by_default;
 260        if (dotdot == arg)
 261                this = head_by_default;
 262
 263        if (this == head_by_default && next == head_by_default &&
 264            !symmetric) {
 265                /*
 266                 * Just ".."?  That is not a range but the
 267                 * pathspec for the parent directory.
 268                 */
 269                *dotdot = '.';
 270                return 0;
 271        }
 272
 273        if (!get_sha1_committish(this, sha1) && !get_sha1_committish(next, end)) {
 274                show_rev(NORMAL, end, next);
 275                show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
 276                if (symmetric) {
 277                        struct commit_list *exclude;
 278                        struct commit *a, *b;
 279                        a = lookup_commit_reference(sha1);
 280                        b = lookup_commit_reference(end);
 281                        exclude = get_merge_bases(a, b);
 282                        while (exclude) {
 283                                struct commit *commit = pop_commit(&exclude);
 284                                show_rev(REVERSED, commit->object.oid.hash, NULL);
 285                        }
 286                }
 287                *dotdot = '.';
 288                return 1;
 289        }
 290        *dotdot = '.';
 291        return 0;
 292}
 293
 294static int try_parent_shorthands(const char *arg)
 295{
 296        char *dotdot;
 297        unsigned char sha1[20];
 298        struct commit *commit;
 299        struct commit_list *parents;
 300        int parent_number;
 301        int include_rev = 0;
 302        int include_parents = 0;
 303        int exclude_parent = 0;
 304
 305        if ((dotdot = strstr(arg, "^!"))) {
 306                include_rev = 1;
 307                if (dotdot[2])
 308                        return 0;
 309        } else if ((dotdot = strstr(arg, "^@"))) {
 310                include_parents = 1;
 311                if (dotdot[2])
 312                        return 0;
 313        } else if ((dotdot = strstr(arg, "^-"))) {
 314                include_rev = 1;
 315                exclude_parent = 1;
 316
 317                if (dotdot[2]) {
 318                        char *end;
 319                        exclude_parent = strtoul(dotdot + 2, &end, 10);
 320                        if (*end != '\0' || !exclude_parent)
 321                                return 0;
 322                }
 323        } else
 324                return 0;
 325
 326        *dotdot = 0;
 327        if (get_sha1_committish(arg, sha1)) {
 328                *dotdot = '^';
 329                return 0;
 330        }
 331
 332        commit = lookup_commit_reference(sha1);
 333        if (exclude_parent &&
 334            exclude_parent > commit_list_count(commit->parents)) {
 335                *dotdot = '^';
 336                return 0;
 337        }
 338
 339        if (include_rev)
 340                show_rev(NORMAL, sha1, arg);
 341        for (parents = commit->parents, parent_number = 1;
 342             parents;
 343             parents = parents->next, parent_number++) {
 344                char *name = NULL;
 345
 346                if (exclude_parent && parent_number != exclude_parent)
 347                        continue;
 348
 349                if (symbolic)
 350                        name = xstrfmt("%s^%d", arg, parent_number);
 351                show_rev(include_parents ? NORMAL : REVERSED,
 352                         parents->item->object.oid.hash, name);
 353                free(name);
 354        }
 355
 356        *dotdot = '^';
 357        return 1;
 358}
 359
 360static int parseopt_dump(const struct option *o, const char *arg, int unset)
 361{
 362        struct strbuf *parsed = o->value;
 363        if (unset)
 364                strbuf_addf(parsed, " --no-%s", o->long_name);
 365        else if (o->short_name && (o->long_name == NULL || !stuck_long))
 366                strbuf_addf(parsed, " -%c", o->short_name);
 367        else
 368                strbuf_addf(parsed, " --%s", o->long_name);
 369        if (arg) {
 370                if (!stuck_long)
 371                        strbuf_addch(parsed, ' ');
 372                else if (o->long_name)
 373                        strbuf_addch(parsed, '=');
 374                sq_quote_buf(parsed, arg);
 375        }
 376        return 0;
 377}
 378
 379static const char *skipspaces(const char *s)
 380{
 381        while (isspace(*s))
 382                s++;
 383        return s;
 384}
 385
 386static int cmd_parseopt(int argc, const char **argv, const char *prefix)
 387{
 388        static int keep_dashdash = 0, stop_at_non_option = 0;
 389        static char const * const parseopt_usage[] = {
 390                N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
 391                NULL
 392        };
 393        static struct option parseopt_opts[] = {
 394                OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
 395                                        N_("keep the `--` passed as an arg")),
 396                OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
 397                                        N_("stop parsing after the "
 398                                           "first non-option argument")),
 399                OPT_BOOL(0, "stuck-long", &stuck_long,
 400                                        N_("output in stuck long form")),
 401                OPT_END(),
 402        };
 403        static const char * const flag_chars = "*=?!";
 404
 405        struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
 406        const char **usage = NULL;
 407        struct option *opts = NULL;
 408        int onb = 0, osz = 0, unb = 0, usz = 0;
 409
 410        strbuf_addstr(&parsed, "set --");
 411        argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
 412                             PARSE_OPT_KEEP_DASHDASH);
 413        if (argc < 1 || strcmp(argv[0], "--"))
 414                usage_with_options(parseopt_usage, parseopt_opts);
 415
 416        /* get the usage up to the first line with a -- on it */
 417        for (;;) {
 418                if (strbuf_getline(&sb, stdin) == EOF)
 419                        die("premature end of input");
 420                ALLOC_GROW(usage, unb + 1, usz);
 421                if (!strcmp("--", sb.buf)) {
 422                        if (unb < 1)
 423                                die("no usage string given before the `--' separator");
 424                        usage[unb] = NULL;
 425                        break;
 426                }
 427                usage[unb++] = strbuf_detach(&sb, NULL);
 428        }
 429
 430        /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
 431        while (strbuf_getline(&sb, stdin) != EOF) {
 432                const char *s;
 433                const char *help;
 434                struct option *o;
 435
 436                if (!sb.len)
 437                        continue;
 438
 439                ALLOC_GROW(opts, onb + 1, osz);
 440                memset(opts + onb, 0, sizeof(opts[onb]));
 441
 442                o = &opts[onb++];
 443                help = strchr(sb.buf, ' ');
 444                if (!help || *sb.buf == ' ') {
 445                        o->type = OPTION_GROUP;
 446                        o->help = xstrdup(skipspaces(sb.buf));
 447                        continue;
 448                }
 449
 450                o->type = OPTION_CALLBACK;
 451                o->help = xstrdup(skipspaces(help));
 452                o->value = &parsed;
 453                o->flags = PARSE_OPT_NOARG;
 454                o->callback = &parseopt_dump;
 455
 456                /* name(s) */
 457                s = strpbrk(sb.buf, flag_chars);
 458                if (s == NULL)
 459                        s = help;
 460
 461                if (s - sb.buf == 1) /* short option only */
 462                        o->short_name = *sb.buf;
 463                else if (sb.buf[1] != ',') /* long option only */
 464                        o->long_name = xmemdupz(sb.buf, s - sb.buf);
 465                else {
 466                        o->short_name = *sb.buf;
 467                        o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
 468                }
 469
 470                /* flags */
 471                while (s < help) {
 472                        switch (*s++) {
 473                        case '=':
 474                                o->flags &= ~PARSE_OPT_NOARG;
 475                                continue;
 476                        case '?':
 477                                o->flags &= ~PARSE_OPT_NOARG;
 478                                o->flags |= PARSE_OPT_OPTARG;
 479                                continue;
 480                        case '!':
 481                                o->flags |= PARSE_OPT_NONEG;
 482                                continue;
 483                        case '*':
 484                                o->flags |= PARSE_OPT_HIDDEN;
 485                                continue;
 486                        }
 487                        s--;
 488                        break;
 489                }
 490
 491                if (s < help)
 492                        o->argh = xmemdupz(s, help - s);
 493        }
 494        strbuf_release(&sb);
 495
 496        /* put an OPT_END() */
 497        ALLOC_GROW(opts, onb + 1, osz);
 498        memset(opts + onb, 0, sizeof(opts[onb]));
 499        argc = parse_options(argc, argv, prefix, opts, usage,
 500                        (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
 501                        (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
 502                        PARSE_OPT_SHELL_EVAL);
 503
 504        strbuf_addstr(&parsed, " --");
 505        sq_quote_argv(&parsed, argv, 0);
 506        puts(parsed.buf);
 507        return 0;
 508}
 509
 510static int cmd_sq_quote(int argc, const char **argv)
 511{
 512        struct strbuf buf = STRBUF_INIT;
 513
 514        if (argc)
 515                sq_quote_argv(&buf, argv, 0);
 516        printf("%s\n", buf.buf);
 517        strbuf_release(&buf);
 518
 519        return 0;
 520}
 521
 522static void die_no_single_rev(int quiet)
 523{
 524        if (quiet)
 525                exit(1);
 526        else
 527                die("Needed a single revision");
 528}
 529
 530static const char builtin_rev_parse_usage[] =
 531N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
 532   "   or: git rev-parse --sq-quote [<arg>...]\n"
 533   "   or: git rev-parse [<options>] [<arg>...]\n"
 534   "\n"
 535   "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
 536
 537int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 538{
 539        int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
 540        int did_repo_setup = 0;
 541        int has_dashdash = 0;
 542        int output_prefix = 0;
 543        unsigned char sha1[20];
 544        unsigned int flags = 0;
 545        const char *name = NULL;
 546        struct object_context unused;
 547        struct strbuf buf = STRBUF_INIT;
 548
 549        if (argc > 1 && !strcmp("--parseopt", argv[1]))
 550                return cmd_parseopt(argc - 1, argv + 1, prefix);
 551
 552        if (argc > 1 && !strcmp("--sq-quote", argv[1]))
 553                return cmd_sq_quote(argc - 2, argv + 2);
 554
 555        if (argc > 1 && !strcmp("-h", argv[1]))
 556                usage(builtin_rev_parse_usage);
 557
 558        for (i = 1; i < argc; i++) {
 559                if (!strcmp(argv[i], "--")) {
 560                        has_dashdash = 1;
 561                        break;
 562                }
 563        }
 564
 565        /* No options; just report on whether we're in a git repo or not. */
 566        if (argc == 1) {
 567                setup_git_directory();
 568                git_config(git_default_config, NULL);
 569                return 0;
 570        }
 571
 572        for (i = 1; i < argc; i++) {
 573                const char *arg = argv[i];
 574
 575                if (!strcmp(arg, "--local-env-vars")) {
 576                        int i;
 577                        for (i = 0; local_repo_env[i]; i++)
 578                                printf("%s\n", local_repo_env[i]);
 579                        continue;
 580                }
 581                if (!strcmp(arg, "--resolve-git-dir")) {
 582                        const char *gitdir = argv[++i];
 583                        if (!gitdir)
 584                                die("--resolve-git-dir requires an argument");
 585                        gitdir = resolve_gitdir(gitdir);
 586                        if (!gitdir)
 587                                die("not a gitdir '%s'", argv[i]);
 588                        puts(gitdir);
 589                        continue;
 590                }
 591
 592                /* The rest of the options require a git repository. */
 593                if (!did_repo_setup) {
 594                        prefix = setup_git_directory();
 595                        git_config(git_default_config, NULL);
 596                        did_repo_setup = 1;
 597                }
 598
 599                if (!strcmp(arg, "--git-path")) {
 600                        if (!argv[i + 1])
 601                                die("--git-path requires an argument");
 602                        strbuf_reset(&buf);
 603                        puts(relative_path(git_path("%s", argv[i + 1]),
 604                                           prefix, &buf));
 605                        i++;
 606                        continue;
 607                }
 608                if (as_is) {
 609                        if (show_file(arg, output_prefix) && as_is < 2)
 610                                verify_filename(prefix, arg, 0);
 611                        continue;
 612                }
 613                if (!strcmp(arg,"-n")) {
 614                        if (++i >= argc)
 615                                die("-n requires an argument");
 616                        if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
 617                                show(arg);
 618                                show(argv[i]);
 619                        }
 620                        continue;
 621                }
 622                if (starts_with(arg, "-n")) {
 623                        if ((filter & DO_FLAGS) && (filter & DO_REVS))
 624                                show(arg);
 625                        continue;
 626                }
 627
 628                if (*arg == '-') {
 629                        if (!strcmp(arg, "--")) {
 630                                as_is = 2;
 631                                /* Pass on the "--" if we show anything but files.. */
 632                                if (filter & (DO_FLAGS | DO_REVS))
 633                                        show_file(arg, 0);
 634                                continue;
 635                        }
 636                        if (!strcmp(arg, "--default")) {
 637                                def = argv[++i];
 638                                if (!def)
 639                                        die("--default requires an argument");
 640                                continue;
 641                        }
 642                        if (!strcmp(arg, "--prefix")) {
 643                                prefix = argv[++i];
 644                                if (!prefix)
 645                                        die("--prefix requires an argument");
 646                                startup_info->prefix = prefix;
 647                                output_prefix = 1;
 648                                continue;
 649                        }
 650                        if (!strcmp(arg, "--revs-only")) {
 651                                filter &= ~DO_NOREV;
 652                                continue;
 653                        }
 654                        if (!strcmp(arg, "--no-revs")) {
 655                                filter &= ~DO_REVS;
 656                                continue;
 657                        }
 658                        if (!strcmp(arg, "--flags")) {
 659                                filter &= ~DO_NONFLAGS;
 660                                continue;
 661                        }
 662                        if (!strcmp(arg, "--no-flags")) {
 663                                filter &= ~DO_FLAGS;
 664                                continue;
 665                        }
 666                        if (!strcmp(arg, "--verify")) {
 667                                filter &= ~(DO_FLAGS|DO_NOREV);
 668                                verify = 1;
 669                                continue;
 670                        }
 671                        if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
 672                                quiet = 1;
 673                                flags |= GET_SHA1_QUIETLY;
 674                                continue;
 675                        }
 676                        if (!strcmp(arg, "--short") ||
 677                            starts_with(arg, "--short=")) {
 678                                filter &= ~(DO_FLAGS|DO_NOREV);
 679                                verify = 1;
 680                                abbrev = DEFAULT_ABBREV;
 681                                if (!arg[7])
 682                                        continue;
 683                                abbrev = strtoul(arg + 8, NULL, 10);
 684                                if (abbrev < MINIMUM_ABBREV)
 685                                        abbrev = MINIMUM_ABBREV;
 686                                else if (40 <= abbrev)
 687                                        abbrev = 40;
 688                                continue;
 689                        }
 690                        if (!strcmp(arg, "--sq")) {
 691                                output_sq = 1;
 692                                continue;
 693                        }
 694                        if (!strcmp(arg, "--not")) {
 695                                show_type ^= REVERSED;
 696                                continue;
 697                        }
 698                        if (!strcmp(arg, "--symbolic")) {
 699                                symbolic = SHOW_SYMBOLIC_ASIS;
 700                                continue;
 701                        }
 702                        if (!strcmp(arg, "--symbolic-full-name")) {
 703                                symbolic = SHOW_SYMBOLIC_FULL;
 704                                continue;
 705                        }
 706                        if (starts_with(arg, "--abbrev-ref") &&
 707                            (!arg[12] || arg[12] == '=')) {
 708                                abbrev_ref = 1;
 709                                abbrev_ref_strict = warn_ambiguous_refs;
 710                                if (arg[12] == '=') {
 711                                        if (!strcmp(arg + 13, "strict"))
 712                                                abbrev_ref_strict = 1;
 713                                        else if (!strcmp(arg + 13, "loose"))
 714                                                abbrev_ref_strict = 0;
 715                                        else
 716                                                die("unknown mode for %s", arg);
 717                                }
 718                                continue;
 719                        }
 720                        if (!strcmp(arg, "--all")) {
 721                                for_each_ref(show_reference, NULL);
 722                                continue;
 723                        }
 724                        if (starts_with(arg, "--disambiguate=")) {
 725                                for_each_abbrev(arg + 15, show_abbrev, NULL);
 726                                continue;
 727                        }
 728                        if (!strcmp(arg, "--bisect")) {
 729                                for_each_ref_in("refs/bisect/bad", show_reference, NULL);
 730                                for_each_ref_in("refs/bisect/good", anti_reference, NULL);
 731                                continue;
 732                        }
 733                        if (starts_with(arg, "--branches=")) {
 734                                for_each_glob_ref_in(show_reference, arg + 11,
 735                                        "refs/heads/", NULL);
 736                                clear_ref_exclusion(&ref_excludes);
 737                                continue;
 738                        }
 739                        if (!strcmp(arg, "--branches")) {
 740                                for_each_branch_ref(show_reference, NULL);
 741                                clear_ref_exclusion(&ref_excludes);
 742                                continue;
 743                        }
 744                        if (starts_with(arg, "--tags=")) {
 745                                for_each_glob_ref_in(show_reference, arg + 7,
 746                                        "refs/tags/", NULL);
 747                                clear_ref_exclusion(&ref_excludes);
 748                                continue;
 749                        }
 750                        if (!strcmp(arg, "--tags")) {
 751                                for_each_tag_ref(show_reference, NULL);
 752                                clear_ref_exclusion(&ref_excludes);
 753                                continue;
 754                        }
 755                        if (starts_with(arg, "--glob=")) {
 756                                for_each_glob_ref(show_reference, arg + 7, NULL);
 757                                clear_ref_exclusion(&ref_excludes);
 758                                continue;
 759                        }
 760                        if (starts_with(arg, "--remotes=")) {
 761                                for_each_glob_ref_in(show_reference, arg + 10,
 762                                        "refs/remotes/", NULL);
 763                                clear_ref_exclusion(&ref_excludes);
 764                                continue;
 765                        }
 766                        if (!strcmp(arg, "--remotes")) {
 767                                for_each_remote_ref(show_reference, NULL);
 768                                clear_ref_exclusion(&ref_excludes);
 769                                continue;
 770                        }
 771                        if (starts_with(arg, "--exclude=")) {
 772                                add_ref_exclusion(&ref_excludes, arg + 10);
 773                                continue;
 774                        }
 775                        if (!strcmp(arg, "--show-toplevel")) {
 776                                const char *work_tree = get_git_work_tree();
 777                                if (work_tree)
 778                                        puts(work_tree);
 779                                continue;
 780                        }
 781                        if (!strcmp(arg, "--show-superproject-working-tree")) {
 782                                const char *superproject = get_superproject_working_tree();
 783                                if (superproject)
 784                                        puts(superproject);
 785                                continue;
 786                        }
 787                        if (!strcmp(arg, "--show-prefix")) {
 788                                if (prefix)
 789                                        puts(prefix);
 790                                else
 791                                        putchar('\n');
 792                                continue;
 793                        }
 794                        if (!strcmp(arg, "--show-cdup")) {
 795                                const char *pfx = prefix;
 796                                if (!is_inside_work_tree()) {
 797                                        const char *work_tree =
 798                                                get_git_work_tree();
 799                                        if (work_tree)
 800                                                printf("%s\n", work_tree);
 801                                        continue;
 802                                }
 803                                while (pfx) {
 804                                        pfx = strchr(pfx, '/');
 805                                        if (pfx) {
 806                                                pfx++;
 807                                                printf("../");
 808                                        }
 809                                }
 810                                putchar('\n');
 811                                continue;
 812                        }
 813                        if (!strcmp(arg, "--git-dir") ||
 814                            !strcmp(arg, "--absolute-git-dir")) {
 815                                const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
 816                                char *cwd;
 817                                int len;
 818                                if (arg[2] == 'g') {    /* --git-dir */
 819                                        if (gitdir) {
 820                                                puts(gitdir);
 821                                                continue;
 822                                        }
 823                                        if (!prefix) {
 824                                                puts(".git");
 825                                                continue;
 826                                        }
 827                                } else {                /* --absolute-git-dir */
 828                                        if (!gitdir && !prefix)
 829                                                gitdir = ".git";
 830                                        if (gitdir) {
 831                                                puts(real_path(gitdir));
 832                                                continue;
 833                                        }
 834                                }
 835                                cwd = xgetcwd();
 836                                len = strlen(cwd);
 837                                printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : "");
 838                                free(cwd);
 839                                continue;
 840                        }
 841                        if (!strcmp(arg, "--git-common-dir")) {
 842                                strbuf_reset(&buf);
 843                                puts(relative_path(get_git_common_dir(),
 844                                                   prefix, &buf));
 845                                continue;
 846                        }
 847                        if (!strcmp(arg, "--is-inside-git-dir")) {
 848                                printf("%s\n", is_inside_git_dir() ? "true"
 849                                                : "false");
 850                                continue;
 851                        }
 852                        if (!strcmp(arg, "--is-inside-work-tree")) {
 853                                printf("%s\n", is_inside_work_tree() ? "true"
 854                                                : "false");
 855                                continue;
 856                        }
 857                        if (!strcmp(arg, "--is-bare-repository")) {
 858                                printf("%s\n", is_bare_repository() ? "true"
 859                                                : "false");
 860                                continue;
 861                        }
 862                        if (!strcmp(arg, "--shared-index-path")) {
 863                                if (read_cache() < 0)
 864                                        die(_("Could not read the index"));
 865                                if (the_index.split_index) {
 866                                        const unsigned char *sha1 = the_index.split_index->base_sha1;
 867                                        const char *path = git_path("sharedindex.%s", sha1_to_hex(sha1));
 868                                        strbuf_reset(&buf);
 869                                        puts(relative_path(path, prefix, &buf));
 870                                }
 871                                continue;
 872                        }
 873                        if (starts_with(arg, "--since=")) {
 874                                show_datestring("--max-age=", arg+8);
 875                                continue;
 876                        }
 877                        if (starts_with(arg, "--after=")) {
 878                                show_datestring("--max-age=", arg+8);
 879                                continue;
 880                        }
 881                        if (starts_with(arg, "--before=")) {
 882                                show_datestring("--min-age=", arg+9);
 883                                continue;
 884                        }
 885                        if (starts_with(arg, "--until=")) {
 886                                show_datestring("--min-age=", arg+8);
 887                                continue;
 888                        }
 889                        if (show_flag(arg) && verify)
 890                                die_no_single_rev(quiet);
 891                        continue;
 892                }
 893
 894                /* Not a flag argument */
 895                if (try_difference(arg))
 896                        continue;
 897                if (try_parent_shorthands(arg))
 898                        continue;
 899                name = arg;
 900                type = NORMAL;
 901                if (*arg == '^') {
 902                        name++;
 903                        type = REVERSED;
 904                }
 905                if (!get_sha1_with_context(name, flags, sha1, &unused)) {
 906                        if (verify)
 907                                revs_count++;
 908                        else
 909                                show_rev(type, sha1, name);
 910                        continue;
 911                }
 912                if (verify)
 913                        die_no_single_rev(quiet);
 914                if (has_dashdash)
 915                        die("bad revision '%s'", arg);
 916                as_is = 1;
 917                if (!show_file(arg, output_prefix))
 918                        continue;
 919                verify_filename(prefix, arg, 1);
 920        }
 921        strbuf_release(&buf);
 922        if (verify) {
 923                if (revs_count == 1) {
 924                        show_rev(type, sha1, name);
 925                        return 0;
 926                } else if (revs_count == 0 && show_default())
 927                        return 0;
 928                die_no_single_rev(quiet);
 929        } else
 930                show_default();
 931        return 0;
 932}