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
.hash
, &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
->hash
, 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 oid
;
247 struct object_id end
;
251 static const char head_by_default
[] = "HEAD";
253 if (!(dotdot
= strstr(arg
, "..")))
257 symmetric
= (*next
== '.');
263 next
= head_by_default
;
265 this = head_by_default
;
267 if (this == head_by_default
&& next
== head_by_default
&&
270 * Just ".."? That is not a range but the
271 * pathspec for the parent directory.
277 if (!get_sha1_committish(this, oid
.hash
) && !get_sha1_committish(next
, end
.hash
)) {
278 show_rev(NORMAL
, &end
, next
);
279 show_rev(symmetric
? NORMAL
: REVERSED
, &oid
, this);
281 struct commit_list
*exclude
;
282 struct commit
*a
, *b
;
283 a
= lookup_commit_reference(&oid
);
284 b
= lookup_commit_reference(&end
);
285 exclude
= get_merge_bases(a
, b
);
287 struct commit
*commit
= pop_commit(&exclude
);
288 show_rev(REVERSED
, &commit
->object
.oid
, NULL
);
298 static int try_parent_shorthands(const char *arg
)
301 struct object_id oid
;
302 struct commit
*commit
;
303 struct commit_list
*parents
;
306 int include_parents
= 0;
307 int exclude_parent
= 0;
309 if ((dotdot
= strstr(arg
, "^!"))) {
313 } else if ((dotdot
= strstr(arg
, "^@"))) {
317 } else if ((dotdot
= strstr(arg
, "^-"))) {
323 exclude_parent
= strtoul(dotdot
+ 2, &end
, 10);
324 if (*end
!= '\0' || !exclude_parent
)
331 if (get_sha1_committish(arg
, oid
.hash
)) {
336 commit
= lookup_commit_reference(&oid
);
337 if (exclude_parent
&&
338 exclude_parent
> commit_list_count(commit
->parents
)) {
344 show_rev(NORMAL
, &oid
, arg
);
345 for (parents
= commit
->parents
, parent_number
= 1;
347 parents
= parents
->next
, parent_number
++) {
350 if (exclude_parent
&& parent_number
!= exclude_parent
)
354 name
= xstrfmt("%s^%d", arg
, parent_number
);
355 show_rev(include_parents
? NORMAL
: REVERSED
,
356 &parents
->item
->object
.oid
, name
);
364 static int parseopt_dump(const struct option
*o
, const char *arg
, int unset
)
366 struct strbuf
*parsed
= o
->value
;
368 strbuf_addf(parsed
, " --no-%s", o
->long_name
);
369 else if (o
->short_name
&& (o
->long_name
== NULL
|| !stuck_long
))
370 strbuf_addf(parsed
, " -%c", o
->short_name
);
372 strbuf_addf(parsed
, " --%s", o
->long_name
);
375 strbuf_addch(parsed
, ' ');
376 else if (o
->long_name
)
377 strbuf_addch(parsed
, '=');
378 sq_quote_buf(parsed
, arg
);
383 static const char *skipspaces(const char *s
)
390 static int cmd_parseopt(int argc
, const char **argv
, const char *prefix
)
392 static int keep_dashdash
= 0, stop_at_non_option
= 0;
393 static char const * const parseopt_usage
[] = {
394 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
397 static struct option parseopt_opts
[] = {
398 OPT_BOOL(0, "keep-dashdash", &keep_dashdash
,
399 N_("keep the `--` passed as an arg")),
400 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option
,
401 N_("stop parsing after the "
402 "first non-option argument")),
403 OPT_BOOL(0, "stuck-long", &stuck_long
,
404 N_("output in stuck long form")),
407 static const char * const flag_chars
= "*=?!";
409 struct strbuf sb
= STRBUF_INIT
, parsed
= STRBUF_INIT
;
410 const char **usage
= NULL
;
411 struct option
*opts
= NULL
;
412 int onb
= 0, osz
= 0, unb
= 0, usz
= 0;
414 strbuf_addstr(&parsed
, "set --");
415 argc
= parse_options(argc
, argv
, prefix
, parseopt_opts
, parseopt_usage
,
416 PARSE_OPT_KEEP_DASHDASH
);
417 if (argc
< 1 || strcmp(argv
[0], "--"))
418 usage_with_options(parseopt_usage
, parseopt_opts
);
420 /* get the usage up to the first line with a -- on it */
422 if (strbuf_getline(&sb
, stdin
) == EOF
)
423 die("premature end of input");
424 ALLOC_GROW(usage
, unb
+ 1, usz
);
425 if (!strcmp("--", sb
.buf
)) {
427 die("no usage string given before the `--' separator");
431 usage
[unb
++] = strbuf_detach(&sb
, NULL
);
434 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
435 while (strbuf_getline(&sb
, stdin
) != EOF
) {
443 ALLOC_GROW(opts
, onb
+ 1, osz
);
444 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
447 help
= strchr(sb
.buf
, ' ');
448 if (!help
|| *sb
.buf
== ' ') {
449 o
->type
= OPTION_GROUP
;
450 o
->help
= xstrdup(skipspaces(sb
.buf
));
454 o
->type
= OPTION_CALLBACK
;
455 o
->help
= xstrdup(skipspaces(help
));
457 o
->flags
= PARSE_OPT_NOARG
;
458 o
->callback
= &parseopt_dump
;
461 s
= strpbrk(sb
.buf
, flag_chars
);
465 if (s
- sb
.buf
== 1) /* short option only */
466 o
->short_name
= *sb
.buf
;
467 else if (sb
.buf
[1] != ',') /* long option only */
468 o
->long_name
= xmemdupz(sb
.buf
, s
- sb
.buf
);
470 o
->short_name
= *sb
.buf
;
471 o
->long_name
= xmemdupz(sb
.buf
+ 2, s
- sb
.buf
- 2);
478 o
->flags
&= ~PARSE_OPT_NOARG
;
481 o
->flags
&= ~PARSE_OPT_NOARG
;
482 o
->flags
|= PARSE_OPT_OPTARG
;
485 o
->flags
|= PARSE_OPT_NONEG
;
488 o
->flags
|= PARSE_OPT_HIDDEN
;
496 o
->argh
= xmemdupz(s
, help
- s
);
500 /* put an OPT_END() */
501 ALLOC_GROW(opts
, onb
+ 1, osz
);
502 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
503 argc
= parse_options(argc
, argv
, prefix
, opts
, usage
,
504 (keep_dashdash
? PARSE_OPT_KEEP_DASHDASH
: 0) |
505 (stop_at_non_option
? PARSE_OPT_STOP_AT_NON_OPTION
: 0) |
506 PARSE_OPT_SHELL_EVAL
);
508 strbuf_addstr(&parsed
, " --");
509 sq_quote_argv(&parsed
, argv
, 0);
514 static int cmd_sq_quote(int argc
, const char **argv
)
516 struct strbuf buf
= STRBUF_INIT
;
519 sq_quote_argv(&buf
, argv
, 0);
520 printf("%s\n", buf
.buf
);
521 strbuf_release(&buf
);
526 static void die_no_single_rev(int quiet
)
531 die("Needed a single revision");
534 static const char builtin_rev_parse_usage
[] =
535 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
536 " or: git rev-parse --sq-quote [<arg>...]\n"
537 " or: git rev-parse [<options>] [<arg>...]\n"
539 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
542 * Parse "opt" or "opt=<value>", setting value respectively to either
543 * NULL or the string after "=".
545 static int opt_with_value(const char *arg
, const char *opt
, const char **value
)
547 if (skip_prefix(arg
, opt
, &arg
)) {
560 static void handle_ref_opt(const char *pattern
, const char *prefix
)
563 for_each_glob_ref_in(show_reference
, pattern
, prefix
, NULL
);
565 for_each_ref_in(prefix
, show_reference
, NULL
);
566 clear_ref_exclusion(&ref_excludes
);
569 int cmd_rev_parse(int argc
, const char **argv
, const char *prefix
)
571 int i
, as_is
= 0, verify
= 0, quiet
= 0, revs_count
= 0, type
= 0;
572 int did_repo_setup
= 0;
573 int has_dashdash
= 0;
574 int output_prefix
= 0;
575 struct object_id oid
;
576 unsigned int flags
= 0;
577 const char *name
= NULL
;
578 struct object_context unused
;
579 struct strbuf buf
= STRBUF_INIT
;
581 if (argc
> 1 && !strcmp("--parseopt", argv
[1]))
582 return cmd_parseopt(argc
- 1, argv
+ 1, prefix
);
584 if (argc
> 1 && !strcmp("--sq-quote", argv
[1]))
585 return cmd_sq_quote(argc
- 2, argv
+ 2);
587 if (argc
> 1 && !strcmp("-h", argv
[1]))
588 usage(builtin_rev_parse_usage
);
590 for (i
= 1; i
< argc
; i
++) {
591 if (!strcmp(argv
[i
], "--")) {
597 /* No options; just report on whether we're in a git repo or not. */
599 setup_git_directory();
600 git_config(git_default_config
, NULL
);
604 for (i
= 1; i
< argc
; i
++) {
605 const char *arg
= argv
[i
];
607 if (!strcmp(arg
, "--local-env-vars")) {
609 for (i
= 0; local_repo_env
[i
]; i
++)
610 printf("%s\n", local_repo_env
[i
]);
613 if (!strcmp(arg
, "--resolve-git-dir")) {
614 const char *gitdir
= argv
[++i
];
616 die("--resolve-git-dir requires an argument");
617 gitdir
= resolve_gitdir(gitdir
);
619 die("not a gitdir '%s'", argv
[i
]);
624 /* The rest of the options require a git repository. */
625 if (!did_repo_setup
) {
626 prefix
= setup_git_directory();
627 git_config(git_default_config
, NULL
);
631 if (!strcmp(arg
, "--git-path")) {
633 die("--git-path requires an argument");
635 puts(relative_path(git_path("%s", argv
[i
+ 1]),
641 if (show_file(arg
, output_prefix
) && as_is
< 2)
642 verify_filename(prefix
, arg
, 0);
645 if (!strcmp(arg
,"-n")) {
647 die("-n requires an argument");
648 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
)) {
654 if (starts_with(arg
, "-n")) {
655 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
))
661 if (!strcmp(arg
, "--")) {
663 /* Pass on the "--" if we show anything but files.. */
664 if (filter
& (DO_FLAGS
| DO_REVS
))
668 if (!strcmp(arg
, "--default")) {
671 die("--default requires an argument");
674 if (!strcmp(arg
, "--prefix")) {
677 die("--prefix requires an argument");
678 startup_info
->prefix
= prefix
;
682 if (!strcmp(arg
, "--revs-only")) {
686 if (!strcmp(arg
, "--no-revs")) {
690 if (!strcmp(arg
, "--flags")) {
691 filter
&= ~DO_NONFLAGS
;
694 if (!strcmp(arg
, "--no-flags")) {
698 if (!strcmp(arg
, "--verify")) {
699 filter
&= ~(DO_FLAGS
|DO_NOREV
);
703 if (!strcmp(arg
, "--quiet") || !strcmp(arg
, "-q")) {
705 flags
|= GET_SHA1_QUIETLY
;
708 if (opt_with_value(arg
, "--short", &arg
)) {
709 filter
&= ~(DO_FLAGS
|DO_NOREV
);
711 abbrev
= DEFAULT_ABBREV
;
714 abbrev
= strtoul(arg
, NULL
, 10);
715 if (abbrev
< MINIMUM_ABBREV
)
716 abbrev
= MINIMUM_ABBREV
;
717 else if (40 <= abbrev
)
721 if (!strcmp(arg
, "--sq")) {
725 if (!strcmp(arg
, "--not")) {
726 show_type
^= REVERSED
;
729 if (!strcmp(arg
, "--symbolic")) {
730 symbolic
= SHOW_SYMBOLIC_ASIS
;
733 if (!strcmp(arg
, "--symbolic-full-name")) {
734 symbolic
= SHOW_SYMBOLIC_FULL
;
737 if (opt_with_value(arg
, "--abbrev-ref", &arg
)) {
739 abbrev_ref_strict
= warn_ambiguous_refs
;
741 if (!strcmp(arg
, "strict"))
742 abbrev_ref_strict
= 1;
743 else if (!strcmp(arg
, "loose"))
744 abbrev_ref_strict
= 0;
746 die("unknown mode for --abbrev-ref: %s",
751 if (!strcmp(arg
, "--all")) {
752 for_each_ref(show_reference
, NULL
);
755 if (skip_prefix(arg
, "--disambiguate=", &arg
)) {
756 for_each_abbrev(arg
, show_abbrev
, NULL
);
759 if (!strcmp(arg
, "--bisect")) {
760 for_each_ref_in("refs/bisect/bad", show_reference
, NULL
);
761 for_each_ref_in("refs/bisect/good", anti_reference
, NULL
);
764 if (opt_with_value(arg
, "--branches", &arg
)) {
765 handle_ref_opt(arg
, "refs/heads/");
768 if (opt_with_value(arg
, "--tags", &arg
)) {
769 handle_ref_opt(arg
, "refs/tags/");
772 if (skip_prefix(arg
, "--glob=", &arg
)) {
773 handle_ref_opt(arg
, NULL
);
776 if (opt_with_value(arg
, "--remotes", &arg
)) {
777 handle_ref_opt(arg
, "refs/remotes/");
780 if (skip_prefix(arg
, "--exclude=", &arg
)) {
781 add_ref_exclusion(&ref_excludes
, arg
);
784 if (!strcmp(arg
, "--show-toplevel")) {
785 const char *work_tree
= get_git_work_tree();
790 if (!strcmp(arg
, "--show-superproject-working-tree")) {
791 const char *superproject
= get_superproject_working_tree();
796 if (!strcmp(arg
, "--show-prefix")) {
803 if (!strcmp(arg
, "--show-cdup")) {
804 const char *pfx
= prefix
;
805 if (!is_inside_work_tree()) {
806 const char *work_tree
=
809 printf("%s\n", work_tree
);
813 pfx
= strchr(pfx
, '/');
822 if (!strcmp(arg
, "--git-dir") ||
823 !strcmp(arg
, "--absolute-git-dir")) {
824 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
827 if (arg
[2] == 'g') { /* --git-dir */
836 } else { /* --absolute-git-dir */
837 if (!gitdir
&& !prefix
)
840 puts(real_path(gitdir
));
846 printf("%s%s.git\n", cwd
, len
&& cwd
[len
-1] != '/' ? "/" : "");
850 if (!strcmp(arg
, "--git-common-dir")) {
852 puts(relative_path(get_git_common_dir(),
856 if (!strcmp(arg
, "--is-inside-git-dir")) {
857 printf("%s\n", is_inside_git_dir() ? "true"
861 if (!strcmp(arg
, "--is-inside-work-tree")) {
862 printf("%s\n", is_inside_work_tree() ? "true"
866 if (!strcmp(arg
, "--is-bare-repository")) {
867 printf("%s\n", is_bare_repository() ? "true"
871 if (!strcmp(arg
, "--shared-index-path")) {
872 if (read_cache() < 0)
873 die(_("Could not read the index"));
874 if (the_index
.split_index
) {
875 const unsigned char *sha1
= the_index
.split_index
->base_sha1
;
876 const char *path
= git_path("sharedindex.%s", sha1_to_hex(sha1
));
878 puts(relative_path(path
, prefix
, &buf
));
882 if (skip_prefix(arg
, "--since=", &arg
)) {
883 show_datestring("--max-age=", arg
);
886 if (skip_prefix(arg
, "--after=", &arg
)) {
887 show_datestring("--max-age=", arg
);
890 if (skip_prefix(arg
, "--before=", &arg
)) {
891 show_datestring("--min-age=", arg
);
894 if (skip_prefix(arg
, "--until=", &arg
)) {
895 show_datestring("--min-age=", arg
);
898 if (show_flag(arg
) && verify
)
899 die_no_single_rev(quiet
);
903 /* Not a flag argument */
904 if (try_difference(arg
))
906 if (try_parent_shorthands(arg
))
914 if (!get_sha1_with_context(name
, flags
, oid
.hash
, &unused
)) {
918 show_rev(type
, &oid
, name
);
922 die_no_single_rev(quiet
);
924 die("bad revision '%s'", arg
);
926 if (!show_file(arg
, output_prefix
))
928 verify_filename(prefix
, arg
, 1);
930 strbuf_release(&buf
);
932 if (revs_count
== 1) {
933 show_rev(type
, &oid
, name
);
935 } else if (revs_count
== 0 && show_default())
937 die_no_single_rev(quiet
);