4 * Copyright (C) Linus Torvalds, 2005
6 #define USE_THE_INDEX_VARIABLE
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
= shorten_unambiguous_ref(full
,
167 show_with_type(type
, full
);
169 default: /* ambiguous */
170 error("refname '%s' is ambiguous", name
);
175 show_with_type(type
, name
);
180 repo_find_unique_abbrev(the_repository
, oid
, abbrev
));
182 show_with_type(type
, oid_to_hex(oid
));
185 /* Output a flag, only if filter allows it. */
186 static int show_flag(const char *arg
)
188 if (!(filter
& DO_FLAGS
))
190 if (filter
& (is_rev_argument(arg
) ? DO_REVS
: DO_NOREV
)) {
197 static int show_default(void)
202 struct object_id oid
;
205 if (!repo_get_oid(the_repository
, s
, &oid
)) {
206 show_rev(NORMAL
, &oid
, s
);
213 static int show_reference(const char *refname
, const struct object_id
*oid
,
214 int flag UNUSED
, void *cb_data UNUSED
)
216 if (ref_excluded(&ref_excludes
, refname
))
218 show_rev(NORMAL
, oid
, refname
);
222 static int anti_reference(const char *refname
, const struct object_id
*oid
,
223 int flag UNUSED
, void *cb_data UNUSED
)
225 show_rev(REVERSED
, oid
, refname
);
229 static int show_abbrev(const struct object_id
*oid
, void *cb_data UNUSED
)
231 show_rev(NORMAL
, oid
, NULL
);
235 static void show_datestring(const char *flag
, const char *datestr
)
239 /* date handling requires both flags and revs */
240 if ((filter
& (DO_FLAGS
| DO_REVS
)) != (DO_FLAGS
| DO_REVS
))
242 buffer
= xstrfmt("%s%"PRItime
, flag
, approxidate(datestr
));
247 static int show_file(const char *arg
, int output_prefix
)
250 if ((filter
& (DO_NONFLAGS
|DO_NOREV
)) == (DO_NONFLAGS
|DO_NOREV
)) {
252 const char *prefix
= startup_info
->prefix
;
253 char *fname
= prefix_filename(prefix
, arg
);
263 static int try_difference(const char *arg
)
266 struct object_id start_oid
;
267 struct object_id end_oid
;
271 static const char head_by_default
[] = "HEAD";
273 if (!(dotdot
= strstr(arg
, "..")))
277 symmetric
= (*end
== '.');
283 end
= head_by_default
;
285 start
= head_by_default
;
287 if (start
== head_by_default
&& end
== head_by_default
&&
290 * Just ".."? That is not a range but the
291 * pathspec for the parent directory.
297 if (!repo_get_oid_committish(the_repository
, start
, &start_oid
) && !repo_get_oid_committish(the_repository
, end
, &end_oid
)) {
298 show_rev(NORMAL
, &end_oid
, end
);
299 show_rev(symmetric
? NORMAL
: REVERSED
, &start_oid
, start
);
301 struct commit_list
*exclude
= NULL
;
302 struct commit
*a
, *b
;
303 a
= lookup_commit_reference(the_repository
, &start_oid
);
304 b
= lookup_commit_reference(the_repository
, &end_oid
);
309 if (repo_get_merge_bases(the_repository
, a
, b
, &exclude
) < 0)
312 struct commit
*commit
= pop_commit(&exclude
);
313 show_rev(REVERSED
, &commit
->object
.oid
, NULL
);
323 static int try_parent_shorthands(const char *arg
)
326 struct object_id oid
;
327 struct commit
*commit
;
328 struct commit_list
*parents
;
331 int include_parents
= 0;
332 int exclude_parent
= 0;
334 if ((dotdot
= strstr(arg
, "^!"))) {
338 } else if ((dotdot
= strstr(arg
, "^@"))) {
342 } else if ((dotdot
= strstr(arg
, "^-"))) {
348 exclude_parent
= strtoul(dotdot
+ 2, &end
, 10);
349 if (*end
!= '\0' || !exclude_parent
)
356 if (repo_get_oid_committish(the_repository
, arg
, &oid
) ||
357 !(commit
= lookup_commit_reference(the_repository
, &oid
))) {
362 if (exclude_parent
&&
363 exclude_parent
> commit_list_count(commit
->parents
)) {
369 show_rev(NORMAL
, &oid
, arg
);
370 for (parents
= commit
->parents
, parent_number
= 1;
372 parents
= parents
->next
, parent_number
++) {
375 if (exclude_parent
&& parent_number
!= exclude_parent
)
379 name
= xstrfmt("%s^%d", arg
, parent_number
);
380 show_rev(include_parents
? NORMAL
: REVERSED
,
381 &parents
->item
->object
.oid
, name
);
389 static int parseopt_dump(const struct option
*o
, const char *arg
, int unset
)
391 struct strbuf
*parsed
= o
->value
;
393 strbuf_addf(parsed
, " --no-%s", o
->long_name
);
394 else if (o
->short_name
&& (o
->long_name
== NULL
|| !stuck_long
))
395 strbuf_addf(parsed
, " -%c", o
->short_name
);
397 strbuf_addf(parsed
, " --%s", o
->long_name
);
400 strbuf_addch(parsed
, ' ');
401 else if (o
->long_name
)
402 strbuf_addch(parsed
, '=');
403 sq_quote_buf(parsed
, arg
);
408 static const char *skipspaces(const char *s
)
415 static char *findspace(const char *s
)
423 static int cmd_parseopt(int argc
, const char **argv
, const char *prefix
)
425 static int keep_dashdash
= 0, stop_at_non_option
= 0;
426 static char const * const parseopt_usage
[] = {
427 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
430 static struct option parseopt_opts
[] = {
431 OPT_BOOL(0, "keep-dashdash", &keep_dashdash
,
432 N_("keep the `--` passed as an arg")),
433 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option
,
434 N_("stop parsing after the "
435 "first non-option argument")),
436 OPT_BOOL(0, "stuck-long", &stuck_long
,
437 N_("output in stuck long form")),
440 static const char * const flag_chars
= "*=?!";
442 struct strbuf sb
= STRBUF_INIT
, parsed
= STRBUF_INIT
;
443 const char **usage
= NULL
;
444 struct option
*opts
= NULL
;
445 int onb
= 0, osz
= 0, unb
= 0, usz
= 0;
447 strbuf_addstr(&parsed
, "set --");
448 argc
= parse_options(argc
, argv
, prefix
, parseopt_opts
, parseopt_usage
,
449 PARSE_OPT_KEEP_DASHDASH
);
450 if (argc
< 1 || strcmp(argv
[0], "--"))
451 usage_with_options(parseopt_usage
, parseopt_opts
);
453 /* get the usage up to the first line with a -- on it */
455 if (strbuf_getline(&sb
, stdin
) == EOF
)
456 die(_("premature end of input"));
457 ALLOC_GROW(usage
, unb
+ 1, usz
);
458 if (!strcmp("--", sb
.buf
)) {
460 die(_("no usage string given before the `--' separator"));
464 usage
[unb
++] = strbuf_detach(&sb
, NULL
);
467 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
468 while (strbuf_getline(&sb
, stdin
) != EOF
) {
476 ALLOC_GROW(opts
, onb
+ 1, osz
);
477 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
480 help
= findspace(sb
.buf
);
481 if (!help
|| sb
.buf
== help
) {
482 o
->type
= OPTION_GROUP
;
483 o
->help
= xstrdup(skipspaces(sb
.buf
));
489 o
->type
= OPTION_CALLBACK
;
490 o
->help
= xstrdup(skipspaces(help
+1));
492 o
->flags
= PARSE_OPT_NOARG
;
493 o
->callback
= &parseopt_dump
;
496 s
= strpbrk(sb
.buf
, flag_chars
);
501 die(_("missing opt-spec before option flags"));
503 if (s
- sb
.buf
== 1) /* short option only */
504 o
->short_name
= *sb
.buf
;
505 else if (sb
.buf
[1] != ',') /* long option only */
506 o
->long_name
= xmemdupz(sb
.buf
, s
- sb
.buf
);
508 o
->short_name
= *sb
.buf
;
509 o
->long_name
= xmemdupz(sb
.buf
+ 2, s
- sb
.buf
- 2);
516 o
->flags
&= ~PARSE_OPT_NOARG
;
519 o
->flags
&= ~PARSE_OPT_NOARG
;
520 o
->flags
|= PARSE_OPT_OPTARG
;
523 o
->flags
|= PARSE_OPT_NONEG
;
526 o
->flags
|= PARSE_OPT_HIDDEN
;
534 o
->argh
= xmemdupz(s
, help
- s
);
538 /* put an OPT_END() */
539 ALLOC_GROW(opts
, onb
+ 1, osz
);
540 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
541 argc
= parse_options(argc
, argv
, prefix
, opts
, usage
,
542 (keep_dashdash
? PARSE_OPT_KEEP_DASHDASH
: 0) |
543 (stop_at_non_option
? PARSE_OPT_STOP_AT_NON_OPTION
: 0) |
544 PARSE_OPT_SHELL_EVAL
);
546 strbuf_addstr(&parsed
, " --");
547 sq_quote_argv(&parsed
, argv
);
549 strbuf_release(&parsed
);
553 static int cmd_sq_quote(int argc
, const char **argv
)
555 struct strbuf buf
= STRBUF_INIT
;
558 sq_quote_argv(&buf
, argv
);
559 printf("%s\n", buf
.buf
);
560 strbuf_release(&buf
);
565 static void die_no_single_rev(int quiet
)
570 die(_("Needed a single revision"));
573 static const char builtin_rev_parse_usage
[] =
574 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
575 " or: git rev-parse --sq-quote [<arg>...]\n"
576 " or: git rev-parse [<options>] [<arg>...]\n"
578 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
581 * Parse "opt" or "opt=<value>", setting value respectively to either
582 * NULL or the string after "=".
584 static int opt_with_value(const char *arg
, const char *opt
, const char **value
)
586 if (skip_prefix(arg
, opt
, &arg
)) {
599 static void handle_ref_opt(const char *pattern
, const char *prefix
)
602 for_each_glob_ref_in(show_reference
, pattern
, prefix
, NULL
);
604 for_each_ref_in(prefix
, show_reference
, NULL
);
605 clear_ref_exclusions(&ref_excludes
);
609 /* We would like a relative path. */
611 /* We would like a canonical absolute path. */
613 /* We would like the default behavior. */
618 /* Our default is a relative path. */
620 /* Our default is a relative path if there's a shared root. */
621 DEFAULT_RELATIVE_IF_SHARED
,
622 /* Our default is a canonical absolute path. */
624 /* Our default is not to modify the item. */
628 static void print_path(const char *path
, const char *prefix
, enum format_type format
, enum default_type def
)
632 * We don't ever produce a relative path if prefix is NULL, so set the
633 * prefix to the current directory so that we can produce a relative
634 * path whenever possible. If we're using RELATIVE_IF_SHARED mode, then
635 * we want an absolute path unless the two share a common prefix, so don't
636 * set it in that case, since doing so causes a relative path to always
637 * be produced if possible.
639 if (!prefix
&& (format
!= FORMAT_DEFAULT
|| def
!= DEFAULT_RELATIVE_IF_SHARED
))
640 prefix
= cwd
= xgetcwd();
641 if (format
== FORMAT_DEFAULT
&& def
== DEFAULT_UNMODIFIED
) {
643 } else if (format
== FORMAT_RELATIVE
||
644 (format
== FORMAT_DEFAULT
&& def
== DEFAULT_RELATIVE
)) {
646 * In order for relative_path to work as expected, we need to
647 * make sure that both paths are absolute paths. If we don't,
648 * we can end up with an unexpected absolute path that the user
651 struct strbuf buf
= STRBUF_INIT
, realbuf
= STRBUF_INIT
, prefixbuf
= STRBUF_INIT
;
652 if (!is_absolute_path(path
)) {
653 strbuf_realpath_forgiving(&realbuf
, path
, 1);
656 if (!is_absolute_path(prefix
)) {
657 strbuf_realpath_forgiving(&prefixbuf
, prefix
, 1);
658 prefix
= prefixbuf
.buf
;
660 puts(relative_path(path
, prefix
, &buf
));
661 strbuf_release(&buf
);
662 strbuf_release(&realbuf
);
663 strbuf_release(&prefixbuf
);
664 } else if (format
== FORMAT_DEFAULT
&& def
== DEFAULT_RELATIVE_IF_SHARED
) {
665 struct strbuf buf
= STRBUF_INIT
;
666 puts(relative_path(path
, prefix
, &buf
));
667 strbuf_release(&buf
);
669 struct strbuf buf
= STRBUF_INIT
;
670 strbuf_realpath_forgiving(&buf
, path
, 1);
672 strbuf_release(&buf
);
677 int cmd_rev_parse(int argc
, const char **argv
, const char *prefix
)
679 int i
, as_is
= 0, verify
= 0, quiet
= 0, revs_count
= 0, type
= 0;
680 const struct git_hash_algo
*output_algo
= NULL
;
681 const struct git_hash_algo
*compat
= NULL
;
682 int did_repo_setup
= 0;
683 int has_dashdash
= 0;
684 int output_prefix
= 0;
685 struct object_id oid
;
686 unsigned int flags
= 0;
687 const char *name
= NULL
;
688 struct object_context unused
;
689 struct strbuf buf
= STRBUF_INIT
;
690 const int hexsz
= the_hash_algo
->hexsz
;
691 int seen_end_of_options
= 0;
692 enum format_type format
= FORMAT_DEFAULT
;
694 if (argc
> 1 && !strcmp("--parseopt", argv
[1]))
695 return cmd_parseopt(argc
- 1, argv
+ 1, prefix
);
697 if (argc
> 1 && !strcmp("--sq-quote", argv
[1]))
698 return cmd_sq_quote(argc
- 2, argv
+ 2);
700 if (argc
> 1 && !strcmp("-h", argv
[1]))
701 usage(builtin_rev_parse_usage
);
703 for (i
= 1; i
< argc
; i
++) {
704 if (!strcmp(argv
[i
], "--")) {
710 /* No options; just report on whether we're in a git repo or not. */
712 setup_git_directory();
713 git_config(git_default_config
, NULL
);
717 for (i
= 1; i
< argc
; i
++) {
718 const char *arg
= argv
[i
];
721 if (show_file(arg
, output_prefix
) && as_is
< 2)
722 verify_filename(prefix
, arg
, 0);
726 if (!seen_end_of_options
) {
727 if (!strcmp(arg
, "--local-env-vars")) {
729 for (i
= 0; local_repo_env
[i
]; i
++)
730 printf("%s\n", local_repo_env
[i
]);
733 if (!strcmp(arg
, "--resolve-git-dir")) {
734 const char *gitdir
= argv
[++i
];
736 die(_("--resolve-git-dir requires an argument"));
737 gitdir
= resolve_gitdir(gitdir
);
739 die(_("not a gitdir '%s'"), argv
[i
]);
745 /* The rest of the options require a git repository. */
746 if (!did_repo_setup
) {
747 prefix
= setup_git_directory();
748 git_config(git_default_config
, NULL
);
751 prepare_repo_settings(the_repository
);
752 the_repository
->settings
.command_requires_full_index
= 0;
753 compat
= the_repository
->compat_hash_algo
;
756 if (!strcmp(arg
, "--")) {
758 /* Pass on the "--" if we show anything but files.. */
759 if (filter
& (DO_FLAGS
| DO_REVS
))
764 if (!seen_end_of_options
&& *arg
== '-') {
765 if (!strcmp(arg
, "--git-path")) {
767 die(_("--git-path requires an argument"));
769 print_path(git_path("%s", argv
[i
+ 1]), prefix
,
771 DEFAULT_RELATIVE_IF_SHARED
);
775 if (!strcmp(arg
,"-n")) {
777 die(_("-n requires an argument"));
778 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
)) {
784 if (starts_with(arg
, "-n")) {
785 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
))
789 if (opt_with_value(arg
, "--path-format", &arg
)) {
791 die(_("--path-format requires an argument"));
792 if (!strcmp(arg
, "absolute")) {
793 format
= FORMAT_CANONICAL
;
794 } else if (!strcmp(arg
, "relative")) {
795 format
= FORMAT_RELATIVE
;
797 die(_("unknown argument to --path-format: %s"), arg
);
801 if (!strcmp(arg
, "--default")) {
804 die(_("--default requires an argument"));
807 if (!strcmp(arg
, "--prefix")) {
810 die(_("--prefix requires an argument"));
811 startup_info
->prefix
= prefix
;
815 if (!strcmp(arg
, "--revs-only")) {
819 if (!strcmp(arg
, "--no-revs")) {
823 if (!strcmp(arg
, "--flags")) {
824 filter
&= ~DO_NONFLAGS
;
827 if (!strcmp(arg
, "--no-flags")) {
831 if (!strcmp(arg
, "--verify")) {
832 filter
&= ~(DO_FLAGS
|DO_NOREV
);
836 if (!strcmp(arg
, "--quiet") || !strcmp(arg
, "-q")) {
838 flags
|= GET_OID_QUIETLY
;
841 if (opt_with_value(arg
, "--output-object-format", &arg
)) {
843 die(_("no object format specified"));
844 if (!strcmp(arg
, the_hash_algo
->name
) ||
845 !strcmp(arg
, "storage")) {
846 flags
|= GET_OID_HASH_ANY
;
847 output_algo
= the_hash_algo
;
850 else if (compat
&& !strcmp(arg
, compat
->name
)) {
851 flags
|= GET_OID_HASH_ANY
;
852 output_algo
= compat
;
855 else die(_("unsupported object format: %s"), arg
);
857 if (opt_with_value(arg
, "--short", &arg
)) {
858 filter
&= ~(DO_FLAGS
|DO_NOREV
);
860 abbrev
= DEFAULT_ABBREV
;
863 abbrev
= strtoul(arg
, NULL
, 10);
864 if (abbrev
< MINIMUM_ABBREV
)
865 abbrev
= MINIMUM_ABBREV
;
866 else if (hexsz
<= abbrev
)
870 if (!strcmp(arg
, "--sq")) {
874 if (!strcmp(arg
, "--not")) {
875 show_type
^= REVERSED
;
878 if (!strcmp(arg
, "--symbolic")) {
879 symbolic
= SHOW_SYMBOLIC_ASIS
;
882 if (!strcmp(arg
, "--symbolic-full-name")) {
883 symbolic
= SHOW_SYMBOLIC_FULL
;
886 if (opt_with_value(arg
, "--abbrev-ref", &arg
)) {
888 abbrev_ref_strict
= warn_ambiguous_refs
;
890 if (!strcmp(arg
, "strict"))
891 abbrev_ref_strict
= 1;
892 else if (!strcmp(arg
, "loose"))
893 abbrev_ref_strict
= 0;
895 die(_("unknown mode for --abbrev-ref: %s"),
900 if (!strcmp(arg
, "--all")) {
901 for_each_ref(show_reference
, NULL
);
902 clear_ref_exclusions(&ref_excludes
);
905 if (skip_prefix(arg
, "--disambiguate=", &arg
)) {
906 repo_for_each_abbrev(the_repository
, arg
, the_hash_algo
,
910 if (!strcmp(arg
, "--bisect")) {
911 for_each_fullref_in("refs/bisect/bad", show_reference
, NULL
);
912 for_each_fullref_in("refs/bisect/good", anti_reference
, NULL
);
915 if (opt_with_value(arg
, "--branches", &arg
)) {
916 if (ref_excludes
.hidden_refs_configured
)
917 return error(_("options '%s' and '%s' cannot be used together"),
918 "--exclude-hidden", "--branches");
919 handle_ref_opt(arg
, "refs/heads/");
922 if (opt_with_value(arg
, "--tags", &arg
)) {
923 if (ref_excludes
.hidden_refs_configured
)
924 return error(_("options '%s' and '%s' cannot be used together"),
925 "--exclude-hidden", "--tags");
926 handle_ref_opt(arg
, "refs/tags/");
929 if (skip_prefix(arg
, "--glob=", &arg
)) {
930 handle_ref_opt(arg
, NULL
);
933 if (opt_with_value(arg
, "--remotes", &arg
)) {
934 if (ref_excludes
.hidden_refs_configured
)
935 return error(_("options '%s' and '%s' cannot be used together"),
936 "--exclude-hidden", "--remotes");
937 handle_ref_opt(arg
, "refs/remotes/");
940 if (skip_prefix(arg
, "--exclude=", &arg
)) {
941 add_ref_exclusion(&ref_excludes
, arg
);
944 if (skip_prefix(arg
, "--exclude-hidden=", &arg
)) {
945 exclude_hidden_refs(&ref_excludes
, arg
);
948 if (!strcmp(arg
, "--show-toplevel")) {
949 const char *work_tree
= get_git_work_tree();
951 print_path(work_tree
, prefix
, format
, DEFAULT_UNMODIFIED
);
953 die(_("this operation must be run in a work tree"));
956 if (!strcmp(arg
, "--show-superproject-working-tree")) {
957 struct strbuf superproject
= STRBUF_INIT
;
958 if (get_superproject_working_tree(&superproject
))
959 print_path(superproject
.buf
, prefix
, format
, DEFAULT_UNMODIFIED
);
960 strbuf_release(&superproject
);
963 if (!strcmp(arg
, "--show-prefix")) {
970 if (!strcmp(arg
, "--show-cdup")) {
971 const char *pfx
= prefix
;
972 if (!is_inside_work_tree()) {
973 const char *work_tree
=
976 printf("%s\n", work_tree
);
980 pfx
= strchr(pfx
, '/');
989 if (!strcmp(arg
, "--git-dir") ||
990 !strcmp(arg
, "--absolute-git-dir")) {
991 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
994 enum format_type wanted
= format
;
995 if (arg
[2] == 'g') { /* --git-dir */
997 print_path(gitdir
, prefix
, format
, DEFAULT_UNMODIFIED
);
1001 print_path(".git", prefix
, format
, DEFAULT_UNMODIFIED
);
1004 } else { /* --absolute-git-dir */
1005 wanted
= FORMAT_CANONICAL
;
1006 if (!gitdir
&& !prefix
)
1009 struct strbuf realpath
= STRBUF_INIT
;
1010 strbuf_realpath(&realpath
, gitdir
, 1);
1012 strbuf_release(&realpath
);
1019 strbuf_addf(&buf
, "%s%s.git", cwd
, len
&& cwd
[len
-1] != '/' ? "/" : "");
1021 print_path(buf
.buf
, prefix
, wanted
, DEFAULT_CANONICAL
);
1024 if (!strcmp(arg
, "--git-common-dir")) {
1025 print_path(get_git_common_dir(), prefix
, format
, DEFAULT_RELATIVE_IF_SHARED
);
1028 if (!strcmp(arg
, "--is-inside-git-dir")) {
1029 printf("%s\n", is_inside_git_dir() ? "true"
1033 if (!strcmp(arg
, "--is-inside-work-tree")) {
1034 printf("%s\n", is_inside_work_tree() ? "true"
1038 if (!strcmp(arg
, "--is-bare-repository")) {
1039 printf("%s\n", is_bare_repository() ? "true"
1043 if (!strcmp(arg
, "--is-shallow-repository")) {
1045 is_repository_shallow(the_repository
) ? "true"
1049 if (!strcmp(arg
, "--shared-index-path")) {
1050 if (repo_read_index(the_repository
) < 0)
1051 die(_("Could not read the index"));
1052 if (the_index
.split_index
) {
1053 const struct object_id
*oid
= &the_index
.split_index
->base_oid
;
1054 const char *path
= git_path("sharedindex.%s", oid_to_hex(oid
));
1055 print_path(path
, prefix
, format
, DEFAULT_RELATIVE
);
1059 if (skip_prefix(arg
, "--since=", &arg
)) {
1060 show_datestring("--max-age=", arg
);
1063 if (skip_prefix(arg
, "--after=", &arg
)) {
1064 show_datestring("--max-age=", arg
);
1067 if (skip_prefix(arg
, "--before=", &arg
)) {
1068 show_datestring("--min-age=", arg
);
1071 if (skip_prefix(arg
, "--until=", &arg
)) {
1072 show_datestring("--min-age=", arg
);
1075 if (opt_with_value(arg
, "--show-object-format", &arg
)) {
1076 const char *val
= arg
? arg
: "storage";
1078 if (strcmp(val
, "storage") &&
1079 strcmp(val
, "input") &&
1080 strcmp(val
, "output"))
1081 die(_("unknown mode for --show-object-format: %s"),
1083 puts(the_hash_algo
->name
);
1086 if (!strcmp(arg
, "--show-ref-format")) {
1087 puts(ref_storage_format_to_name(the_repository
->ref_storage_format
));
1090 if (!strcmp(arg
, "--end-of-options")) {
1091 seen_end_of_options
= 1;
1092 if (filter
& (DO_FLAGS
| DO_REVS
))
1096 if (show_flag(arg
) && verify
)
1097 die_no_single_rev(quiet
);
1101 /* Not a flag argument */
1102 if (try_difference(arg
))
1104 if (try_parent_shorthands(arg
))
1112 if (!get_oid_with_context(the_repository
, name
,
1113 flags
, &oid
, &unused
)) {
1115 repo_oid_to_algop(the_repository
, &oid
,
1120 show_rev(type
, &oid
, name
);
1124 die_no_single_rev(quiet
);
1126 die(_("bad revision '%s'"), arg
);
1128 if (!show_file(arg
, output_prefix
))
1130 verify_filename(prefix
, arg
, 1);
1132 strbuf_release(&buf
);
1134 if (revs_count
== 1) {
1135 show_rev(type
, &oid
, name
);
1137 } else if (revs_count
== 0 && show_default())
1139 die_no_single_rev(quiet
);