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