transport-helper: drop read/write errno checks
[git.git] / builtin / rev-parse.c
blob10d4dab894e25a73d6a761205ce53c88019a4a8b
1 /*
2 * rev-parse.c
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "config.h"
8 #include "commit.h"
9 #include "refs.h"
10 #include "quote.h"
11 #include "builtin.h"
12 #include "parse-options.h"
13 #include "diff.h"
14 #include "revision.h"
15 #include "split-index.h"
16 #include "submodule.h"
17 #include "commit-reach.h"
19 #define DO_REVS 1
20 #define DO_NOREV 2
21 #define DO_FLAGS 4
22 #define DO_NONFLAGS 8
23 static int filter = ~0;
25 static const char *def;
27 #define NORMAL 0
28 #define REVERSED 1
29 static int show_type = NORMAL;
31 #define SHOW_SYMBOLIC_ASIS 1
32 #define SHOW_SYMBOLIC_FULL 2
33 static int symbolic;
34 static int abbrev;
35 static int abbrev_ref;
36 static int abbrev_ref_strict;
37 static int output_sq;
39 static int stuck_long;
40 static struct string_list *ref_excludes;
43 * Some arguments are relevant "revision" arguments,
44 * others are about output format or other details.
45 * This sorts it all out.
47 static int is_rev_argument(const char *arg)
49 static const char *rev_args[] = {
50 "--all",
51 "--bisect",
52 "--dense",
53 "--branches=",
54 "--branches",
55 "--header",
56 "--ignore-missing",
57 "--max-age=",
58 "--max-count=",
59 "--min-age=",
60 "--no-merges",
61 "--min-parents=",
62 "--no-min-parents",
63 "--max-parents=",
64 "--no-max-parents",
65 "--objects",
66 "--objects-edge",
67 "--parents",
68 "--pretty",
69 "--remotes=",
70 "--remotes",
71 "--glob=",
72 "--sparse",
73 "--tags=",
74 "--tags",
75 "--topo-order",
76 "--date-order",
77 "--unpacked",
78 NULL
80 const char **p = rev_args;
82 /* accept -<digit>, like traditional "head" */
83 if ((*arg == '-') && isdigit(arg[1]))
84 return 1;
86 for (;;) {
87 const char *str = *p++;
88 int len;
89 if (!str)
90 return 0;
91 len = strlen(str);
92 if (!strcmp(arg, str) ||
93 (str[len-1] == '=' && !strncmp(arg, str, len)))
94 return 1;
98 /* Output argument as a string, either SQ or normal */
99 static void show(const char *arg)
101 if (output_sq) {
102 int sq = '\'', ch;
104 putchar(sq);
105 while ((ch = *arg++)) {
106 if (ch == sq)
107 fputs("'\\'", stdout);
108 putchar(ch);
110 putchar(sq);
111 putchar(' ');
113 else
114 puts(arg);
117 /* Like show(), but with a negation prefix according to type */
118 static void show_with_type(int type, const char *arg)
120 if (type != show_type)
121 putchar('^');
122 show(arg);
125 /* Output a revision, only if filter allows it */
126 static void show_rev(int type, const struct object_id *oid, const char *name)
128 if (!(filter & DO_REVS))
129 return;
130 def = NULL;
132 if ((symbolic || abbrev_ref) && name) {
133 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
134 struct object_id discard;
135 char *full;
137 switch (dwim_ref(name, strlen(name), &discard, &full)) {
138 case 0:
140 * Not found -- not a ref. We could
141 * emit "name" here, but symbolic-full
142 * users are interested in finding the
143 * refs spelled in full, and they would
144 * need to filter non-refs if we did so.
146 break;
147 case 1: /* happy */
148 if (abbrev_ref)
149 full = shorten_unambiguous_ref(full,
150 abbrev_ref_strict);
151 show_with_type(type, full);
152 break;
153 default: /* ambiguous */
154 error("refname '%s' is ambiguous", name);
155 break;
157 free(full);
158 } else {
159 show_with_type(type, name);
162 else if (abbrev)
163 show_with_type(type, find_unique_abbrev(oid, abbrev));
164 else
165 show_with_type(type, oid_to_hex(oid));
168 /* Output a flag, only if filter allows it. */
169 static int show_flag(const char *arg)
171 if (!(filter & DO_FLAGS))
172 return 0;
173 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
174 show(arg);
175 return 1;
177 return 0;
180 static int show_default(void)
182 const char *s = def;
184 if (s) {
185 struct object_id oid;
187 def = NULL;
188 if (!get_oid(s, &oid)) {
189 show_rev(NORMAL, &oid, s);
190 return 1;
193 return 0;
196 static int show_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
198 if (ref_excluded(ref_excludes, refname))
199 return 0;
200 show_rev(NORMAL, oid, refname);
201 return 0;
204 static int anti_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data)
206 show_rev(REVERSED, oid, refname);
207 return 0;
210 static int show_abbrev(const struct object_id *oid, void *cb_data)
212 show_rev(NORMAL, oid, NULL);
213 return 0;
216 static void show_datestring(const char *flag, const char *datestr)
218 char *buffer;
220 /* date handling requires both flags and revs */
221 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
222 return;
223 buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr));
224 show(buffer);
225 free(buffer);
228 static int show_file(const char *arg, int output_prefix)
230 show_default();
231 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
232 if (output_prefix) {
233 const char *prefix = startup_info->prefix;
234 char *fname = prefix_filename(prefix, arg);
235 show(fname);
236 free(fname);
237 } else
238 show(arg);
239 return 1;
241 return 0;
244 static int try_difference(const char *arg)
246 char *dotdot;
247 struct object_id start_oid;
248 struct object_id end_oid;
249 const char *end;
250 const char *start;
251 int symmetric;
252 static const char head_by_default[] = "HEAD";
254 if (!(dotdot = strstr(arg, "..")))
255 return 0;
256 end = dotdot + 2;
257 start = arg;
258 symmetric = (*end == '.');
260 *dotdot = 0;
261 end += symmetric;
263 if (!*end)
264 end = head_by_default;
265 if (dotdot == arg)
266 start = head_by_default;
268 if (start == head_by_default && end == head_by_default &&
269 !symmetric) {
271 * Just ".."? That is not a range but the
272 * pathspec for the parent directory.
274 *dotdot = '.';
275 return 0;
278 if (!get_oid_committish(start, &start_oid) && !get_oid_committish(end, &end_oid)) {
279 show_rev(NORMAL, &end_oid, end);
280 show_rev(symmetric ? NORMAL : REVERSED, &start_oid, start);
281 if (symmetric) {
282 struct commit_list *exclude;
283 struct commit *a, *b;
284 a = lookup_commit_reference(the_repository, &start_oid);
285 b = lookup_commit_reference(the_repository, &end_oid);
286 if (!a || !b) {
287 *dotdot = '.';
288 return 0;
290 exclude = get_merge_bases(a, b);
291 while (exclude) {
292 struct commit *commit = pop_commit(&exclude);
293 show_rev(REVERSED, &commit->object.oid, NULL);
296 *dotdot = '.';
297 return 1;
299 *dotdot = '.';
300 return 0;
303 static int try_parent_shorthands(const char *arg)
305 char *dotdot;
306 struct object_id oid;
307 struct commit *commit;
308 struct commit_list *parents;
309 int parent_number;
310 int include_rev = 0;
311 int include_parents = 0;
312 int exclude_parent = 0;
314 if ((dotdot = strstr(arg, "^!"))) {
315 include_rev = 1;
316 if (dotdot[2])
317 return 0;
318 } else if ((dotdot = strstr(arg, "^@"))) {
319 include_parents = 1;
320 if (dotdot[2])
321 return 0;
322 } else if ((dotdot = strstr(arg, "^-"))) {
323 include_rev = 1;
324 exclude_parent = 1;
326 if (dotdot[2]) {
327 char *end;
328 exclude_parent = strtoul(dotdot + 2, &end, 10);
329 if (*end != '\0' || !exclude_parent)
330 return 0;
332 } else
333 return 0;
335 *dotdot = 0;
336 if (get_oid_committish(arg, &oid) ||
337 !(commit = lookup_commit_reference(the_repository, &oid))) {
338 *dotdot = '^';
339 return 0;
342 if (exclude_parent &&
343 exclude_parent > commit_list_count(commit->parents)) {
344 *dotdot = '^';
345 return 0;
348 if (include_rev)
349 show_rev(NORMAL, &oid, arg);
350 for (parents = commit->parents, parent_number = 1;
351 parents;
352 parents = parents->next, parent_number++) {
353 char *name = NULL;
355 if (exclude_parent && parent_number != exclude_parent)
356 continue;
358 if (symbolic)
359 name = xstrfmt("%s^%d", arg, parent_number);
360 show_rev(include_parents ? NORMAL : REVERSED,
361 &parents->item->object.oid, name);
362 free(name);
365 *dotdot = '^';
366 return 1;
369 static int parseopt_dump(const struct option *o, const char *arg, int unset)
371 struct strbuf *parsed = o->value;
372 if (unset)
373 strbuf_addf(parsed, " --no-%s", o->long_name);
374 else if (o->short_name && (o->long_name == NULL || !stuck_long))
375 strbuf_addf(parsed, " -%c", o->short_name);
376 else
377 strbuf_addf(parsed, " --%s", o->long_name);
378 if (arg) {
379 if (!stuck_long)
380 strbuf_addch(parsed, ' ');
381 else if (o->long_name)
382 strbuf_addch(parsed, '=');
383 sq_quote_buf(parsed, arg);
385 return 0;
388 static const char *skipspaces(const char *s)
390 while (isspace(*s))
391 s++;
392 return s;
395 static char *findspace(const char *s)
397 for (; *s; s++)
398 if (isspace(*s))
399 return (char*)s;
400 return NULL;
403 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
405 static int keep_dashdash = 0, stop_at_non_option = 0;
406 static char const * const parseopt_usage[] = {
407 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
408 NULL
410 static struct option parseopt_opts[] = {
411 OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
412 N_("keep the `--` passed as an arg")),
413 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
414 N_("stop parsing after the "
415 "first non-option argument")),
416 OPT_BOOL(0, "stuck-long", &stuck_long,
417 N_("output in stuck long form")),
418 OPT_END(),
420 static const char * const flag_chars = "*=?!";
422 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
423 const char **usage = NULL;
424 struct option *opts = NULL;
425 int onb = 0, osz = 0, unb = 0, usz = 0;
427 strbuf_addstr(&parsed, "set --");
428 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
429 PARSE_OPT_KEEP_DASHDASH);
430 if (argc < 1 || strcmp(argv[0], "--"))
431 usage_with_options(parseopt_usage, parseopt_opts);
433 /* get the usage up to the first line with a -- on it */
434 for (;;) {
435 if (strbuf_getline(&sb, stdin) == EOF)
436 die("premature end of input");
437 ALLOC_GROW(usage, unb + 1, usz);
438 if (!strcmp("--", sb.buf)) {
439 if (unb < 1)
440 die("no usage string given before the `--' separator");
441 usage[unb] = NULL;
442 break;
444 usage[unb++] = strbuf_detach(&sb, NULL);
447 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
448 while (strbuf_getline(&sb, stdin) != EOF) {
449 const char *s;
450 char *help;
451 struct option *o;
453 if (!sb.len)
454 continue;
456 ALLOC_GROW(opts, onb + 1, osz);
457 memset(opts + onb, 0, sizeof(opts[onb]));
459 o = &opts[onb++];
460 help = findspace(sb.buf);
461 if (!help || sb.buf == help) {
462 o->type = OPTION_GROUP;
463 o->help = xstrdup(skipspaces(sb.buf));
464 continue;
467 *help = '\0';
469 o->type = OPTION_CALLBACK;
470 o->help = xstrdup(skipspaces(help+1));
471 o->value = &parsed;
472 o->flags = PARSE_OPT_NOARG;
473 o->callback = &parseopt_dump;
475 /* name(s) */
476 s = strpbrk(sb.buf, flag_chars);
477 if (s == NULL)
478 s = help;
480 if (s - sb.buf == 1) /* short option only */
481 o->short_name = *sb.buf;
482 else if (sb.buf[1] != ',') /* long option only */
483 o->long_name = xmemdupz(sb.buf, s - sb.buf);
484 else {
485 o->short_name = *sb.buf;
486 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
489 /* flags */
490 while (s < help) {
491 switch (*s++) {
492 case '=':
493 o->flags &= ~PARSE_OPT_NOARG;
494 continue;
495 case '?':
496 o->flags &= ~PARSE_OPT_NOARG;
497 o->flags |= PARSE_OPT_OPTARG;
498 continue;
499 case '!':
500 o->flags |= PARSE_OPT_NONEG;
501 continue;
502 case '*':
503 o->flags |= PARSE_OPT_HIDDEN;
504 continue;
506 s--;
507 break;
510 if (s < help)
511 o->argh = xmemdupz(s, help - s);
513 strbuf_release(&sb);
515 /* put an OPT_END() */
516 ALLOC_GROW(opts, onb + 1, osz);
517 memset(opts + onb, 0, sizeof(opts[onb]));
518 argc = parse_options(argc, argv, prefix, opts, usage,
519 (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
520 (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
521 PARSE_OPT_SHELL_EVAL);
523 strbuf_addstr(&parsed, " --");
524 sq_quote_argv(&parsed, argv);
525 puts(parsed.buf);
526 return 0;
529 static int cmd_sq_quote(int argc, const char **argv)
531 struct strbuf buf = STRBUF_INIT;
533 if (argc)
534 sq_quote_argv(&buf, argv);
535 printf("%s\n", buf.buf);
536 strbuf_release(&buf);
538 return 0;
541 static void die_no_single_rev(int quiet)
543 if (quiet)
544 exit(1);
545 else
546 die("Needed a single revision");
549 static const char builtin_rev_parse_usage[] =
550 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
551 " or: git rev-parse --sq-quote [<arg>...]\n"
552 " or: git rev-parse [<options>] [<arg>...]\n"
553 "\n"
554 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
557 * Parse "opt" or "opt=<value>", setting value respectively to either
558 * NULL or the string after "=".
560 static int opt_with_value(const char *arg, const char *opt, const char **value)
562 if (skip_prefix(arg, opt, &arg)) {
563 if (!*arg) {
564 *value = NULL;
565 return 1;
567 if (*arg++ == '=') {
568 *value = arg;
569 return 1;
572 return 0;
575 static void handle_ref_opt(const char *pattern, const char *prefix)
577 if (pattern)
578 for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
579 else
580 for_each_ref_in(prefix, show_reference, NULL);
581 clear_ref_exclusion(&ref_excludes);
584 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
586 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
587 int did_repo_setup = 0;
588 int has_dashdash = 0;
589 int output_prefix = 0;
590 struct object_id oid;
591 unsigned int flags = 0;
592 const char *name = NULL;
593 struct object_context unused;
594 struct strbuf buf = STRBUF_INIT;
596 if (argc > 1 && !strcmp("--parseopt", argv[1]))
597 return cmd_parseopt(argc - 1, argv + 1, prefix);
599 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
600 return cmd_sq_quote(argc - 2, argv + 2);
602 if (argc > 1 && !strcmp("-h", argv[1]))
603 usage(builtin_rev_parse_usage);
605 for (i = 1; i < argc; i++) {
606 if (!strcmp(argv[i], "--")) {
607 has_dashdash = 1;
608 break;
612 /* No options; just report on whether we're in a git repo or not. */
613 if (argc == 1) {
614 setup_git_directory();
615 git_config(git_default_config, NULL);
616 return 0;
619 for (i = 1; i < argc; i++) {
620 const char *arg = argv[i];
622 if (!strcmp(arg, "--local-env-vars")) {
623 int i;
624 for (i = 0; local_repo_env[i]; i++)
625 printf("%s\n", local_repo_env[i]);
626 continue;
628 if (!strcmp(arg, "--resolve-git-dir")) {
629 const char *gitdir = argv[++i];
630 if (!gitdir)
631 die("--resolve-git-dir requires an argument");
632 gitdir = resolve_gitdir(gitdir);
633 if (!gitdir)
634 die("not a gitdir '%s'", argv[i]);
635 puts(gitdir);
636 continue;
639 /* The rest of the options require a git repository. */
640 if (!did_repo_setup) {
641 prefix = setup_git_directory();
642 git_config(git_default_config, NULL);
643 did_repo_setup = 1;
646 if (!strcmp(arg, "--git-path")) {
647 if (!argv[i + 1])
648 die("--git-path requires an argument");
649 strbuf_reset(&buf);
650 puts(relative_path(git_path("%s", argv[i + 1]),
651 prefix, &buf));
652 i++;
653 continue;
655 if (as_is) {
656 if (show_file(arg, output_prefix) && as_is < 2)
657 verify_filename(prefix, arg, 0);
658 continue;
660 if (!strcmp(arg,"-n")) {
661 if (++i >= argc)
662 die("-n requires an argument");
663 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
664 show(arg);
665 show(argv[i]);
667 continue;
669 if (starts_with(arg, "-n")) {
670 if ((filter & DO_FLAGS) && (filter & DO_REVS))
671 show(arg);
672 continue;
675 if (*arg == '-') {
676 if (!strcmp(arg, "--")) {
677 as_is = 2;
678 /* Pass on the "--" if we show anything but files.. */
679 if (filter & (DO_FLAGS | DO_REVS))
680 show_file(arg, 0);
681 continue;
683 if (!strcmp(arg, "--default")) {
684 def = argv[++i];
685 if (!def)
686 die("--default requires an argument");
687 continue;
689 if (!strcmp(arg, "--prefix")) {
690 prefix = argv[++i];
691 if (!prefix)
692 die("--prefix requires an argument");
693 startup_info->prefix = prefix;
694 output_prefix = 1;
695 continue;
697 if (!strcmp(arg, "--revs-only")) {
698 filter &= ~DO_NOREV;
699 continue;
701 if (!strcmp(arg, "--no-revs")) {
702 filter &= ~DO_REVS;
703 continue;
705 if (!strcmp(arg, "--flags")) {
706 filter &= ~DO_NONFLAGS;
707 continue;
709 if (!strcmp(arg, "--no-flags")) {
710 filter &= ~DO_FLAGS;
711 continue;
713 if (!strcmp(arg, "--verify")) {
714 filter &= ~(DO_FLAGS|DO_NOREV);
715 verify = 1;
716 continue;
718 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
719 quiet = 1;
720 flags |= GET_OID_QUIETLY;
721 continue;
723 if (opt_with_value(arg, "--short", &arg)) {
724 filter &= ~(DO_FLAGS|DO_NOREV);
725 verify = 1;
726 abbrev = DEFAULT_ABBREV;
727 if (!arg)
728 continue;
729 abbrev = strtoul(arg, NULL, 10);
730 if (abbrev < MINIMUM_ABBREV)
731 abbrev = MINIMUM_ABBREV;
732 else if (40 <= abbrev)
733 abbrev = 40;
734 continue;
736 if (!strcmp(arg, "--sq")) {
737 output_sq = 1;
738 continue;
740 if (!strcmp(arg, "--not")) {
741 show_type ^= REVERSED;
742 continue;
744 if (!strcmp(arg, "--symbolic")) {
745 symbolic = SHOW_SYMBOLIC_ASIS;
746 continue;
748 if (!strcmp(arg, "--symbolic-full-name")) {
749 symbolic = SHOW_SYMBOLIC_FULL;
750 continue;
752 if (opt_with_value(arg, "--abbrev-ref", &arg)) {
753 abbrev_ref = 1;
754 abbrev_ref_strict = warn_ambiguous_refs;
755 if (arg) {
756 if (!strcmp(arg, "strict"))
757 abbrev_ref_strict = 1;
758 else if (!strcmp(arg, "loose"))
759 abbrev_ref_strict = 0;
760 else
761 die("unknown mode for --abbrev-ref: %s",
762 arg);
764 continue;
766 if (!strcmp(arg, "--all")) {
767 for_each_ref(show_reference, NULL);
768 clear_ref_exclusion(&ref_excludes);
769 continue;
771 if (skip_prefix(arg, "--disambiguate=", &arg)) {
772 for_each_abbrev(arg, show_abbrev, NULL);
773 continue;
775 if (!strcmp(arg, "--bisect")) {
776 for_each_fullref_in("refs/bisect/bad", show_reference, NULL, 0);
777 for_each_fullref_in("refs/bisect/good", anti_reference, NULL, 0);
778 continue;
780 if (opt_with_value(arg, "--branches", &arg)) {
781 handle_ref_opt(arg, "refs/heads/");
782 continue;
784 if (opt_with_value(arg, "--tags", &arg)) {
785 handle_ref_opt(arg, "refs/tags/");
786 continue;
788 if (skip_prefix(arg, "--glob=", &arg)) {
789 handle_ref_opt(arg, NULL);
790 continue;
792 if (opt_with_value(arg, "--remotes", &arg)) {
793 handle_ref_opt(arg, "refs/remotes/");
794 continue;
796 if (skip_prefix(arg, "--exclude=", &arg)) {
797 add_ref_exclusion(&ref_excludes, arg);
798 continue;
800 if (!strcmp(arg, "--show-toplevel")) {
801 const char *work_tree = get_git_work_tree();
802 if (work_tree)
803 puts(work_tree);
804 continue;
806 if (!strcmp(arg, "--show-superproject-working-tree")) {
807 const char *superproject = get_superproject_working_tree();
808 if (superproject)
809 puts(superproject);
810 continue;
812 if (!strcmp(arg, "--show-prefix")) {
813 if (prefix)
814 puts(prefix);
815 else
816 putchar('\n');
817 continue;
819 if (!strcmp(arg, "--show-cdup")) {
820 const char *pfx = prefix;
821 if (!is_inside_work_tree()) {
822 const char *work_tree =
823 get_git_work_tree();
824 if (work_tree)
825 printf("%s\n", work_tree);
826 continue;
828 while (pfx) {
829 pfx = strchr(pfx, '/');
830 if (pfx) {
831 pfx++;
832 printf("../");
835 putchar('\n');
836 continue;
838 if (!strcmp(arg, "--git-dir") ||
839 !strcmp(arg, "--absolute-git-dir")) {
840 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
841 char *cwd;
842 int len;
843 if (arg[2] == 'g') { /* --git-dir */
844 if (gitdir) {
845 puts(gitdir);
846 continue;
848 if (!prefix) {
849 puts(".git");
850 continue;
852 } else { /* --absolute-git-dir */
853 if (!gitdir && !prefix)
854 gitdir = ".git";
855 if (gitdir) {
856 puts(real_path(gitdir));
857 continue;
860 cwd = xgetcwd();
861 len = strlen(cwd);
862 printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : "");
863 free(cwd);
864 continue;
866 if (!strcmp(arg, "--git-common-dir")) {
867 strbuf_reset(&buf);
868 puts(relative_path(get_git_common_dir(),
869 prefix, &buf));
870 continue;
872 if (!strcmp(arg, "--is-inside-git-dir")) {
873 printf("%s\n", is_inside_git_dir() ? "true"
874 : "false");
875 continue;
877 if (!strcmp(arg, "--is-inside-work-tree")) {
878 printf("%s\n", is_inside_work_tree() ? "true"
879 : "false");
880 continue;
882 if (!strcmp(arg, "--is-bare-repository")) {
883 printf("%s\n", is_bare_repository() ? "true"
884 : "false");
885 continue;
887 if (!strcmp(arg, "--is-shallow-repository")) {
888 printf("%s\n",
889 is_repository_shallow(the_repository) ? "true"
890 : "false");
891 continue;
893 if (!strcmp(arg, "--shared-index-path")) {
894 if (read_cache() < 0)
895 die(_("Could not read the index"));
896 if (the_index.split_index) {
897 const struct object_id *oid = &the_index.split_index->base_oid;
898 const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
899 strbuf_reset(&buf);
900 puts(relative_path(path, prefix, &buf));
902 continue;
904 if (skip_prefix(arg, "--since=", &arg)) {
905 show_datestring("--max-age=", arg);
906 continue;
908 if (skip_prefix(arg, "--after=", &arg)) {
909 show_datestring("--max-age=", arg);
910 continue;
912 if (skip_prefix(arg, "--before=", &arg)) {
913 show_datestring("--min-age=", arg);
914 continue;
916 if (skip_prefix(arg, "--until=", &arg)) {
917 show_datestring("--min-age=", arg);
918 continue;
920 if (show_flag(arg) && verify)
921 die_no_single_rev(quiet);
922 continue;
925 /* Not a flag argument */
926 if (try_difference(arg))
927 continue;
928 if (try_parent_shorthands(arg))
929 continue;
930 name = arg;
931 type = NORMAL;
932 if (*arg == '^') {
933 name++;
934 type = REVERSED;
936 if (!get_oid_with_context(name, flags, &oid, &unused)) {
937 if (verify)
938 revs_count++;
939 else
940 show_rev(type, &oid, name);
941 continue;
943 if (verify)
944 die_no_single_rev(quiet);
945 if (has_dashdash)
946 die("bad revision '%s'", arg);
947 as_is = 1;
948 if (!show_file(arg, output_prefix))
949 continue;
950 verify_filename(prefix, arg, 1);
952 strbuf_release(&buf);
953 if (verify) {
954 if (revs_count == 1) {
955 show_rev(type, &oid, name);
956 return 0;
957 } else if (revs_count == 0 && show_default())
958 return 0;
959 die_no_single_rev(quiet);
960 } else
961 show_default();
962 return 0;