archive: improve support for running in subdirectory
[git.git] / builtin / rev-parse.c
blobe67999e5ebc5ef640fe5dcd45dfa1b4126a9c7a8
1 /*
2 * rev-parse.c
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #define USE_THE_INDEX_VARIABLE
7 #include "cache.h"
8 #include "config.h"
9 #include "commit.h"
10 #include "refs.h"
11 #include "quote.h"
12 #include "builtin.h"
13 #include "parse-options.h"
14 #include "diff.h"
15 #include "revision.h"
16 #include "split-index.h"
17 #include "submodule.h"
18 #include "commit-reach.h"
19 #include "shallow.h"
21 #define DO_REVS 1
22 #define DO_NOREV 2
23 #define DO_FLAGS 4
24 #define DO_NONFLAGS 8
25 static int filter = ~0;
27 static const char *def;
29 #define NORMAL 0
30 #define REVERSED 1
31 static int show_type = NORMAL;
33 #define SHOW_SYMBOLIC_ASIS 1
34 #define SHOW_SYMBOLIC_FULL 2
35 static int symbolic;
36 static int abbrev;
37 static int abbrev_ref;
38 static int abbrev_ref_strict;
39 static int output_sq;
41 static int stuck_long;
42 static struct ref_exclusions ref_excludes = REF_EXCLUSIONS_INIT;
45 * Some arguments are relevant "revision" arguments,
46 * others are about output format or other details.
47 * This sorts it all out.
49 static int is_rev_argument(const char *arg)
51 static const char *rev_args[] = {
52 "--all",
53 "--bisect",
54 "--dense",
55 "--branches=",
56 "--branches",
57 "--header",
58 "--ignore-missing",
59 "--max-age=",
60 "--max-count=",
61 "--min-age=",
62 "--no-merges",
63 "--min-parents=",
64 "--no-min-parents",
65 "--max-parents=",
66 "--no-max-parents",
67 "--objects",
68 "--objects-edge",
69 "--parents",
70 "--pretty",
71 "--remotes=",
72 "--remotes",
73 "--glob=",
74 "--sparse",
75 "--tags=",
76 "--tags",
77 "--topo-order",
78 "--date-order",
79 "--unpacked",
80 NULL
82 const char **p = rev_args;
84 /* accept -<digit>, like traditional "head" */
85 if ((*arg == '-') && isdigit(arg[1]))
86 return 1;
88 for (;;) {
89 const char *str = *p++;
90 int len;
91 if (!str)
92 return 0;
93 len = strlen(str);
94 if (!strcmp(arg, str) ||
95 (str[len-1] == '=' && !strncmp(arg, str, len)))
96 return 1;
100 /* Output argument as a string, either SQ or normal */
101 static void show(const char *arg)
103 if (output_sq) {
104 int sq = '\'', ch;
106 putchar(sq);
107 while ((ch = *arg++)) {
108 if (ch == sq)
109 fputs("'\\'", stdout);
110 putchar(ch);
112 putchar(sq);
113 putchar(' ');
115 else
116 puts(arg);
119 /* Like show(), but with a negation prefix according to type */
120 static void show_with_type(int type, const char *arg)
122 if (type != show_type)
123 putchar('^');
124 show(arg);
127 /* Output a revision, only if filter allows it */
128 static void show_rev(int type, const struct object_id *oid, const char *name)
130 if (!(filter & DO_REVS))
131 return;
132 def = NULL;
134 if ((symbolic || abbrev_ref) && name) {
135 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
136 struct object_id discard;
137 char *full;
139 switch (dwim_ref(name, strlen(name), &discard, &full, 0)) {
140 case 0:
142 * Not found -- not a ref. We could
143 * emit "name" here, but symbolic-full
144 * users are interested in finding the
145 * refs spelled in full, and they would
146 * need to filter non-refs if we did so.
148 break;
149 case 1: /* happy */
150 if (abbrev_ref)
151 full = shorten_unambiguous_ref(full,
152 abbrev_ref_strict);
153 show_with_type(type, full);
154 break;
155 default: /* ambiguous */
156 error("refname '%s' is ambiguous", name);
157 break;
159 free(full);
160 } else {
161 show_with_type(type, name);
164 else if (abbrev)
165 show_with_type(type, find_unique_abbrev(oid, abbrev));
166 else
167 show_with_type(type, oid_to_hex(oid));
170 /* Output a flag, only if filter allows it. */
171 static int show_flag(const char *arg)
173 if (!(filter & DO_FLAGS))
174 return 0;
175 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
176 show(arg);
177 return 1;
179 return 0;
182 static int show_default(void)
184 const char *s = def;
186 if (s) {
187 struct object_id oid;
189 def = NULL;
190 if (!get_oid(s, &oid)) {
191 show_rev(NORMAL, &oid, s);
192 return 1;
195 return 0;
198 static int show_reference(const char *refname, const struct object_id *oid,
199 int flag UNUSED, void *cb_data UNUSED)
201 if (ref_excluded(&ref_excludes, refname))
202 return 0;
203 show_rev(NORMAL, oid, refname);
204 return 0;
207 static int anti_reference(const char *refname, const struct object_id *oid,
208 int flag UNUSED, void *cb_data UNUSED)
210 show_rev(REVERSED, oid, refname);
211 return 0;
214 static int show_abbrev(const struct object_id *oid, void *cb_data)
216 show_rev(NORMAL, oid, NULL);
217 return 0;
220 static void show_datestring(const char *flag, const char *datestr)
222 char *buffer;
224 /* date handling requires both flags and revs */
225 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
226 return;
227 buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr));
228 show(buffer);
229 free(buffer);
232 static int show_file(const char *arg, int output_prefix)
234 show_default();
235 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
236 if (output_prefix) {
237 const char *prefix = startup_info->prefix;
238 char *fname = prefix_filename(prefix, arg);
239 show(fname);
240 free(fname);
241 } else
242 show(arg);
243 return 1;
245 return 0;
248 static int try_difference(const char *arg)
250 char *dotdot;
251 struct object_id start_oid;
252 struct object_id end_oid;
253 const char *end;
254 const char *start;
255 int symmetric;
256 static const char head_by_default[] = "HEAD";
258 if (!(dotdot = strstr(arg, "..")))
259 return 0;
260 end = dotdot + 2;
261 start = arg;
262 symmetric = (*end == '.');
264 *dotdot = 0;
265 end += symmetric;
267 if (!*end)
268 end = head_by_default;
269 if (dotdot == arg)
270 start = head_by_default;
272 if (start == head_by_default && end == head_by_default &&
273 !symmetric) {
275 * Just ".."? That is not a range but the
276 * pathspec for the parent directory.
278 *dotdot = '.';
279 return 0;
282 if (!get_oid_committish(start, &start_oid) && !get_oid_committish(end, &end_oid)) {
283 show_rev(NORMAL, &end_oid, end);
284 show_rev(symmetric ? NORMAL : REVERSED, &start_oid, start);
285 if (symmetric) {
286 struct commit_list *exclude;
287 struct commit *a, *b;
288 a = lookup_commit_reference(the_repository, &start_oid);
289 b = lookup_commit_reference(the_repository, &end_oid);
290 if (!a || !b) {
291 *dotdot = '.';
292 return 0;
294 exclude = get_merge_bases(a, b);
295 while (exclude) {
296 struct commit *commit = pop_commit(&exclude);
297 show_rev(REVERSED, &commit->object.oid, NULL);
300 *dotdot = '.';
301 return 1;
303 *dotdot = '.';
304 return 0;
307 static int try_parent_shorthands(const char *arg)
309 char *dotdot;
310 struct object_id oid;
311 struct commit *commit;
312 struct commit_list *parents;
313 int parent_number;
314 int include_rev = 0;
315 int include_parents = 0;
316 int exclude_parent = 0;
318 if ((dotdot = strstr(arg, "^!"))) {
319 include_rev = 1;
320 if (dotdot[2])
321 return 0;
322 } else if ((dotdot = strstr(arg, "^@"))) {
323 include_parents = 1;
324 if (dotdot[2])
325 return 0;
326 } else if ((dotdot = strstr(arg, "^-"))) {
327 include_rev = 1;
328 exclude_parent = 1;
330 if (dotdot[2]) {
331 char *end;
332 exclude_parent = strtoul(dotdot + 2, &end, 10);
333 if (*end != '\0' || !exclude_parent)
334 return 0;
336 } else
337 return 0;
339 *dotdot = 0;
340 if (get_oid_committish(arg, &oid) ||
341 !(commit = lookup_commit_reference(the_repository, &oid))) {
342 *dotdot = '^';
343 return 0;
346 if (exclude_parent &&
347 exclude_parent > commit_list_count(commit->parents)) {
348 *dotdot = '^';
349 return 0;
352 if (include_rev)
353 show_rev(NORMAL, &oid, arg);
354 for (parents = commit->parents, parent_number = 1;
355 parents;
356 parents = parents->next, parent_number++) {
357 char *name = NULL;
359 if (exclude_parent && parent_number != exclude_parent)
360 continue;
362 if (symbolic)
363 name = xstrfmt("%s^%d", arg, parent_number);
364 show_rev(include_parents ? NORMAL : REVERSED,
365 &parents->item->object.oid, name);
366 free(name);
369 *dotdot = '^';
370 return 1;
373 static int parseopt_dump(const struct option *o, const char *arg, int unset)
375 struct strbuf *parsed = o->value;
376 if (unset)
377 strbuf_addf(parsed, " --no-%s", o->long_name);
378 else if (o->short_name && (o->long_name == NULL || !stuck_long))
379 strbuf_addf(parsed, " -%c", o->short_name);
380 else
381 strbuf_addf(parsed, " --%s", o->long_name);
382 if (arg) {
383 if (!stuck_long)
384 strbuf_addch(parsed, ' ');
385 else if (o->long_name)
386 strbuf_addch(parsed, '=');
387 sq_quote_buf(parsed, arg);
389 return 0;
392 static const char *skipspaces(const char *s)
394 while (isspace(*s))
395 s++;
396 return s;
399 static char *findspace(const char *s)
401 for (; *s; s++)
402 if (isspace(*s))
403 return (char*)s;
404 return NULL;
407 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
409 static int keep_dashdash = 0, stop_at_non_option = 0;
410 static char const * const parseopt_usage[] = {
411 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
412 NULL
414 static struct option parseopt_opts[] = {
415 OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
416 N_("keep the `--` passed as an arg")),
417 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
418 N_("stop parsing after the "
419 "first non-option argument")),
420 OPT_BOOL(0, "stuck-long", &stuck_long,
421 N_("output in stuck long form")),
422 OPT_END(),
424 static const char * const flag_chars = "*=?!";
426 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
427 const char **usage = NULL;
428 struct option *opts = NULL;
429 int onb = 0, osz = 0, unb = 0, usz = 0;
431 strbuf_addstr(&parsed, "set --");
432 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
433 PARSE_OPT_KEEP_DASHDASH);
434 if (argc < 1 || strcmp(argv[0], "--"))
435 usage_with_options(parseopt_usage, parseopt_opts);
437 /* get the usage up to the first line with a -- on it */
438 for (;;) {
439 if (strbuf_getline(&sb, stdin) == EOF)
440 die(_("premature end of input"));
441 ALLOC_GROW(usage, unb + 1, usz);
442 if (!strcmp("--", sb.buf)) {
443 if (unb < 1)
444 die(_("no usage string given before the `--' separator"));
445 usage[unb] = NULL;
446 break;
448 usage[unb++] = strbuf_detach(&sb, NULL);
451 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
452 while (strbuf_getline(&sb, stdin) != EOF) {
453 const char *s;
454 char *help;
455 struct option *o;
457 if (!sb.len)
458 continue;
460 ALLOC_GROW(opts, onb + 1, osz);
461 memset(opts + onb, 0, sizeof(opts[onb]));
463 o = &opts[onb++];
464 help = findspace(sb.buf);
465 if (!help || sb.buf == help) {
466 o->type = OPTION_GROUP;
467 o->help = xstrdup(skipspaces(sb.buf));
468 continue;
471 *help = '\0';
473 o->type = OPTION_CALLBACK;
474 o->help = xstrdup(skipspaces(help+1));
475 o->value = &parsed;
476 o->flags = PARSE_OPT_NOARG;
477 o->callback = &parseopt_dump;
479 /* name(s) */
480 s = strpbrk(sb.buf, flag_chars);
481 if (!s)
482 s = help;
484 if (s == sb.buf)
485 die(_("missing opt-spec before option flags"));
487 if (s - sb.buf == 1) /* short option only */
488 o->short_name = *sb.buf;
489 else if (sb.buf[1] != ',') /* long option only */
490 o->long_name = xmemdupz(sb.buf, s - sb.buf);
491 else {
492 o->short_name = *sb.buf;
493 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
496 /* flags */
497 while (s < help) {
498 switch (*s++) {
499 case '=':
500 o->flags &= ~PARSE_OPT_NOARG;
501 continue;
502 case '?':
503 o->flags &= ~PARSE_OPT_NOARG;
504 o->flags |= PARSE_OPT_OPTARG;
505 continue;
506 case '!':
507 o->flags |= PARSE_OPT_NONEG;
508 continue;
509 case '*':
510 o->flags |= PARSE_OPT_HIDDEN;
511 continue;
513 s--;
514 break;
517 if (s < help)
518 o->argh = xmemdupz(s, help - s);
520 strbuf_release(&sb);
522 /* put an OPT_END() */
523 ALLOC_GROW(opts, onb + 1, osz);
524 memset(opts + onb, 0, sizeof(opts[onb]));
525 argc = parse_options(argc, argv, prefix, opts, usage,
526 (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
527 (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
528 PARSE_OPT_SHELL_EVAL);
530 strbuf_addstr(&parsed, " --");
531 sq_quote_argv(&parsed, argv);
532 puts(parsed.buf);
533 strbuf_release(&parsed);
534 return 0;
537 static int cmd_sq_quote(int argc, const char **argv)
539 struct strbuf buf = STRBUF_INIT;
541 if (argc)
542 sq_quote_argv(&buf, argv);
543 printf("%s\n", buf.buf);
544 strbuf_release(&buf);
546 return 0;
549 static void die_no_single_rev(int quiet)
551 if (quiet)
552 exit(1);
553 else
554 die(_("Needed a single revision"));
557 static const char builtin_rev_parse_usage[] =
558 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
559 " or: git rev-parse --sq-quote [<arg>...]\n"
560 " or: git rev-parse [<options>] [<arg>...]\n"
561 "\n"
562 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
565 * Parse "opt" or "opt=<value>", setting value respectively to either
566 * NULL or the string after "=".
568 static int opt_with_value(const char *arg, const char *opt, const char **value)
570 if (skip_prefix(arg, opt, &arg)) {
571 if (!*arg) {
572 *value = NULL;
573 return 1;
575 if (*arg++ == '=') {
576 *value = arg;
577 return 1;
580 return 0;
583 static void handle_ref_opt(const char *pattern, const char *prefix)
585 if (pattern)
586 for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
587 else
588 for_each_ref_in(prefix, show_reference, NULL);
589 clear_ref_exclusions(&ref_excludes);
592 enum format_type {
593 /* We would like a relative path. */
594 FORMAT_RELATIVE,
595 /* We would like a canonical absolute path. */
596 FORMAT_CANONICAL,
597 /* We would like the default behavior. */
598 FORMAT_DEFAULT,
601 enum default_type {
602 /* Our default is a relative path. */
603 DEFAULT_RELATIVE,
604 /* Our default is a relative path if there's a shared root. */
605 DEFAULT_RELATIVE_IF_SHARED,
606 /* Our default is a canonical absolute path. */
607 DEFAULT_CANONICAL,
608 /* Our default is not to modify the item. */
609 DEFAULT_UNMODIFIED,
612 static void print_path(const char *path, const char *prefix, enum format_type format, enum default_type def)
614 char *cwd = NULL;
616 * We don't ever produce a relative path if prefix is NULL, so set the
617 * prefix to the current directory so that we can produce a relative
618 * path whenever possible. If we're using RELATIVE_IF_SHARED mode, then
619 * we want an absolute path unless the two share a common prefix, so don't
620 * set it in that case, since doing so causes a relative path to always
621 * be produced if possible.
623 if (!prefix && (format != FORMAT_DEFAULT || def != DEFAULT_RELATIVE_IF_SHARED))
624 prefix = cwd = xgetcwd();
625 if (format == FORMAT_DEFAULT && def == DEFAULT_UNMODIFIED) {
626 puts(path);
627 } else if (format == FORMAT_RELATIVE ||
628 (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE)) {
630 * In order for relative_path to work as expected, we need to
631 * make sure that both paths are absolute paths. If we don't,
632 * we can end up with an unexpected absolute path that the user
633 * didn't want.
635 struct strbuf buf = STRBUF_INIT, realbuf = STRBUF_INIT, prefixbuf = STRBUF_INIT;
636 if (!is_absolute_path(path)) {
637 strbuf_realpath_forgiving(&realbuf, path, 1);
638 path = realbuf.buf;
640 if (!is_absolute_path(prefix)) {
641 strbuf_realpath_forgiving(&prefixbuf, prefix, 1);
642 prefix = prefixbuf.buf;
644 puts(relative_path(path, prefix, &buf));
645 strbuf_release(&buf);
646 strbuf_release(&realbuf);
647 strbuf_release(&prefixbuf);
648 } else if (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE_IF_SHARED) {
649 struct strbuf buf = STRBUF_INIT;
650 puts(relative_path(path, prefix, &buf));
651 strbuf_release(&buf);
652 } else {
653 struct strbuf buf = STRBUF_INIT;
654 strbuf_realpath_forgiving(&buf, path, 1);
655 puts(buf.buf);
656 strbuf_release(&buf);
658 free(cwd);
661 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
663 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
664 int did_repo_setup = 0;
665 int has_dashdash = 0;
666 int output_prefix = 0;
667 struct object_id oid;
668 unsigned int flags = 0;
669 const char *name = NULL;
670 struct object_context unused;
671 struct strbuf buf = STRBUF_INIT;
672 const int hexsz = the_hash_algo->hexsz;
673 int seen_end_of_options = 0;
674 enum format_type format = FORMAT_DEFAULT;
676 if (argc > 1 && !strcmp("--parseopt", argv[1]))
677 return cmd_parseopt(argc - 1, argv + 1, prefix);
679 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
680 return cmd_sq_quote(argc - 2, argv + 2);
682 if (argc > 1 && !strcmp("-h", argv[1]))
683 usage(builtin_rev_parse_usage);
685 for (i = 1; i < argc; i++) {
686 if (!strcmp(argv[i], "--")) {
687 has_dashdash = 1;
688 break;
692 /* No options; just report on whether we're in a git repo or not. */
693 if (argc == 1) {
694 setup_git_directory();
695 git_config(git_default_config, NULL);
696 return 0;
699 for (i = 1; i < argc; i++) {
700 const char *arg = argv[i];
702 if (as_is) {
703 if (show_file(arg, output_prefix) && as_is < 2)
704 verify_filename(prefix, arg, 0);
705 continue;
708 if (!seen_end_of_options) {
709 if (!strcmp(arg, "--local-env-vars")) {
710 int i;
711 for (i = 0; local_repo_env[i]; i++)
712 printf("%s\n", local_repo_env[i]);
713 continue;
715 if (!strcmp(arg, "--resolve-git-dir")) {
716 const char *gitdir = argv[++i];
717 if (!gitdir)
718 die(_("--resolve-git-dir requires an argument"));
719 gitdir = resolve_gitdir(gitdir);
720 if (!gitdir)
721 die(_("not a gitdir '%s'"), argv[i]);
722 puts(gitdir);
723 continue;
727 /* The rest of the options require a git repository. */
728 if (!did_repo_setup) {
729 prefix = setup_git_directory();
730 git_config(git_default_config, NULL);
731 did_repo_setup = 1;
733 prepare_repo_settings(the_repository);
734 the_repository->settings.command_requires_full_index = 0;
737 if (!strcmp(arg, "--")) {
738 as_is = 2;
739 /* Pass on the "--" if we show anything but files.. */
740 if (filter & (DO_FLAGS | DO_REVS))
741 show_file(arg, 0);
742 continue;
745 if (!seen_end_of_options && *arg == '-') {
746 if (!strcmp(arg, "--git-path")) {
747 if (!argv[i + 1])
748 die(_("--git-path requires an argument"));
749 strbuf_reset(&buf);
750 print_path(git_path("%s", argv[i + 1]), prefix,
751 format,
752 DEFAULT_RELATIVE_IF_SHARED);
753 i++;
754 continue;
756 if (!strcmp(arg,"-n")) {
757 if (++i >= argc)
758 die(_("-n requires an argument"));
759 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
760 show(arg);
761 show(argv[i]);
763 continue;
765 if (starts_with(arg, "-n")) {
766 if ((filter & DO_FLAGS) && (filter & DO_REVS))
767 show(arg);
768 continue;
770 if (opt_with_value(arg, "--path-format", &arg)) {
771 if (!arg)
772 die(_("--path-format requires an argument"));
773 if (!strcmp(arg, "absolute")) {
774 format = FORMAT_CANONICAL;
775 } else if (!strcmp(arg, "relative")) {
776 format = FORMAT_RELATIVE;
777 } else {
778 die(_("unknown argument to --path-format: %s"), arg);
780 continue;
782 if (!strcmp(arg, "--default")) {
783 def = argv[++i];
784 if (!def)
785 die(_("--default requires an argument"));
786 continue;
788 if (!strcmp(arg, "--prefix")) {
789 prefix = argv[++i];
790 if (!prefix)
791 die(_("--prefix requires an argument"));
792 startup_info->prefix = prefix;
793 output_prefix = 1;
794 continue;
796 if (!strcmp(arg, "--revs-only")) {
797 filter &= ~DO_NOREV;
798 continue;
800 if (!strcmp(arg, "--no-revs")) {
801 filter &= ~DO_REVS;
802 continue;
804 if (!strcmp(arg, "--flags")) {
805 filter &= ~DO_NONFLAGS;
806 continue;
808 if (!strcmp(arg, "--no-flags")) {
809 filter &= ~DO_FLAGS;
810 continue;
812 if (!strcmp(arg, "--verify")) {
813 filter &= ~(DO_FLAGS|DO_NOREV);
814 verify = 1;
815 continue;
817 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
818 quiet = 1;
819 flags |= GET_OID_QUIETLY;
820 continue;
822 if (opt_with_value(arg, "--short", &arg)) {
823 filter &= ~(DO_FLAGS|DO_NOREV);
824 verify = 1;
825 abbrev = DEFAULT_ABBREV;
826 if (!arg)
827 continue;
828 abbrev = strtoul(arg, NULL, 10);
829 if (abbrev < MINIMUM_ABBREV)
830 abbrev = MINIMUM_ABBREV;
831 else if (hexsz <= abbrev)
832 abbrev = hexsz;
833 continue;
835 if (!strcmp(arg, "--sq")) {
836 output_sq = 1;
837 continue;
839 if (!strcmp(arg, "--not")) {
840 show_type ^= REVERSED;
841 continue;
843 if (!strcmp(arg, "--symbolic")) {
844 symbolic = SHOW_SYMBOLIC_ASIS;
845 continue;
847 if (!strcmp(arg, "--symbolic-full-name")) {
848 symbolic = SHOW_SYMBOLIC_FULL;
849 continue;
851 if (opt_with_value(arg, "--abbrev-ref", &arg)) {
852 abbrev_ref = 1;
853 abbrev_ref_strict = warn_ambiguous_refs;
854 if (arg) {
855 if (!strcmp(arg, "strict"))
856 abbrev_ref_strict = 1;
857 else if (!strcmp(arg, "loose"))
858 abbrev_ref_strict = 0;
859 else
860 die(_("unknown mode for --abbrev-ref: %s"),
861 arg);
863 continue;
865 if (!strcmp(arg, "--all")) {
866 for_each_ref(show_reference, NULL);
867 clear_ref_exclusions(&ref_excludes);
868 continue;
870 if (skip_prefix(arg, "--disambiguate=", &arg)) {
871 for_each_abbrev(arg, show_abbrev, NULL);
872 continue;
874 if (!strcmp(arg, "--bisect")) {
875 for_each_fullref_in("refs/bisect/bad", show_reference, NULL);
876 for_each_fullref_in("refs/bisect/good", anti_reference, NULL);
877 continue;
879 if (opt_with_value(arg, "--branches", &arg)) {
880 if (ref_excludes.hidden_refs_configured)
881 return error(_("--exclude-hidden cannot be used together with --branches"));
882 handle_ref_opt(arg, "refs/heads/");
883 continue;
885 if (opt_with_value(arg, "--tags", &arg)) {
886 if (ref_excludes.hidden_refs_configured)
887 return error(_("--exclude-hidden cannot be used together with --tags"));
888 handle_ref_opt(arg, "refs/tags/");
889 continue;
891 if (skip_prefix(arg, "--glob=", &arg)) {
892 handle_ref_opt(arg, NULL);
893 continue;
895 if (opt_with_value(arg, "--remotes", &arg)) {
896 if (ref_excludes.hidden_refs_configured)
897 return error(_("--exclude-hidden cannot be used together with --remotes"));
898 handle_ref_opt(arg, "refs/remotes/");
899 continue;
901 if (skip_prefix(arg, "--exclude=", &arg)) {
902 add_ref_exclusion(&ref_excludes, arg);
903 continue;
905 if (skip_prefix(arg, "--exclude-hidden=", &arg)) {
906 exclude_hidden_refs(&ref_excludes, arg);
907 continue;
909 if (!strcmp(arg, "--show-toplevel")) {
910 const char *work_tree = get_git_work_tree();
911 if (work_tree)
912 print_path(work_tree, prefix, format, DEFAULT_UNMODIFIED);
913 else
914 die(_("this operation must be run in a work tree"));
915 continue;
917 if (!strcmp(arg, "--show-superproject-working-tree")) {
918 struct strbuf superproject = STRBUF_INIT;
919 if (get_superproject_working_tree(&superproject))
920 print_path(superproject.buf, prefix, format, DEFAULT_UNMODIFIED);
921 strbuf_release(&superproject);
922 continue;
924 if (!strcmp(arg, "--show-prefix")) {
925 if (prefix)
926 puts(prefix);
927 else
928 putchar('\n');
929 continue;
931 if (!strcmp(arg, "--show-cdup")) {
932 const char *pfx = prefix;
933 if (!is_inside_work_tree()) {
934 const char *work_tree =
935 get_git_work_tree();
936 if (work_tree)
937 printf("%s\n", work_tree);
938 continue;
940 while (pfx) {
941 pfx = strchr(pfx, '/');
942 if (pfx) {
943 pfx++;
944 printf("../");
947 putchar('\n');
948 continue;
950 if (!strcmp(arg, "--git-dir") ||
951 !strcmp(arg, "--absolute-git-dir")) {
952 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
953 char *cwd;
954 int len;
955 enum format_type wanted = format;
956 if (arg[2] == 'g') { /* --git-dir */
957 if (gitdir) {
958 print_path(gitdir, prefix, format, DEFAULT_UNMODIFIED);
959 continue;
961 if (!prefix) {
962 print_path(".git", prefix, format, DEFAULT_UNMODIFIED);
963 continue;
965 } else { /* --absolute-git-dir */
966 wanted = FORMAT_CANONICAL;
967 if (!gitdir && !prefix)
968 gitdir = ".git";
969 if (gitdir) {
970 struct strbuf realpath = STRBUF_INIT;
971 strbuf_realpath(&realpath, gitdir, 1);
972 puts(realpath.buf);
973 strbuf_release(&realpath);
974 continue;
977 cwd = xgetcwd();
978 len = strlen(cwd);
979 strbuf_reset(&buf);
980 strbuf_addf(&buf, "%s%s.git", cwd, len && cwd[len-1] != '/' ? "/" : "");
981 free(cwd);
982 print_path(buf.buf, prefix, wanted, DEFAULT_CANONICAL);
983 continue;
985 if (!strcmp(arg, "--git-common-dir")) {
986 print_path(get_git_common_dir(), prefix, format, DEFAULT_RELATIVE_IF_SHARED);
987 continue;
989 if (!strcmp(arg, "--is-inside-git-dir")) {
990 printf("%s\n", is_inside_git_dir() ? "true"
991 : "false");
992 continue;
994 if (!strcmp(arg, "--is-inside-work-tree")) {
995 printf("%s\n", is_inside_work_tree() ? "true"
996 : "false");
997 continue;
999 if (!strcmp(arg, "--is-bare-repository")) {
1000 printf("%s\n", is_bare_repository() ? "true"
1001 : "false");
1002 continue;
1004 if (!strcmp(arg, "--is-shallow-repository")) {
1005 printf("%s\n",
1006 is_repository_shallow(the_repository) ? "true"
1007 : "false");
1008 continue;
1010 if (!strcmp(arg, "--shared-index-path")) {
1011 if (repo_read_index(the_repository) < 0)
1012 die(_("Could not read the index"));
1013 if (the_index.split_index) {
1014 const struct object_id *oid = &the_index.split_index->base_oid;
1015 const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
1016 print_path(path, prefix, format, DEFAULT_RELATIVE);
1018 continue;
1020 if (skip_prefix(arg, "--since=", &arg)) {
1021 show_datestring("--max-age=", arg);
1022 continue;
1024 if (skip_prefix(arg, "--after=", &arg)) {
1025 show_datestring("--max-age=", arg);
1026 continue;
1028 if (skip_prefix(arg, "--before=", &arg)) {
1029 show_datestring("--min-age=", arg);
1030 continue;
1032 if (skip_prefix(arg, "--until=", &arg)) {
1033 show_datestring("--min-age=", arg);
1034 continue;
1036 if (opt_with_value(arg, "--show-object-format", &arg)) {
1037 const char *val = arg ? arg : "storage";
1039 if (strcmp(val, "storage") &&
1040 strcmp(val, "input") &&
1041 strcmp(val, "output"))
1042 die(_("unknown mode for --show-object-format: %s"),
1043 arg);
1044 puts(the_hash_algo->name);
1045 continue;
1047 if (!strcmp(arg, "--end-of-options")) {
1048 seen_end_of_options = 1;
1049 if (filter & (DO_FLAGS | DO_REVS))
1050 show_file(arg, 0);
1051 continue;
1053 if (show_flag(arg) && verify)
1054 die_no_single_rev(quiet);
1055 continue;
1058 /* Not a flag argument */
1059 if (try_difference(arg))
1060 continue;
1061 if (try_parent_shorthands(arg))
1062 continue;
1063 name = arg;
1064 type = NORMAL;
1065 if (*arg == '^') {
1066 name++;
1067 type = REVERSED;
1069 if (!get_oid_with_context(the_repository, name,
1070 flags, &oid, &unused)) {
1071 if (verify)
1072 revs_count++;
1073 else
1074 show_rev(type, &oid, name);
1075 continue;
1077 if (verify)
1078 die_no_single_rev(quiet);
1079 if (has_dashdash)
1080 die(_("bad revision '%s'"), arg);
1081 as_is = 1;
1082 if (!show_file(arg, output_prefix))
1083 continue;
1084 verify_filename(prefix, arg, 1);
1086 strbuf_release(&buf);
1087 if (verify) {
1088 if (revs_count == 1) {
1089 show_rev(type, &oid, name);
1090 return 0;
1091 } else if (revs_count == 0 && show_default())
1092 return 0;
1093 die_no_single_rev(quiet);
1094 } else
1095 show_default();
1096 return 0;