setup.h: move declarations for setup.c functions from cache.h
[git.git] / builtin / rev-parse.c
blob3a5a2ee5b2d02bfa60946db975616b89ba764923
1 /*
2 * rev-parse.c
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #define USE_THE_INDEX_VARIABLE
7 #include "cache.h"
8 #include "abspath.h"
9 #include "alloc.h"
10 #include "config.h"
11 #include "commit.h"
12 #include "environment.h"
13 #include "gettext.h"
14 #include "hex.h"
15 #include "refs.h"
16 #include "quote.h"
17 #include "builtin.h"
18 #include "parse-options.h"
19 #include "diff.h"
20 #include "revision.h"
21 #include "setup.h"
22 #include "split-index.h"
23 #include "submodule.h"
24 #include "commit-reach.h"
25 #include "shallow.h"
27 #define DO_REVS 1
28 #define DO_NOREV 2
29 #define DO_FLAGS 4
30 #define DO_NONFLAGS 8
31 static int filter = ~0;
33 static const char *def;
35 #define NORMAL 0
36 #define REVERSED 1
37 static int show_type = NORMAL;
39 #define SHOW_SYMBOLIC_ASIS 1
40 #define SHOW_SYMBOLIC_FULL 2
41 static int symbolic;
42 static int abbrev;
43 static int abbrev_ref;
44 static int abbrev_ref_strict;
45 static int output_sq;
47 static int stuck_long;
48 static struct ref_exclusions ref_excludes = REF_EXCLUSIONS_INIT;
51 * Some arguments are relevant "revision" arguments,
52 * others are about output format or other details.
53 * This sorts it all out.
55 static int is_rev_argument(const char *arg)
57 static const char *rev_args[] = {
58 "--all",
59 "--bisect",
60 "--dense",
61 "--branches=",
62 "--branches",
63 "--header",
64 "--ignore-missing",
65 "--max-age=",
66 "--max-count=",
67 "--min-age=",
68 "--no-merges",
69 "--min-parents=",
70 "--no-min-parents",
71 "--max-parents=",
72 "--no-max-parents",
73 "--objects",
74 "--objects-edge",
75 "--parents",
76 "--pretty",
77 "--remotes=",
78 "--remotes",
79 "--glob=",
80 "--sparse",
81 "--tags=",
82 "--tags",
83 "--topo-order",
84 "--date-order",
85 "--unpacked",
86 NULL
88 const char **p = rev_args;
90 /* accept -<digit>, like traditional "head" */
91 if ((*arg == '-') && isdigit(arg[1]))
92 return 1;
94 for (;;) {
95 const char *str = *p++;
96 int len;
97 if (!str)
98 return 0;
99 len = strlen(str);
100 if (!strcmp(arg, str) ||
101 (str[len-1] == '=' && !strncmp(arg, str, len)))
102 return 1;
106 /* Output argument as a string, either SQ or normal */
107 static void show(const char *arg)
109 if (output_sq) {
110 int sq = '\'', ch;
112 putchar(sq);
113 while ((ch = *arg++)) {
114 if (ch == sq)
115 fputs("'\\'", stdout);
116 putchar(ch);
118 putchar(sq);
119 putchar(' ');
121 else
122 puts(arg);
125 /* Like show(), but with a negation prefix according to type */
126 static void show_with_type(int type, const char *arg)
128 if (type != show_type)
129 putchar('^');
130 show(arg);
133 /* Output a revision, only if filter allows it */
134 static void show_rev(int type, const struct object_id *oid, const char *name)
136 if (!(filter & DO_REVS))
137 return;
138 def = NULL;
140 if ((symbolic || abbrev_ref) && name) {
141 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
142 struct object_id discard;
143 char *full;
145 switch (dwim_ref(name, strlen(name), &discard, &full, 0)) {
146 case 0:
148 * Not found -- not a ref. We could
149 * emit "name" here, but symbolic-full
150 * users are interested in finding the
151 * refs spelled in full, and they would
152 * need to filter non-refs if we did so.
154 break;
155 case 1: /* happy */
156 if (abbrev_ref)
157 full = shorten_unambiguous_ref(full,
158 abbrev_ref_strict);
159 show_with_type(type, full);
160 break;
161 default: /* ambiguous */
162 error("refname '%s' is ambiguous", name);
163 break;
165 free(full);
166 } else {
167 show_with_type(type, name);
170 else if (abbrev)
171 show_with_type(type, find_unique_abbrev(oid, abbrev));
172 else
173 show_with_type(type, oid_to_hex(oid));
176 /* Output a flag, only if filter allows it. */
177 static int show_flag(const char *arg)
179 if (!(filter & DO_FLAGS))
180 return 0;
181 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
182 show(arg);
183 return 1;
185 return 0;
188 static int show_default(void)
190 const char *s = def;
192 if (s) {
193 struct object_id oid;
195 def = NULL;
196 if (!get_oid(s, &oid)) {
197 show_rev(NORMAL, &oid, s);
198 return 1;
201 return 0;
204 static int show_reference(const char *refname, const struct object_id *oid,
205 int flag UNUSED, void *cb_data UNUSED)
207 if (ref_excluded(&ref_excludes, refname))
208 return 0;
209 show_rev(NORMAL, oid, refname);
210 return 0;
213 static int anti_reference(const char *refname, const struct object_id *oid,
214 int flag UNUSED, void *cb_data UNUSED)
216 show_rev(REVERSED, oid, refname);
217 return 0;
220 static int show_abbrev(const struct object_id *oid, void *cb_data)
222 show_rev(NORMAL, oid, NULL);
223 return 0;
226 static void show_datestring(const char *flag, const char *datestr)
228 char *buffer;
230 /* date handling requires both flags and revs */
231 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
232 return;
233 buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr));
234 show(buffer);
235 free(buffer);
238 static int show_file(const char *arg, int output_prefix)
240 show_default();
241 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
242 if (output_prefix) {
243 const char *prefix = startup_info->prefix;
244 char *fname = prefix_filename(prefix, arg);
245 show(fname);
246 free(fname);
247 } else
248 show(arg);
249 return 1;
251 return 0;
254 static int try_difference(const char *arg)
256 char *dotdot;
257 struct object_id start_oid;
258 struct object_id end_oid;
259 const char *end;
260 const char *start;
261 int symmetric;
262 static const char head_by_default[] = "HEAD";
264 if (!(dotdot = strstr(arg, "..")))
265 return 0;
266 end = dotdot + 2;
267 start = arg;
268 symmetric = (*end == '.');
270 *dotdot = 0;
271 end += symmetric;
273 if (!*end)
274 end = head_by_default;
275 if (dotdot == arg)
276 start = head_by_default;
278 if (start == head_by_default && end == head_by_default &&
279 !symmetric) {
281 * Just ".."? That is not a range but the
282 * pathspec for the parent directory.
284 *dotdot = '.';
285 return 0;
288 if (!get_oid_committish(start, &start_oid) && !get_oid_committish(end, &end_oid)) {
289 show_rev(NORMAL, &end_oid, end);
290 show_rev(symmetric ? NORMAL : REVERSED, &start_oid, start);
291 if (symmetric) {
292 struct commit_list *exclude;
293 struct commit *a, *b;
294 a = lookup_commit_reference(the_repository, &start_oid);
295 b = lookup_commit_reference(the_repository, &end_oid);
296 if (!a || !b) {
297 *dotdot = '.';
298 return 0;
300 exclude = get_merge_bases(a, b);
301 while (exclude) {
302 struct commit *commit = pop_commit(&exclude);
303 show_rev(REVERSED, &commit->object.oid, NULL);
306 *dotdot = '.';
307 return 1;
309 *dotdot = '.';
310 return 0;
313 static int try_parent_shorthands(const char *arg)
315 char *dotdot;
316 struct object_id oid;
317 struct commit *commit;
318 struct commit_list *parents;
319 int parent_number;
320 int include_rev = 0;
321 int include_parents = 0;
322 int exclude_parent = 0;
324 if ((dotdot = strstr(arg, "^!"))) {
325 include_rev = 1;
326 if (dotdot[2])
327 return 0;
328 } else if ((dotdot = strstr(arg, "^@"))) {
329 include_parents = 1;
330 if (dotdot[2])
331 return 0;
332 } else if ((dotdot = strstr(arg, "^-"))) {
333 include_rev = 1;
334 exclude_parent = 1;
336 if (dotdot[2]) {
337 char *end;
338 exclude_parent = strtoul(dotdot + 2, &end, 10);
339 if (*end != '\0' || !exclude_parent)
340 return 0;
342 } else
343 return 0;
345 *dotdot = 0;
346 if (get_oid_committish(arg, &oid) ||
347 !(commit = lookup_commit_reference(the_repository, &oid))) {
348 *dotdot = '^';
349 return 0;
352 if (exclude_parent &&
353 exclude_parent > commit_list_count(commit->parents)) {
354 *dotdot = '^';
355 return 0;
358 if (include_rev)
359 show_rev(NORMAL, &oid, arg);
360 for (parents = commit->parents, parent_number = 1;
361 parents;
362 parents = parents->next, parent_number++) {
363 char *name = NULL;
365 if (exclude_parent && parent_number != exclude_parent)
366 continue;
368 if (symbolic)
369 name = xstrfmt("%s^%d", arg, parent_number);
370 show_rev(include_parents ? NORMAL : REVERSED,
371 &parents->item->object.oid, name);
372 free(name);
375 *dotdot = '^';
376 return 1;
379 static int parseopt_dump(const struct option *o, const char *arg, int unset)
381 struct strbuf *parsed = o->value;
382 if (unset)
383 strbuf_addf(parsed, " --no-%s", o->long_name);
384 else if (o->short_name && (o->long_name == NULL || !stuck_long))
385 strbuf_addf(parsed, " -%c", o->short_name);
386 else
387 strbuf_addf(parsed, " --%s", o->long_name);
388 if (arg) {
389 if (!stuck_long)
390 strbuf_addch(parsed, ' ');
391 else if (o->long_name)
392 strbuf_addch(parsed, '=');
393 sq_quote_buf(parsed, arg);
395 return 0;
398 static const char *skipspaces(const char *s)
400 while (isspace(*s))
401 s++;
402 return s;
405 static char *findspace(const char *s)
407 for (; *s; s++)
408 if (isspace(*s))
409 return (char*)s;
410 return NULL;
413 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
415 static int keep_dashdash = 0, stop_at_non_option = 0;
416 static char const * const parseopt_usage[] = {
417 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
418 NULL
420 static struct option parseopt_opts[] = {
421 OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
422 N_("keep the `--` passed as an arg")),
423 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
424 N_("stop parsing after the "
425 "first non-option argument")),
426 OPT_BOOL(0, "stuck-long", &stuck_long,
427 N_("output in stuck long form")),
428 OPT_END(),
430 static const char * const flag_chars = "*=?!";
432 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
433 const char **usage = NULL;
434 struct option *opts = NULL;
435 int onb = 0, osz = 0, unb = 0, usz = 0;
437 strbuf_addstr(&parsed, "set --");
438 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
439 PARSE_OPT_KEEP_DASHDASH);
440 if (argc < 1 || strcmp(argv[0], "--"))
441 usage_with_options(parseopt_usage, parseopt_opts);
443 /* get the usage up to the first line with a -- on it */
444 for (;;) {
445 if (strbuf_getline(&sb, stdin) == EOF)
446 die(_("premature end of input"));
447 ALLOC_GROW(usage, unb + 1, usz);
448 if (!strcmp("--", sb.buf)) {
449 if (unb < 1)
450 die(_("no usage string given before the `--' separator"));
451 usage[unb] = NULL;
452 break;
454 usage[unb++] = strbuf_detach(&sb, NULL);
457 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
458 while (strbuf_getline(&sb, stdin) != EOF) {
459 const char *s;
460 char *help;
461 struct option *o;
463 if (!sb.len)
464 continue;
466 ALLOC_GROW(opts, onb + 1, osz);
467 memset(opts + onb, 0, sizeof(opts[onb]));
469 o = &opts[onb++];
470 help = findspace(sb.buf);
471 if (!help || sb.buf == help) {
472 o->type = OPTION_GROUP;
473 o->help = xstrdup(skipspaces(sb.buf));
474 continue;
477 *help = '\0';
479 o->type = OPTION_CALLBACK;
480 o->help = xstrdup(skipspaces(help+1));
481 o->value = &parsed;
482 o->flags = PARSE_OPT_NOARG;
483 o->callback = &parseopt_dump;
485 /* name(s) */
486 s = strpbrk(sb.buf, flag_chars);
487 if (!s)
488 s = help;
490 if (s == sb.buf)
491 die(_("missing opt-spec before option flags"));
493 if (s - sb.buf == 1) /* short option only */
494 o->short_name = *sb.buf;
495 else if (sb.buf[1] != ',') /* long option only */
496 o->long_name = xmemdupz(sb.buf, s - sb.buf);
497 else {
498 o->short_name = *sb.buf;
499 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
502 /* flags */
503 while (s < help) {
504 switch (*s++) {
505 case '=':
506 o->flags &= ~PARSE_OPT_NOARG;
507 continue;
508 case '?':
509 o->flags &= ~PARSE_OPT_NOARG;
510 o->flags |= PARSE_OPT_OPTARG;
511 continue;
512 case '!':
513 o->flags |= PARSE_OPT_NONEG;
514 continue;
515 case '*':
516 o->flags |= PARSE_OPT_HIDDEN;
517 continue;
519 s--;
520 break;
523 if (s < help)
524 o->argh = xmemdupz(s, help - s);
526 strbuf_release(&sb);
528 /* put an OPT_END() */
529 ALLOC_GROW(opts, onb + 1, osz);
530 memset(opts + onb, 0, sizeof(opts[onb]));
531 argc = parse_options(argc, argv, prefix, opts, usage,
532 (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
533 (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
534 PARSE_OPT_SHELL_EVAL);
536 strbuf_addstr(&parsed, " --");
537 sq_quote_argv(&parsed, argv);
538 puts(parsed.buf);
539 strbuf_release(&parsed);
540 return 0;
543 static int cmd_sq_quote(int argc, const char **argv)
545 struct strbuf buf = STRBUF_INIT;
547 if (argc)
548 sq_quote_argv(&buf, argv);
549 printf("%s\n", buf.buf);
550 strbuf_release(&buf);
552 return 0;
555 static void die_no_single_rev(int quiet)
557 if (quiet)
558 exit(1);
559 else
560 die(_("Needed a single revision"));
563 static const char builtin_rev_parse_usage[] =
564 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
565 " or: git rev-parse --sq-quote [<arg>...]\n"
566 " or: git rev-parse [<options>] [<arg>...]\n"
567 "\n"
568 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
571 * Parse "opt" or "opt=<value>", setting value respectively to either
572 * NULL or the string after "=".
574 static int opt_with_value(const char *arg, const char *opt, const char **value)
576 if (skip_prefix(arg, opt, &arg)) {
577 if (!*arg) {
578 *value = NULL;
579 return 1;
581 if (*arg++ == '=') {
582 *value = arg;
583 return 1;
586 return 0;
589 static void handle_ref_opt(const char *pattern, const char *prefix)
591 if (pattern)
592 for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
593 else
594 for_each_ref_in(prefix, show_reference, NULL);
595 clear_ref_exclusions(&ref_excludes);
598 enum format_type {
599 /* We would like a relative path. */
600 FORMAT_RELATIVE,
601 /* We would like a canonical absolute path. */
602 FORMAT_CANONICAL,
603 /* We would like the default behavior. */
604 FORMAT_DEFAULT,
607 enum default_type {
608 /* Our default is a relative path. */
609 DEFAULT_RELATIVE,
610 /* Our default is a relative path if there's a shared root. */
611 DEFAULT_RELATIVE_IF_SHARED,
612 /* Our default is a canonical absolute path. */
613 DEFAULT_CANONICAL,
614 /* Our default is not to modify the item. */
615 DEFAULT_UNMODIFIED,
618 static void print_path(const char *path, const char *prefix, enum format_type format, enum default_type def)
620 char *cwd = NULL;
622 * We don't ever produce a relative path if prefix is NULL, so set the
623 * prefix to the current directory so that we can produce a relative
624 * path whenever possible. If we're using RELATIVE_IF_SHARED mode, then
625 * we want an absolute path unless the two share a common prefix, so don't
626 * set it in that case, since doing so causes a relative path to always
627 * be produced if possible.
629 if (!prefix && (format != FORMAT_DEFAULT || def != DEFAULT_RELATIVE_IF_SHARED))
630 prefix = cwd = xgetcwd();
631 if (format == FORMAT_DEFAULT && def == DEFAULT_UNMODIFIED) {
632 puts(path);
633 } else if (format == FORMAT_RELATIVE ||
634 (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE)) {
636 * In order for relative_path to work as expected, we need to
637 * make sure that both paths are absolute paths. If we don't,
638 * we can end up with an unexpected absolute path that the user
639 * didn't want.
641 struct strbuf buf = STRBUF_INIT, realbuf = STRBUF_INIT, prefixbuf = STRBUF_INIT;
642 if (!is_absolute_path(path)) {
643 strbuf_realpath_forgiving(&realbuf, path, 1);
644 path = realbuf.buf;
646 if (!is_absolute_path(prefix)) {
647 strbuf_realpath_forgiving(&prefixbuf, prefix, 1);
648 prefix = prefixbuf.buf;
650 puts(relative_path(path, prefix, &buf));
651 strbuf_release(&buf);
652 strbuf_release(&realbuf);
653 strbuf_release(&prefixbuf);
654 } else if (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE_IF_SHARED) {
655 struct strbuf buf = STRBUF_INIT;
656 puts(relative_path(path, prefix, &buf));
657 strbuf_release(&buf);
658 } else {
659 struct strbuf buf = STRBUF_INIT;
660 strbuf_realpath_forgiving(&buf, path, 1);
661 puts(buf.buf);
662 strbuf_release(&buf);
664 free(cwd);
667 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
669 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
670 int did_repo_setup = 0;
671 int has_dashdash = 0;
672 int output_prefix = 0;
673 struct object_id oid;
674 unsigned int flags = 0;
675 const char *name = NULL;
676 struct object_context unused;
677 struct strbuf buf = STRBUF_INIT;
678 const int hexsz = the_hash_algo->hexsz;
679 int seen_end_of_options = 0;
680 enum format_type format = FORMAT_DEFAULT;
682 if (argc > 1 && !strcmp("--parseopt", argv[1]))
683 return cmd_parseopt(argc - 1, argv + 1, prefix);
685 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
686 return cmd_sq_quote(argc - 2, argv + 2);
688 if (argc > 1 && !strcmp("-h", argv[1]))
689 usage(builtin_rev_parse_usage);
691 for (i = 1; i < argc; i++) {
692 if (!strcmp(argv[i], "--")) {
693 has_dashdash = 1;
694 break;
698 /* No options; just report on whether we're in a git repo or not. */
699 if (argc == 1) {
700 setup_git_directory();
701 git_config(git_default_config, NULL);
702 return 0;
705 for (i = 1; i < argc; i++) {
706 const char *arg = argv[i];
708 if (as_is) {
709 if (show_file(arg, output_prefix) && as_is < 2)
710 verify_filename(prefix, arg, 0);
711 continue;
714 if (!seen_end_of_options) {
715 if (!strcmp(arg, "--local-env-vars")) {
716 int i;
717 for (i = 0; local_repo_env[i]; i++)
718 printf("%s\n", local_repo_env[i]);
719 continue;
721 if (!strcmp(arg, "--resolve-git-dir")) {
722 const char *gitdir = argv[++i];
723 if (!gitdir)
724 die(_("--resolve-git-dir requires an argument"));
725 gitdir = resolve_gitdir(gitdir);
726 if (!gitdir)
727 die(_("not a gitdir '%s'"), argv[i]);
728 puts(gitdir);
729 continue;
733 /* The rest of the options require a git repository. */
734 if (!did_repo_setup) {
735 prefix = setup_git_directory();
736 git_config(git_default_config, NULL);
737 did_repo_setup = 1;
739 prepare_repo_settings(the_repository);
740 the_repository->settings.command_requires_full_index = 0;
743 if (!strcmp(arg, "--")) {
744 as_is = 2;
745 /* Pass on the "--" if we show anything but files.. */
746 if (filter & (DO_FLAGS | DO_REVS))
747 show_file(arg, 0);
748 continue;
751 if (!seen_end_of_options && *arg == '-') {
752 if (!strcmp(arg, "--git-path")) {
753 if (!argv[i + 1])
754 die(_("--git-path requires an argument"));
755 strbuf_reset(&buf);
756 print_path(git_path("%s", argv[i + 1]), prefix,
757 format,
758 DEFAULT_RELATIVE_IF_SHARED);
759 i++;
760 continue;
762 if (!strcmp(arg,"-n")) {
763 if (++i >= argc)
764 die(_("-n requires an argument"));
765 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
766 show(arg);
767 show(argv[i]);
769 continue;
771 if (starts_with(arg, "-n")) {
772 if ((filter & DO_FLAGS) && (filter & DO_REVS))
773 show(arg);
774 continue;
776 if (opt_with_value(arg, "--path-format", &arg)) {
777 if (!arg)
778 die(_("--path-format requires an argument"));
779 if (!strcmp(arg, "absolute")) {
780 format = FORMAT_CANONICAL;
781 } else if (!strcmp(arg, "relative")) {
782 format = FORMAT_RELATIVE;
783 } else {
784 die(_("unknown argument to --path-format: %s"), arg);
786 continue;
788 if (!strcmp(arg, "--default")) {
789 def = argv[++i];
790 if (!def)
791 die(_("--default requires an argument"));
792 continue;
794 if (!strcmp(arg, "--prefix")) {
795 prefix = argv[++i];
796 if (!prefix)
797 die(_("--prefix requires an argument"));
798 startup_info->prefix = prefix;
799 output_prefix = 1;
800 continue;
802 if (!strcmp(arg, "--revs-only")) {
803 filter &= ~DO_NOREV;
804 continue;
806 if (!strcmp(arg, "--no-revs")) {
807 filter &= ~DO_REVS;
808 continue;
810 if (!strcmp(arg, "--flags")) {
811 filter &= ~DO_NONFLAGS;
812 continue;
814 if (!strcmp(arg, "--no-flags")) {
815 filter &= ~DO_FLAGS;
816 continue;
818 if (!strcmp(arg, "--verify")) {
819 filter &= ~(DO_FLAGS|DO_NOREV);
820 verify = 1;
821 continue;
823 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
824 quiet = 1;
825 flags |= GET_OID_QUIETLY;
826 continue;
828 if (opt_with_value(arg, "--short", &arg)) {
829 filter &= ~(DO_FLAGS|DO_NOREV);
830 verify = 1;
831 abbrev = DEFAULT_ABBREV;
832 if (!arg)
833 continue;
834 abbrev = strtoul(arg, NULL, 10);
835 if (abbrev < MINIMUM_ABBREV)
836 abbrev = MINIMUM_ABBREV;
837 else if (hexsz <= abbrev)
838 abbrev = hexsz;
839 continue;
841 if (!strcmp(arg, "--sq")) {
842 output_sq = 1;
843 continue;
845 if (!strcmp(arg, "--not")) {
846 show_type ^= REVERSED;
847 continue;
849 if (!strcmp(arg, "--symbolic")) {
850 symbolic = SHOW_SYMBOLIC_ASIS;
851 continue;
853 if (!strcmp(arg, "--symbolic-full-name")) {
854 symbolic = SHOW_SYMBOLIC_FULL;
855 continue;
857 if (opt_with_value(arg, "--abbrev-ref", &arg)) {
858 abbrev_ref = 1;
859 abbrev_ref_strict = warn_ambiguous_refs;
860 if (arg) {
861 if (!strcmp(arg, "strict"))
862 abbrev_ref_strict = 1;
863 else if (!strcmp(arg, "loose"))
864 abbrev_ref_strict = 0;
865 else
866 die(_("unknown mode for --abbrev-ref: %s"),
867 arg);
869 continue;
871 if (!strcmp(arg, "--all")) {
872 for_each_ref(show_reference, NULL);
873 clear_ref_exclusions(&ref_excludes);
874 continue;
876 if (skip_prefix(arg, "--disambiguate=", &arg)) {
877 for_each_abbrev(arg, show_abbrev, NULL);
878 continue;
880 if (!strcmp(arg, "--bisect")) {
881 for_each_fullref_in("refs/bisect/bad", show_reference, NULL);
882 for_each_fullref_in("refs/bisect/good", anti_reference, NULL);
883 continue;
885 if (opt_with_value(arg, "--branches", &arg)) {
886 if (ref_excludes.hidden_refs_configured)
887 return error(_("--exclude-hidden cannot be used together with --branches"));
888 handle_ref_opt(arg, "refs/heads/");
889 continue;
891 if (opt_with_value(arg, "--tags", &arg)) {
892 if (ref_excludes.hidden_refs_configured)
893 return error(_("--exclude-hidden cannot be used together with --tags"));
894 handle_ref_opt(arg, "refs/tags/");
895 continue;
897 if (skip_prefix(arg, "--glob=", &arg)) {
898 handle_ref_opt(arg, NULL);
899 continue;
901 if (opt_with_value(arg, "--remotes", &arg)) {
902 if (ref_excludes.hidden_refs_configured)
903 return error(_("--exclude-hidden cannot be used together with --remotes"));
904 handle_ref_opt(arg, "refs/remotes/");
905 continue;
907 if (skip_prefix(arg, "--exclude=", &arg)) {
908 add_ref_exclusion(&ref_excludes, arg);
909 continue;
911 if (skip_prefix(arg, "--exclude-hidden=", &arg)) {
912 exclude_hidden_refs(&ref_excludes, arg);
913 continue;
915 if (!strcmp(arg, "--show-toplevel")) {
916 const char *work_tree = get_git_work_tree();
917 if (work_tree)
918 print_path(work_tree, prefix, format, DEFAULT_UNMODIFIED);
919 else
920 die(_("this operation must be run in a work tree"));
921 continue;
923 if (!strcmp(arg, "--show-superproject-working-tree")) {
924 struct strbuf superproject = STRBUF_INIT;
925 if (get_superproject_working_tree(&superproject))
926 print_path(superproject.buf, prefix, format, DEFAULT_UNMODIFIED);
927 strbuf_release(&superproject);
928 continue;
930 if (!strcmp(arg, "--show-prefix")) {
931 if (prefix)
932 puts(prefix);
933 else
934 putchar('\n');
935 continue;
937 if (!strcmp(arg, "--show-cdup")) {
938 const char *pfx = prefix;
939 if (!is_inside_work_tree()) {
940 const char *work_tree =
941 get_git_work_tree();
942 if (work_tree)
943 printf("%s\n", work_tree);
944 continue;
946 while (pfx) {
947 pfx = strchr(pfx, '/');
948 if (pfx) {
949 pfx++;
950 printf("../");
953 putchar('\n');
954 continue;
956 if (!strcmp(arg, "--git-dir") ||
957 !strcmp(arg, "--absolute-git-dir")) {
958 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
959 char *cwd;
960 int len;
961 enum format_type wanted = format;
962 if (arg[2] == 'g') { /* --git-dir */
963 if (gitdir) {
964 print_path(gitdir, prefix, format, DEFAULT_UNMODIFIED);
965 continue;
967 if (!prefix) {
968 print_path(".git", prefix, format, DEFAULT_UNMODIFIED);
969 continue;
971 } else { /* --absolute-git-dir */
972 wanted = FORMAT_CANONICAL;
973 if (!gitdir && !prefix)
974 gitdir = ".git";
975 if (gitdir) {
976 struct strbuf realpath = STRBUF_INIT;
977 strbuf_realpath(&realpath, gitdir, 1);
978 puts(realpath.buf);
979 strbuf_release(&realpath);
980 continue;
983 cwd = xgetcwd();
984 len = strlen(cwd);
985 strbuf_reset(&buf);
986 strbuf_addf(&buf, "%s%s.git", cwd, len && cwd[len-1] != '/' ? "/" : "");
987 free(cwd);
988 print_path(buf.buf, prefix, wanted, DEFAULT_CANONICAL);
989 continue;
991 if (!strcmp(arg, "--git-common-dir")) {
992 print_path(get_git_common_dir(), prefix, format, DEFAULT_RELATIVE_IF_SHARED);
993 continue;
995 if (!strcmp(arg, "--is-inside-git-dir")) {
996 printf("%s\n", is_inside_git_dir() ? "true"
997 : "false");
998 continue;
1000 if (!strcmp(arg, "--is-inside-work-tree")) {
1001 printf("%s\n", is_inside_work_tree() ? "true"
1002 : "false");
1003 continue;
1005 if (!strcmp(arg, "--is-bare-repository")) {
1006 printf("%s\n", is_bare_repository() ? "true"
1007 : "false");
1008 continue;
1010 if (!strcmp(arg, "--is-shallow-repository")) {
1011 printf("%s\n",
1012 is_repository_shallow(the_repository) ? "true"
1013 : "false");
1014 continue;
1016 if (!strcmp(arg, "--shared-index-path")) {
1017 if (repo_read_index(the_repository) < 0)
1018 die(_("Could not read the index"));
1019 if (the_index.split_index) {
1020 const struct object_id *oid = &the_index.split_index->base_oid;
1021 const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
1022 print_path(path, prefix, format, DEFAULT_RELATIVE);
1024 continue;
1026 if (skip_prefix(arg, "--since=", &arg)) {
1027 show_datestring("--max-age=", arg);
1028 continue;
1030 if (skip_prefix(arg, "--after=", &arg)) {
1031 show_datestring("--max-age=", arg);
1032 continue;
1034 if (skip_prefix(arg, "--before=", &arg)) {
1035 show_datestring("--min-age=", arg);
1036 continue;
1038 if (skip_prefix(arg, "--until=", &arg)) {
1039 show_datestring("--min-age=", arg);
1040 continue;
1042 if (opt_with_value(arg, "--show-object-format", &arg)) {
1043 const char *val = arg ? arg : "storage";
1045 if (strcmp(val, "storage") &&
1046 strcmp(val, "input") &&
1047 strcmp(val, "output"))
1048 die(_("unknown mode for --show-object-format: %s"),
1049 arg);
1050 puts(the_hash_algo->name);
1051 continue;
1053 if (!strcmp(arg, "--end-of-options")) {
1054 seen_end_of_options = 1;
1055 if (filter & (DO_FLAGS | DO_REVS))
1056 show_file(arg, 0);
1057 continue;
1059 if (show_flag(arg) && verify)
1060 die_no_single_rev(quiet);
1061 continue;
1064 /* Not a flag argument */
1065 if (try_difference(arg))
1066 continue;
1067 if (try_parent_shorthands(arg))
1068 continue;
1069 name = arg;
1070 type = NORMAL;
1071 if (*arg == '^') {
1072 name++;
1073 type = REVERSED;
1075 if (!get_oid_with_context(the_repository, name,
1076 flags, &oid, &unused)) {
1077 if (verify)
1078 revs_count++;
1079 else
1080 show_rev(type, &oid, name);
1081 continue;
1083 if (verify)
1084 die_no_single_rev(quiet);
1085 if (has_dashdash)
1086 die(_("bad revision '%s'"), arg);
1087 as_is = 1;
1088 if (!show_file(arg, output_prefix))
1089 continue;
1090 verify_filename(prefix, arg, 1);
1092 strbuf_release(&buf);
1093 if (verify) {
1094 if (revs_count == 1) {
1095 show_rev(type, &oid, name);
1096 return 0;
1097 } else if (revs_count == 0 && show_default())
1098 return 0;
1099 die_no_single_rev(quiet);
1100 } else
1101 show_default();
1102 return 0;