4 * Copyright (C) Linus Torvalds, 2005
12 #include "parse-options.h"
15 #include "split-index.h"
16 #include "submodule.h"
22 static int filter = ~0;
24 static const char *def;
28 static int show_type = NORMAL;
30 #define SHOW_SYMBOLIC_ASIS 1
31 #define SHOW_SYMBOLIC_FULL 2
34 static int abbrev_ref;
35 static int abbrev_ref_strict;
38 static int stuck_long;
39 static struct string_list *ref_excludes;
42 * Some arguments are relevant "revision" arguments,
43 * others are about output format or other details.
44 * This sorts it all out.
46 static int is_rev_argument(const char *arg)
48 static const char *rev_args[] = {
79 const char **p = rev_args;
81 /* accept -<digit>, like traditional "head" */
82 if ((*arg == '-') && isdigit(arg[1]))
86 const char *str = *p++;
91 if (!strcmp(arg, str) ||
92 (str[len-1] == '=' && !strncmp(arg, str, len)))
97 /* Output argument as a string, either SQ or normal */
98 static void show(const char *arg)
104 while ((ch = *arg++)) {
106 fputs("'\\'", stdout);
116 /* Like show(), but with a negation prefix according to type */
117 static void show_with_type(int type, const char *arg)
119 if (type != show_type)
124 /* Output a revision, only if filter allows it */
125 static void show_rev(int type, const struct object_id *oid, const char *name)
127 if (!(filter & DO_REVS))
131 if ((symbolic || abbrev_ref) && name) {
132 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
133 struct object_id discard;
136 switch (dwim_ref(name, strlen(name), &discard, &full)) {
139 * Not found -- not a ref. We could
140 * emit "name" here, but symbolic-full
141 * users are interested in finding the
142 * refs spelled in full, and they would
143 * need to filter non-refs if we did so.
148 full = shorten_unambiguous_ref(full,
150 show_with_type(type, full);
152 default: /* ambiguous */
153 error("refname '%s' is ambiguous", name);
158 show_with_type(type, name);
162 show_with_type(type, find_unique_abbrev(oid, abbrev));
164 show_with_type(type, oid_to_hex(oid));
167 /* Output a flag, only if filter allows it. */
168 static int show_flag(const char *arg)
170 if (!(filter & DO_FLAGS))
172 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
179 static int show_default(void)
184 struct object_id oid;
187 if (!get_oid(s, &oid)) {
188 show_rev(NORMAL, &oid, s);
195 static int show_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
197 if (ref_excluded(ref_excludes, refname))
199 show_rev(NORMAL, oid, refname);
203 static int anti_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
205 show_rev(REVERSED, oid, refname);
209 static int show_abbrev(const struct object_id *oid, void *cb_data)
211 show_rev(NORMAL, oid, NULL);
215 static void show_datestring(const char *flag, const char *datestr)
219 /* date handling requires both flags and revs */
220 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
222 buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr));
227 static int show_file(const char *arg, int output_prefix)
230 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
232 const char *prefix = startup_info->prefix;
233 char *fname = prefix_filename(prefix, arg);
243 static int try_difference(const char *arg)
246 struct object_id start_oid;
247 struct object_id end_oid;
251 static const char head_by_default[] = "HEAD";
253 if (!(dotdot = strstr(arg, "..")))
257 symmetric = (*end == '.');
263 end = head_by_default;
265 start = head_by_default;
267 if (start == head_by_default && end == head_by_default &&
270 * Just ".."? That is not a range but the
271 * pathspec for the parent directory.
277 if (!get_oid_committish(start, &start_oid) && !get_oid_committish(end, &end_oid)) {
278 show_rev(NORMAL, &end_oid, end);
279 show_rev(symmetric ? NORMAL : REVERSED, &start_oid, start);
281 struct commit_list *exclude;
282 struct commit *a, *b;
283 a = lookup_commit_reference(the_repository, &start_oid);
284 b = lookup_commit_reference(the_repository, &end_oid);
289 exclude = get_merge_bases(a, b);
291 struct commit *commit = pop_commit(&exclude);
292 show_rev(REVERSED, &commit->object.oid, NULL);
302 static int try_parent_shorthands(const char *arg)
305 struct object_id oid;
306 struct commit *commit;
307 struct commit_list *parents;
310 int include_parents = 0;
311 int exclude_parent = 0;
313 if ((dotdot = strstr(arg, "^!"))) {
317 } else if ((dotdot = strstr(arg, "^@"))) {
321 } else if ((dotdot = strstr(arg, "^-"))) {
327 exclude_parent = strtoul(dotdot + 2, &end, 10);
328 if (*end != '\0' || !exclude_parent)
335 if (get_oid_committish(arg, &oid) ||
336 !(commit = lookup_commit_reference(the_repository, &oid))) {
341 if (exclude_parent &&
342 exclude_parent > commit_list_count(commit->parents)) {
348 show_rev(NORMAL, &oid, arg);
349 for (parents = commit->parents, parent_number = 1;
351 parents = parents->next, parent_number++) {
354 if (exclude_parent && parent_number != exclude_parent)
358 name = xstrfmt("%s^%d", arg, parent_number);
359 show_rev(include_parents ? NORMAL : REVERSED,
360 &parents->item->object.oid, name);
368 static int parseopt_dump(const struct option *o, const char *arg, int unset)
370 struct strbuf *parsed = o->value;
372 strbuf_addf(parsed, " --no-%s", o->long_name);
373 else if (o->short_name && (o->long_name == NULL || !stuck_long))
374 strbuf_addf(parsed, " -%c", o->short_name);
376 strbuf_addf(parsed, " --%s", o->long_name);
379 strbuf_addch(parsed, ' ');
380 else if (o->long_name)
381 strbuf_addch(parsed, '=');
382 sq_quote_buf(parsed, arg);
387 static const char *skipspaces(const char *s)
394 static char *findspace(const char *s)
402 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
404 static int keep_dashdash = 0, stop_at_non_option = 0;
405 static char const * const parseopt_usage[] = {
406 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
409 static struct option parseopt_opts[] = {
410 OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
411 N_("keep the `--` passed as an arg")),
412 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
413 N_("stop parsing after the "
414 "first non-option argument")),
415 OPT_BOOL(0, "stuck-long", &stuck_long,
416 N_("output in stuck long form")),
419 static const char * const flag_chars = "*=?!";
421 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
422 const char **usage = NULL;
423 struct option *opts = NULL;
424 int onb = 0, osz = 0, unb = 0, usz = 0;
426 strbuf_addstr(&parsed, "set --");
427 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
428 PARSE_OPT_KEEP_DASHDASH);
429 if (argc < 1 || strcmp(argv[0], "--"))
430 usage_with_options(parseopt_usage, parseopt_opts);
432 /* get the usage up to the first line with a -- on it */
434 if (strbuf_getline(&sb, stdin) == EOF)
435 die("premature end of input");
436 ALLOC_GROW(usage, unb + 1, usz);
437 if (!strcmp("--", sb.buf)) {
439 die("no usage string given before the `--' separator");
443 usage[unb++] = strbuf_detach(&sb, NULL);
446 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
447 while (strbuf_getline(&sb, stdin) != EOF) {
455 ALLOC_GROW(opts, onb + 1, osz);
456 memset(opts + onb, 0, sizeof(opts[onb]));
459 help = findspace(sb.buf);
460 if (!help || sb.buf == help) {
461 o->type = OPTION_GROUP;
462 o->help = xstrdup(skipspaces(sb.buf));
468 o->type = OPTION_CALLBACK;
469 o->help = xstrdup(skipspaces(help+1));
471 o->flags = PARSE_OPT_NOARG;
472 o->callback = &parseopt_dump;
475 s = strpbrk(sb.buf, flag_chars);
479 if (s - sb.buf == 1) /* short option only */
480 o->short_name = *sb.buf;
481 else if (sb.buf[1] != ',') /* long option only */
482 o->long_name = xmemdupz(sb.buf, s - sb.buf);
484 o->short_name = *sb.buf;
485 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
492 o->flags &= ~PARSE_OPT_NOARG;
495 o->flags &= ~PARSE_OPT_NOARG;
496 o->flags |= PARSE_OPT_OPTARG;
499 o->flags |= PARSE_OPT_NONEG;
502 o->flags |= PARSE_OPT_HIDDEN;
510 o->argh = xmemdupz(s, help - s);
514 /* put an OPT_END() */
515 ALLOC_GROW(opts, onb + 1, osz);
516 memset(opts + onb, 0, sizeof(opts[onb]));
517 argc = parse_options(argc, argv, prefix, opts, usage,
518 (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
519 (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
520 PARSE_OPT_SHELL_EVAL);
522 strbuf_addstr(&parsed, " --");
523 sq_quote_argv(&parsed, argv);
528 static int cmd_sq_quote(int argc, const char **argv)
530 struct strbuf buf = STRBUF_INIT;
533 sq_quote_argv(&buf, argv);
534 printf("%s\n", buf.buf);
535 strbuf_release(&buf);
540 static void die_no_single_rev(int quiet)
545 die("Needed a single revision");
548 static const char builtin_rev_parse_usage[] =
549 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
550 " or: git rev-parse --sq-quote [<arg>...]\n"
551 " or: git rev-parse [<options>] [<arg>...]\n"
553 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
556 * Parse "opt" or "opt=<value>", setting value respectively to either
557 * NULL or the string after "=".
559 static int opt_with_value(const char *arg, const char *opt, const char **value)
561 if (skip_prefix(arg, opt, &arg)) {
574 static void handle_ref_opt(const char *pattern, const char *prefix)
577 for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
579 for_each_ref_in(prefix, show_reference, NULL);
580 clear_ref_exclusion(&ref_excludes);
583 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
585 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
586 int did_repo_setup = 0;
587 int has_dashdash = 0;
588 int output_prefix = 0;
589 struct object_id oid;
590 unsigned int flags = 0;
591 const char *name = NULL;
592 struct object_context unused;
593 struct strbuf buf = STRBUF_INIT;
595 if (argc > 1 && !strcmp("--parseopt", argv[1]))
596 return cmd_parseopt(argc - 1, argv + 1, prefix);
598 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
599 return cmd_sq_quote(argc - 2, argv + 2);
601 if (argc > 1 && !strcmp("-h", argv[1]))
602 usage(builtin_rev_parse_usage);
604 for (i = 1; i < argc; i++) {
605 if (!strcmp(argv[i], "--")) {
611 /* No options; just report on whether we're in a git repo or not. */
613 setup_git_directory();
614 git_config(git_default_config, NULL);
618 for (i = 1; i < argc; i++) {
619 const char *arg = argv[i];
621 if (!strcmp(arg, "--local-env-vars")) {
623 for (i = 0; local_repo_env[i]; i++)
624 printf("%s\n", local_repo_env[i]);
627 if (!strcmp(arg, "--resolve-git-dir")) {
628 const char *gitdir = argv[++i];
630 die("--resolve-git-dir requires an argument");
631 gitdir = resolve_gitdir(gitdir);
633 die("not a gitdir '%s'", argv[i]);
638 /* The rest of the options require a git repository. */
639 if (!did_repo_setup) {
640 prefix = setup_git_directory();
641 git_config(git_default_config, NULL);
645 if (!strcmp(arg, "--git-path")) {
647 die("--git-path requires an argument");
649 puts(relative_path(git_path("%s", argv[i + 1]),
655 if (show_file(arg, output_prefix) && as_is < 2)
656 verify_filename(prefix, arg, 0);
659 if (!strcmp(arg,"-n")) {
661 die("-n requires an argument");
662 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
668 if (starts_with(arg, "-n")) {
669 if ((filter & DO_FLAGS) && (filter & DO_REVS))
675 if (!strcmp(arg, "--")) {
677 /* Pass on the "--" if we show anything but files.. */
678 if (filter & (DO_FLAGS | DO_REVS))
682 if (!strcmp(arg, "--default")) {
685 die("--default requires an argument");
688 if (!strcmp(arg, "--prefix")) {
691 die("--prefix requires an argument");
692 startup_info->prefix = prefix;
696 if (!strcmp(arg, "--revs-only")) {
700 if (!strcmp(arg, "--no-revs")) {
704 if (!strcmp(arg, "--flags")) {
705 filter &= ~DO_NONFLAGS;
708 if (!strcmp(arg, "--no-flags")) {
712 if (!strcmp(arg, "--verify")) {
713 filter &= ~(DO_FLAGS|DO_NOREV);
717 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
719 flags |= GET_OID_QUIETLY;
722 if (opt_with_value(arg, "--short", &arg)) {
723 filter &= ~(DO_FLAGS|DO_NOREV);
725 abbrev = DEFAULT_ABBREV;
728 abbrev = strtoul(arg, NULL, 10);
729 if (abbrev < MINIMUM_ABBREV)
730 abbrev = MINIMUM_ABBREV;
731 else if (40 <= abbrev)
735 if (!strcmp(arg, "--sq")) {
739 if (!strcmp(arg, "--not")) {
740 show_type ^= REVERSED;
743 if (!strcmp(arg, "--symbolic")) {
744 symbolic = SHOW_SYMBOLIC_ASIS;
747 if (!strcmp(arg, "--symbolic-full-name")) {
748 symbolic = SHOW_SYMBOLIC_FULL;
751 if (opt_with_value(arg, "--abbrev-ref", &arg)) {
753 abbrev_ref_strict = warn_ambiguous_refs;
755 if (!strcmp(arg, "strict"))
756 abbrev_ref_strict = 1;
757 else if (!strcmp(arg, "loose"))
758 abbrev_ref_strict = 0;
760 die("unknown mode for --abbrev-ref: %s",
765 if (!strcmp(arg, "--all")) {
766 for_each_ref(show_reference, NULL);
769 if (skip_prefix(arg, "--disambiguate=", &arg)) {
770 for_each_abbrev(arg, show_abbrev, NULL);
773 if (!strcmp(arg, "--bisect")) {
774 for_each_fullref_in("refs/bisect/bad", show_reference, NULL, 0);
775 for_each_fullref_in("refs/bisect/good", anti_reference, NULL, 0);
778 if (opt_with_value(arg, "--branches", &arg)) {
779 handle_ref_opt(arg, "refs/heads/");
782 if (opt_with_value(arg, "--tags", &arg)) {
783 handle_ref_opt(arg, "refs/tags/");
786 if (skip_prefix(arg, "--glob=", &arg)) {
787 handle_ref_opt(arg, NULL);
790 if (opt_with_value(arg, "--remotes", &arg)) {
791 handle_ref_opt(arg, "refs/remotes/");
794 if (skip_prefix(arg, "--exclude=", &arg)) {
795 add_ref_exclusion(&ref_excludes, arg);
798 if (!strcmp(arg, "--show-toplevel")) {
799 const char *work_tree = get_git_work_tree();
804 if (!strcmp(arg, "--show-superproject-working-tree")) {
805 const char *superproject = get_superproject_working_tree();
810 if (!strcmp(arg, "--show-prefix")) {
817 if (!strcmp(arg, "--show-cdup")) {
818 const char *pfx = prefix;
819 if (!is_inside_work_tree()) {
820 const char *work_tree =
823 printf("%s\n", work_tree);
827 pfx = strchr(pfx, '/');
836 if (!strcmp(arg, "--git-dir") ||
837 !strcmp(arg, "--absolute-git-dir")) {
838 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
841 if (arg[2] == 'g') { /* --git-dir */
850 } else { /* --absolute-git-dir */
851 if (!gitdir && !prefix)
854 puts(real_path(gitdir));
860 printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : "");
864 if (!strcmp(arg, "--git-common-dir")) {
866 puts(relative_path(get_git_common_dir(),
870 if (!strcmp(arg, "--is-inside-git-dir")) {
871 printf("%s\n", is_inside_git_dir() ? "true"
875 if (!strcmp(arg, "--is-inside-work-tree")) {
876 printf("%s\n", is_inside_work_tree() ? "true"
880 if (!strcmp(arg, "--is-bare-repository")) {
881 printf("%s\n", is_bare_repository() ? "true"
885 if (!strcmp(arg, "--is-shallow-repository")) {
887 is_repository_shallow(the_repository) ? "true"
891 if (!strcmp(arg, "--shared-index-path")) {
892 if (read_cache() < 0)
893 die(_("Could not read the index"));
894 if (the_index.split_index) {
895 const struct object_id *oid = &the_index.split_index->base_oid;
896 const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
898 puts(relative_path(path, prefix, &buf));
902 if (skip_prefix(arg, "--since=", &arg)) {
903 show_datestring("--max-age=", arg);
906 if (skip_prefix(arg, "--after=", &arg)) {
907 show_datestring("--max-age=", arg);
910 if (skip_prefix(arg, "--before=", &arg)) {
911 show_datestring("--min-age=", arg);
914 if (skip_prefix(arg, "--until=", &arg)) {
915 show_datestring("--min-age=", arg);
918 if (show_flag(arg) && verify)
919 die_no_single_rev(quiet);
923 /* Not a flag argument */
924 if (try_difference(arg))
926 if (try_parent_shorthands(arg))
934 if (!get_oid_with_context(name, flags, &oid, &unused)) {
938 show_rev(type, &oid, name);
942 die_no_single_rev(quiet);
944 die("bad revision '%s'", arg);
946 if (!show_file(arg, output_prefix))
948 verify_filename(prefix, arg, 1);
950 strbuf_release(&buf);
952 if (revs_count == 1) {
953 show_rev(type, &oid, name);
955 } else if (revs_count == 0 && show_default())
957 die_no_single_rev(quiet);