4 * Copyright (C) Linus Torvalds, 2005
11 #include "environment.h"
17 #include "object-name.h"
18 #include "parse-options.h"
21 #include "read-cache-ll.h"
24 #include "split-index.h"
25 #include "submodule.h"
26 #include "commit-reach.h"
28 #include "object-file-convert.h"
34 static int filter
= ~0;
36 static const char *def
;
40 static int show_type
= NORMAL
;
42 #define SHOW_SYMBOLIC_ASIS 1
43 #define SHOW_SYMBOLIC_FULL 2
46 static int abbrev_ref
;
47 static int abbrev_ref_strict
;
50 static int stuck_long
;
51 static struct ref_exclusions ref_excludes
= REF_EXCLUSIONS_INIT
;
54 * Some arguments are relevant "revision" arguments,
55 * others are about output format or other details.
56 * This sorts it all out.
58 static int is_rev_argument(const char *arg
)
60 static const char *rev_args
[] = {
91 const char **p
= rev_args
;
93 /* accept -<digit>, like traditional "head" */
94 if ((*arg
== '-') && isdigit(arg
[1]))
98 const char *str
= *p
++;
103 if (!strcmp(arg
, str
) ||
104 (str
[len
-1] == '=' && !strncmp(arg
, str
, len
)))
109 /* Output argument as a string, either SQ or normal */
110 static void show(const char *arg
)
116 while ((ch
= *arg
++)) {
118 fputs("'\\'", stdout
);
128 /* Like show(), but with a negation prefix according to type */
129 static void show_with_type(int type
, const char *arg
)
131 if (type
!= show_type
)
136 /* Output a revision, only if filter allows it */
137 static void show_rev(int type
, const struct object_id
*oid
, const char *name
)
139 if (!(filter
& DO_REVS
))
143 if ((symbolic
|| abbrev_ref
) && name
) {
144 if (symbolic
== SHOW_SYMBOLIC_FULL
|| abbrev_ref
) {
145 struct object_id discard
;
148 switch (repo_dwim_ref(the_repository
, name
,
149 strlen(name
), &discard
, &full
,
153 * Not found -- not a ref. We could
154 * emit "name" here, but symbolic-full
155 * users are interested in finding the
156 * refs spelled in full, and they would
157 * need to filter non-refs if we did so.
163 full
= refs_shorten_unambiguous_ref(get_main_ref_store(the_repository
),
168 show_with_type(type
, full
);
170 default: /* ambiguous */
171 error("refname '%s' is ambiguous", name
);
176 show_with_type(type
, name
);
181 repo_find_unique_abbrev(the_repository
, oid
, abbrev
));
183 show_with_type(type
, oid_to_hex(oid
));
186 /* Output a flag, only if filter allows it. */
187 static int show_flag(const char *arg
)
189 if (!(filter
& DO_FLAGS
))
191 if (filter
& (is_rev_argument(arg
) ? DO_REVS
: DO_NOREV
)) {
198 static int show_default(void)
203 struct object_id oid
;
206 if (!repo_get_oid(the_repository
, s
, &oid
)) {
207 show_rev(NORMAL
, &oid
, s
);
214 static int show_reference(const char *refname
, const struct object_id
*oid
,
215 int flag UNUSED
, void *cb_data UNUSED
)
217 if (ref_excluded(&ref_excludes
, refname
))
219 show_rev(NORMAL
, oid
, refname
);
223 static int anti_reference(const char *refname
, const struct object_id
*oid
,
224 int flag UNUSED
, void *cb_data UNUSED
)
226 show_rev(REVERSED
, oid
, refname
);
230 static int show_abbrev(const struct object_id
*oid
, void *cb_data UNUSED
)
232 show_rev(NORMAL
, oid
, NULL
);
236 static void show_datestring(const char *flag
, const char *datestr
)
240 /* date handling requires both flags and revs */
241 if ((filter
& (DO_FLAGS
| DO_REVS
)) != (DO_FLAGS
| DO_REVS
))
243 buffer
= xstrfmt("%s%"PRItime
, flag
, approxidate(datestr
));
248 static int show_file(const char *arg
, int output_prefix
)
251 if ((filter
& (DO_NONFLAGS
|DO_NOREV
)) == (DO_NONFLAGS
|DO_NOREV
)) {
253 const char *prefix
= startup_info
->prefix
;
254 char *fname
= prefix_filename(prefix
, arg
);
264 static int try_difference(const char *arg
)
267 struct object_id start_oid
;
268 struct object_id end_oid
;
272 static const char head_by_default
[] = "HEAD";
274 if (!(dotdot
= strstr(arg
, "..")))
278 symmetric
= (*end
== '.');
284 end
= head_by_default
;
286 start
= head_by_default
;
288 if (start
== head_by_default
&& end
== head_by_default
&&
291 * Just ".."? That is not a range but the
292 * pathspec for the parent directory.
298 if (!repo_get_oid_committish(the_repository
, start
, &start_oid
) && !repo_get_oid_committish(the_repository
, end
, &end_oid
)) {
299 show_rev(NORMAL
, &end_oid
, end
);
300 show_rev(symmetric
? NORMAL
: REVERSED
, &start_oid
, start
);
302 struct commit_list
*exclude
= NULL
;
303 struct commit
*a
, *b
;
304 a
= lookup_commit_reference(the_repository
, &start_oid
);
305 b
= lookup_commit_reference(the_repository
, &end_oid
);
310 if (repo_get_merge_bases(the_repository
, a
, b
, &exclude
) < 0)
313 struct commit
*commit
= pop_commit(&exclude
);
314 show_rev(REVERSED
, &commit
->object
.oid
, NULL
);
324 static int try_parent_shorthands(const char *arg
)
327 struct object_id oid
;
328 struct commit
*commit
;
329 struct commit_list
*parents
;
332 int include_parents
= 0;
333 int exclude_parent
= 0;
335 if ((dotdot
= strstr(arg
, "^!"))) {
339 } else if ((dotdot
= strstr(arg
, "^@"))) {
343 } else if ((dotdot
= strstr(arg
, "^-"))) {
349 exclude_parent
= strtoul(dotdot
+ 2, &end
, 10);
350 if (*end
!= '\0' || !exclude_parent
)
357 if (repo_get_oid_committish(the_repository
, arg
, &oid
) ||
358 !(commit
= lookup_commit_reference(the_repository
, &oid
))) {
363 if (exclude_parent
&&
364 exclude_parent
> commit_list_count(commit
->parents
)) {
370 show_rev(NORMAL
, &oid
, arg
);
371 for (parents
= commit
->parents
, parent_number
= 1;
373 parents
= parents
->next
, parent_number
++) {
376 if (exclude_parent
&& parent_number
!= exclude_parent
)
380 name
= xstrfmt("%s^%d", arg
, parent_number
);
381 show_rev(include_parents
? NORMAL
: REVERSED
,
382 &parents
->item
->object
.oid
, name
);
390 static int parseopt_dump(const struct option
*o
, const char *arg
, int unset
)
392 struct strbuf
*parsed
= o
->value
;
394 strbuf_addf(parsed
, " --no-%s", o
->long_name
);
395 else if (o
->short_name
&& (o
->long_name
== NULL
|| !stuck_long
))
396 strbuf_addf(parsed
, " -%c", o
->short_name
);
398 strbuf_addf(parsed
, " --%s", o
->long_name
);
401 strbuf_addch(parsed
, ' ');
402 else if (o
->long_name
)
403 strbuf_addch(parsed
, '=');
404 sq_quote_buf(parsed
, arg
);
409 static const char *skipspaces(const char *s
)
416 static char *findspace(const char *s
)
424 static int cmd_parseopt(int argc
, const char **argv
, const char *prefix
)
426 static int keep_dashdash
= 0, stop_at_non_option
= 0;
427 static char const * const parseopt_usage
[] = {
428 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
431 static struct option parseopt_opts
[] = {
432 OPT_BOOL(0, "keep-dashdash", &keep_dashdash
,
433 N_("keep the `--` passed as an arg")),
434 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option
,
435 N_("stop parsing after the "
436 "first non-option argument")),
437 OPT_BOOL(0, "stuck-long", &stuck_long
,
438 N_("output in stuck long form")),
441 static const char * const flag_chars
= "*=?!";
443 struct strbuf sb
= STRBUF_INIT
, parsed
= STRBUF_INIT
;
444 const char **usage
= NULL
;
445 struct option
*opts
= NULL
;
446 int onb
= 0, osz
= 0, unb
= 0, usz
= 0;
448 strbuf_addstr(&parsed
, "set --");
449 argc
= parse_options(argc
, argv
, prefix
, parseopt_opts
, parseopt_usage
,
450 PARSE_OPT_KEEP_DASHDASH
);
451 if (argc
< 1 || strcmp(argv
[0], "--"))
452 usage_with_options(parseopt_usage
, parseopt_opts
);
454 /* get the usage up to the first line with a -- on it */
456 if (strbuf_getline(&sb
, stdin
) == EOF
)
457 die(_("premature end of input"));
458 ALLOC_GROW(usage
, unb
+ 1, usz
);
459 if (!strcmp("--", sb
.buf
)) {
461 die(_("no usage string given before the `--' separator"));
465 usage
[unb
++] = strbuf_detach(&sb
, NULL
);
468 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
469 while (strbuf_getline(&sb
, stdin
) != EOF
) {
477 ALLOC_GROW(opts
, onb
+ 1, osz
);
478 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
481 help
= findspace(sb
.buf
);
482 if (!help
|| sb
.buf
== help
) {
483 o
->type
= OPTION_GROUP
;
484 o
->help
= xstrdup(skipspaces(sb
.buf
));
490 o
->type
= OPTION_CALLBACK
;
491 o
->help
= xstrdup(skipspaces(help
+1));
493 o
->flags
= PARSE_OPT_NOARG
;
494 o
->callback
= &parseopt_dump
;
497 s
= strpbrk(sb
.buf
, flag_chars
);
502 die(_("missing opt-spec before option flags"));
504 if (s
- sb
.buf
== 1) /* short option only */
505 o
->short_name
= *sb
.buf
;
506 else if (sb
.buf
[1] != ',') /* long option only */
507 o
->long_name
= xmemdupz(sb
.buf
, s
- sb
.buf
);
509 o
->short_name
= *sb
.buf
;
510 o
->long_name
= xmemdupz(sb
.buf
+ 2, s
- sb
.buf
- 2);
517 o
->flags
&= ~PARSE_OPT_NOARG
;
520 o
->flags
&= ~PARSE_OPT_NOARG
;
521 o
->flags
|= PARSE_OPT_OPTARG
;
524 o
->flags
|= PARSE_OPT_NONEG
;
527 o
->flags
|= PARSE_OPT_HIDDEN
;
535 o
->argh
= xmemdupz(s
, help
- s
);
539 /* put an OPT_END() */
540 ALLOC_GROW(opts
, onb
+ 1, osz
);
541 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
542 argc
= parse_options(argc
, argv
, prefix
, opts
, usage
,
543 (keep_dashdash
? PARSE_OPT_KEEP_DASHDASH
: 0) |
544 (stop_at_non_option
? PARSE_OPT_STOP_AT_NON_OPTION
: 0) |
545 PARSE_OPT_SHELL_EVAL
);
547 strbuf_addstr(&parsed
, " --");
548 sq_quote_argv(&parsed
, argv
);
550 strbuf_release(&parsed
);
554 static int cmd_sq_quote(int argc
, const char **argv
)
556 struct strbuf buf
= STRBUF_INIT
;
559 sq_quote_argv(&buf
, argv
);
560 printf("%s\n", buf
.buf
);
561 strbuf_release(&buf
);
566 static void die_no_single_rev(int quiet
)
571 die(_("Needed a single revision"));
574 static const char builtin_rev_parse_usage
[] =
575 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
576 " or: git rev-parse --sq-quote [<arg>...]\n"
577 " or: git rev-parse [<options>] [<arg>...]\n"
579 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
582 * Parse "opt" or "opt=<value>", setting value respectively to either
583 * NULL or the string after "=".
585 static int opt_with_value(const char *arg
, const char *opt
, const char **value
)
587 if (skip_prefix(arg
, opt
, &arg
)) {
600 static void handle_ref_opt(const char *pattern
, const char *prefix
)
603 refs_for_each_glob_ref_in(get_main_ref_store(the_repository
),
604 show_reference
, pattern
, prefix
,
607 refs_for_each_ref_in(get_main_ref_store(the_repository
),
608 prefix
, show_reference
, NULL
);
609 clear_ref_exclusions(&ref_excludes
);
613 /* We would like a relative path. */
615 /* We would like a canonical absolute path. */
617 /* We would like the default behavior. */
622 /* Our default is a relative path. */
624 /* Our default is a relative path if there's a shared root. */
625 DEFAULT_RELATIVE_IF_SHARED
,
626 /* Our default is a canonical absolute path. */
628 /* Our default is not to modify the item. */
632 static void print_path(const char *path
, const char *prefix
, enum format_type format
, enum default_type def
)
636 * We don't ever produce a relative path if prefix is NULL, so set the
637 * prefix to the current directory so that we can produce a relative
638 * path whenever possible. If we're using RELATIVE_IF_SHARED mode, then
639 * we want an absolute path unless the two share a common prefix, so don't
640 * set it in that case, since doing so causes a relative path to always
641 * be produced if possible.
643 if (!prefix
&& (format
!= FORMAT_DEFAULT
|| def
!= DEFAULT_RELATIVE_IF_SHARED
))
644 prefix
= cwd
= xgetcwd();
645 if (format
== FORMAT_DEFAULT
&& def
== DEFAULT_UNMODIFIED
) {
647 } else if (format
== FORMAT_RELATIVE
||
648 (format
== FORMAT_DEFAULT
&& def
== DEFAULT_RELATIVE
)) {
650 * In order for relative_path to work as expected, we need to
651 * make sure that both paths are absolute paths. If we don't,
652 * we can end up with an unexpected absolute path that the user
655 struct strbuf buf
= STRBUF_INIT
, realbuf
= STRBUF_INIT
, prefixbuf
= STRBUF_INIT
;
656 if (!is_absolute_path(path
)) {
657 strbuf_realpath_forgiving(&realbuf
, path
, 1);
660 if (!is_absolute_path(prefix
)) {
661 strbuf_realpath_forgiving(&prefixbuf
, prefix
, 1);
662 prefix
= prefixbuf
.buf
;
664 puts(relative_path(path
, prefix
, &buf
));
665 strbuf_release(&buf
);
666 strbuf_release(&realbuf
);
667 strbuf_release(&prefixbuf
);
668 } else if (format
== FORMAT_DEFAULT
&& def
== DEFAULT_RELATIVE_IF_SHARED
) {
669 struct strbuf buf
= STRBUF_INIT
;
670 puts(relative_path(path
, prefix
, &buf
));
671 strbuf_release(&buf
);
673 struct strbuf buf
= STRBUF_INIT
;
674 strbuf_realpath_forgiving(&buf
, path
, 1);
676 strbuf_release(&buf
);
681 int cmd_rev_parse(int argc
, const char **argv
, const char *prefix
)
683 int i
, as_is
= 0, verify
= 0, quiet
= 0, revs_count
= 0, type
= 0;
684 const struct git_hash_algo
*output_algo
= NULL
;
685 const struct git_hash_algo
*compat
= NULL
;
686 int did_repo_setup
= 0;
687 int has_dashdash
= 0;
688 int output_prefix
= 0;
689 struct object_id oid
;
690 unsigned int flags
= 0;
691 const char *name
= NULL
;
692 struct object_context unused
;
693 struct strbuf buf
= STRBUF_INIT
;
694 const int hexsz
= the_hash_algo
->hexsz
;
695 int seen_end_of_options
= 0;
696 enum format_type format
= FORMAT_DEFAULT
;
698 if (argc
> 1 && !strcmp("--parseopt", argv
[1]))
699 return cmd_parseopt(argc
- 1, argv
+ 1, prefix
);
701 if (argc
> 1 && !strcmp("--sq-quote", argv
[1]))
702 return cmd_sq_quote(argc
- 2, argv
+ 2);
704 if (argc
> 1 && !strcmp("-h", argv
[1]))
705 usage(builtin_rev_parse_usage
);
707 for (i
= 1; i
< argc
; i
++) {
708 if (!strcmp(argv
[i
], "--")) {
714 /* No options; just report on whether we're in a git repo or not. */
716 setup_git_directory();
717 git_config(git_default_config
, NULL
);
721 for (i
= 1; i
< argc
; i
++) {
722 const char *arg
= argv
[i
];
725 if (show_file(arg
, output_prefix
) && as_is
< 2)
726 verify_filename(prefix
, arg
, 0);
730 if (!seen_end_of_options
) {
731 if (!strcmp(arg
, "--local-env-vars")) {
733 for (i
= 0; local_repo_env
[i
]; i
++)
734 printf("%s\n", local_repo_env
[i
]);
737 if (!strcmp(arg
, "--resolve-git-dir")) {
738 const char *gitdir
= argv
[++i
];
740 die(_("--resolve-git-dir requires an argument"));
741 gitdir
= resolve_gitdir(gitdir
);
743 die(_("not a gitdir '%s'"), argv
[i
]);
749 /* The rest of the options require a git repository. */
750 if (!did_repo_setup
) {
751 prefix
= setup_git_directory();
752 git_config(git_default_config
, NULL
);
755 prepare_repo_settings(the_repository
);
756 the_repository
->settings
.command_requires_full_index
= 0;
757 compat
= the_repository
->compat_hash_algo
;
760 if (!strcmp(arg
, "--")) {
762 /* Pass on the "--" if we show anything but files.. */
763 if (filter
& (DO_FLAGS
| DO_REVS
))
768 if (!seen_end_of_options
&& *arg
== '-') {
769 if (!strcmp(arg
, "--git-path")) {
771 die(_("--git-path requires an argument"));
773 print_path(git_path("%s", argv
[i
+ 1]), prefix
,
775 DEFAULT_RELATIVE_IF_SHARED
);
779 if (!strcmp(arg
,"-n")) {
781 die(_("-n requires an argument"));
782 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
)) {
788 if (starts_with(arg
, "-n")) {
789 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
))
793 if (opt_with_value(arg
, "--path-format", &arg
)) {
795 die(_("--path-format requires an argument"));
796 if (!strcmp(arg
, "absolute")) {
797 format
= FORMAT_CANONICAL
;
798 } else if (!strcmp(arg
, "relative")) {
799 format
= FORMAT_RELATIVE
;
801 die(_("unknown argument to --path-format: %s"), arg
);
805 if (!strcmp(arg
, "--default")) {
808 die(_("--default requires an argument"));
811 if (!strcmp(arg
, "--prefix")) {
814 die(_("--prefix requires an argument"));
815 startup_info
->prefix
= prefix
;
819 if (!strcmp(arg
, "--revs-only")) {
823 if (!strcmp(arg
, "--no-revs")) {
827 if (!strcmp(arg
, "--flags")) {
828 filter
&= ~DO_NONFLAGS
;
831 if (!strcmp(arg
, "--no-flags")) {
835 if (!strcmp(arg
, "--verify")) {
836 filter
&= ~(DO_FLAGS
|DO_NOREV
);
840 if (!strcmp(arg
, "--quiet") || !strcmp(arg
, "-q")) {
842 flags
|= GET_OID_QUIETLY
;
845 if (opt_with_value(arg
, "--output-object-format", &arg
)) {
847 die(_("no object format specified"));
848 if (!strcmp(arg
, the_hash_algo
->name
) ||
849 !strcmp(arg
, "storage")) {
850 flags
|= GET_OID_HASH_ANY
;
851 output_algo
= the_hash_algo
;
854 else if (compat
&& !strcmp(arg
, compat
->name
)) {
855 flags
|= GET_OID_HASH_ANY
;
856 output_algo
= compat
;
859 else die(_("unsupported object format: %s"), arg
);
861 if (opt_with_value(arg
, "--short", &arg
)) {
862 filter
&= ~(DO_FLAGS
|DO_NOREV
);
864 abbrev
= DEFAULT_ABBREV
;
867 abbrev
= strtoul(arg
, NULL
, 10);
868 if (abbrev
< MINIMUM_ABBREV
)
869 abbrev
= MINIMUM_ABBREV
;
870 else if (hexsz
<= abbrev
)
874 if (!strcmp(arg
, "--sq")) {
878 if (!strcmp(arg
, "--not")) {
879 show_type
^= REVERSED
;
882 if (!strcmp(arg
, "--symbolic")) {
883 symbolic
= SHOW_SYMBOLIC_ASIS
;
886 if (!strcmp(arg
, "--symbolic-full-name")) {
887 symbolic
= SHOW_SYMBOLIC_FULL
;
890 if (opt_with_value(arg
, "--abbrev-ref", &arg
)) {
892 abbrev_ref_strict
= warn_ambiguous_refs
;
894 if (!strcmp(arg
, "strict"))
895 abbrev_ref_strict
= 1;
896 else if (!strcmp(arg
, "loose"))
897 abbrev_ref_strict
= 0;
899 die(_("unknown mode for --abbrev-ref: %s"),
904 if (!strcmp(arg
, "--all")) {
905 refs_for_each_ref(get_main_ref_store(the_repository
),
906 show_reference
, NULL
);
907 clear_ref_exclusions(&ref_excludes
);
910 if (skip_prefix(arg
, "--disambiguate=", &arg
)) {
911 repo_for_each_abbrev(the_repository
, arg
, the_hash_algo
,
915 if (!strcmp(arg
, "--bisect")) {
916 refs_for_each_fullref_in(get_main_ref_store(the_repository
),
918 NULL
, show_reference
,
920 refs_for_each_fullref_in(get_main_ref_store(the_repository
),
922 NULL
, anti_reference
,
926 if (opt_with_value(arg
, "--branches", &arg
)) {
927 if (ref_excludes
.hidden_refs_configured
)
928 return error(_("options '%s' and '%s' cannot be used together"),
929 "--exclude-hidden", "--branches");
930 handle_ref_opt(arg
, "refs/heads/");
933 if (opt_with_value(arg
, "--tags", &arg
)) {
934 if (ref_excludes
.hidden_refs_configured
)
935 return error(_("options '%s' and '%s' cannot be used together"),
936 "--exclude-hidden", "--tags");
937 handle_ref_opt(arg
, "refs/tags/");
940 if (skip_prefix(arg
, "--glob=", &arg
)) {
941 handle_ref_opt(arg
, NULL
);
944 if (opt_with_value(arg
, "--remotes", &arg
)) {
945 if (ref_excludes
.hidden_refs_configured
)
946 return error(_("options '%s' and '%s' cannot be used together"),
947 "--exclude-hidden", "--remotes");
948 handle_ref_opt(arg
, "refs/remotes/");
951 if (skip_prefix(arg
, "--exclude=", &arg
)) {
952 add_ref_exclusion(&ref_excludes
, arg
);
955 if (skip_prefix(arg
, "--exclude-hidden=", &arg
)) {
956 exclude_hidden_refs(&ref_excludes
, arg
);
959 if (!strcmp(arg
, "--show-toplevel")) {
960 const char *work_tree
= get_git_work_tree();
962 print_path(work_tree
, prefix
, format
, DEFAULT_UNMODIFIED
);
964 die(_("this operation must be run in a work tree"));
967 if (!strcmp(arg
, "--show-superproject-working-tree")) {
968 struct strbuf superproject
= STRBUF_INIT
;
969 if (get_superproject_working_tree(&superproject
))
970 print_path(superproject
.buf
, prefix
, format
, DEFAULT_UNMODIFIED
);
971 strbuf_release(&superproject
);
974 if (!strcmp(arg
, "--show-prefix")) {
981 if (!strcmp(arg
, "--show-cdup")) {
982 const char *pfx
= prefix
;
983 if (!is_inside_work_tree()) {
984 const char *work_tree
=
987 printf("%s\n", work_tree
);
991 pfx
= strchr(pfx
, '/');
1000 if (!strcmp(arg
, "--git-dir") ||
1001 !strcmp(arg
, "--absolute-git-dir")) {
1002 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
1005 enum format_type wanted
= format
;
1006 if (arg
[2] == 'g') { /* --git-dir */
1008 print_path(gitdir
, prefix
, format
, DEFAULT_UNMODIFIED
);
1012 print_path(".git", prefix
, format
, DEFAULT_UNMODIFIED
);
1015 } else { /* --absolute-git-dir */
1016 wanted
= FORMAT_CANONICAL
;
1017 if (!gitdir
&& !prefix
)
1020 struct strbuf realpath
= STRBUF_INIT
;
1021 strbuf_realpath(&realpath
, gitdir
, 1);
1023 strbuf_release(&realpath
);
1030 strbuf_addf(&buf
, "%s%s.git", cwd
, len
&& cwd
[len
-1] != '/' ? "/" : "");
1032 print_path(buf
.buf
, prefix
, wanted
, DEFAULT_CANONICAL
);
1035 if (!strcmp(arg
, "--git-common-dir")) {
1036 print_path(get_git_common_dir(), prefix
, format
, DEFAULT_RELATIVE_IF_SHARED
);
1039 if (!strcmp(arg
, "--is-inside-git-dir")) {
1040 printf("%s\n", is_inside_git_dir() ? "true"
1044 if (!strcmp(arg
, "--is-inside-work-tree")) {
1045 printf("%s\n", is_inside_work_tree() ? "true"
1049 if (!strcmp(arg
, "--is-bare-repository")) {
1050 printf("%s\n", is_bare_repository() ? "true"
1054 if (!strcmp(arg
, "--is-shallow-repository")) {
1056 is_repository_shallow(the_repository
) ? "true"
1060 if (!strcmp(arg
, "--shared-index-path")) {
1061 if (repo_read_index(the_repository
) < 0)
1062 die(_("Could not read the index"));
1063 if (the_repository
->index
->split_index
) {
1064 const struct object_id
*oid
= &the_repository
->index
->split_index
->base_oid
;
1065 const char *path
= git_path("sharedindex.%s", oid_to_hex(oid
));
1066 print_path(path
, prefix
, format
, DEFAULT_RELATIVE
);
1070 if (skip_prefix(arg
, "--since=", &arg
)) {
1071 show_datestring("--max-age=", arg
);
1074 if (skip_prefix(arg
, "--after=", &arg
)) {
1075 show_datestring("--max-age=", arg
);
1078 if (skip_prefix(arg
, "--before=", &arg
)) {
1079 show_datestring("--min-age=", arg
);
1082 if (skip_prefix(arg
, "--until=", &arg
)) {
1083 show_datestring("--min-age=", arg
);
1086 if (opt_with_value(arg
, "--show-object-format", &arg
)) {
1087 const char *val
= arg
? arg
: "storage";
1089 if (strcmp(val
, "storage") &&
1090 strcmp(val
, "input") &&
1091 strcmp(val
, "output"))
1092 die(_("unknown mode for --show-object-format: %s"),
1094 puts(the_hash_algo
->name
);
1097 if (!strcmp(arg
, "--show-ref-format")) {
1098 puts(ref_storage_format_to_name(the_repository
->ref_storage_format
));
1101 if (!strcmp(arg
, "--end-of-options")) {
1102 seen_end_of_options
= 1;
1103 if (filter
& (DO_FLAGS
| DO_REVS
))
1107 if (show_flag(arg
) && verify
)
1108 die_no_single_rev(quiet
);
1112 /* Not a flag argument */
1113 if (try_difference(arg
))
1115 if (try_parent_shorthands(arg
))
1123 if (!get_oid_with_context(the_repository
, name
,
1124 flags
, &oid
, &unused
)) {
1126 repo_oid_to_algop(the_repository
, &oid
,
1131 show_rev(type
, &oid
, name
);
1135 die_no_single_rev(quiet
);
1137 die(_("bad revision '%s'"), arg
);
1139 if (!show_file(arg
, output_prefix
))
1141 verify_filename(prefix
, arg
, 1);
1143 strbuf_release(&buf
);
1145 if (revs_count
== 1) {
1146 show_rev(type
, &oid
, name
);
1148 } else if (revs_count
== 0 && show_default())
1150 die_no_single_rev(quiet
);