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"
25 static int filter
= ~0;
27 static const char *def
;
31 static int show_type
= NORMAL
;
33 #define SHOW_SYMBOLIC_ASIS 1
34 #define SHOW_SYMBOLIC_FULL 2
37 static int abbrev_ref
;
38 static int abbrev_ref_strict
;
41 static int stuck_long
;
42 static struct string_list
*ref_excludes
;
45 * Some arguments are relevant "revision" arguments,
46 * others are about output format or other details.
47 * This sorts it all out.
49 static int is_rev_argument(const char *arg
)
51 static const char *rev_args
[] = {
82 const char **p
= rev_args
;
84 /* accept -<digit>, like traditional "head" */
85 if ((*arg
== '-') && isdigit(arg
[1]))
89 const char *str
= *p
++;
94 if (!strcmp(arg
, str
) ||
95 (str
[len
-1] == '=' && !strncmp(arg
, str
, len
)))
100 /* Output argument as a string, either SQ or normal */
101 static void show(const char *arg
)
107 while ((ch
= *arg
++)) {
109 fputs("'\\'", stdout
);
119 /* Like show(), but with a negation prefix according to type */
120 static void show_with_type(int type
, const char *arg
)
122 if (type
!= show_type
)
127 /* Output a revision, only if filter allows it */
128 static void show_rev(int type
, const struct object_id
*oid
, const char *name
)
130 if (!(filter
& DO_REVS
))
134 if ((symbolic
|| abbrev_ref
) && name
) {
135 if (symbolic
== SHOW_SYMBOLIC_FULL
|| abbrev_ref
) {
136 struct object_id discard
;
139 switch (dwim_ref(name
, strlen(name
), &discard
, &full
, 0)) {
142 * Not found -- not a ref. We could
143 * emit "name" here, but symbolic-full
144 * users are interested in finding the
145 * refs spelled in full, and they would
146 * need to filter non-refs if we did so.
151 full
= shorten_unambiguous_ref(full
,
153 show_with_type(type
, full
);
155 default: /* ambiguous */
156 error("refname '%s' is ambiguous", name
);
161 show_with_type(type
, name
);
165 show_with_type(type
, find_unique_abbrev(oid
, abbrev
));
167 show_with_type(type
, oid_to_hex(oid
));
170 /* Output a flag, only if filter allows it. */
171 static int show_flag(const char *arg
)
173 if (!(filter
& DO_FLAGS
))
175 if (filter
& (is_rev_argument(arg
) ? DO_REVS
: DO_NOREV
)) {
182 static int show_default(void)
187 struct object_id oid
;
190 if (!get_oid(s
, &oid
)) {
191 show_rev(NORMAL
, &oid
, s
);
198 static int show_reference(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
200 if (ref_excluded(ref_excludes
, refname
))
202 show_rev(NORMAL
, oid
, refname
);
206 static int anti_reference(const char *refname
, const struct object_id
*oid
, int flag
, void *cb_data
)
208 show_rev(REVERSED
, oid
, refname
);
212 static int show_abbrev(const struct object_id
*oid
, void *cb_data
)
214 show_rev(NORMAL
, oid
, NULL
);
218 static void show_datestring(const char *flag
, const char *datestr
)
222 /* date handling requires both flags and revs */
223 if ((filter
& (DO_FLAGS
| DO_REVS
)) != (DO_FLAGS
| DO_REVS
))
225 buffer
= xstrfmt("%s%"PRItime
, flag
, approxidate(datestr
));
230 static int show_file(const char *arg
, int output_prefix
)
233 if ((filter
& (DO_NONFLAGS
|DO_NOREV
)) == (DO_NONFLAGS
|DO_NOREV
)) {
235 const char *prefix
= startup_info
->prefix
;
236 char *fname
= prefix_filename(prefix
, arg
);
246 static int try_difference(const char *arg
)
249 struct object_id start_oid
;
250 struct object_id end_oid
;
254 static const char head_by_default
[] = "HEAD";
256 if (!(dotdot
= strstr(arg
, "..")))
260 symmetric
= (*end
== '.');
266 end
= head_by_default
;
268 start
= head_by_default
;
270 if (start
== head_by_default
&& end
== head_by_default
&&
273 * Just ".."? That is not a range but the
274 * pathspec for the parent directory.
280 if (!get_oid_committish(start
, &start_oid
) && !get_oid_committish(end
, &end_oid
)) {
281 show_rev(NORMAL
, &end_oid
, end
);
282 show_rev(symmetric
? NORMAL
: REVERSED
, &start_oid
, start
);
284 struct commit_list
*exclude
;
285 struct commit
*a
, *b
;
286 a
= lookup_commit_reference(the_repository
, &start_oid
);
287 b
= lookup_commit_reference(the_repository
, &end_oid
);
292 exclude
= get_merge_bases(a
, b
);
294 struct commit
*commit
= pop_commit(&exclude
);
295 show_rev(REVERSED
, &commit
->object
.oid
, NULL
);
305 static int try_parent_shorthands(const char *arg
)
308 struct object_id oid
;
309 struct commit
*commit
;
310 struct commit_list
*parents
;
313 int include_parents
= 0;
314 int exclude_parent
= 0;
316 if ((dotdot
= strstr(arg
, "^!"))) {
320 } else if ((dotdot
= strstr(arg
, "^@"))) {
324 } else if ((dotdot
= strstr(arg
, "^-"))) {
330 exclude_parent
= strtoul(dotdot
+ 2, &end
, 10);
331 if (*end
!= '\0' || !exclude_parent
)
338 if (get_oid_committish(arg
, &oid
) ||
339 !(commit
= lookup_commit_reference(the_repository
, &oid
))) {
344 if (exclude_parent
&&
345 exclude_parent
> commit_list_count(commit
->parents
)) {
351 show_rev(NORMAL
, &oid
, arg
);
352 for (parents
= commit
->parents
, parent_number
= 1;
354 parents
= parents
->next
, parent_number
++) {
357 if (exclude_parent
&& parent_number
!= exclude_parent
)
361 name
= xstrfmt("%s^%d", arg
, parent_number
);
362 show_rev(include_parents
? NORMAL
: REVERSED
,
363 &parents
->item
->object
.oid
, name
);
371 static int parseopt_dump(const struct option
*o
, const char *arg
, int unset
)
373 struct strbuf
*parsed
= o
->value
;
375 strbuf_addf(parsed
, " --no-%s", o
->long_name
);
376 else if (o
->short_name
&& (o
->long_name
== NULL
|| !stuck_long
))
377 strbuf_addf(parsed
, " -%c", o
->short_name
);
379 strbuf_addf(parsed
, " --%s", o
->long_name
);
382 strbuf_addch(parsed
, ' ');
383 else if (o
->long_name
)
384 strbuf_addch(parsed
, '=');
385 sq_quote_buf(parsed
, arg
);
390 static const char *skipspaces(const char *s
)
397 static char *findspace(const char *s
)
405 static int cmd_parseopt(int argc
, const char **argv
, const char *prefix
)
407 static int keep_dashdash
= 0, stop_at_non_option
= 0;
408 static char const * const parseopt_usage
[] = {
409 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
412 static struct option parseopt_opts
[] = {
413 OPT_BOOL(0, "keep-dashdash", &keep_dashdash
,
414 N_("keep the `--` passed as an arg")),
415 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option
,
416 N_("stop parsing after the "
417 "first non-option argument")),
418 OPT_BOOL(0, "stuck-long", &stuck_long
,
419 N_("output in stuck long form")),
422 static const char * const flag_chars
= "*=?!";
424 struct strbuf sb
= STRBUF_INIT
, parsed
= STRBUF_INIT
;
425 const char **usage
= NULL
;
426 struct option
*opts
= NULL
;
427 int onb
= 0, osz
= 0, unb
= 0, usz
= 0;
429 strbuf_addstr(&parsed
, "set --");
430 argc
= parse_options(argc
, argv
, prefix
, parseopt_opts
, parseopt_usage
,
431 PARSE_OPT_KEEP_DASHDASH
);
432 if (argc
< 1 || strcmp(argv
[0], "--"))
433 usage_with_options(parseopt_usage
, parseopt_opts
);
435 /* get the usage up to the first line with a -- on it */
437 if (strbuf_getline(&sb
, stdin
) == EOF
)
438 die("premature end of input");
439 ALLOC_GROW(usage
, unb
+ 1, usz
);
440 if (!strcmp("--", sb
.buf
)) {
442 die("no usage string given before the `--' separator");
446 usage
[unb
++] = strbuf_detach(&sb
, NULL
);
449 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
450 while (strbuf_getline(&sb
, stdin
) != EOF
) {
458 ALLOC_GROW(opts
, onb
+ 1, osz
);
459 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
462 help
= findspace(sb
.buf
);
463 if (!help
|| sb
.buf
== help
) {
464 o
->type
= OPTION_GROUP
;
465 o
->help
= xstrdup(skipspaces(sb
.buf
));
471 o
->type
= OPTION_CALLBACK
;
472 o
->help
= xstrdup(skipspaces(help
+1));
474 o
->flags
= PARSE_OPT_NOARG
;
475 o
->callback
= &parseopt_dump
;
478 s
= strpbrk(sb
.buf
, flag_chars
);
482 if (s
- sb
.buf
== 1) /* short option only */
483 o
->short_name
= *sb
.buf
;
484 else if (sb
.buf
[1] != ',') /* long option only */
485 o
->long_name
= xmemdupz(sb
.buf
, s
- sb
.buf
);
487 o
->short_name
= *sb
.buf
;
488 o
->long_name
= xmemdupz(sb
.buf
+ 2, s
- sb
.buf
- 2);
495 o
->flags
&= ~PARSE_OPT_NOARG
;
498 o
->flags
&= ~PARSE_OPT_NOARG
;
499 o
->flags
|= PARSE_OPT_OPTARG
;
502 o
->flags
|= PARSE_OPT_NONEG
;
505 o
->flags
|= PARSE_OPT_HIDDEN
;
513 o
->argh
= xmemdupz(s
, help
- s
);
517 /* put an OPT_END() */
518 ALLOC_GROW(opts
, onb
+ 1, osz
);
519 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
520 argc
= parse_options(argc
, argv
, prefix
, opts
, usage
,
521 (keep_dashdash
? PARSE_OPT_KEEP_DASHDASH
: 0) |
522 (stop_at_non_option
? PARSE_OPT_STOP_AT_NON_OPTION
: 0) |
523 PARSE_OPT_SHELL_EVAL
);
525 strbuf_addstr(&parsed
, " --");
526 sq_quote_argv(&parsed
, argv
);
531 static int cmd_sq_quote(int argc
, const char **argv
)
533 struct strbuf buf
= STRBUF_INIT
;
536 sq_quote_argv(&buf
, argv
);
537 printf("%s\n", buf
.buf
);
538 strbuf_release(&buf
);
543 static void die_no_single_rev(int quiet
)
548 die("Needed a single revision");
551 static const char builtin_rev_parse_usage
[] =
552 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
553 " or: git rev-parse --sq-quote [<arg>...]\n"
554 " or: git rev-parse [<options>] [<arg>...]\n"
556 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
559 * Parse "opt" or "opt=<value>", setting value respectively to either
560 * NULL or the string after "=".
562 static int opt_with_value(const char *arg
, const char *opt
, const char **value
)
564 if (skip_prefix(arg
, opt
, &arg
)) {
577 static void handle_ref_opt(const char *pattern
, const char *prefix
)
580 for_each_glob_ref_in(show_reference
, pattern
, prefix
, NULL
);
582 for_each_ref_in(prefix
, show_reference
, NULL
);
583 clear_ref_exclusion(&ref_excludes
);
586 int cmd_rev_parse(int argc
, const char **argv
, const char *prefix
)
588 int i
, as_is
= 0, verify
= 0, quiet
= 0, revs_count
= 0, type
= 0;
589 int did_repo_setup
= 0;
590 int has_dashdash
= 0;
591 int output_prefix
= 0;
592 struct object_id oid
;
593 unsigned int flags
= 0;
594 const char *name
= NULL
;
595 struct object_context unused
;
596 struct strbuf buf
= STRBUF_INIT
;
597 const int hexsz
= the_hash_algo
->hexsz
;
598 int seen_end_of_options
= 0;
600 if (argc
> 1 && !strcmp("--parseopt", argv
[1]))
601 return cmd_parseopt(argc
- 1, argv
+ 1, prefix
);
603 if (argc
> 1 && !strcmp("--sq-quote", argv
[1]))
604 return cmd_sq_quote(argc
- 2, argv
+ 2);
606 if (argc
> 1 && !strcmp("-h", argv
[1]))
607 usage(builtin_rev_parse_usage
);
609 for (i
= 1; i
< argc
; i
++) {
610 if (!strcmp(argv
[i
], "--")) {
616 /* No options; just report on whether we're in a git repo or not. */
618 setup_git_directory();
619 git_config(git_default_config
, NULL
);
623 for (i
= 1; i
< argc
; i
++) {
624 const char *arg
= argv
[i
];
627 if (show_file(arg
, output_prefix
) && as_is
< 2)
628 verify_filename(prefix
, arg
, 0);
632 if (!seen_end_of_options
) {
633 if (!strcmp(arg
, "--local-env-vars")) {
635 for (i
= 0; local_repo_env
[i
]; i
++)
636 printf("%s\n", local_repo_env
[i
]);
639 if (!strcmp(arg
, "--resolve-git-dir")) {
640 const char *gitdir
= argv
[++i
];
642 die("--resolve-git-dir requires an argument");
643 gitdir
= resolve_gitdir(gitdir
);
645 die("not a gitdir '%s'", argv
[i
]);
651 /* The rest of the options require a git repository. */
652 if (!did_repo_setup
) {
653 prefix
= setup_git_directory();
654 git_config(git_default_config
, NULL
);
658 if (!strcmp(arg
, "--")) {
660 /* Pass on the "--" if we show anything but files.. */
661 if (filter
& (DO_FLAGS
| DO_REVS
))
666 if (!seen_end_of_options
&& *arg
== '-') {
667 if (!strcmp(arg
, "--git-path")) {
669 die("--git-path requires an argument");
671 puts(relative_path(git_path("%s", argv
[i
+ 1]),
676 if (!strcmp(arg
,"-n")) {
678 die("-n requires an argument");
679 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
)) {
685 if (starts_with(arg
, "-n")) {
686 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
))
690 if (!strcmp(arg
, "--default")) {
693 die("--default requires an argument");
696 if (!strcmp(arg
, "--prefix")) {
699 die("--prefix requires an argument");
700 startup_info
->prefix
= prefix
;
704 if (!strcmp(arg
, "--revs-only")) {
708 if (!strcmp(arg
, "--no-revs")) {
712 if (!strcmp(arg
, "--flags")) {
713 filter
&= ~DO_NONFLAGS
;
716 if (!strcmp(arg
, "--no-flags")) {
720 if (!strcmp(arg
, "--verify")) {
721 filter
&= ~(DO_FLAGS
|DO_NOREV
);
725 if (!strcmp(arg
, "--quiet") || !strcmp(arg
, "-q")) {
727 flags
|= GET_OID_QUIETLY
;
730 if (opt_with_value(arg
, "--short", &arg
)) {
731 filter
&= ~(DO_FLAGS
|DO_NOREV
);
733 abbrev
= DEFAULT_ABBREV
;
736 abbrev
= strtoul(arg
, NULL
, 10);
737 if (abbrev
< MINIMUM_ABBREV
)
738 abbrev
= MINIMUM_ABBREV
;
739 else if (hexsz
<= abbrev
)
743 if (!strcmp(arg
, "--sq")) {
747 if (!strcmp(arg
, "--not")) {
748 show_type
^= REVERSED
;
751 if (!strcmp(arg
, "--symbolic")) {
752 symbolic
= SHOW_SYMBOLIC_ASIS
;
755 if (!strcmp(arg
, "--symbolic-full-name")) {
756 symbolic
= SHOW_SYMBOLIC_FULL
;
759 if (opt_with_value(arg
, "--abbrev-ref", &arg
)) {
761 abbrev_ref_strict
= warn_ambiguous_refs
;
763 if (!strcmp(arg
, "strict"))
764 abbrev_ref_strict
= 1;
765 else if (!strcmp(arg
, "loose"))
766 abbrev_ref_strict
= 0;
768 die("unknown mode for --abbrev-ref: %s",
773 if (!strcmp(arg
, "--all")) {
774 for_each_ref(show_reference
, NULL
);
775 clear_ref_exclusion(&ref_excludes
);
778 if (skip_prefix(arg
, "--disambiguate=", &arg
)) {
779 for_each_abbrev(arg
, show_abbrev
, NULL
);
782 if (!strcmp(arg
, "--bisect")) {
783 for_each_fullref_in("refs/bisect/bad", show_reference
, NULL
, 0);
784 for_each_fullref_in("refs/bisect/good", anti_reference
, NULL
, 0);
787 if (opt_with_value(arg
, "--branches", &arg
)) {
788 handle_ref_opt(arg
, "refs/heads/");
791 if (opt_with_value(arg
, "--tags", &arg
)) {
792 handle_ref_opt(arg
, "refs/tags/");
795 if (skip_prefix(arg
, "--glob=", &arg
)) {
796 handle_ref_opt(arg
, NULL
);
799 if (opt_with_value(arg
, "--remotes", &arg
)) {
800 handle_ref_opt(arg
, "refs/remotes/");
803 if (skip_prefix(arg
, "--exclude=", &arg
)) {
804 add_ref_exclusion(&ref_excludes
, arg
);
807 if (!strcmp(arg
, "--show-toplevel")) {
808 const char *work_tree
= get_git_work_tree();
812 die("this operation must be run in a work tree");
815 if (!strcmp(arg
, "--show-superproject-working-tree")) {
816 struct strbuf superproject
= STRBUF_INIT
;
817 if (get_superproject_working_tree(&superproject
))
818 puts(superproject
.buf
);
819 strbuf_release(&superproject
);
822 if (!strcmp(arg
, "--show-prefix")) {
829 if (!strcmp(arg
, "--show-cdup")) {
830 const char *pfx
= prefix
;
831 if (!is_inside_work_tree()) {
832 const char *work_tree
=
835 printf("%s\n", work_tree
);
839 pfx
= strchr(pfx
, '/');
848 if (!strcmp(arg
, "--git-dir") ||
849 !strcmp(arg
, "--absolute-git-dir")) {
850 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
853 if (arg
[2] == 'g') { /* --git-dir */
862 } else { /* --absolute-git-dir */
863 if (!gitdir
&& !prefix
)
866 struct strbuf realpath
= STRBUF_INIT
;
867 strbuf_realpath(&realpath
, gitdir
, 1);
869 strbuf_release(&realpath
);
875 printf("%s%s.git\n", cwd
, len
&& cwd
[len
-1] != '/' ? "/" : "");
879 if (!strcmp(arg
, "--git-common-dir")) {
881 puts(relative_path(get_git_common_dir(),
885 if (!strcmp(arg
, "--is-inside-git-dir")) {
886 printf("%s\n", is_inside_git_dir() ? "true"
890 if (!strcmp(arg
, "--is-inside-work-tree")) {
891 printf("%s\n", is_inside_work_tree() ? "true"
895 if (!strcmp(arg
, "--is-bare-repository")) {
896 printf("%s\n", is_bare_repository() ? "true"
900 if (!strcmp(arg
, "--is-shallow-repository")) {
902 is_repository_shallow(the_repository
) ? "true"
906 if (!strcmp(arg
, "--shared-index-path")) {
907 if (read_cache() < 0)
908 die(_("Could not read the index"));
909 if (the_index
.split_index
) {
910 const struct object_id
*oid
= &the_index
.split_index
->base_oid
;
911 const char *path
= git_path("sharedindex.%s", oid_to_hex(oid
));
913 puts(relative_path(path
, prefix
, &buf
));
917 if (skip_prefix(arg
, "--since=", &arg
)) {
918 show_datestring("--max-age=", arg
);
921 if (skip_prefix(arg
, "--after=", &arg
)) {
922 show_datestring("--max-age=", arg
);
925 if (skip_prefix(arg
, "--before=", &arg
)) {
926 show_datestring("--min-age=", arg
);
929 if (skip_prefix(arg
, "--until=", &arg
)) {
930 show_datestring("--min-age=", arg
);
933 if (opt_with_value(arg
, "--show-object-format", &arg
)) {
934 const char *val
= arg
? arg
: "storage";
936 if (strcmp(val
, "storage") &&
937 strcmp(val
, "input") &&
938 strcmp(val
, "output"))
939 die("unknown mode for --show-object-format: %s",
941 puts(the_hash_algo
->name
);
944 if (!strcmp(arg
, "--end-of-options")) {
945 seen_end_of_options
= 1;
946 if (filter
& (DO_FLAGS
| DO_REVS
))
950 if (show_flag(arg
) && verify
)
951 die_no_single_rev(quiet
);
955 /* Not a flag argument */
956 if (try_difference(arg
))
958 if (try_parent_shorthands(arg
))
966 if (!get_oid_with_context(the_repository
, name
,
967 flags
, &oid
, &unused
)) {
971 show_rev(type
, &oid
, name
);
975 die_no_single_rev(quiet
);
977 die("bad revision '%s'", arg
);
979 if (!show_file(arg
, output_prefix
))
981 verify_filename(prefix
, arg
, 1);
983 strbuf_release(&buf
);
985 if (revs_count
== 1) {
986 show_rev(type
, &oid
, name
);
988 } else if (revs_count
== 0 && show_default())
990 die_no_single_rev(quiet
);