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 struct object_id
*oid
, void *cb_data
)
210 show_rev(NORMAL
, oid
->hash
, NULL
);
214 static void show_datestring(const char *flag
, const char *datestr
)
218 /* date handling requires both flags and revs */
219 if ((filter
& (DO_FLAGS
| DO_REVS
)) != (DO_FLAGS
| DO_REVS
))
221 buffer
= xstrfmt("%s%lu", flag
, approxidate(datestr
));
226 static int show_file(const char *arg
, int output_prefix
)
229 if ((filter
& (DO_NONFLAGS
|DO_NOREV
)) == (DO_NONFLAGS
|DO_NOREV
)) {
231 const char *prefix
= startup_info
->prefix
;
232 char *fname
= prefix_filename(prefix
, arg
);
242 static int try_difference(const char *arg
)
245 unsigned char sha1
[20];
246 unsigned char end
[20];
250 static const char head_by_default
[] = "HEAD";
252 if (!(dotdot
= strstr(arg
, "..")))
256 symmetric
= (*next
== '.');
262 next
= head_by_default
;
264 this = head_by_default
;
266 if (this == head_by_default
&& next
== head_by_default
&&
269 * Just ".."? That is not a range but the
270 * pathspec for the parent directory.
276 if (!get_sha1_committish(this, sha1
) && !get_sha1_committish(next
, end
)) {
277 show_rev(NORMAL
, end
, next
);
278 show_rev(symmetric
? NORMAL
: REVERSED
, sha1
, this);
280 struct commit_list
*exclude
;
281 struct commit
*a
, *b
;
282 a
= lookup_commit_reference(sha1
);
283 b
= lookup_commit_reference(end
);
284 exclude
= get_merge_bases(a
, b
);
286 struct commit
*commit
= pop_commit(&exclude
);
287 show_rev(REVERSED
, commit
->object
.oid
.hash
, NULL
);
297 static int try_parent_shorthands(const char *arg
)
300 unsigned char sha1
[20];
301 struct commit
*commit
;
302 struct commit_list
*parents
;
305 int include_parents
= 0;
306 int exclude_parent
= 0;
308 if ((dotdot
= strstr(arg
, "^!"))) {
312 } else if ((dotdot
= strstr(arg
, "^@"))) {
316 } else if ((dotdot
= strstr(arg
, "^-"))) {
322 exclude_parent
= strtoul(dotdot
+ 2, &end
, 10);
323 if (*end
!= '\0' || !exclude_parent
)
330 if (get_sha1_committish(arg
, sha1
)) {
335 commit
= lookup_commit_reference(sha1
);
336 if (exclude_parent
&&
337 exclude_parent
> commit_list_count(commit
->parents
)) {
343 show_rev(NORMAL
, sha1
, arg
);
344 for (parents
= commit
->parents
, parent_number
= 1;
346 parents
= parents
->next
, parent_number
++) {
349 if (exclude_parent
&& parent_number
!= exclude_parent
)
353 name
= xstrfmt("%s^%d", arg
, parent_number
);
354 show_rev(include_parents
? NORMAL
: REVERSED
,
355 parents
->item
->object
.oid
.hash
, name
);
363 static int parseopt_dump(const struct option
*o
, const char *arg
, int unset
)
365 struct strbuf
*parsed
= o
->value
;
367 strbuf_addf(parsed
, " --no-%s", o
->long_name
);
368 else if (o
->short_name
&& (o
->long_name
== NULL
|| !stuck_long
))
369 strbuf_addf(parsed
, " -%c", o
->short_name
);
371 strbuf_addf(parsed
, " --%s", o
->long_name
);
374 strbuf_addch(parsed
, ' ');
375 else if (o
->long_name
)
376 strbuf_addch(parsed
, '=');
377 sq_quote_buf(parsed
, arg
);
382 static const char *skipspaces(const char *s
)
389 static int cmd_parseopt(int argc
, const char **argv
, const char *prefix
)
391 static int keep_dashdash
= 0, stop_at_non_option
= 0;
392 static char const * const parseopt_usage
[] = {
393 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
396 static struct option parseopt_opts
[] = {
397 OPT_BOOL(0, "keep-dashdash", &keep_dashdash
,
398 N_("keep the `--` passed as an arg")),
399 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option
,
400 N_("stop parsing after the "
401 "first non-option argument")),
402 OPT_BOOL(0, "stuck-long", &stuck_long
,
403 N_("output in stuck long form")),
406 static const char * const flag_chars
= "*=?!";
408 struct strbuf sb
= STRBUF_INIT
, parsed
= STRBUF_INIT
;
409 const char **usage
= NULL
;
410 struct option
*opts
= NULL
;
411 int onb
= 0, osz
= 0, unb
= 0, usz
= 0;
413 strbuf_addstr(&parsed
, "set --");
414 argc
= parse_options(argc
, argv
, prefix
, parseopt_opts
, parseopt_usage
,
415 PARSE_OPT_KEEP_DASHDASH
);
416 if (argc
< 1 || strcmp(argv
[0], "--"))
417 usage_with_options(parseopt_usage
, parseopt_opts
);
419 /* get the usage up to the first line with a -- on it */
421 if (strbuf_getline(&sb
, stdin
) == EOF
)
422 die("premature end of input");
423 ALLOC_GROW(usage
, unb
+ 1, usz
);
424 if (!strcmp("--", sb
.buf
)) {
426 die("no usage string given before the `--' separator");
430 usage
[unb
++] = strbuf_detach(&sb
, NULL
);
433 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
434 while (strbuf_getline(&sb
, stdin
) != EOF
) {
442 ALLOC_GROW(opts
, onb
+ 1, osz
);
443 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
446 help
= strchr(sb
.buf
, ' ');
447 if (!help
|| *sb
.buf
== ' ') {
448 o
->type
= OPTION_GROUP
;
449 o
->help
= xstrdup(skipspaces(sb
.buf
));
453 o
->type
= OPTION_CALLBACK
;
454 o
->help
= xstrdup(skipspaces(help
));
456 o
->flags
= PARSE_OPT_NOARG
;
457 o
->callback
= &parseopt_dump
;
460 s
= strpbrk(sb
.buf
, flag_chars
);
464 if (s
- sb
.buf
== 1) /* short option only */
465 o
->short_name
= *sb
.buf
;
466 else if (sb
.buf
[1] != ',') /* long option only */
467 o
->long_name
= xmemdupz(sb
.buf
, s
- sb
.buf
);
469 o
->short_name
= *sb
.buf
;
470 o
->long_name
= xmemdupz(sb
.buf
+ 2, s
- sb
.buf
- 2);
477 o
->flags
&= ~PARSE_OPT_NOARG
;
480 o
->flags
&= ~PARSE_OPT_NOARG
;
481 o
->flags
|= PARSE_OPT_OPTARG
;
484 o
->flags
|= PARSE_OPT_NONEG
;
487 o
->flags
|= PARSE_OPT_HIDDEN
;
495 o
->argh
= xmemdupz(s
, help
- s
);
499 /* put an OPT_END() */
500 ALLOC_GROW(opts
, onb
+ 1, osz
);
501 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
502 argc
= parse_options(argc
, argv
, prefix
, opts
, usage
,
503 (keep_dashdash
? PARSE_OPT_KEEP_DASHDASH
: 0) |
504 (stop_at_non_option
? PARSE_OPT_STOP_AT_NON_OPTION
: 0) |
505 PARSE_OPT_SHELL_EVAL
);
507 strbuf_addstr(&parsed
, " --");
508 sq_quote_argv(&parsed
, argv
, 0);
513 static int cmd_sq_quote(int argc
, const char **argv
)
515 struct strbuf buf
= STRBUF_INIT
;
518 sq_quote_argv(&buf
, argv
, 0);
519 printf("%s\n", buf
.buf
);
520 strbuf_release(&buf
);
525 static void die_no_single_rev(int quiet
)
530 die("Needed a single revision");
533 static const char builtin_rev_parse_usage
[] =
534 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
535 " or: git rev-parse --sq-quote [<arg>...]\n"
536 " or: git rev-parse [<options>] [<arg>...]\n"
538 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
541 * Parse "opt" or "opt=<value>", setting value respectively to either
542 * NULL or the string after "=".
544 static int opt_with_value(const char *arg
, const char *opt
, const char **value
)
546 if (skip_prefix(arg
, opt
, &arg
)) {
559 static void handle_ref_opt(const char *pattern
, const char *prefix
)
562 for_each_glob_ref_in(show_reference
, pattern
, prefix
, NULL
);
564 for_each_ref_in(prefix
, show_reference
, NULL
);
565 clear_ref_exclusion(&ref_excludes
);
568 int cmd_rev_parse(int argc
, const char **argv
, const char *prefix
)
570 int i
, as_is
= 0, verify
= 0, quiet
= 0, revs_count
= 0, type
= 0;
571 int did_repo_setup
= 0;
572 int has_dashdash
= 0;
573 int output_prefix
= 0;
574 unsigned char sha1
[20];
575 unsigned int flags
= 0;
576 const char *name
= NULL
;
577 struct object_context unused
;
578 struct strbuf buf
= STRBUF_INIT
;
580 if (argc
> 1 && !strcmp("--parseopt", argv
[1]))
581 return cmd_parseopt(argc
- 1, argv
+ 1, prefix
);
583 if (argc
> 1 && !strcmp("--sq-quote", argv
[1]))
584 return cmd_sq_quote(argc
- 2, argv
+ 2);
586 if (argc
> 1 && !strcmp("-h", argv
[1]))
587 usage(builtin_rev_parse_usage
);
589 for (i
= 1; i
< argc
; i
++) {
590 if (!strcmp(argv
[i
], "--")) {
596 /* No options; just report on whether we're in a git repo or not. */
598 setup_git_directory();
599 git_config(git_default_config
, NULL
);
603 for (i
= 1; i
< argc
; i
++) {
604 const char *arg
= argv
[i
];
606 if (!strcmp(arg
, "--local-env-vars")) {
608 for (i
= 0; local_repo_env
[i
]; i
++)
609 printf("%s\n", local_repo_env
[i
]);
612 if (!strcmp(arg
, "--resolve-git-dir")) {
613 const char *gitdir
= argv
[++i
];
615 die("--resolve-git-dir requires an argument");
616 gitdir
= resolve_gitdir(gitdir
);
618 die("not a gitdir '%s'", argv
[i
]);
623 /* The rest of the options require a git repository. */
624 if (!did_repo_setup
) {
625 prefix
= setup_git_directory();
626 git_config(git_default_config
, NULL
);
630 if (!strcmp(arg
, "--git-path")) {
632 die("--git-path requires an argument");
634 puts(relative_path(git_path("%s", argv
[i
+ 1]),
640 if (show_file(arg
, output_prefix
) && as_is
< 2)
641 verify_filename(prefix
, arg
, 0);
644 if (!strcmp(arg
,"-n")) {
646 die("-n requires an argument");
647 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
)) {
653 if (starts_with(arg
, "-n")) {
654 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
))
660 if (!strcmp(arg
, "--")) {
662 /* Pass on the "--" if we show anything but files.. */
663 if (filter
& (DO_FLAGS
| DO_REVS
))
667 if (!strcmp(arg
, "--default")) {
670 die("--default requires an argument");
673 if (!strcmp(arg
, "--prefix")) {
676 die("--prefix requires an argument");
677 startup_info
->prefix
= prefix
;
681 if (!strcmp(arg
, "--revs-only")) {
685 if (!strcmp(arg
, "--no-revs")) {
689 if (!strcmp(arg
, "--flags")) {
690 filter
&= ~DO_NONFLAGS
;
693 if (!strcmp(arg
, "--no-flags")) {
697 if (!strcmp(arg
, "--verify")) {
698 filter
&= ~(DO_FLAGS
|DO_NOREV
);
702 if (!strcmp(arg
, "--quiet") || !strcmp(arg
, "-q")) {
704 flags
|= GET_SHA1_QUIETLY
;
707 if (opt_with_value(arg
, "--short", &arg
)) {
708 filter
&= ~(DO_FLAGS
|DO_NOREV
);
710 abbrev
= DEFAULT_ABBREV
;
713 abbrev
= strtoul(arg
, NULL
, 10);
714 if (abbrev
< MINIMUM_ABBREV
)
715 abbrev
= MINIMUM_ABBREV
;
716 else if (40 <= abbrev
)
720 if (!strcmp(arg
, "--sq")) {
724 if (!strcmp(arg
, "--not")) {
725 show_type
^= REVERSED
;
728 if (!strcmp(arg
, "--symbolic")) {
729 symbolic
= SHOW_SYMBOLIC_ASIS
;
732 if (!strcmp(arg
, "--symbolic-full-name")) {
733 symbolic
= SHOW_SYMBOLIC_FULL
;
736 if (opt_with_value(arg
, "--abbrev-ref", &arg
)) {
738 abbrev_ref_strict
= warn_ambiguous_refs
;
740 if (!strcmp(arg
, "strict"))
741 abbrev_ref_strict
= 1;
742 else if (!strcmp(arg
, "loose"))
743 abbrev_ref_strict
= 0;
745 die("unknown mode for --abbrev-ref: %s",
750 if (!strcmp(arg
, "--all")) {
751 for_each_ref(show_reference
, NULL
);
754 if (skip_prefix(arg
, "--disambiguate=", &arg
)) {
755 for_each_abbrev(arg
, show_abbrev
, NULL
);
758 if (!strcmp(arg
, "--bisect")) {
759 for_each_ref_in("refs/bisect/bad", show_reference
, NULL
);
760 for_each_ref_in("refs/bisect/good", anti_reference
, NULL
);
763 if (opt_with_value(arg
, "--branches", &arg
)) {
764 handle_ref_opt(arg
, "refs/heads/");
767 if (opt_with_value(arg
, "--tags", &arg
)) {
768 handle_ref_opt(arg
, "refs/tags/");
771 if (skip_prefix(arg
, "--glob=", &arg
)) {
772 handle_ref_opt(arg
, NULL
);
775 if (opt_with_value(arg
, "--remotes", &arg
)) {
776 handle_ref_opt(arg
, "refs/remotes/");
779 if (skip_prefix(arg
, "--exclude=", &arg
)) {
780 add_ref_exclusion(&ref_excludes
, arg
);
783 if (!strcmp(arg
, "--show-toplevel")) {
784 const char *work_tree
= get_git_work_tree();
789 if (!strcmp(arg
, "--show-superproject-working-tree")) {
790 const char *superproject
= get_superproject_working_tree();
795 if (!strcmp(arg
, "--show-prefix")) {
802 if (!strcmp(arg
, "--show-cdup")) {
803 const char *pfx
= prefix
;
804 if (!is_inside_work_tree()) {
805 const char *work_tree
=
808 printf("%s\n", work_tree
);
812 pfx
= strchr(pfx
, '/');
821 if (!strcmp(arg
, "--git-dir") ||
822 !strcmp(arg
, "--absolute-git-dir")) {
823 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
826 if (arg
[2] == 'g') { /* --git-dir */
835 } else { /* --absolute-git-dir */
836 if (!gitdir
&& !prefix
)
839 puts(real_path(gitdir
));
845 printf("%s%s.git\n", cwd
, len
&& cwd
[len
-1] != '/' ? "/" : "");
849 if (!strcmp(arg
, "--git-common-dir")) {
851 puts(relative_path(get_git_common_dir(),
855 if (!strcmp(arg
, "--is-inside-git-dir")) {
856 printf("%s\n", is_inside_git_dir() ? "true"
860 if (!strcmp(arg
, "--is-inside-work-tree")) {
861 printf("%s\n", is_inside_work_tree() ? "true"
865 if (!strcmp(arg
, "--is-bare-repository")) {
866 printf("%s\n", is_bare_repository() ? "true"
870 if (!strcmp(arg
, "--shared-index-path")) {
871 if (read_cache() < 0)
872 die(_("Could not read the index"));
873 if (the_index
.split_index
) {
874 const unsigned char *sha1
= the_index
.split_index
->base_sha1
;
875 const char *path
= git_path("sharedindex.%s", sha1_to_hex(sha1
));
877 puts(relative_path(path
, prefix
, &buf
));
881 if (skip_prefix(arg
, "--since=", &arg
)) {
882 show_datestring("--max-age=", arg
);
885 if (skip_prefix(arg
, "--after=", &arg
)) {
886 show_datestring("--max-age=", arg
);
889 if (skip_prefix(arg
, "--before=", &arg
)) {
890 show_datestring("--min-age=", arg
);
893 if (skip_prefix(arg
, "--until=", &arg
)) {
894 show_datestring("--min-age=", arg
);
897 if (show_flag(arg
) && verify
)
898 die_no_single_rev(quiet
);
902 /* Not a flag argument */
903 if (try_difference(arg
))
905 if (try_parent_shorthands(arg
))
913 if (!get_sha1_with_context(name
, flags
, sha1
, &unused
)) {
917 show_rev(type
, sha1
, name
);
921 die_no_single_rev(quiet
);
923 die("bad revision '%s'", arg
);
925 if (!show_file(arg
, output_prefix
))
927 verify_filename(prefix
, arg
, 1);
929 strbuf_release(&buf
);
931 if (revs_count
== 1) {
932 show_rev(type
, sha1
, name
);
934 } else if (revs_count
== 0 && show_default())
936 die_no_single_rev(quiet
);