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