4 * Copyright (C) Linus Torvalds, 2005
12 #include "parse-options.h"
15 #include "split-index.h"
16 #include "submodule.h"
17 #include "commit-reach.h"
23 static int filter = ~0;
25 static const char *def;
29 static int show_type = NORMAL;
31 #define SHOW_SYMBOLIC_ASIS 1
32 #define SHOW_SYMBOLIC_FULL 2
35 static int abbrev_ref;
36 static int abbrev_ref_strict;
39 static int stuck_long;
40 static struct string_list *ref_excludes;
43 * Some arguments are relevant "revision" arguments,
44 * others are about output format or other details.
45 * This sorts it all out.
47 static int is_rev_argument(const char *arg)
49 static const char *rev_args[] = {
80 const char **p = rev_args;
82 /* accept -<digit>, like traditional "head" */
83 if ((*arg == '-') && isdigit(arg[1]))
87 const char *str = *p++;
92 if (!strcmp(arg, str) ||
93 (str[len-1] == '=' && !strncmp(arg, str, len)))
98 /* Output argument as a string, either SQ or normal */
99 static void show(const char *arg)
105 while ((ch = *arg++)) {
107 fputs("'\\'", stdout);
117 /* Like show(), but with a negation prefix according to type */
118 static void show_with_type(int type, const char *arg)
120 if (type != show_type)
125 /* Output a revision, only if filter allows it */
126 static void show_rev(int type, const struct object_id *oid, const char *name)
128 if (!(filter & DO_REVS))
132 if ((symbolic || abbrev_ref) && name) {
133 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
134 struct object_id discard;
137 switch (dwim_ref(name, strlen(name), &discard, &full)) {
140 * Not found -- not a ref. We could
141 * emit "name" here, but symbolic-full
142 * users are interested in finding the
143 * refs spelled in full, and they would
144 * need to filter non-refs if we did so.
149 full = shorten_unambiguous_ref(full,
151 show_with_type(type, full);
153 default: /* ambiguous */
154 error("refname '%s' is ambiguous", name);
159 show_with_type(type, name);
163 show_with_type(type, find_unique_abbrev(oid, abbrev));
165 show_with_type(type, oid_to_hex(oid));
168 /* Output a flag, only if filter allows it. */
169 static int show_flag(const char *arg)
171 if (!(filter & DO_FLAGS))
173 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
180 static int show_default(void)
185 struct object_id oid;
188 if (!get_oid(s, &oid)) {
189 show_rev(NORMAL, &oid, s);
196 static int show_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
198 if (ref_excluded(ref_excludes, refname))
200 show_rev(NORMAL, oid, refname);
204 static int anti_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
206 show_rev(REVERSED, oid, refname);
210 static int show_abbrev(const struct object_id *oid, void *cb_data)
212 show_rev(NORMAL, oid, NULL);
216 static void show_datestring(const char *flag, const char *datestr)
220 /* date handling requires both flags and revs */
221 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
223 buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr));
228 static int show_file(const char *arg, int output_prefix)
231 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
233 const char *prefix = startup_info->prefix;
234 char *fname = prefix_filename(prefix, arg);
244 static int try_difference(const char *arg)
247 struct object_id start_oid;
248 struct object_id end_oid;
252 static const char head_by_default[] = "HEAD";
254 if (!(dotdot = strstr(arg, "..")))
258 symmetric = (*end == '.');
264 end = head_by_default;
266 start = head_by_default;
268 if (start == head_by_default && end == head_by_default &&
271 * Just ".."? That is not a range but the
272 * pathspec for the parent directory.
278 if (!get_oid_committish(start, &start_oid) && !get_oid_committish(end, &end_oid)) {
279 show_rev(NORMAL, &end_oid, end);
280 show_rev(symmetric ? NORMAL : REVERSED, &start_oid, start);
282 struct commit_list *exclude;
283 struct commit *a, *b;
284 a = lookup_commit_reference(the_repository, &start_oid);
285 b = lookup_commit_reference(the_repository, &end_oid);
290 exclude = get_merge_bases(a, b);
292 struct commit *commit = pop_commit(&exclude);
293 show_rev(REVERSED, &commit->object.oid, NULL);
303 static int try_parent_shorthands(const char *arg)
306 struct object_id oid;
307 struct commit *commit;
308 struct commit_list *parents;
311 int include_parents = 0;
312 int exclude_parent = 0;
314 if ((dotdot = strstr(arg, "^!"))) {
318 } else if ((dotdot = strstr(arg, "^@"))) {
322 } else if ((dotdot = strstr(arg, "^-"))) {
328 exclude_parent = strtoul(dotdot + 2, &end, 10);
329 if (*end != '\0' || !exclude_parent)
336 if (get_oid_committish(arg, &oid) ||
337 !(commit = lookup_commit_reference(the_repository, &oid))) {
342 if (exclude_parent &&
343 exclude_parent > commit_list_count(commit->parents)) {
349 show_rev(NORMAL, &oid, arg);
350 for (parents = commit->parents, parent_number = 1;
352 parents = parents->next, parent_number++) {
355 if (exclude_parent && parent_number != exclude_parent)
359 name = xstrfmt("%s^%d", arg, parent_number);
360 show_rev(include_parents ? NORMAL : REVERSED,
361 &parents->item->object.oid, name);
369 static int parseopt_dump(const struct option *o, const char *arg, int unset)
371 struct strbuf *parsed = o->value;
373 strbuf_addf(parsed, " --no-%s", o->long_name);
374 else if (o->short_name && (o->long_name == NULL || !stuck_long))
375 strbuf_addf(parsed, " -%c", o->short_name);
377 strbuf_addf(parsed, " --%s", o->long_name);
380 strbuf_addch(parsed, ' ');
381 else if (o->long_name)
382 strbuf_addch(parsed, '=');
383 sq_quote_buf(parsed, arg);
388 static const char *skipspaces(const char *s)
395 static char *findspace(const char *s)
403 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
405 static int keep_dashdash = 0, stop_at_non_option = 0;
406 static char const * const parseopt_usage[] = {
407 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
410 static struct option parseopt_opts[] = {
411 OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
412 N_("keep the `--` passed as an arg")),
413 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
414 N_("stop parsing after the "
415 "first non-option argument")),
416 OPT_BOOL(0, "stuck-long", &stuck_long,
417 N_("output in stuck long form")),
420 static const char * const flag_chars = "*=?!";
422 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
423 const char **usage = NULL;
424 struct option *opts = NULL;
425 int onb = 0, osz = 0, unb = 0, usz = 0;
427 strbuf_addstr(&parsed, "set --");
428 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
429 PARSE_OPT_KEEP_DASHDASH);
430 if (argc < 1 || strcmp(argv[0], "--"))
431 usage_with_options(parseopt_usage, parseopt_opts);
433 /* get the usage up to the first line with a -- on it */
435 if (strbuf_getline(&sb, stdin) == EOF)
436 die("premature end of input");
437 ALLOC_GROW(usage, unb + 1, usz);
438 if (!strcmp("--", sb.buf)) {
440 die("no usage string given before the `--' separator");
444 usage[unb++] = strbuf_detach(&sb, NULL);
447 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
448 while (strbuf_getline(&sb, stdin) != EOF) {
456 ALLOC_GROW(opts, onb + 1, osz);
457 memset(opts + onb, 0, sizeof(opts[onb]));
460 help = findspace(sb.buf);
461 if (!help || sb.buf == help) {
462 o->type = OPTION_GROUP;
463 o->help = xstrdup(skipspaces(sb.buf));
469 o->type = OPTION_CALLBACK;
470 o->help = xstrdup(skipspaces(help+1));
472 o->flags = PARSE_OPT_NOARG;
473 o->callback = &parseopt_dump;
476 s = strpbrk(sb.buf, flag_chars);
480 if (s - sb.buf == 1) /* short option only */
481 o->short_name = *sb.buf;
482 else if (sb.buf[1] != ',') /* long option only */
483 o->long_name = xmemdupz(sb.buf, s - sb.buf);
485 o->short_name = *sb.buf;
486 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
493 o->flags &= ~PARSE_OPT_NOARG;
496 o->flags &= ~PARSE_OPT_NOARG;
497 o->flags |= PARSE_OPT_OPTARG;
500 o->flags |= PARSE_OPT_NONEG;
503 o->flags |= PARSE_OPT_HIDDEN;
511 o->argh = xmemdupz(s, help - s);
515 /* put an OPT_END() */
516 ALLOC_GROW(opts, onb + 1, osz);
517 memset(opts + onb, 0, sizeof(opts[onb]));
518 argc = parse_options(argc, argv, prefix, opts, usage,
519 (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
520 (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
521 PARSE_OPT_SHELL_EVAL);
523 strbuf_addstr(&parsed, " --");
524 sq_quote_argv(&parsed, argv);
529 static int cmd_sq_quote(int argc, const char **argv)
531 struct strbuf buf = STRBUF_INIT;
534 sq_quote_argv(&buf, argv);
535 printf("%s\n", buf.buf);
536 strbuf_release(&buf);
541 static void die_no_single_rev(int quiet)
546 die("Needed a single revision");
549 static const char builtin_rev_parse_usage[] =
550 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
551 " or: git rev-parse --sq-quote [<arg>...]\n"
552 " or: git rev-parse [<options>] [<arg>...]\n"
554 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
557 * Parse "opt" or "opt=<value>", setting value respectively to either
558 * NULL or the string after "=".
560 static int opt_with_value(const char *arg, const char *opt, const char **value)
562 if (skip_prefix(arg, opt, &arg)) {
575 static void handle_ref_opt(const char *pattern, const char *prefix)
578 for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
580 for_each_ref_in(prefix, show_reference, NULL);
581 clear_ref_exclusion(&ref_excludes);
584 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
586 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
587 int did_repo_setup = 0;
588 int has_dashdash = 0;
589 int output_prefix = 0;
590 struct object_id oid;
591 unsigned int flags = 0;
592 const char *name = NULL;
593 struct object_context unused;
594 struct strbuf buf = STRBUF_INIT;
596 if (argc > 1 && !strcmp("--parseopt", argv[1]))
597 return cmd_parseopt(argc - 1, argv + 1, prefix);
599 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
600 return cmd_sq_quote(argc - 2, argv + 2);
602 if (argc > 1 && !strcmp("-h", argv[1]))
603 usage(builtin_rev_parse_usage);
605 for (i = 1; i < argc; i++) {
606 if (!strcmp(argv[i], "--")) {
612 /* No options; just report on whether we're in a git repo or not. */
614 setup_git_directory();
615 git_config(git_default_config, NULL);
619 for (i = 1; i < argc; i++) {
620 const char *arg = argv[i];
622 if (!strcmp(arg, "--local-env-vars")) {
624 for (i = 0; local_repo_env[i]; i++)
625 printf("%s\n", local_repo_env[i]);
628 if (!strcmp(arg, "--resolve-git-dir")) {
629 const char *gitdir = argv[++i];
631 die("--resolve-git-dir requires an argument");
632 gitdir = resolve_gitdir(gitdir);
634 die("not a gitdir '%s'", argv[i]);
639 /* The rest of the options require a git repository. */
640 if (!did_repo_setup) {
641 prefix = setup_git_directory();
642 git_config(git_default_config, NULL);
646 if (!strcmp(arg, "--git-path")) {
648 die("--git-path requires an argument");
650 puts(relative_path(git_path("%s", argv[i + 1]),
656 if (show_file(arg, output_prefix) && as_is < 2)
657 verify_filename(prefix, arg, 0);
660 if (!strcmp(arg,"-n")) {
662 die("-n requires an argument");
663 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
669 if (starts_with(arg, "-n")) {
670 if ((filter & DO_FLAGS) && (filter & DO_REVS))
676 if (!strcmp(arg, "--")) {
678 /* Pass on the "--" if we show anything but files.. */
679 if (filter & (DO_FLAGS | DO_REVS))
683 if (!strcmp(arg, "--default")) {
686 die("--default requires an argument");
689 if (!strcmp(arg, "--prefix")) {
692 die("--prefix requires an argument");
693 startup_info->prefix = prefix;
697 if (!strcmp(arg, "--revs-only")) {
701 if (!strcmp(arg, "--no-revs")) {
705 if (!strcmp(arg, "--flags")) {
706 filter &= ~DO_NONFLAGS;
709 if (!strcmp(arg, "--no-flags")) {
713 if (!strcmp(arg, "--verify")) {
714 filter &= ~(DO_FLAGS|DO_NOREV);
718 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
720 flags |= GET_OID_QUIETLY;
723 if (opt_with_value(arg, "--short", &arg)) {
724 filter &= ~(DO_FLAGS|DO_NOREV);
726 abbrev = DEFAULT_ABBREV;
729 abbrev = strtoul(arg, NULL, 10);
730 if (abbrev < MINIMUM_ABBREV)
731 abbrev = MINIMUM_ABBREV;
732 else if (40 <= abbrev)
736 if (!strcmp(arg, "--sq")) {
740 if (!strcmp(arg, "--not")) {
741 show_type ^= REVERSED;
744 if (!strcmp(arg, "--symbolic")) {
745 symbolic = SHOW_SYMBOLIC_ASIS;
748 if (!strcmp(arg, "--symbolic-full-name")) {
749 symbolic = SHOW_SYMBOLIC_FULL;
752 if (opt_with_value(arg, "--abbrev-ref", &arg)) {
754 abbrev_ref_strict = warn_ambiguous_refs;
756 if (!strcmp(arg, "strict"))
757 abbrev_ref_strict = 1;
758 else if (!strcmp(arg, "loose"))
759 abbrev_ref_strict = 0;
761 die("unknown mode for --abbrev-ref: %s",
766 if (!strcmp(arg, "--all")) {
767 for_each_ref(show_reference, NULL);
770 if (skip_prefix(arg, "--disambiguate=", &arg)) {
771 for_each_abbrev(arg, show_abbrev, NULL);
774 if (!strcmp(arg, "--bisect")) {
775 for_each_fullref_in("refs/bisect/bad", show_reference, NULL, 0);
776 for_each_fullref_in("refs/bisect/good", anti_reference, NULL, 0);
779 if (opt_with_value(arg, "--branches", &arg)) {
780 handle_ref_opt(arg, "refs/heads/");
783 if (opt_with_value(arg, "--tags", &arg)) {
784 handle_ref_opt(arg, "refs/tags/");
787 if (skip_prefix(arg, "--glob=", &arg)) {
788 handle_ref_opt(arg, NULL);
791 if (opt_with_value(arg, "--remotes", &arg)) {
792 handle_ref_opt(arg, "refs/remotes/");
795 if (skip_prefix(arg, "--exclude=", &arg)) {
796 add_ref_exclusion(&ref_excludes, arg);
799 if (!strcmp(arg, "--show-toplevel")) {
800 const char *work_tree = get_git_work_tree();
805 if (!strcmp(arg, "--show-superproject-working-tree")) {
806 const char *superproject = get_superproject_working_tree();
811 if (!strcmp(arg, "--show-prefix")) {
818 if (!strcmp(arg, "--show-cdup")) {
819 const char *pfx = prefix;
820 if (!is_inside_work_tree()) {
821 const char *work_tree =
824 printf("%s\n", work_tree);
828 pfx = strchr(pfx, '/');
837 if (!strcmp(arg, "--git-dir") ||
838 !strcmp(arg, "--absolute-git-dir")) {
839 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
842 if (arg[2] == 'g') { /* --git-dir */
851 } else { /* --absolute-git-dir */
852 if (!gitdir && !prefix)
855 puts(real_path(gitdir));
861 printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : "");
865 if (!strcmp(arg, "--git-common-dir")) {
867 puts(relative_path(get_git_common_dir(),
871 if (!strcmp(arg, "--is-inside-git-dir")) {
872 printf("%s\n", is_inside_git_dir() ? "true"
876 if (!strcmp(arg, "--is-inside-work-tree")) {
877 printf("%s\n", is_inside_work_tree() ? "true"
881 if (!strcmp(arg, "--is-bare-repository")) {
882 printf("%s\n", is_bare_repository() ? "true"
886 if (!strcmp(arg, "--is-shallow-repository")) {
888 is_repository_shallow(the_repository) ? "true"
892 if (!strcmp(arg, "--shared-index-path")) {
893 if (read_cache() < 0)
894 die(_("Could not read the index"));
895 if (the_index.split_index) {
896 const struct object_id *oid = &the_index.split_index->base_oid;
897 const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
899 puts(relative_path(path, prefix, &buf));
903 if (skip_prefix(arg, "--since=", &arg)) {
904 show_datestring("--max-age=", arg);
907 if (skip_prefix(arg, "--after=", &arg)) {
908 show_datestring("--max-age=", arg);
911 if (skip_prefix(arg, "--before=", &arg)) {
912 show_datestring("--min-age=", arg);
915 if (skip_prefix(arg, "--until=", &arg)) {
916 show_datestring("--min-age=", arg);
919 if (show_flag(arg) && verify)
920 die_no_single_rev(quiet);
924 /* Not a flag argument */
925 if (try_difference(arg))
927 if (try_parent_shorthands(arg))
935 if (!get_oid_with_context(name, flags, &oid, &unused)) {
939 show_rev(type, &oid, name);
943 die_no_single_rev(quiet);
945 die("bad revision '%s'", arg);
947 if (!show_file(arg, output_prefix))
949 verify_filename(prefix, arg, 1);
951 strbuf_release(&buf);
953 if (revs_count == 1) {
954 show_rev(type, &oid, name);
956 } else if (revs_count == 0 && show_default())
958 die_no_single_rev(quiet);