4 * Copyright (C) Linus Torvalds, 2005
6 #define USE_THE_INDEX_COMPATIBILITY_MACROS
13 #include "parse-options.h"
16 #include "split-index.h"
17 #include "submodule.h"
18 #include "commit-reach.h"
24 static int filter = ~0;
26 static const char *def;
30 static int show_type = NORMAL;
32 #define SHOW_SYMBOLIC_ASIS 1
33 #define SHOW_SYMBOLIC_FULL 2
36 static int abbrev_ref;
37 static int abbrev_ref_strict;
40 static int stuck_long;
41 static struct string_list *ref_excludes;
44 * Some arguments are relevant "revision" arguments,
45 * others are about output format or other details.
46 * This sorts it all out.
48 static int is_rev_argument(const char *arg)
50 static const char *rev_args[] = {
81 const char **p = rev_args;
83 /* accept -<digit>, like traditional "head" */
84 if ((*arg == '-') && isdigit(arg[1]))
88 const char *str = *p++;
93 if (!strcmp(arg, str) ||
94 (str[len-1] == '=' && !strncmp(arg, str, len)))
99 /* Output argument as a string, either SQ or normal */
100 static void show(const char *arg)
106 while ((ch = *arg++)) {
108 fputs("'\\'", stdout);
118 /* Like show(), but with a negation prefix according to type */
119 static void show_with_type(int type, const char *arg)
121 if (type != show_type)
126 /* Output a revision, only if filter allows it */
127 static void show_rev(int type, const struct object_id *oid, const char *name)
129 if (!(filter & DO_REVS))
133 if ((symbolic || abbrev_ref) && name) {
134 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
135 struct object_id discard;
138 switch (dwim_ref(name, strlen(name), &discard, &full)) {
141 * Not found -- not a ref. We could
142 * emit "name" here, but symbolic-full
143 * users are interested in finding the
144 * refs spelled in full, and they would
145 * need to filter non-refs if we did so.
150 full = shorten_unambiguous_ref(full,
152 show_with_type(type, full);
154 default: /* ambiguous */
155 error("refname '%s' is ambiguous", name);
160 show_with_type(type, name);
164 show_with_type(type, find_unique_abbrev(oid, abbrev));
166 show_with_type(type, oid_to_hex(oid));
169 /* Output a flag, only if filter allows it. */
170 static int show_flag(const char *arg)
172 if (!(filter & DO_FLAGS))
174 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
181 static int show_default(void)
186 struct object_id oid;
189 if (!get_oid(s, &oid)) {
190 show_rev(NORMAL, &oid, s);
197 static int show_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
199 if (ref_excluded(ref_excludes, refname))
201 show_rev(NORMAL, oid, refname);
205 static int anti_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
207 show_rev(REVERSED, oid, refname);
211 static int show_abbrev(const struct object_id *oid, void *cb_data)
213 show_rev(NORMAL, oid, NULL);
217 static void show_datestring(const char *flag, const char *datestr)
221 /* date handling requires both flags and revs */
222 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
224 buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr));
229 static int show_file(const char *arg, int output_prefix)
232 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
234 const char *prefix = startup_info->prefix;
235 char *fname = prefix_filename(prefix, arg);
245 static int try_difference(const char *arg)
248 struct object_id start_oid;
249 struct object_id end_oid;
253 static const char head_by_default[] = "HEAD";
255 if (!(dotdot = strstr(arg, "..")))
259 symmetric = (*end == '.');
265 end = head_by_default;
267 start = head_by_default;
269 if (start == head_by_default && end == head_by_default &&
272 * Just ".."? That is not a range but the
273 * pathspec for the parent directory.
279 if (!get_oid_committish(start, &start_oid) && !get_oid_committish(end, &end_oid)) {
280 show_rev(NORMAL, &end_oid, end);
281 show_rev(symmetric ? NORMAL : REVERSED, &start_oid, start);
283 struct commit_list *exclude;
284 struct commit *a, *b;
285 a = lookup_commit_reference(the_repository, &start_oid);
286 b = lookup_commit_reference(the_repository, &end_oid);
291 exclude = get_merge_bases(a, b);
293 struct commit *commit = pop_commit(&exclude);
294 show_rev(REVERSED, &commit->object.oid, NULL);
304 static int try_parent_shorthands(const char *arg)
307 struct object_id oid;
308 struct commit *commit;
309 struct commit_list *parents;
312 int include_parents = 0;
313 int exclude_parent = 0;
315 if ((dotdot = strstr(arg, "^!"))) {
319 } else if ((dotdot = strstr(arg, "^@"))) {
323 } else if ((dotdot = strstr(arg, "^-"))) {
329 exclude_parent = strtoul(dotdot + 2, &end, 10);
330 if (*end != '\0' || !exclude_parent)
337 if (get_oid_committish(arg, &oid) ||
338 !(commit = lookup_commit_reference(the_repository, &oid))) {
343 if (exclude_parent &&
344 exclude_parent > commit_list_count(commit->parents)) {
350 show_rev(NORMAL, &oid, arg);
351 for (parents = commit->parents, parent_number = 1;
353 parents = parents->next, parent_number++) {
356 if (exclude_parent && parent_number != exclude_parent)
360 name = xstrfmt("%s^%d", arg, parent_number);
361 show_rev(include_parents ? NORMAL : REVERSED,
362 &parents->item->object.oid, name);
370 static int parseopt_dump(const struct option *o, const char *arg, int unset)
372 struct strbuf *parsed = o->value;
374 strbuf_addf(parsed, " --no-%s", o->long_name);
375 else if (o->short_name && (o->long_name == NULL || !stuck_long))
376 strbuf_addf(parsed, " -%c", o->short_name);
378 strbuf_addf(parsed, " --%s", o->long_name);
381 strbuf_addch(parsed, ' ');
382 else if (o->long_name)
383 strbuf_addch(parsed, '=');
384 sq_quote_buf(parsed, arg);
389 static const char *skipspaces(const char *s)
396 static char *findspace(const char *s)
404 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
406 static int keep_dashdash = 0, stop_at_non_option = 0;
407 static char const * const parseopt_usage[] = {
408 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
411 static struct option parseopt_opts[] = {
412 OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
413 N_("keep the `--` passed as an arg")),
414 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
415 N_("stop parsing after the "
416 "first non-option argument")),
417 OPT_BOOL(0, "stuck-long", &stuck_long,
418 N_("output in stuck long form")),
421 static const char * const flag_chars = "*=?!";
423 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
424 const char **usage = NULL;
425 struct option *opts = NULL;
426 int onb = 0, osz = 0, unb = 0, usz = 0;
428 strbuf_addstr(&parsed, "set --");
429 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
430 PARSE_OPT_KEEP_DASHDASH);
431 if (argc < 1 || strcmp(argv[0], "--"))
432 usage_with_options(parseopt_usage, parseopt_opts);
434 /* get the usage up to the first line with a -- on it */
436 if (strbuf_getline(&sb, stdin) == EOF)
437 die("premature end of input");
438 ALLOC_GROW(usage, unb + 1, usz);
439 if (!strcmp("--", sb.buf)) {
441 die("no usage string given before the `--' separator");
445 usage[unb++] = strbuf_detach(&sb, NULL);
448 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
449 while (strbuf_getline(&sb, stdin) != EOF) {
457 ALLOC_GROW(opts, onb + 1, osz);
458 memset(opts + onb, 0, sizeof(opts[onb]));
461 help = findspace(sb.buf);
462 if (!help || sb.buf == help) {
463 o->type = OPTION_GROUP;
464 o->help = xstrdup(skipspaces(sb.buf));
470 o->type = OPTION_CALLBACK;
471 o->help = xstrdup(skipspaces(help+1));
473 o->flags = PARSE_OPT_NOARG;
474 o->callback = &parseopt_dump;
477 s = strpbrk(sb.buf, flag_chars);
481 if (s - sb.buf == 1) /* short option only */
482 o->short_name = *sb.buf;
483 else if (sb.buf[1] != ',') /* long option only */
484 o->long_name = xmemdupz(sb.buf, s - sb.buf);
486 o->short_name = *sb.buf;
487 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
494 o->flags &= ~PARSE_OPT_NOARG;
497 o->flags &= ~PARSE_OPT_NOARG;
498 o->flags |= PARSE_OPT_OPTARG;
501 o->flags |= PARSE_OPT_NONEG;
504 o->flags |= PARSE_OPT_HIDDEN;
512 o->argh = xmemdupz(s, help - s);
516 /* put an OPT_END() */
517 ALLOC_GROW(opts, onb + 1, osz);
518 memset(opts + onb, 0, sizeof(opts[onb]));
519 argc = parse_options(argc, argv, prefix, opts, usage,
520 (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
521 (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
522 PARSE_OPT_SHELL_EVAL);
524 strbuf_addstr(&parsed, " --");
525 sq_quote_argv(&parsed, argv);
530 static int cmd_sq_quote(int argc, const char **argv)
532 struct strbuf buf = STRBUF_INIT;
535 sq_quote_argv(&buf, argv);
536 printf("%s\n", buf.buf);
537 strbuf_release(&buf);
542 static void die_no_single_rev(int quiet)
547 die("Needed a single revision");
550 static const char builtin_rev_parse_usage[] =
551 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
552 " or: git rev-parse --sq-quote [<arg>...]\n"
553 " or: git rev-parse [<options>] [<arg>...]\n"
555 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
558 * Parse "opt" or "opt=<value>", setting value respectively to either
559 * NULL or the string after "=".
561 static int opt_with_value(const char *arg, const char *opt, const char **value)
563 if (skip_prefix(arg, opt, &arg)) {
576 static void handle_ref_opt(const char *pattern, const char *prefix)
579 for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
581 for_each_ref_in(prefix, show_reference, NULL);
582 clear_ref_exclusion(&ref_excludes);
585 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
587 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
588 int did_repo_setup = 0;
589 int has_dashdash = 0;
590 int output_prefix = 0;
591 struct object_id oid;
592 unsigned int flags = 0;
593 const char *name = NULL;
594 struct object_context unused;
595 struct strbuf buf = STRBUF_INIT;
596 const int hexsz = the_hash_algo->hexsz;
598 if (argc > 1 && !strcmp("--parseopt", argv[1]))
599 return cmd_parseopt(argc - 1, argv + 1, prefix);
601 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
602 return cmd_sq_quote(argc - 2, argv + 2);
604 if (argc > 1 && !strcmp("-h", argv[1]))
605 usage(builtin_rev_parse_usage);
607 for (i = 1; i < argc; i++) {
608 if (!strcmp(argv[i], "--")) {
614 /* No options; just report on whether we're in a git repo or not. */
616 setup_git_directory();
617 git_config(git_default_config, NULL);
621 for (i = 1; i < argc; i++) {
622 const char *arg = argv[i];
624 if (!strcmp(arg, "--local-env-vars")) {
626 for (i = 0; local_repo_env[i]; i++)
627 printf("%s\n", local_repo_env[i]);
630 if (!strcmp(arg, "--resolve-git-dir")) {
631 const char *gitdir = argv[++i];
633 die("--resolve-git-dir requires an argument");
634 gitdir = resolve_gitdir(gitdir);
636 die("not a gitdir '%s'", argv[i]);
641 /* The rest of the options require a git repository. */
642 if (!did_repo_setup) {
643 prefix = setup_git_directory();
644 git_config(git_default_config, NULL);
648 if (!strcmp(arg, "--git-path")) {
650 die("--git-path requires an argument");
652 puts(relative_path(git_path("%s", argv[i + 1]),
658 if (show_file(arg, output_prefix) && as_is < 2)
659 verify_filename(prefix, arg, 0);
662 if (!strcmp(arg,"-n")) {
664 die("-n requires an argument");
665 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
671 if (starts_with(arg, "-n")) {
672 if ((filter & DO_FLAGS) && (filter & DO_REVS))
678 if (!strcmp(arg, "--")) {
680 /* Pass on the "--" if we show anything but files.. */
681 if (filter & (DO_FLAGS | DO_REVS))
685 if (!strcmp(arg, "--default")) {
688 die("--default requires an argument");
691 if (!strcmp(arg, "--prefix")) {
694 die("--prefix requires an argument");
695 startup_info->prefix = prefix;
699 if (!strcmp(arg, "--revs-only")) {
703 if (!strcmp(arg, "--no-revs")) {
707 if (!strcmp(arg, "--flags")) {
708 filter &= ~DO_NONFLAGS;
711 if (!strcmp(arg, "--no-flags")) {
715 if (!strcmp(arg, "--verify")) {
716 filter &= ~(DO_FLAGS|DO_NOREV);
720 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
722 flags |= GET_OID_QUIETLY;
725 if (opt_with_value(arg, "--short", &arg)) {
726 filter &= ~(DO_FLAGS|DO_NOREV);
728 abbrev = DEFAULT_ABBREV;
731 abbrev = strtoul(arg, NULL, 10);
732 if (abbrev < MINIMUM_ABBREV)
733 abbrev = MINIMUM_ABBREV;
734 else if (hexsz <= abbrev)
738 if (!strcmp(arg, "--sq")) {
742 if (!strcmp(arg, "--not")) {
743 show_type ^= REVERSED;
746 if (!strcmp(arg, "--symbolic")) {
747 symbolic = SHOW_SYMBOLIC_ASIS;
750 if (!strcmp(arg, "--symbolic-full-name")) {
751 symbolic = SHOW_SYMBOLIC_FULL;
754 if (opt_with_value(arg, "--abbrev-ref", &arg)) {
756 abbrev_ref_strict = warn_ambiguous_refs;
758 if (!strcmp(arg, "strict"))
759 abbrev_ref_strict = 1;
760 else if (!strcmp(arg, "loose"))
761 abbrev_ref_strict = 0;
763 die("unknown mode for --abbrev-ref: %s",
768 if (!strcmp(arg, "--all")) {
769 for_each_ref(show_reference, NULL);
770 clear_ref_exclusion(&ref_excludes);
773 if (skip_prefix(arg, "--disambiguate=", &arg)) {
774 for_each_abbrev(arg, show_abbrev, NULL);
777 if (!strcmp(arg, "--bisect")) {
778 for_each_fullref_in("refs/bisect/bad", show_reference, NULL, 0);
779 for_each_fullref_in("refs/bisect/good", anti_reference, NULL, 0);
782 if (opt_with_value(arg, "--branches", &arg)) {
783 handle_ref_opt(arg, "refs/heads/");
786 if (opt_with_value(arg, "--tags", &arg)) {
787 handle_ref_opt(arg, "refs/tags/");
790 if (skip_prefix(arg, "--glob=", &arg)) {
791 handle_ref_opt(arg, NULL);
794 if (opt_with_value(arg, "--remotes", &arg)) {
795 handle_ref_opt(arg, "refs/remotes/");
798 if (skip_prefix(arg, "--exclude=", &arg)) {
799 add_ref_exclusion(&ref_excludes, arg);
802 if (!strcmp(arg, "--show-toplevel")) {
803 const char *work_tree = get_git_work_tree();
807 die("this operation must be run in a work tree");
810 if (!strcmp(arg, "--show-superproject-working-tree")) {
811 const char *superproject = get_superproject_working_tree();
816 if (!strcmp(arg, "--show-prefix")) {
823 if (!strcmp(arg, "--show-cdup")) {
824 const char *pfx = prefix;
825 if (!is_inside_work_tree()) {
826 const char *work_tree =
829 printf("%s\n", work_tree);
833 pfx = strchr(pfx, '/');
842 if (!strcmp(arg, "--git-dir") ||
843 !strcmp(arg, "--absolute-git-dir")) {
844 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
847 if (arg[2] == 'g') { /* --git-dir */
856 } else { /* --absolute-git-dir */
857 if (!gitdir && !prefix)
860 puts(real_path(gitdir));
866 printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : "");
870 if (!strcmp(arg, "--git-common-dir")) {
872 puts(relative_path(get_git_common_dir(),
876 if (!strcmp(arg, "--is-inside-git-dir")) {
877 printf("%s\n", is_inside_git_dir() ? "true"
881 if (!strcmp(arg, "--is-inside-work-tree")) {
882 printf("%s\n", is_inside_work_tree() ? "true"
886 if (!strcmp(arg, "--is-bare-repository")) {
887 printf("%s\n", is_bare_repository() ? "true"
891 if (!strcmp(arg, "--is-shallow-repository")) {
893 is_repository_shallow(the_repository) ? "true"
897 if (!strcmp(arg, "--shared-index-path")) {
898 if (read_cache() < 0)
899 die(_("Could not read the index"));
900 if (the_index.split_index) {
901 const struct object_id *oid = &the_index.split_index->base_oid;
902 const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
904 puts(relative_path(path, prefix, &buf));
908 if (skip_prefix(arg, "--since=", &arg)) {
909 show_datestring("--max-age=", arg);
912 if (skip_prefix(arg, "--after=", &arg)) {
913 show_datestring("--max-age=", arg);
916 if (skip_prefix(arg, "--before=", &arg)) {
917 show_datestring("--min-age=", arg);
920 if (skip_prefix(arg, "--until=", &arg)) {
921 show_datestring("--min-age=", arg);
924 if (opt_with_value(arg, "--show-object-format", &arg)) {
925 const char *val = arg ? arg : "storage";
927 if (strcmp(val, "storage") &&
928 strcmp(val, "input") &&
929 strcmp(val, "output"))
930 die("unknown mode for --show-object-format: %s",
932 puts(the_hash_algo->name);
935 if (show_flag(arg) && verify)
936 die_no_single_rev(quiet);
940 /* Not a flag argument */
941 if (try_difference(arg))
943 if (try_parent_shorthands(arg))
951 if (!get_oid_with_context(the_repository, name,
952 flags, &oid, &unused)) {
956 show_rev(type, &oid, name);
960 die_no_single_rev(quiet);
962 die("bad revision '%s'", arg);
964 if (!show_file(arg, output_prefix))
966 verify_filename(prefix, arg, 1);
968 strbuf_release(&buf);
970 if (revs_count == 1) {
971 show_rev(type, &oid, name);
973 } else if (revs_count == 0 && show_default())
975 die_no_single_rev(quiet);