builtin-rev-parse.con commit Merge branch 'maint' (6d2c1c2)
   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
  13#define DO_REVS         1
  14#define DO_NOREV        2
  15#define DO_FLAGS        4
  16#define DO_NONFLAGS     8
  17static int filter = ~0;
  18
  19static const char *def;
  20
  21#define NORMAL 0
  22#define REVERSED 1
  23static int show_type = NORMAL;
  24
  25#define SHOW_SYMBOLIC_ASIS 1
  26#define SHOW_SYMBOLIC_FULL 2
  27static int symbolic;
  28static int abbrev;
  29static int output_sq;
  30
  31/*
  32 * Some arguments are relevant "revision" arguments,
  33 * others are about output format or other details.
  34 * This sorts it all out.
  35 */
  36static int is_rev_argument(const char *arg)
  37{
  38        static const char *rev_args[] = {
  39                "--all",
  40                "--bisect",
  41                "--dense",
  42                "--branches",
  43                "--header",
  44                "--max-age=",
  45                "--max-count=",
  46                "--min-age=",
  47                "--no-merges",
  48                "--objects",
  49                "--objects-edge",
  50                "--parents",
  51                "--pretty",
  52                "--remotes",
  53                "--sparse",
  54                "--tags",
  55                "--topo-order",
  56                "--date-order",
  57                "--unpacked",
  58                NULL
  59        };
  60        const char **p = rev_args;
  61
  62        /* accept -<digit>, like traditional "head" */
  63        if ((*arg == '-') && isdigit(arg[1]))
  64                return 1;
  65
  66        for (;;) {
  67                const char *str = *p++;
  68                int len;
  69                if (!str)
  70                        return 0;
  71                len = strlen(str);
  72                if (!strcmp(arg, str) ||
  73                    (str[len-1] == '=' && !strncmp(arg, str, len)))
  74                        return 1;
  75        }
  76}
  77
  78/* Output argument as a string, either SQ or normal */
  79static void show(const char *arg)
  80{
  81        if (output_sq) {
  82                int sq = '\'', ch;
  83
  84                putchar(sq);
  85                while ((ch = *arg++)) {
  86                        if (ch == sq)
  87                                fputs("'\\'", stdout);
  88                        putchar(ch);
  89                }
  90                putchar(sq);
  91                putchar(' ');
  92        }
  93        else
  94                puts(arg);
  95}
  96
  97/* Output a revision, only if filter allows it */
  98static void show_rev(int type, const unsigned char *sha1, const char *name)
  99{
 100        if (!(filter & DO_REVS))
 101                return;
 102        def = NULL;
 103
 104        if (type != show_type)
 105                putchar('^');
 106        if (symbolic && name) {
 107                if (symbolic == SHOW_SYMBOLIC_FULL) {
 108                        unsigned char discard[20];
 109                        char *full;
 110
 111                        switch (dwim_ref(name, strlen(name), discard, &full)) {
 112                        case 0:
 113                                /*
 114                                 * Not found -- not a ref.  We could
 115                                 * emit "name" here, but symbolic-full
 116                                 * users are interested in finding the
 117                                 * refs spelled in full, and they would
 118                                 * need to filter non-refs if we did so.
 119                                 */
 120                                break;
 121                        case 1: /* happy */
 122                                show(full);
 123                                break;
 124                        default: /* ambiguous */
 125                                error("refname '%s' is ambiguous", name);
 126                                break;
 127                        }
 128                } else {
 129                        show(name);
 130                }
 131        }
 132        else if (abbrev)
 133                show(find_unique_abbrev(sha1, abbrev));
 134        else
 135                show(sha1_to_hex(sha1));
 136}
 137
 138/* Output a flag, only if filter allows it. */
 139static int show_flag(const char *arg)
 140{
 141        if (!(filter & DO_FLAGS))
 142                return 0;
 143        if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
 144                show(arg);
 145                return 1;
 146        }
 147        return 0;
 148}
 149
 150static int show_default(void)
 151{
 152        const char *s = def;
 153
 154        if (s) {
 155                unsigned char sha1[20];
 156
 157                def = NULL;
 158                if (!get_sha1(s, sha1)) {
 159                        show_rev(NORMAL, sha1, s);
 160                        return 1;
 161                }
 162        }
 163        return 0;
 164}
 165
 166static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
 167{
 168        show_rev(NORMAL, sha1, refname);
 169        return 0;
 170}
 171
 172static void show_datestring(const char *flag, const char *datestr)
 173{
 174        static char buffer[100];
 175
 176        /* date handling requires both flags and revs */
 177        if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
 178                return;
 179        snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
 180        show(buffer);
 181}
 182
 183static int show_file(const char *arg)
 184{
 185        show_default();
 186        if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
 187                show(arg);
 188                return 1;
 189        }
 190        return 0;
 191}
 192
 193static int try_difference(const char *arg)
 194{
 195        char *dotdot;
 196        unsigned char sha1[20];
 197        unsigned char end[20];
 198        const char *next;
 199        const char *this;
 200        int symmetric;
 201
 202        if (!(dotdot = strstr(arg, "..")))
 203                return 0;
 204        next = dotdot + 2;
 205        this = arg;
 206        symmetric = (*next == '.');
 207
 208        *dotdot = 0;
 209        next += symmetric;
 210
 211        if (!*next)
 212                next = "HEAD";
 213        if (dotdot == arg)
 214                this = "HEAD";
 215        if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
 216                show_rev(NORMAL, end, next);
 217                show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
 218                if (symmetric) {
 219                        struct commit_list *exclude;
 220                        struct commit *a, *b;
 221                        a = lookup_commit_reference(sha1);
 222                        b = lookup_commit_reference(end);
 223                        exclude = get_merge_bases(a, b, 1);
 224                        while (exclude) {
 225                                struct commit_list *n = exclude->next;
 226                                show_rev(REVERSED,
 227                                         exclude->item->object.sha1,NULL);
 228                                free(exclude);
 229                                exclude = n;
 230                        }
 231                }
 232                return 1;
 233        }
 234        *dotdot = '.';
 235        return 0;
 236}
 237
 238static int parseopt_dump(const struct option *o, const char *arg, int unset)
 239{
 240        struct strbuf *parsed = o->value;
 241        if (unset)
 242                strbuf_addf(parsed, " --no-%s", o->long_name);
 243        else if (o->short_name)
 244                strbuf_addf(parsed, " -%c", o->short_name);
 245        else
 246                strbuf_addf(parsed, " --%s", o->long_name);
 247        if (arg) {
 248                strbuf_addch(parsed, ' ');
 249                sq_quote_buf(parsed, arg);
 250        }
 251        return 0;
 252}
 253
 254static const char *skipspaces(const char *s)
 255{
 256        while (isspace(*s))
 257                s++;
 258        return s;
 259}
 260
 261static int cmd_parseopt(int argc, const char **argv, const char *prefix)
 262{
 263        static int keep_dashdash = 0;
 264        static char const * const parseopt_usage[] = {
 265                "git-rev-parse --parseopt [options] -- [<args>...]",
 266                NULL
 267        };
 268        static struct option parseopt_opts[] = {
 269                OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash,
 270                                        "keep the `--` passed as an arg"),
 271                OPT_END(),
 272        };
 273
 274        struct strbuf sb, parsed;
 275        const char **usage = NULL;
 276        struct option *opts = NULL;
 277        int onb = 0, osz = 0, unb = 0, usz = 0;
 278
 279        strbuf_init(&parsed, 0);
 280        strbuf_addstr(&parsed, "set --");
 281        argc = parse_options(argc, argv, parseopt_opts, parseopt_usage,
 282                             PARSE_OPT_KEEP_DASHDASH);
 283        if (argc < 1 || strcmp(argv[0], "--"))
 284                usage_with_options(parseopt_usage, parseopt_opts);
 285
 286        strbuf_init(&sb, 0);
 287        /* get the usage up to the first line with a -- on it */
 288        for (;;) {
 289                if (strbuf_getline(&sb, stdin, '\n') == EOF)
 290                        die("premature end of input");
 291                ALLOC_GROW(usage, unb + 1, usz);
 292                if (!strcmp("--", sb.buf)) {
 293                        if (unb < 1)
 294                                die("no usage string given before the `--' separator");
 295                        usage[unb] = NULL;
 296                        break;
 297                }
 298                usage[unb++] = strbuf_detach(&sb, NULL);
 299        }
 300
 301        /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
 302        while (strbuf_getline(&sb, stdin, '\n') != EOF) {
 303                const char *s;
 304                struct option *o;
 305
 306                if (!sb.len)
 307                        continue;
 308
 309                ALLOC_GROW(opts, onb + 1, osz);
 310                memset(opts + onb, 0, sizeof(opts[onb]));
 311
 312                o = &opts[onb++];
 313                s = strchr(sb.buf, ' ');
 314                if (!s || *sb.buf == ' ') {
 315                        o->type = OPTION_GROUP;
 316                        o->help = xstrdup(skipspaces(sb.buf));
 317                        continue;
 318                }
 319
 320                o->type = OPTION_CALLBACK;
 321                o->help = xstrdup(skipspaces(s));
 322                o->value = &parsed;
 323                o->flags = PARSE_OPT_NOARG;
 324                o->callback = &parseopt_dump;
 325                while (s > sb.buf && strchr("*=?!", s[-1])) {
 326                        switch (*--s) {
 327                        case '=':
 328                                o->flags &= ~PARSE_OPT_NOARG;
 329                                break;
 330                        case '?':
 331                                o->flags &= ~PARSE_OPT_NOARG;
 332                                o->flags |= PARSE_OPT_OPTARG;
 333                                break;
 334                        case '!':
 335                                o->flags |= PARSE_OPT_NONEG;
 336                                break;
 337                        case '*':
 338                                o->flags |= PARSE_OPT_HIDDEN;
 339                                break;
 340                        }
 341                }
 342
 343                if (s - sb.buf == 1) /* short option only */
 344                        o->short_name = *sb.buf;
 345                else if (sb.buf[1] != ',') /* long option only */
 346                        o->long_name = xmemdupz(sb.buf, s - sb.buf);
 347                else {
 348                        o->short_name = *sb.buf;
 349                        o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
 350                }
 351        }
 352        strbuf_release(&sb);
 353
 354        /* put an OPT_END() */
 355        ALLOC_GROW(opts, onb + 1, osz);
 356        memset(opts + onb, 0, sizeof(opts[onb]));
 357        argc = parse_options(argc, argv, opts, usage,
 358                             keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0);
 359
 360        strbuf_addf(&parsed, " --");
 361        sq_quote_argv(&parsed, argv, 0);
 362        puts(parsed.buf);
 363        return 0;
 364}
 365
 366static void die_no_single_rev(int quiet)
 367{
 368        if (quiet)
 369                exit(1);
 370        else
 371                die("Needed a single revision");
 372}
 373
 374int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 375{
 376        int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
 377        unsigned char sha1[20];
 378        const char *name = NULL;
 379
 380        if (argc > 1 && !strcmp("--parseopt", argv[1]))
 381                return cmd_parseopt(argc - 1, argv + 1, prefix);
 382
 383        prefix = setup_git_directory();
 384        git_config(git_default_config);
 385        for (i = 1; i < argc; i++) {
 386                const char *arg = argv[i];
 387
 388                if (as_is) {
 389                        if (show_file(arg) && as_is < 2)
 390                                verify_filename(prefix, arg);
 391                        continue;
 392                }
 393                if (!strcmp(arg,"-n")) {
 394                        if (++i >= argc)
 395                                die("-n requires an argument");
 396                        if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
 397                                show(arg);
 398                                show(argv[i]);
 399                        }
 400                        continue;
 401                }
 402                if (!prefixcmp(arg, "-n")) {
 403                        if ((filter & DO_FLAGS) && (filter & DO_REVS))
 404                                show(arg);
 405                        continue;
 406                }
 407
 408                if (*arg == '-') {
 409                        if (!strcmp(arg, "--")) {
 410                                as_is = 2;
 411                                /* Pass on the "--" if we show anything but files.. */
 412                                if (filter & (DO_FLAGS | DO_REVS))
 413                                        show_file(arg);
 414                                continue;
 415                        }
 416                        if (!strcmp(arg, "--default")) {
 417                                def = argv[i+1];
 418                                i++;
 419                                continue;
 420                        }
 421                        if (!strcmp(arg, "--revs-only")) {
 422                                filter &= ~DO_NOREV;
 423                                continue;
 424                        }
 425                        if (!strcmp(arg, "--no-revs")) {
 426                                filter &= ~DO_REVS;
 427                                continue;
 428                        }
 429                        if (!strcmp(arg, "--flags")) {
 430                                filter &= ~DO_NONFLAGS;
 431                                continue;
 432                        }
 433                        if (!strcmp(arg, "--no-flags")) {
 434                                filter &= ~DO_FLAGS;
 435                                continue;
 436                        }
 437                        if (!strcmp(arg, "--verify")) {
 438                                filter &= ~(DO_FLAGS|DO_NOREV);
 439                                verify = 1;
 440                                continue;
 441                        }
 442                        if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
 443                                quiet = 1;
 444                                continue;
 445                        }
 446                        if (!strcmp(arg, "--short") ||
 447                            !prefixcmp(arg, "--short=")) {
 448                                filter &= ~(DO_FLAGS|DO_NOREV);
 449                                verify = 1;
 450                                abbrev = DEFAULT_ABBREV;
 451                                if (arg[7] == '=')
 452                                        abbrev = strtoul(arg + 8, NULL, 10);
 453                                if (abbrev < MINIMUM_ABBREV)
 454                                        abbrev = MINIMUM_ABBREV;
 455                                else if (40 <= abbrev)
 456                                        abbrev = 40;
 457                                continue;
 458                        }
 459                        if (!strcmp(arg, "--sq")) {
 460                                output_sq = 1;
 461                                continue;
 462                        }
 463                        if (!strcmp(arg, "--not")) {
 464                                show_type ^= REVERSED;
 465                                continue;
 466                        }
 467                        if (!strcmp(arg, "--symbolic")) {
 468                                symbolic = SHOW_SYMBOLIC_ASIS;
 469                                continue;
 470                        }
 471                        if (!strcmp(arg, "--symbolic-full-name")) {
 472                                symbolic = SHOW_SYMBOLIC_FULL;
 473                                continue;
 474                        }
 475                        if (!strcmp(arg, "--all")) {
 476                                for_each_ref(show_reference, NULL);
 477                                continue;
 478                        }
 479                        if (!strcmp(arg, "--branches")) {
 480                                for_each_branch_ref(show_reference, NULL);
 481                                continue;
 482                        }
 483                        if (!strcmp(arg, "--tags")) {
 484                                for_each_tag_ref(show_reference, NULL);
 485                                continue;
 486                        }
 487                        if (!strcmp(arg, "--remotes")) {
 488                                for_each_remote_ref(show_reference, NULL);
 489                                continue;
 490                        }
 491                        if (!strcmp(arg, "--show-prefix")) {
 492                                if (prefix)
 493                                        puts(prefix);
 494                                continue;
 495                        }
 496                        if (!strcmp(arg, "--show-cdup")) {
 497                                const char *pfx = prefix;
 498                                if (!is_inside_work_tree()) {
 499                                        const char *work_tree =
 500                                                get_git_work_tree();
 501                                        if (work_tree)
 502                                                printf("%s\n", work_tree);
 503                                        continue;
 504                                }
 505                                while (pfx) {
 506                                        pfx = strchr(pfx, '/');
 507                                        if (pfx) {
 508                                                pfx++;
 509                                                printf("../");
 510                                        }
 511                                }
 512                                putchar('\n');
 513                                continue;
 514                        }
 515                        if (!strcmp(arg, "--git-dir")) {
 516                                const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
 517                                static char cwd[PATH_MAX];
 518                                if (gitdir) {
 519                                        puts(gitdir);
 520                                        continue;
 521                                }
 522                                if (!prefix) {
 523                                        puts(".git");
 524                                        continue;
 525                                }
 526                                if (!getcwd(cwd, PATH_MAX))
 527                                        die("unable to get current working directory");
 528                                printf("%s/.git\n", cwd);
 529                                continue;
 530                        }
 531                        if (!strcmp(arg, "--is-inside-git-dir")) {
 532                                printf("%s\n", is_inside_git_dir() ? "true"
 533                                                : "false");
 534                                continue;
 535                        }
 536                        if (!strcmp(arg, "--is-inside-work-tree")) {
 537                                printf("%s\n", is_inside_work_tree() ? "true"
 538                                                : "false");
 539                                continue;
 540                        }
 541                        if (!strcmp(arg, "--is-bare-repository")) {
 542                                printf("%s\n", is_bare_repository() ? "true"
 543                                                : "false");
 544                                continue;
 545                        }
 546                        if (!prefixcmp(arg, "--since=")) {
 547                                show_datestring("--max-age=", arg+8);
 548                                continue;
 549                        }
 550                        if (!prefixcmp(arg, "--after=")) {
 551                                show_datestring("--max-age=", arg+8);
 552                                continue;
 553                        }
 554                        if (!prefixcmp(arg, "--before=")) {
 555                                show_datestring("--min-age=", arg+9);
 556                                continue;
 557                        }
 558                        if (!prefixcmp(arg, "--until=")) {
 559                                show_datestring("--min-age=", arg+8);
 560                                continue;
 561                        }
 562                        if (show_flag(arg) && verify)
 563                                die_no_single_rev(quiet);
 564                        continue;
 565                }
 566
 567                /* Not a flag argument */
 568                if (try_difference(arg))
 569                        continue;
 570                name = arg;
 571                type = NORMAL;
 572                if (*arg == '^') {
 573                        name++;
 574                        type = REVERSED;
 575                }
 576                if (!get_sha1(name, sha1)) {
 577                        if (verify)
 578                                revs_count++;
 579                        else
 580                                show_rev(type, sha1, name);
 581                        continue;
 582                }
 583                if (verify)
 584                        die_no_single_rev(quiet);
 585                as_is = 1;
 586                if (!show_file(arg))
 587                        continue;
 588                verify_filename(prefix, arg);
 589        }
 590        if (verify) {
 591                if (revs_count == 1) {
 592                        show_rev(type, sha1, name);
 593                        return 0;
 594                } else if (revs_count == 0 && show_default())
 595                        return 0;
 596                die_no_single_rev(quiet);
 597        } else
 598                show_default();
 599        return 0;
 600}