4 * Copyright (C) Linus Torvalds, 2005
11 #include "parse-options.h"
14 #include "split-index.h"
15 #include "submodule.h"
21 static int filter = ~0;
23 static const char *def;
27 static int show_type = NORMAL;
29 #define SHOW_SYMBOLIC_ASIS 1
30 #define SHOW_SYMBOLIC_FULL 2
33 static int abbrev_ref;
34 static int abbrev_ref_strict;
37 static int stuck_long;
38 static struct string_list *ref_excludes;
41 * Some arguments are relevant "revision" arguments,
42 * others are about output format or other details.
43 * This sorts it all out.
45 static int is_rev_argument(const char *arg)
47 static const char *rev_args[] = {
78 const char **p = rev_args;
80 /* accept -<digit>, like traditional "head" */
81 if ((*arg == '-') && isdigit(arg[1]))
85 const char *str = *p++;
90 if (!strcmp(arg, str) ||
91 (str[len-1] == '=' && !strncmp(arg, str, len)))
96 /* Output argument as a string, either SQ or normal */
97 static void show(const char *arg)
103 while ((ch = *arg++)) {
105 fputs("'\\'", stdout);
115 /* Like show(), but with a negation prefix according to type */
116 static void show_with_type(int type, const char *arg)
118 if (type != show_type)
123 /* Output a revision, only if filter allows it */
124 static void show_rev(int type, const unsigned char *sha1, const char *name)
126 if (!(filter & DO_REVS))
130 if ((symbolic || abbrev_ref) && name) {
131 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
132 unsigned char discard[20];
135 switch (dwim_ref(name, strlen(name), discard, &full)) {
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.
147 full = shorten_unambiguous_ref(full,
149 show_with_type(type, full);
151 default: /* ambiguous */
152 error("refname '%s' is ambiguous", name);
157 show_with_type(type, name);
161 show_with_type(type, find_unique_abbrev(sha1, abbrev));
163 show_with_type(type, sha1_to_hex(sha1));
166 /* Output a flag, only if filter allows it. */
167 static int show_flag(const char *arg)
169 if (!(filter & DO_FLAGS))
171 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
178 static int show_default(void)
183 unsigned char sha1[20];
186 if (!get_sha1(s, sha1)) {
187 show_rev(NORMAL, sha1, s);
194 static int show_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
196 if (ref_excluded(ref_excludes, refname))
198 show_rev(NORMAL, oid->hash, refname);
202 static int anti_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
204 show_rev(REVERSED, oid->hash, refname);
208 static int show_abbrev(const unsigned char *sha1, void *cb_data)
210 show_rev(NORMAL, sha1, NULL);
214 static void show_datestring(const char *flag, const char *datestr)
216 static char buffer[100];
218 /* date handling requires both flags and revs */
219 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
221 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
225 static int show_file(const char *arg, int output_prefix)
228 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
230 const char *prefix = startup_info->prefix;
231 show(prefix_filename(prefix,
232 prefix ? strlen(prefix) : 0,
241 static int try_difference(const char *arg)
244 unsigned char sha1[20];
245 unsigned char end[20];
249 static const char head_by_default[] = "HEAD";
251 if (!(dotdot = strstr(arg, "..")))
255 symmetric = (*next == '.');
261 next = head_by_default;
263 this = head_by_default;
265 if (this == head_by_default && next == head_by_default &&
268 * Just ".."? That is not a range but the
269 * pathspec for the parent directory.
275 if (!get_sha1_committish(this, sha1) && !get_sha1_committish(next, end)) {
276 show_rev(NORMAL, end, next);
277 show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
279 struct commit_list *exclude;
280 struct commit *a, *b;
281 a = lookup_commit_reference(sha1);
282 b = lookup_commit_reference(end);
283 exclude = get_merge_bases(a, b);
285 struct commit *commit = pop_commit(&exclude);
286 show_rev(REVERSED, commit->object.oid.hash, NULL);
296 static int try_parent_shorthands(const char *arg)
299 unsigned char sha1[20];
300 struct commit *commit;
301 struct commit_list *parents;
304 int include_parents = 0;
305 int exclude_parent = 0;
307 if ((dotdot = strstr(arg, "^!"))) {
311 } else if ((dotdot = strstr(arg, "^@"))) {
315 } else if ((dotdot = strstr(arg, "^-"))) {
321 exclude_parent = strtoul(dotdot + 2, &end, 10);
322 if (*end != '\0' || !exclude_parent)
329 if (get_sha1_committish(arg, sha1)) {
334 commit = lookup_commit_reference(sha1);
335 if (exclude_parent &&
336 exclude_parent > commit_list_count(commit->parents)) {
342 show_rev(NORMAL, sha1, arg);
343 for (parents = commit->parents, parent_number = 1;
345 parents = parents->next, parent_number++) {
348 if (exclude_parent && parent_number != exclude_parent)
352 name = xstrfmt("%s^%d", arg, parent_number);
353 show_rev(include_parents ? NORMAL : REVERSED,
354 parents->item->object.oid.hash, name);
362 static int parseopt_dump(const struct option *o, const char *arg, int unset)
364 struct strbuf *parsed = o->value;
366 strbuf_addf(parsed, " --no-%s", o->long_name);
367 else if (o->short_name && (o->long_name == NULL || !stuck_long))
368 strbuf_addf(parsed, " -%c", o->short_name);
370 strbuf_addf(parsed, " --%s", o->long_name);
373 strbuf_addch(parsed, ' ');
374 else if (o->long_name)
375 strbuf_addch(parsed, '=');
376 sq_quote_buf(parsed, arg);
381 static const char *skipspaces(const char *s)
388 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
390 static int keep_dashdash = 0, stop_at_non_option = 0;
391 static char const * const parseopt_usage[] = {
392 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
395 static struct option parseopt_opts[] = {
396 OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
397 N_("keep the `--` passed as an arg")),
398 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
399 N_("stop parsing after the "
400 "first non-option argument")),
401 OPT_BOOL(0, "stuck-long", &stuck_long,
402 N_("output in stuck long form")),
405 static const char * const flag_chars = "*=?!";
407 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
408 const char **usage = NULL;
409 struct option *opts = NULL;
410 int onb = 0, osz = 0, unb = 0, usz = 0;
412 strbuf_addstr(&parsed, "set --");
413 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
414 PARSE_OPT_KEEP_DASHDASH);
415 if (argc < 1 || strcmp(argv[0], "--"))
416 usage_with_options(parseopt_usage, parseopt_opts);
418 /* get the usage up to the first line with a -- on it */
420 if (strbuf_getline(&sb, stdin) == EOF)
421 die("premature end of input");
422 ALLOC_GROW(usage, unb + 1, usz);
423 if (!strcmp("--", sb.buf)) {
425 die("no usage string given before the `--' separator");
429 usage[unb++] = strbuf_detach(&sb, NULL);
432 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
433 while (strbuf_getline(&sb, stdin) != EOF) {
441 ALLOC_GROW(opts, onb + 1, osz);
442 memset(opts + onb, 0, sizeof(opts[onb]));
445 help = strchr(sb.buf, ' ');
446 if (!help || *sb.buf == ' ') {
447 o->type = OPTION_GROUP;
448 o->help = xstrdup(skipspaces(sb.buf));
452 o->type = OPTION_CALLBACK;
453 o->help = xstrdup(skipspaces(help));
455 o->flags = PARSE_OPT_NOARG;
456 o->callback = &parseopt_dump;
459 s = strpbrk(sb.buf, flag_chars);
463 if (s - sb.buf == 1) /* short option only */
464 o->short_name = *sb.buf;
465 else if (sb.buf[1] != ',') /* long option only */
466 o->long_name = xmemdupz(sb.buf, s - sb.buf);
468 o->short_name = *sb.buf;
469 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
476 o->flags &= ~PARSE_OPT_NOARG;
479 o->flags &= ~PARSE_OPT_NOARG;
480 o->flags |= PARSE_OPT_OPTARG;
483 o->flags |= PARSE_OPT_NONEG;
486 o->flags |= PARSE_OPT_HIDDEN;
494 o->argh = xmemdupz(s, help - s);
498 /* put an OPT_END() */
499 ALLOC_GROW(opts, onb + 1, osz);
500 memset(opts + onb, 0, sizeof(opts[onb]));
501 argc = parse_options(argc, argv, prefix, opts, usage,
502 (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
503 (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
504 PARSE_OPT_SHELL_EVAL);
506 strbuf_addstr(&parsed, " --");
507 sq_quote_argv(&parsed, argv, 0);
512 static int cmd_sq_quote(int argc, const char **argv)
514 struct strbuf buf = STRBUF_INIT;
517 sq_quote_argv(&buf, argv, 0);
518 printf("%s\n", buf.buf);
519 strbuf_release(&buf);
524 static void die_no_single_rev(int quiet)
529 die("Needed a single revision");
532 static const char builtin_rev_parse_usage[] =
533 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
534 " or: git rev-parse --sq-quote [<arg>...]\n"
535 " or: git rev-parse [<options>] [<arg>...]\n"
537 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
540 * Parse "opt" or "opt=<value>", setting value respectively to either
541 * NULL or the string after "=".
543 static int opt_with_value(const char *arg, const char *opt, const char **value)
545 if (skip_prefix(arg, opt, &arg)) {
558 static void handle_ref_opt(const char *pattern, const char *prefix)
561 for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
563 for_each_ref_in(prefix, show_reference, NULL);
564 clear_ref_exclusion(&ref_excludes);
567 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
569 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
570 int did_repo_setup = 0;
571 int has_dashdash = 0;
572 int output_prefix = 0;
573 unsigned char sha1[20];
574 unsigned int flags = 0;
575 const char *name = NULL;
576 struct object_context unused;
577 struct strbuf buf = STRBUF_INIT;
579 if (argc > 1 && !strcmp("--parseopt", argv[1]))
580 return cmd_parseopt(argc - 1, argv + 1, prefix);
582 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
583 return cmd_sq_quote(argc - 2, argv + 2);
585 if (argc > 1 && !strcmp("-h", argv[1]))
586 usage(builtin_rev_parse_usage);
588 for (i = 1; i < argc; i++) {
589 if (!strcmp(argv[i], "--")) {
595 /* No options; just report on whether we're in a git repo or not. */
597 setup_git_directory();
598 git_config(git_default_config, NULL);
602 for (i = 1; i < argc; i++) {
603 const char *arg = argv[i];
605 if (!strcmp(arg, "--local-env-vars")) {
607 for (i = 0; local_repo_env[i]; i++)
608 printf("%s\n", local_repo_env[i]);
611 if (!strcmp(arg, "--resolve-git-dir")) {
612 const char *gitdir = argv[++i];
614 die("--resolve-git-dir requires an argument");
615 gitdir = resolve_gitdir(gitdir);
617 die("not a gitdir '%s'", argv[i]);
622 /* The rest of the options require a git repository. */
623 if (!did_repo_setup) {
624 prefix = setup_git_directory();
625 git_config(git_default_config, NULL);
629 if (!strcmp(arg, "--git-path")) {
631 die("--git-path requires an argument");
633 puts(relative_path(git_path("%s", argv[i + 1]),
639 if (show_file(arg, output_prefix) && as_is < 2)
640 verify_filename(prefix, arg, 0);
643 if (!strcmp(arg,"-n")) {
645 die("-n requires an argument");
646 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
652 if (starts_with(arg, "-n")) {
653 if ((filter & DO_FLAGS) && (filter & DO_REVS))
659 if (!strcmp(arg, "--")) {
661 /* Pass on the "--" if we show anything but files.. */
662 if (filter & (DO_FLAGS | DO_REVS))
666 if (!strcmp(arg, "--default")) {
669 die("--default requires an argument");
672 if (!strcmp(arg, "--prefix")) {
675 die("--prefix requires an argument");
676 startup_info->prefix = prefix;
680 if (!strcmp(arg, "--revs-only")) {
684 if (!strcmp(arg, "--no-revs")) {
688 if (!strcmp(arg, "--flags")) {
689 filter &= ~DO_NONFLAGS;
692 if (!strcmp(arg, "--no-flags")) {
696 if (!strcmp(arg, "--verify")) {
697 filter &= ~(DO_FLAGS|DO_NOREV);
701 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
703 flags |= GET_SHA1_QUIETLY;
706 if (opt_with_value(arg, "--short", &arg)) {
707 filter &= ~(DO_FLAGS|DO_NOREV);
709 abbrev = DEFAULT_ABBREV;
712 abbrev = strtoul(arg, NULL, 10);
713 if (abbrev < MINIMUM_ABBREV)
714 abbrev = MINIMUM_ABBREV;
715 else if (40 <= abbrev)
719 if (!strcmp(arg, "--sq")) {
723 if (!strcmp(arg, "--not")) {
724 show_type ^= REVERSED;
727 if (!strcmp(arg, "--symbolic")) {
728 symbolic = SHOW_SYMBOLIC_ASIS;
731 if (!strcmp(arg, "--symbolic-full-name")) {
732 symbolic = SHOW_SYMBOLIC_FULL;
735 if (opt_with_value(arg, "--abbrev-ref", &arg)) {
737 abbrev_ref_strict = warn_ambiguous_refs;
739 if (!strcmp(arg, "strict"))
740 abbrev_ref_strict = 1;
741 else if (!strcmp(arg, "loose"))
742 abbrev_ref_strict = 0;
744 die("unknown mode for --abbrev-ref: %s",
749 if (!strcmp(arg, "--all")) {
750 for_each_ref(show_reference, NULL);
753 if (skip_prefix(arg, "--disambiguate=", &arg)) {
754 for_each_abbrev(arg, show_abbrev, NULL);
757 if (!strcmp(arg, "--bisect")) {
758 for_each_ref_in("refs/bisect/bad", show_reference, NULL);
759 for_each_ref_in("refs/bisect/good", anti_reference, NULL);
762 if (opt_with_value(arg, "--branches", &arg)) {
763 handle_ref_opt(arg, "refs/heads/");
766 if (opt_with_value(arg, "--tags", &arg)) {
767 handle_ref_opt(arg, "refs/tags/");
770 if (skip_prefix(arg, "--glob=", &arg)) {
771 handle_ref_opt(arg, NULL);
774 if (opt_with_value(arg, "--remotes", &arg)) {
775 handle_ref_opt(arg, "refs/remotes/");
778 if (skip_prefix(arg, "--exclude=", &arg)) {
779 add_ref_exclusion(&ref_excludes, arg);
782 if (!strcmp(arg, "--show-toplevel")) {
783 const char *work_tree = get_git_work_tree();
788 if (!strcmp(arg, "--show-superproject-working-tree")) {
789 const char *superproject = get_superproject_working_tree();
794 if (!strcmp(arg, "--show-prefix")) {
801 if (!strcmp(arg, "--show-cdup")) {
802 const char *pfx = prefix;
803 if (!is_inside_work_tree()) {
804 const char *work_tree =
807 printf("%s\n", work_tree);
811 pfx = strchr(pfx, '/');
820 if (!strcmp(arg, "--git-dir") ||
821 !strcmp(arg, "--absolute-git-dir")) {
822 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
825 if (arg[2] == 'g') { /* --git-dir */
834 } else { /* --absolute-git-dir */
835 if (!gitdir && !prefix)
838 puts(real_path(gitdir));
844 printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : "");
848 if (!strcmp(arg, "--git-common-dir")) {
850 puts(relative_path(get_git_common_dir(),
854 if (!strcmp(arg, "--is-inside-git-dir")) {
855 printf("%s\n", is_inside_git_dir() ? "true"
859 if (!strcmp(arg, "--is-inside-work-tree")) {
860 printf("%s\n", is_inside_work_tree() ? "true"
864 if (!strcmp(arg, "--is-bare-repository")) {
865 printf("%s\n", is_bare_repository() ? "true"
869 if (!strcmp(arg, "--shared-index-path")) {
870 if (read_cache() < 0)
871 die(_("Could not read the index"));
872 if (the_index.split_index) {
873 const unsigned char *sha1 = the_index.split_index->base_sha1;
874 const char *path = git_path("sharedindex.%s", sha1_to_hex(sha1));
876 puts(relative_path(path, prefix, &buf));
880 if (skip_prefix(arg, "--since=", &arg)) {
881 show_datestring("--max-age=", arg);
884 if (skip_prefix(arg, "--after=", &arg)) {
885 show_datestring("--max-age=", arg);
888 if (skip_prefix(arg, "--before=", &arg)) {
889 show_datestring("--min-age=", arg);
892 if (skip_prefix(arg, "--until=", &arg)) {
893 show_datestring("--min-age=", arg);
896 if (show_flag(arg) && verify)
897 die_no_single_rev(quiet);
901 /* Not a flag argument */
902 if (try_difference(arg))
904 if (try_parent_shorthands(arg))
912 if (!get_sha1_with_context(name, flags, sha1, &unused)) {
916 show_rev(type, sha1, name);
920 die_no_single_rev(quiet);
922 die("bad revision '%s'", arg);
924 if (!show_file(arg, output_prefix))
926 verify_filename(prefix, arg, 1);
928 strbuf_release(&buf);
930 if (revs_count == 1) {
931 show_rev(type, sha1, name);
933 } else if (revs_count == 0 && show_default())
935 die_no_single_rev(quiet);