4 * Copyright (C) Linus Torvalds, 2005
6 #define USE_THE_REPOSITORY_VARIABLE
12 #include "environment.h"
18 #include "object-name.h"
19 #include "parse-options.h"
22 #include "read-cache-ll.h"
23 #include "repo-settings.h"
24 #include "repository.h"
27 #include "split-index.h"
28 #include "submodule.h"
29 #include "commit-reach.h"
31 #include "object-file-convert.h"
37 static int filter
= ~0;
39 static const char *def
;
43 static int show_type
= NORMAL
;
45 #define SHOW_SYMBOLIC_ASIS 1
46 #define SHOW_SYMBOLIC_FULL 2
49 static int abbrev_ref
;
50 static int abbrev_ref_strict
;
53 static int stuck_long
;
54 static struct ref_exclusions ref_excludes
= REF_EXCLUSIONS_INIT
;
57 * Some arguments are relevant "revision" arguments,
58 * others are about output format or other details.
59 * This sorts it all out.
61 static int is_rev_argument(const char *arg
)
63 static const char *rev_args
[] = {
94 const char **p
= rev_args
;
96 /* accept -<digit>, like traditional "head" */
97 if ((*arg
== '-') && isdigit(arg
[1]))
101 const char *str
= *p
++;
106 if (!strcmp(arg
, str
) ||
107 (str
[len
-1] == '=' && !strncmp(arg
, str
, len
)))
112 /* Output argument as a string, either SQ or normal */
113 static void show(const char *arg
)
119 while ((ch
= *arg
++)) {
121 fputs("'\\'", stdout
);
131 /* Like show(), but with a negation prefix according to type */
132 static void show_with_type(int type
, const char *arg
)
134 if (type
!= show_type
)
139 /* Output a revision, only if filter allows it */
140 static void show_rev(int type
, const struct object_id
*oid
, const char *name
)
142 if (!(filter
& DO_REVS
))
146 if ((symbolic
|| abbrev_ref
) && name
) {
147 if (symbolic
== SHOW_SYMBOLIC_FULL
|| abbrev_ref
) {
148 struct object_id discard
;
151 switch (repo_dwim_ref(the_repository
, name
,
152 strlen(name
), &discard
, &full
,
156 * Not found -- not a ref. We could
157 * emit "name" here, but symbolic-full
158 * users are interested in finding the
159 * refs spelled in full, and they would
160 * need to filter non-refs if we did so.
166 full
= refs_shorten_unambiguous_ref(get_main_ref_store(the_repository
),
171 show_with_type(type
, full
);
173 default: /* ambiguous */
174 error("refname '%s' is ambiguous", name
);
179 show_with_type(type
, name
);
184 repo_find_unique_abbrev(the_repository
, oid
, abbrev
));
186 show_with_type(type
, oid_to_hex(oid
));
189 /* Output a flag, only if filter allows it. */
190 static int show_flag(const char *arg
)
192 if (!(filter
& DO_FLAGS
))
194 if (filter
& (is_rev_argument(arg
) ? DO_REVS
: DO_NOREV
)) {
201 static int show_default(void)
206 struct object_id oid
;
209 if (!repo_get_oid(the_repository
, s
, &oid
)) {
210 show_rev(NORMAL
, &oid
, s
);
217 static int show_reference(const char *refname
, const char *referent UNUSED
, const struct object_id
*oid
,
218 int flag UNUSED
, void *cb_data UNUSED
)
220 if (ref_excluded(&ref_excludes
, refname
))
222 show_rev(NORMAL
, oid
, refname
);
226 static int anti_reference(const char *refname
, const char *referent UNUSED
, const struct object_id
*oid
,
227 int flag UNUSED
, void *cb_data UNUSED
)
229 show_rev(REVERSED
, oid
, refname
);
233 static int show_abbrev(const struct object_id
*oid
, void *cb_data UNUSED
)
235 show_rev(NORMAL
, oid
, NULL
);
239 static void show_datestring(const char *flag
, const char *datestr
)
243 /* date handling requires both flags and revs */
244 if ((filter
& (DO_FLAGS
| DO_REVS
)) != (DO_FLAGS
| DO_REVS
))
246 buffer
= xstrfmt("%s%"PRItime
, flag
, approxidate(datestr
));
251 static int show_file(const char *arg
, int output_prefix
)
254 if ((filter
& (DO_NONFLAGS
|DO_NOREV
)) == (DO_NONFLAGS
|DO_NOREV
)) {
256 const char *prefix
= startup_info
->prefix
;
257 char *fname
= prefix_filename(prefix
, arg
);
267 static int try_difference(const char *arg
)
270 struct object_id start_oid
;
271 struct object_id end_oid
;
275 static const char head_by_default
[] = "HEAD";
277 if (!(dotdot
= strstr(arg
, "..")))
281 symmetric
= (*end
== '.');
287 end
= head_by_default
;
289 start
= head_by_default
;
291 if (start
== head_by_default
&& end
== head_by_default
&&
294 * Just ".."? That is not a range but the
295 * pathspec for the parent directory.
301 if (!repo_get_oid_committish(the_repository
, start
, &start_oid
) && !repo_get_oid_committish(the_repository
, end
, &end_oid
)) {
302 show_rev(NORMAL
, &end_oid
, end
);
303 show_rev(symmetric
? NORMAL
: REVERSED
, &start_oid
, start
);
305 struct commit_list
*exclude
= NULL
;
306 struct commit
*a
, *b
;
307 a
= lookup_commit_reference(the_repository
, &start_oid
);
308 b
= lookup_commit_reference(the_repository
, &end_oid
);
313 if (repo_get_merge_bases(the_repository
, a
, b
, &exclude
) < 0)
316 struct commit
*commit
= pop_commit(&exclude
);
317 show_rev(REVERSED
, &commit
->object
.oid
, NULL
);
327 static int try_parent_shorthands(const char *arg
)
330 struct object_id oid
;
331 struct commit
*commit
;
332 struct commit_list
*parents
;
335 int include_parents
= 0;
336 int exclude_parent
= 0;
338 if ((dotdot
= strstr(arg
, "^!"))) {
342 } else if ((dotdot
= strstr(arg
, "^@"))) {
346 } else if ((dotdot
= strstr(arg
, "^-"))) {
352 exclude_parent
= strtoul(dotdot
+ 2, &end
, 10);
353 if (*end
!= '\0' || !exclude_parent
)
360 if (repo_get_oid_committish(the_repository
, arg
, &oid
) ||
361 !(commit
= lookup_commit_reference(the_repository
, &oid
))) {
366 if (exclude_parent
&&
367 exclude_parent
> commit_list_count(commit
->parents
)) {
373 show_rev(NORMAL
, &oid
, arg
);
374 for (parents
= commit
->parents
, parent_number
= 1;
376 parents
= parents
->next
, parent_number
++) {
379 if (exclude_parent
&& parent_number
!= exclude_parent
)
383 name
= xstrfmt("%s^%d", arg
, parent_number
);
384 show_rev(include_parents
? NORMAL
: REVERSED
,
385 &parents
->item
->object
.oid
, name
);
393 static int parseopt_dump(const struct option
*o
, const char *arg
, int unset
)
395 struct strbuf
*parsed
= o
->value
;
397 strbuf_addf(parsed
, " --no-%s", o
->long_name
);
398 else if (o
->short_name
&& (o
->long_name
== NULL
|| !stuck_long
))
399 strbuf_addf(parsed
, " -%c", o
->short_name
);
401 strbuf_addf(parsed
, " --%s", o
->long_name
);
404 strbuf_addch(parsed
, ' ');
405 else if (o
->long_name
)
406 strbuf_addch(parsed
, '=');
407 sq_quote_buf(parsed
, arg
);
412 static const char *skipspaces(const char *s
)
419 static char *findspace(const char *s
)
427 static int cmd_parseopt(int argc
, const char **argv
, const char *prefix
)
429 int keep_dashdash
= 0, stop_at_non_option
= 0;
430 char const * const parseopt_usage
[] = {
431 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
434 struct option parseopt_opts
[] = {
435 OPT_BOOL(0, "keep-dashdash", &keep_dashdash
,
436 N_("keep the `--` passed as an arg")),
437 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option
,
438 N_("stop parsing after the "
439 "first non-option argument")),
440 OPT_BOOL(0, "stuck-long", &stuck_long
,
441 N_("output in stuck long form")),
444 struct strbuf sb
= STRBUF_INIT
, parsed
= STRBUF_INIT
;
445 struct strvec longnames
= STRVEC_INIT
;
446 struct strvec usage
= STRVEC_INIT
;
447 struct option
*opts
= NULL
;
448 size_t opts_nr
= 0, opts_alloc
= 0;
450 strbuf_addstr(&parsed
, "set --");
451 argc
= parse_options(argc
, argv
, prefix
, parseopt_opts
, parseopt_usage
,
452 PARSE_OPT_KEEP_DASHDASH
);
453 if (argc
< 1 || strcmp(argv
[0], "--"))
454 usage_with_options(parseopt_usage
, parseopt_opts
);
456 /* get the usage up to the first line with a -- on it */
459 if (strbuf_getline(&sb
, stdin
) == EOF
)
460 die(_("premature end of input"));
461 if (!strcmp("--", sb
.buf
)) {
463 die(_("no usage string given before the `--' separator"));
467 strvec_push(&usage
, sb
.buf
);
470 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
471 while (strbuf_getline(&sb
, stdin
) != EOF
) {
479 ALLOC_GROW(opts
, opts_nr
+ 1, opts_alloc
);
480 memset(opts
+ opts_nr
, 0, sizeof(*opts
));
482 o
= &opts
[opts_nr
++];
483 help
= findspace(sb
.buf
);
484 if (!help
|| sb
.buf
== help
) {
485 o
->type
= OPTION_GROUP
;
486 o
->help
= xstrdup(skipspaces(sb
.buf
));
492 o
->type
= OPTION_CALLBACK
;
493 o
->help
= xstrdup(skipspaces(help
+1));
495 o
->flags
= PARSE_OPT_NOARG
;
496 o
->callback
= &parseopt_dump
;
499 s
= strpbrk(sb
.buf
, "*=?!");
504 die(_("missing opt-spec before option flags"));
506 if (s
- sb
.buf
== 1) { /* short option only */
507 o
->short_name
= *sb
.buf
;
508 } else if (sb
.buf
[1] != ',') { /* long option only */
509 o
->long_name
= strvec_pushf(&longnames
, "%.*s",
510 (int)(s
- sb
.buf
), sb
.buf
);
512 o
->short_name
= *sb
.buf
;
513 o
->long_name
= strvec_pushf(&longnames
, "%.*s",
514 (int)(s
- sb
.buf
- 2), sb
.buf
+ 2);
521 o
->flags
&= ~PARSE_OPT_NOARG
;
524 o
->flags
&= ~PARSE_OPT_NOARG
;
525 o
->flags
|= PARSE_OPT_OPTARG
;
528 o
->flags
|= PARSE_OPT_NONEG
;
531 o
->flags
|= PARSE_OPT_HIDDEN
;
539 o
->argh
= xmemdupz(s
, help
- s
);
543 /* put an OPT_END() */
544 ALLOC_GROW(opts
, opts_nr
+ 1, opts_alloc
);
545 memset(opts
+ opts_nr
, 0, sizeof(*opts
));
546 argc
= parse_options(argc
, argv
, prefix
, opts
, usage
.v
,
547 (keep_dashdash
? PARSE_OPT_KEEP_DASHDASH
: 0) |
548 (stop_at_non_option
? PARSE_OPT_STOP_AT_NON_OPTION
: 0) |
549 PARSE_OPT_SHELL_EVAL
);
551 strbuf_addstr(&parsed
, " --");
552 sq_quote_argv(&parsed
, argv
);
555 strbuf_release(&parsed
);
557 strvec_clear(&longnames
);
558 strvec_clear(&usage
);
559 for (size_t i
= 0; i
< opts_nr
; i
++) {
560 free((char *) opts
[i
].help
);
561 free((char *) opts
[i
].argh
);
567 static int cmd_sq_quote(int argc
, const char **argv
)
569 struct strbuf buf
= STRBUF_INIT
;
572 sq_quote_argv(&buf
, argv
);
573 printf("%s\n", buf
.buf
);
574 strbuf_release(&buf
);
579 static void die_no_single_rev(int quiet
)
584 die(_("Needed a single revision"));
587 static const char builtin_rev_parse_usage
[] =
588 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
589 " or: git rev-parse --sq-quote [<arg>...]\n"
590 " or: git rev-parse [<options>] [<arg>...]\n"
592 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
595 * Parse "opt" or "opt=<value>", setting value respectively to either
596 * NULL or the string after "=".
598 static int opt_with_value(const char *arg
, const char *opt
, const char **value
)
600 if (skip_prefix(arg
, opt
, &arg
)) {
613 static void handle_ref_opt(const char *pattern
, const char *prefix
)
616 refs_for_each_glob_ref_in(get_main_ref_store(the_repository
),
617 show_reference
, pattern
, prefix
,
620 refs_for_each_ref_in(get_main_ref_store(the_repository
),
621 prefix
, show_reference
, NULL
);
622 clear_ref_exclusions(&ref_excludes
);
626 /* We would like a relative path. */
628 /* We would like a canonical absolute path. */
630 /* We would like the default behavior. */
635 /* Our default is a relative path. */
637 /* Our default is a relative path if there's a shared root. */
638 DEFAULT_RELATIVE_IF_SHARED
,
639 /* Our default is a canonical absolute path. */
641 /* Our default is not to modify the item. */
645 static void print_path(const char *path
, const char *prefix
, enum format_type format
, enum default_type def
)
649 * We don't ever produce a relative path if prefix is NULL, so set the
650 * prefix to the current directory so that we can produce a relative
651 * path whenever possible. If we're using RELATIVE_IF_SHARED mode, then
652 * we want an absolute path unless the two share a common prefix, so don't
653 * set it in that case, since doing so causes a relative path to always
654 * be produced if possible.
656 if (!prefix
&& (format
!= FORMAT_DEFAULT
|| def
!= DEFAULT_RELATIVE_IF_SHARED
))
657 prefix
= cwd
= xgetcwd();
658 if (format
== FORMAT_DEFAULT
&& def
== DEFAULT_UNMODIFIED
) {
660 } else if (format
== FORMAT_RELATIVE
||
661 (format
== FORMAT_DEFAULT
&& def
== DEFAULT_RELATIVE
)) {
663 * In order for relative_path to work as expected, we need to
664 * make sure that both paths are absolute paths. If we don't,
665 * we can end up with an unexpected absolute path that the user
668 struct strbuf buf
= STRBUF_INIT
, realbuf
= STRBUF_INIT
, prefixbuf
= STRBUF_INIT
;
669 if (!is_absolute_path(path
)) {
670 strbuf_realpath_forgiving(&realbuf
, path
, 1);
673 if (!is_absolute_path(prefix
)) {
674 strbuf_realpath_forgiving(&prefixbuf
, prefix
, 1);
675 prefix
= prefixbuf
.buf
;
677 puts(relative_path(path
, prefix
, &buf
));
678 strbuf_release(&buf
);
679 strbuf_release(&realbuf
);
680 strbuf_release(&prefixbuf
);
681 } else if (format
== FORMAT_DEFAULT
&& def
== DEFAULT_RELATIVE_IF_SHARED
) {
682 struct strbuf buf
= STRBUF_INIT
;
683 puts(relative_path(path
, prefix
, &buf
));
684 strbuf_release(&buf
);
686 struct strbuf buf
= STRBUF_INIT
;
687 strbuf_realpath_forgiving(&buf
, path
, 1);
689 strbuf_release(&buf
);
694 int cmd_rev_parse(int argc
,
697 struct repository
*repo UNUSED
)
699 int i
, as_is
= 0, verify
= 0, quiet
= 0, revs_count
= 0, type
= 0;
700 const struct git_hash_algo
*output_algo
= NULL
;
701 const struct git_hash_algo
*compat
= NULL
;
702 int did_repo_setup
= 0;
703 int has_dashdash
= 0;
704 int output_prefix
= 0;
705 struct object_id oid
;
706 unsigned int flags
= 0;
707 const char *name
= NULL
;
708 struct object_context unused
;
709 struct strbuf buf
= STRBUF_INIT
;
710 int seen_end_of_options
= 0;
711 enum format_type format
= FORMAT_DEFAULT
;
713 if (argc
> 1 && !strcmp("--parseopt", argv
[1]))
714 return cmd_parseopt(argc
- 1, argv
+ 1, prefix
);
716 if (argc
> 1 && !strcmp("--sq-quote", argv
[1]))
717 return cmd_sq_quote(argc
- 2, argv
+ 2);
719 if (argc
> 1 && !strcmp("-h", argv
[1]))
720 usage(builtin_rev_parse_usage
);
722 for (i
= 1; i
< argc
; i
++) {
723 if (!strcmp(argv
[i
], "--")) {
729 /* No options; just report on whether we're in a git repo or not. */
731 setup_git_directory();
732 git_config(git_default_config
, NULL
);
736 for (i
= 1; i
< argc
; i
++) {
737 const char *arg
= argv
[i
];
740 if (show_file(arg
, output_prefix
) && as_is
< 2)
741 verify_filename(prefix
, arg
, 0);
745 if (!seen_end_of_options
) {
746 if (!strcmp(arg
, "--local-env-vars")) {
748 for (i
= 0; local_repo_env
[i
]; i
++)
749 printf("%s\n", local_repo_env
[i
]);
752 if (!strcmp(arg
, "--resolve-git-dir")) {
753 const char *gitdir
= argv
[++i
];
755 die(_("--resolve-git-dir requires an argument"));
756 gitdir
= resolve_gitdir(gitdir
);
758 die(_("not a gitdir '%s'"), argv
[i
]);
764 /* The rest of the options require a git repository. */
765 if (!did_repo_setup
) {
766 prefix
= setup_git_directory();
767 git_config(git_default_config
, NULL
);
770 prepare_repo_settings(the_repository
);
771 the_repository
->settings
.command_requires_full_index
= 0;
772 compat
= the_repository
->compat_hash_algo
;
775 if (!strcmp(arg
, "--")) {
777 /* Pass on the "--" if we show anything but files.. */
778 if (filter
& (DO_FLAGS
| DO_REVS
))
783 if (!seen_end_of_options
&& *arg
== '-') {
784 if (!strcmp(arg
, "--git-path")) {
786 die(_("--git-path requires an argument"));
788 print_path(git_path("%s", argv
[i
+ 1]), prefix
,
790 DEFAULT_RELATIVE_IF_SHARED
);
794 if (!strcmp(arg
,"-n")) {
796 die(_("-n requires an argument"));
797 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
)) {
803 if (starts_with(arg
, "-n")) {
804 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
))
808 if (opt_with_value(arg
, "--path-format", &arg
)) {
810 die(_("--path-format requires an argument"));
811 if (!strcmp(arg
, "absolute")) {
812 format
= FORMAT_CANONICAL
;
813 } else if (!strcmp(arg
, "relative")) {
814 format
= FORMAT_RELATIVE
;
816 die(_("unknown argument to --path-format: %s"), arg
);
820 if (!strcmp(arg
, "--default")) {
823 die(_("--default requires an argument"));
826 if (!strcmp(arg
, "--prefix")) {
829 die(_("--prefix requires an argument"));
830 startup_info
->prefix
= prefix
;
834 if (!strcmp(arg
, "--revs-only")) {
838 if (!strcmp(arg
, "--no-revs")) {
842 if (!strcmp(arg
, "--flags")) {
843 filter
&= ~DO_NONFLAGS
;
846 if (!strcmp(arg
, "--no-flags")) {
850 if (!strcmp(arg
, "--verify")) {
851 filter
&= ~(DO_FLAGS
|DO_NOREV
);
855 if (!strcmp(arg
, "--quiet") || !strcmp(arg
, "-q")) {
857 flags
|= GET_OID_QUIETLY
;
860 if (opt_with_value(arg
, "--output-object-format", &arg
)) {
862 die(_("no object format specified"));
863 if (!strcmp(arg
, the_hash_algo
->name
) ||
864 !strcmp(arg
, "storage")) {
865 flags
|= GET_OID_HASH_ANY
;
866 output_algo
= the_hash_algo
;
869 else if (compat
&& !strcmp(arg
, compat
->name
)) {
870 flags
|= GET_OID_HASH_ANY
;
871 output_algo
= compat
;
874 else die(_("unsupported object format: %s"), arg
);
876 if (opt_with_value(arg
, "--short", &arg
)) {
877 filter
&= ~(DO_FLAGS
|DO_NOREV
);
879 abbrev
= DEFAULT_ABBREV
;
882 abbrev
= strtoul(arg
, NULL
, 10);
883 if (abbrev
< MINIMUM_ABBREV
)
884 abbrev
= MINIMUM_ABBREV
;
885 else if ((int)the_hash_algo
->hexsz
<= abbrev
)
886 abbrev
= the_hash_algo
->hexsz
;
889 if (!strcmp(arg
, "--sq")) {
893 if (!strcmp(arg
, "--not")) {
894 show_type
^= REVERSED
;
897 if (!strcmp(arg
, "--symbolic")) {
898 symbolic
= SHOW_SYMBOLIC_ASIS
;
901 if (!strcmp(arg
, "--symbolic-full-name")) {
902 symbolic
= SHOW_SYMBOLIC_FULL
;
905 if (opt_with_value(arg
, "--abbrev-ref", &arg
)) {
908 repo_settings_get_warn_ambiguous_refs(the_repository
);
910 if (!strcmp(arg
, "strict"))
911 abbrev_ref_strict
= 1;
912 else if (!strcmp(arg
, "loose"))
913 abbrev_ref_strict
= 0;
915 die(_("unknown mode for --abbrev-ref: %s"),
920 if (!strcmp(arg
, "--all")) {
921 refs_for_each_ref(get_main_ref_store(the_repository
),
922 show_reference
, NULL
);
923 clear_ref_exclusions(&ref_excludes
);
926 if (skip_prefix(arg
, "--disambiguate=", &arg
)) {
927 repo_for_each_abbrev(the_repository
, arg
, the_hash_algo
,
931 if (!strcmp(arg
, "--bisect")) {
932 refs_for_each_fullref_in(get_main_ref_store(the_repository
),
934 NULL
, show_reference
,
936 refs_for_each_fullref_in(get_main_ref_store(the_repository
),
938 NULL
, anti_reference
,
942 if (opt_with_value(arg
, "--branches", &arg
)) {
943 if (ref_excludes
.hidden_refs_configured
)
944 return error(_("options '%s' and '%s' cannot be used together"),
945 "--exclude-hidden", "--branches");
946 handle_ref_opt(arg
, "refs/heads/");
949 if (opt_with_value(arg
, "--tags", &arg
)) {
950 if (ref_excludes
.hidden_refs_configured
)
951 return error(_("options '%s' and '%s' cannot be used together"),
952 "--exclude-hidden", "--tags");
953 handle_ref_opt(arg
, "refs/tags/");
956 if (skip_prefix(arg
, "--glob=", &arg
)) {
957 handle_ref_opt(arg
, NULL
);
960 if (opt_with_value(arg
, "--remotes", &arg
)) {
961 if (ref_excludes
.hidden_refs_configured
)
962 return error(_("options '%s' and '%s' cannot be used together"),
963 "--exclude-hidden", "--remotes");
964 handle_ref_opt(arg
, "refs/remotes/");
967 if (skip_prefix(arg
, "--exclude=", &arg
)) {
968 add_ref_exclusion(&ref_excludes
, arg
);
971 if (skip_prefix(arg
, "--exclude-hidden=", &arg
)) {
972 exclude_hidden_refs(&ref_excludes
, arg
);
975 if (!strcmp(arg
, "--show-toplevel")) {
976 const char *work_tree
= repo_get_work_tree(the_repository
);
978 print_path(work_tree
, prefix
, format
, DEFAULT_UNMODIFIED
);
980 die(_("this operation must be run in a work tree"));
983 if (!strcmp(arg
, "--show-superproject-working-tree")) {
984 struct strbuf superproject
= STRBUF_INIT
;
985 if (get_superproject_working_tree(&superproject
))
986 print_path(superproject
.buf
, prefix
, format
, DEFAULT_UNMODIFIED
);
987 strbuf_release(&superproject
);
990 if (!strcmp(arg
, "--show-prefix")) {
997 if (!strcmp(arg
, "--show-cdup")) {
998 const char *pfx
= prefix
;
999 if (!is_inside_work_tree()) {
1000 const char *work_tree
=
1001 repo_get_work_tree(the_repository
);
1003 printf("%s\n", work_tree
);
1007 pfx
= strchr(pfx
, '/');
1016 if (!strcmp(arg
, "--git-dir") ||
1017 !strcmp(arg
, "--absolute-git-dir")) {
1018 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
1021 enum format_type wanted
= format
;
1022 if (arg
[2] == 'g') { /* --git-dir */
1024 print_path(gitdir
, prefix
, format
, DEFAULT_UNMODIFIED
);
1028 print_path(".git", prefix
, format
, DEFAULT_UNMODIFIED
);
1031 } else { /* --absolute-git-dir */
1032 wanted
= FORMAT_CANONICAL
;
1033 if (!gitdir
&& !prefix
)
1036 struct strbuf realpath
= STRBUF_INIT
;
1037 strbuf_realpath(&realpath
, gitdir
, 1);
1039 strbuf_release(&realpath
);
1046 strbuf_addf(&buf
, "%s%s.git", cwd
, len
&& cwd
[len
-1] != '/' ? "/" : "");
1048 print_path(buf
.buf
, prefix
, wanted
, DEFAULT_CANONICAL
);
1051 if (!strcmp(arg
, "--git-common-dir")) {
1052 print_path(repo_get_common_dir(the_repository
), prefix
, format
, DEFAULT_RELATIVE_IF_SHARED
);
1055 if (!strcmp(arg
, "--is-inside-git-dir")) {
1056 printf("%s\n", is_inside_git_dir() ? "true"
1060 if (!strcmp(arg
, "--is-inside-work-tree")) {
1061 printf("%s\n", is_inside_work_tree() ? "true"
1065 if (!strcmp(arg
, "--is-bare-repository")) {
1066 printf("%s\n", is_bare_repository() ? "true"
1070 if (!strcmp(arg
, "--is-shallow-repository")) {
1072 is_repository_shallow(the_repository
) ? "true"
1076 if (!strcmp(arg
, "--shared-index-path")) {
1077 if (repo_read_index(the_repository
) < 0)
1078 die(_("Could not read the index"));
1079 if (the_repository
->index
->split_index
) {
1080 const struct object_id
*oid
= &the_repository
->index
->split_index
->base_oid
;
1081 const char *path
= git_path("sharedindex.%s", oid_to_hex(oid
));
1082 print_path(path
, prefix
, format
, DEFAULT_RELATIVE
);
1086 if (skip_prefix(arg
, "--since=", &arg
)) {
1087 show_datestring("--max-age=", arg
);
1090 if (skip_prefix(arg
, "--after=", &arg
)) {
1091 show_datestring("--max-age=", arg
);
1094 if (skip_prefix(arg
, "--before=", &arg
)) {
1095 show_datestring("--min-age=", arg
);
1098 if (skip_prefix(arg
, "--until=", &arg
)) {
1099 show_datestring("--min-age=", arg
);
1102 if (opt_with_value(arg
, "--show-object-format", &arg
)) {
1103 const char *val
= arg
? arg
: "storage";
1105 if (strcmp(val
, "storage") &&
1106 strcmp(val
, "input") &&
1107 strcmp(val
, "output"))
1108 die(_("unknown mode for --show-object-format: %s"),
1110 puts(the_hash_algo
->name
);
1113 if (!strcmp(arg
, "--show-ref-format")) {
1114 puts(ref_storage_format_to_name(the_repository
->ref_storage_format
));
1117 if (!strcmp(arg
, "--end-of-options")) {
1118 seen_end_of_options
= 1;
1119 if (filter
& (DO_FLAGS
| DO_REVS
))
1123 if (show_flag(arg
) && verify
)
1124 die_no_single_rev(quiet
);
1128 /* Not a flag argument */
1129 if (try_difference(arg
))
1131 if (try_parent_shorthands(arg
))
1139 if (!get_oid_with_context(the_repository
, name
,
1140 flags
, &oid
, &unused
)) {
1141 object_context_release(&unused
);
1143 repo_oid_to_algop(the_repository
, &oid
,
1148 show_rev(type
, &oid
, name
);
1151 object_context_release(&unused
);
1153 die_no_single_rev(quiet
);
1155 die(_("bad revision '%s'"), arg
);
1157 if (!show_file(arg
, output_prefix
))
1159 verify_filename(prefix
, arg
, 1);
1161 strbuf_release(&buf
);
1163 if (revs_count
== 1) {
1164 show_rev(type
, &oid
, name
);
1166 } else if (revs_count
== 0 && show_default())
1168 die_no_single_rev(quiet
);