read-cache*.h: move declarations for read-cache.c functions from cache.h
[alt-git.git] / builtin / rev-parse.c
blobad93e5fad88c44a400601dc134ba34c0b5fb56d1
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 "object-name.h"
19 #include "parse-options.h"
20 #include "diff.h"
21 #include "read-cache-ll.h"
22 #include "revision.h"
23 #include "setup.h"
24 #include "split-index.h"
25 #include "submodule.h"
26 #include "commit-reach.h"
27 #include "shallow.h"
29 #define DO_REVS 1
30 #define DO_NOREV 2
31 #define DO_FLAGS 4
32 #define DO_NONFLAGS 8
33 static int filter = ~0;
35 static const char *def;
37 #define NORMAL 0
38 #define REVERSED 1
39 static int show_type = NORMAL;
41 #define SHOW_SYMBOLIC_ASIS 1
42 #define SHOW_SYMBOLIC_FULL 2
43 static int symbolic;
44 static int abbrev;
45 static int abbrev_ref;
46 static int abbrev_ref_strict;
47 static int output_sq;
49 static int stuck_long;
50 static struct ref_exclusions ref_excludes = REF_EXCLUSIONS_INIT;
53 * Some arguments are relevant "revision" arguments,
54 * others are about output format or other details.
55 * This sorts it all out.
57 static int is_rev_argument(const char *arg)
59 static const char *rev_args[] = {
60 "--all",
61 "--bisect",
62 "--dense",
63 "--branches=",
64 "--branches",
65 "--header",
66 "--ignore-missing",
67 "--max-age=",
68 "--max-count=",
69 "--min-age=",
70 "--no-merges",
71 "--min-parents=",
72 "--no-min-parents",
73 "--max-parents=",
74 "--no-max-parents",
75 "--objects",
76 "--objects-edge",
77 "--parents",
78 "--pretty",
79 "--remotes=",
80 "--remotes",
81 "--glob=",
82 "--sparse",
83 "--tags=",
84 "--tags",
85 "--topo-order",
86 "--date-order",
87 "--unpacked",
88 NULL
90 const char **p = rev_args;
92 /* accept -<digit>, like traditional "head" */
93 if ((*arg == '-') && isdigit(arg[1]))
94 return 1;
96 for (;;) {
97 const char *str = *p++;
98 int len;
99 if (!str)
100 return 0;
101 len = strlen(str);
102 if (!strcmp(arg, str) ||
103 (str[len-1] == '=' && !strncmp(arg, str, len)))
104 return 1;
108 /* Output argument as a string, either SQ or normal */
109 static void show(const char *arg)
111 if (output_sq) {
112 int sq = '\'', ch;
114 putchar(sq);
115 while ((ch = *arg++)) {
116 if (ch == sq)
117 fputs("'\\'", stdout);
118 putchar(ch);
120 putchar(sq);
121 putchar(' ');
123 else
124 puts(arg);
127 /* Like show(), but with a negation prefix according to type */
128 static void show_with_type(int type, const char *arg)
130 if (type != show_type)
131 putchar('^');
132 show(arg);
135 /* Output a revision, only if filter allows it */
136 static void show_rev(int type, const struct object_id *oid, const char *name)
138 if (!(filter & DO_REVS))
139 return;
140 def = NULL;
142 if ((symbolic || abbrev_ref) && name) {
143 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
144 struct object_id discard;
145 char *full;
147 switch (repo_dwim_ref(the_repository, name,
148 strlen(name), &discard, &full,
149 0)) {
150 case 0:
152 * Not found -- not a ref. We could
153 * emit "name" here, but symbolic-full
154 * users are interested in finding the
155 * refs spelled in full, and they would
156 * need to filter non-refs if we did so.
158 break;
159 case 1: /* happy */
160 if (abbrev_ref)
161 full = shorten_unambiguous_ref(full,
162 abbrev_ref_strict);
163 show_with_type(type, full);
164 break;
165 default: /* ambiguous */
166 error("refname '%s' is ambiguous", name);
167 break;
169 free(full);
170 } else {
171 show_with_type(type, name);
174 else if (abbrev)
175 show_with_type(type,
176 repo_find_unique_abbrev(the_repository, oid, abbrev));
177 else
178 show_with_type(type, oid_to_hex(oid));
181 /* Output a flag, only if filter allows it. */
182 static int show_flag(const char *arg)
184 if (!(filter & DO_FLAGS))
185 return 0;
186 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
187 show(arg);
188 return 1;
190 return 0;
193 static int show_default(void)
195 const char *s = def;
197 if (s) {
198 struct object_id oid;
200 def = NULL;
201 if (!repo_get_oid(the_repository, s, &oid)) {
202 show_rev(NORMAL, &oid, s);
203 return 1;
206 return 0;
209 static int show_reference(const char *refname, const struct object_id *oid,
210 int flag UNUSED, void *cb_data UNUSED)
212 if (ref_excluded(&ref_excludes, refname))
213 return 0;
214 show_rev(NORMAL, oid, refname);
215 return 0;
218 static int anti_reference(const char *refname, const struct object_id *oid,
219 int flag UNUSED, void *cb_data UNUSED)
221 show_rev(REVERSED, oid, refname);
222 return 0;
225 static int show_abbrev(const struct object_id *oid, void *cb_data)
227 show_rev(NORMAL, oid, NULL);
228 return 0;
231 static void show_datestring(const char *flag, const char *datestr)
233 char *buffer;
235 /* date handling requires both flags and revs */
236 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
237 return;
238 buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr));
239 show(buffer);
240 free(buffer);
243 static int show_file(const char *arg, int output_prefix)
245 show_default();
246 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
247 if (output_prefix) {
248 const char *prefix = startup_info->prefix;
249 char *fname = prefix_filename(prefix, arg);
250 show(fname);
251 free(fname);
252 } else
253 show(arg);
254 return 1;
256 return 0;
259 static int try_difference(const char *arg)
261 char *dotdot;
262 struct object_id start_oid;
263 struct object_id end_oid;
264 const char *end;
265 const char *start;
266 int symmetric;
267 static const char head_by_default[] = "HEAD";
269 if (!(dotdot = strstr(arg, "..")))
270 return 0;
271 end = dotdot + 2;
272 start = arg;
273 symmetric = (*end == '.');
275 *dotdot = 0;
276 end += symmetric;
278 if (!*end)
279 end = head_by_default;
280 if (dotdot == arg)
281 start = head_by_default;
283 if (start == head_by_default && end == head_by_default &&
284 !symmetric) {
286 * Just ".."? That is not a range but the
287 * pathspec for the parent directory.
289 *dotdot = '.';
290 return 0;
293 if (!repo_get_oid_committish(the_repository, start, &start_oid) && !repo_get_oid_committish(the_repository, end, &end_oid)) {
294 show_rev(NORMAL, &end_oid, end);
295 show_rev(symmetric ? NORMAL : REVERSED, &start_oid, start);
296 if (symmetric) {
297 struct commit_list *exclude;
298 struct commit *a, *b;
299 a = lookup_commit_reference(the_repository, &start_oid);
300 b = lookup_commit_reference(the_repository, &end_oid);
301 if (!a || !b) {
302 *dotdot = '.';
303 return 0;
305 exclude = repo_get_merge_bases(the_repository, a, b);
306 while (exclude) {
307 struct commit *commit = pop_commit(&exclude);
308 show_rev(REVERSED, &commit->object.oid, NULL);
311 *dotdot = '.';
312 return 1;
314 *dotdot = '.';
315 return 0;
318 static int try_parent_shorthands(const char *arg)
320 char *dotdot;
321 struct object_id oid;
322 struct commit *commit;
323 struct commit_list *parents;
324 int parent_number;
325 int include_rev = 0;
326 int include_parents = 0;
327 int exclude_parent = 0;
329 if ((dotdot = strstr(arg, "^!"))) {
330 include_rev = 1;
331 if (dotdot[2])
332 return 0;
333 } else if ((dotdot = strstr(arg, "^@"))) {
334 include_parents = 1;
335 if (dotdot[2])
336 return 0;
337 } else if ((dotdot = strstr(arg, "^-"))) {
338 include_rev = 1;
339 exclude_parent = 1;
341 if (dotdot[2]) {
342 char *end;
343 exclude_parent = strtoul(dotdot + 2, &end, 10);
344 if (*end != '\0' || !exclude_parent)
345 return 0;
347 } else
348 return 0;
350 *dotdot = 0;
351 if (repo_get_oid_committish(the_repository, arg, &oid) ||
352 !(commit = lookup_commit_reference(the_repository, &oid))) {
353 *dotdot = '^';
354 return 0;
357 if (exclude_parent &&
358 exclude_parent > commit_list_count(commit->parents)) {
359 *dotdot = '^';
360 return 0;
363 if (include_rev)
364 show_rev(NORMAL, &oid, arg);
365 for (parents = commit->parents, parent_number = 1;
366 parents;
367 parents = parents->next, parent_number++) {
368 char *name = NULL;
370 if (exclude_parent && parent_number != exclude_parent)
371 continue;
373 if (symbolic)
374 name = xstrfmt("%s^%d", arg, parent_number);
375 show_rev(include_parents ? NORMAL : REVERSED,
376 &parents->item->object.oid, name);
377 free(name);
380 *dotdot = '^';
381 return 1;
384 static int parseopt_dump(const struct option *o, const char *arg, int unset)
386 struct strbuf *parsed = o->value;
387 if (unset)
388 strbuf_addf(parsed, " --no-%s", o->long_name);
389 else if (o->short_name && (o->long_name == NULL || !stuck_long))
390 strbuf_addf(parsed, " -%c", o->short_name);
391 else
392 strbuf_addf(parsed, " --%s", o->long_name);
393 if (arg) {
394 if (!stuck_long)
395 strbuf_addch(parsed, ' ');
396 else if (o->long_name)
397 strbuf_addch(parsed, '=');
398 sq_quote_buf(parsed, arg);
400 return 0;
403 static const char *skipspaces(const char *s)
405 while (isspace(*s))
406 s++;
407 return s;
410 static char *findspace(const char *s)
412 for (; *s; s++)
413 if (isspace(*s))
414 return (char*)s;
415 return NULL;
418 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
420 static int keep_dashdash = 0, stop_at_non_option = 0;
421 static char const * const parseopt_usage[] = {
422 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
423 NULL
425 static struct option parseopt_opts[] = {
426 OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
427 N_("keep the `--` passed as an arg")),
428 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
429 N_("stop parsing after the "
430 "first non-option argument")),
431 OPT_BOOL(0, "stuck-long", &stuck_long,
432 N_("output in stuck long form")),
433 OPT_END(),
435 static const char * const flag_chars = "*=?!";
437 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
438 const char **usage = NULL;
439 struct option *opts = NULL;
440 int onb = 0, osz = 0, unb = 0, usz = 0;
442 strbuf_addstr(&parsed, "set --");
443 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
444 PARSE_OPT_KEEP_DASHDASH);
445 if (argc < 1 || strcmp(argv[0], "--"))
446 usage_with_options(parseopt_usage, parseopt_opts);
448 /* get the usage up to the first line with a -- on it */
449 for (;;) {
450 if (strbuf_getline(&sb, stdin) == EOF)
451 die(_("premature end of input"));
452 ALLOC_GROW(usage, unb + 1, usz);
453 if (!strcmp("--", sb.buf)) {
454 if (unb < 1)
455 die(_("no usage string given before the `--' separator"));
456 usage[unb] = NULL;
457 break;
459 usage[unb++] = strbuf_detach(&sb, NULL);
462 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
463 while (strbuf_getline(&sb, stdin) != EOF) {
464 const char *s;
465 char *help;
466 struct option *o;
468 if (!sb.len)
469 continue;
471 ALLOC_GROW(opts, onb + 1, osz);
472 memset(opts + onb, 0, sizeof(opts[onb]));
474 o = &opts[onb++];
475 help = findspace(sb.buf);
476 if (!help || sb.buf == help) {
477 o->type = OPTION_GROUP;
478 o->help = xstrdup(skipspaces(sb.buf));
479 continue;
482 *help = '\0';
484 o->type = OPTION_CALLBACK;
485 o->help = xstrdup(skipspaces(help+1));
486 o->value = &parsed;
487 o->flags = PARSE_OPT_NOARG;
488 o->callback = &parseopt_dump;
490 /* name(s) */
491 s = strpbrk(sb.buf, flag_chars);
492 if (!s)
493 s = help;
495 if (s == sb.buf)
496 die(_("missing opt-spec before option flags"));
498 if (s - sb.buf == 1) /* short option only */
499 o->short_name = *sb.buf;
500 else if (sb.buf[1] != ',') /* long option only */
501 o->long_name = xmemdupz(sb.buf, s - sb.buf);
502 else {
503 o->short_name = *sb.buf;
504 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
507 /* flags */
508 while (s < help) {
509 switch (*s++) {
510 case '=':
511 o->flags &= ~PARSE_OPT_NOARG;
512 continue;
513 case '?':
514 o->flags &= ~PARSE_OPT_NOARG;
515 o->flags |= PARSE_OPT_OPTARG;
516 continue;
517 case '!':
518 o->flags |= PARSE_OPT_NONEG;
519 continue;
520 case '*':
521 o->flags |= PARSE_OPT_HIDDEN;
522 continue;
524 s--;
525 break;
528 if (s < help)
529 o->argh = xmemdupz(s, help - s);
531 strbuf_release(&sb);
533 /* put an OPT_END() */
534 ALLOC_GROW(opts, onb + 1, osz);
535 memset(opts + onb, 0, sizeof(opts[onb]));
536 argc = parse_options(argc, argv, prefix, opts, usage,
537 (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
538 (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
539 PARSE_OPT_SHELL_EVAL);
541 strbuf_addstr(&parsed, " --");
542 sq_quote_argv(&parsed, argv);
543 puts(parsed.buf);
544 strbuf_release(&parsed);
545 return 0;
548 static int cmd_sq_quote(int argc, const char **argv)
550 struct strbuf buf = STRBUF_INIT;
552 if (argc)
553 sq_quote_argv(&buf, argv);
554 printf("%s\n", buf.buf);
555 strbuf_release(&buf);
557 return 0;
560 static void die_no_single_rev(int quiet)
562 if (quiet)
563 exit(1);
564 else
565 die(_("Needed a single revision"));
568 static const char builtin_rev_parse_usage[] =
569 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
570 " or: git rev-parse --sq-quote [<arg>...]\n"
571 " or: git rev-parse [<options>] [<arg>...]\n"
572 "\n"
573 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
576 * Parse "opt" or "opt=<value>", setting value respectively to either
577 * NULL or the string after "=".
579 static int opt_with_value(const char *arg, const char *opt, const char **value)
581 if (skip_prefix(arg, opt, &arg)) {
582 if (!*arg) {
583 *value = NULL;
584 return 1;
586 if (*arg++ == '=') {
587 *value = arg;
588 return 1;
591 return 0;
594 static void handle_ref_opt(const char *pattern, const char *prefix)
596 if (pattern)
597 for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
598 else
599 for_each_ref_in(prefix, show_reference, NULL);
600 clear_ref_exclusions(&ref_excludes);
603 enum format_type {
604 /* We would like a relative path. */
605 FORMAT_RELATIVE,
606 /* We would like a canonical absolute path. */
607 FORMAT_CANONICAL,
608 /* We would like the default behavior. */
609 FORMAT_DEFAULT,
612 enum default_type {
613 /* Our default is a relative path. */
614 DEFAULT_RELATIVE,
615 /* Our default is a relative path if there's a shared root. */
616 DEFAULT_RELATIVE_IF_SHARED,
617 /* Our default is a canonical absolute path. */
618 DEFAULT_CANONICAL,
619 /* Our default is not to modify the item. */
620 DEFAULT_UNMODIFIED,
623 static void print_path(const char *path, const char *prefix, enum format_type format, enum default_type def)
625 char *cwd = NULL;
627 * We don't ever produce a relative path if prefix is NULL, so set the
628 * prefix to the current directory so that we can produce a relative
629 * path whenever possible. If we're using RELATIVE_IF_SHARED mode, then
630 * we want an absolute path unless the two share a common prefix, so don't
631 * set it in that case, since doing so causes a relative path to always
632 * be produced if possible.
634 if (!prefix && (format != FORMAT_DEFAULT || def != DEFAULT_RELATIVE_IF_SHARED))
635 prefix = cwd = xgetcwd();
636 if (format == FORMAT_DEFAULT && def == DEFAULT_UNMODIFIED) {
637 puts(path);
638 } else if (format == FORMAT_RELATIVE ||
639 (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE)) {
641 * In order for relative_path to work as expected, we need to
642 * make sure that both paths are absolute paths. If we don't,
643 * we can end up with an unexpected absolute path that the user
644 * didn't want.
646 struct strbuf buf = STRBUF_INIT, realbuf = STRBUF_INIT, prefixbuf = STRBUF_INIT;
647 if (!is_absolute_path(path)) {
648 strbuf_realpath_forgiving(&realbuf, path, 1);
649 path = realbuf.buf;
651 if (!is_absolute_path(prefix)) {
652 strbuf_realpath_forgiving(&prefixbuf, prefix, 1);
653 prefix = prefixbuf.buf;
655 puts(relative_path(path, prefix, &buf));
656 strbuf_release(&buf);
657 strbuf_release(&realbuf);
658 strbuf_release(&prefixbuf);
659 } else if (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE_IF_SHARED) {
660 struct strbuf buf = STRBUF_INIT;
661 puts(relative_path(path, prefix, &buf));
662 strbuf_release(&buf);
663 } else {
664 struct strbuf buf = STRBUF_INIT;
665 strbuf_realpath_forgiving(&buf, path, 1);
666 puts(buf.buf);
667 strbuf_release(&buf);
669 free(cwd);
672 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
674 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
675 int did_repo_setup = 0;
676 int has_dashdash = 0;
677 int output_prefix = 0;
678 struct object_id oid;
679 unsigned int flags = 0;
680 const char *name = NULL;
681 struct object_context unused;
682 struct strbuf buf = STRBUF_INIT;
683 const int hexsz = the_hash_algo->hexsz;
684 int seen_end_of_options = 0;
685 enum format_type format = FORMAT_DEFAULT;
687 if (argc > 1 && !strcmp("--parseopt", argv[1]))
688 return cmd_parseopt(argc - 1, argv + 1, prefix);
690 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
691 return cmd_sq_quote(argc - 2, argv + 2);
693 if (argc > 1 && !strcmp("-h", argv[1]))
694 usage(builtin_rev_parse_usage);
696 for (i = 1; i < argc; i++) {
697 if (!strcmp(argv[i], "--")) {
698 has_dashdash = 1;
699 break;
703 /* No options; just report on whether we're in a git repo or not. */
704 if (argc == 1) {
705 setup_git_directory();
706 git_config(git_default_config, NULL);
707 return 0;
710 for (i = 1; i < argc; i++) {
711 const char *arg = argv[i];
713 if (as_is) {
714 if (show_file(arg, output_prefix) && as_is < 2)
715 verify_filename(prefix, arg, 0);
716 continue;
719 if (!seen_end_of_options) {
720 if (!strcmp(arg, "--local-env-vars")) {
721 int i;
722 for (i = 0; local_repo_env[i]; i++)
723 printf("%s\n", local_repo_env[i]);
724 continue;
726 if (!strcmp(arg, "--resolve-git-dir")) {
727 const char *gitdir = argv[++i];
728 if (!gitdir)
729 die(_("--resolve-git-dir requires an argument"));
730 gitdir = resolve_gitdir(gitdir);
731 if (!gitdir)
732 die(_("not a gitdir '%s'"), argv[i]);
733 puts(gitdir);
734 continue;
738 /* The rest of the options require a git repository. */
739 if (!did_repo_setup) {
740 prefix = setup_git_directory();
741 git_config(git_default_config, NULL);
742 did_repo_setup = 1;
744 prepare_repo_settings(the_repository);
745 the_repository->settings.command_requires_full_index = 0;
748 if (!strcmp(arg, "--")) {
749 as_is = 2;
750 /* Pass on the "--" if we show anything but files.. */
751 if (filter & (DO_FLAGS | DO_REVS))
752 show_file(arg, 0);
753 continue;
756 if (!seen_end_of_options && *arg == '-') {
757 if (!strcmp(arg, "--git-path")) {
758 if (!argv[i + 1])
759 die(_("--git-path requires an argument"));
760 strbuf_reset(&buf);
761 print_path(git_path("%s", argv[i + 1]), prefix,
762 format,
763 DEFAULT_RELATIVE_IF_SHARED);
764 i++;
765 continue;
767 if (!strcmp(arg,"-n")) {
768 if (++i >= argc)
769 die(_("-n requires an argument"));
770 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
771 show(arg);
772 show(argv[i]);
774 continue;
776 if (starts_with(arg, "-n")) {
777 if ((filter & DO_FLAGS) && (filter & DO_REVS))
778 show(arg);
779 continue;
781 if (opt_with_value(arg, "--path-format", &arg)) {
782 if (!arg)
783 die(_("--path-format requires an argument"));
784 if (!strcmp(arg, "absolute")) {
785 format = FORMAT_CANONICAL;
786 } else if (!strcmp(arg, "relative")) {
787 format = FORMAT_RELATIVE;
788 } else {
789 die(_("unknown argument to --path-format: %s"), arg);
791 continue;
793 if (!strcmp(arg, "--default")) {
794 def = argv[++i];
795 if (!def)
796 die(_("--default requires an argument"));
797 continue;
799 if (!strcmp(arg, "--prefix")) {
800 prefix = argv[++i];
801 if (!prefix)
802 die(_("--prefix requires an argument"));
803 startup_info->prefix = prefix;
804 output_prefix = 1;
805 continue;
807 if (!strcmp(arg, "--revs-only")) {
808 filter &= ~DO_NOREV;
809 continue;
811 if (!strcmp(arg, "--no-revs")) {
812 filter &= ~DO_REVS;
813 continue;
815 if (!strcmp(arg, "--flags")) {
816 filter &= ~DO_NONFLAGS;
817 continue;
819 if (!strcmp(arg, "--no-flags")) {
820 filter &= ~DO_FLAGS;
821 continue;
823 if (!strcmp(arg, "--verify")) {
824 filter &= ~(DO_FLAGS|DO_NOREV);
825 verify = 1;
826 continue;
828 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
829 quiet = 1;
830 flags |= GET_OID_QUIETLY;
831 continue;
833 if (opt_with_value(arg, "--short", &arg)) {
834 filter &= ~(DO_FLAGS|DO_NOREV);
835 verify = 1;
836 abbrev = DEFAULT_ABBREV;
837 if (!arg)
838 continue;
839 abbrev = strtoul(arg, NULL, 10);
840 if (abbrev < MINIMUM_ABBREV)
841 abbrev = MINIMUM_ABBREV;
842 else if (hexsz <= abbrev)
843 abbrev = hexsz;
844 continue;
846 if (!strcmp(arg, "--sq")) {
847 output_sq = 1;
848 continue;
850 if (!strcmp(arg, "--not")) {
851 show_type ^= REVERSED;
852 continue;
854 if (!strcmp(arg, "--symbolic")) {
855 symbolic = SHOW_SYMBOLIC_ASIS;
856 continue;
858 if (!strcmp(arg, "--symbolic-full-name")) {
859 symbolic = SHOW_SYMBOLIC_FULL;
860 continue;
862 if (opt_with_value(arg, "--abbrev-ref", &arg)) {
863 abbrev_ref = 1;
864 abbrev_ref_strict = warn_ambiguous_refs;
865 if (arg) {
866 if (!strcmp(arg, "strict"))
867 abbrev_ref_strict = 1;
868 else if (!strcmp(arg, "loose"))
869 abbrev_ref_strict = 0;
870 else
871 die(_("unknown mode for --abbrev-ref: %s"),
872 arg);
874 continue;
876 if (!strcmp(arg, "--all")) {
877 for_each_ref(show_reference, NULL);
878 clear_ref_exclusions(&ref_excludes);
879 continue;
881 if (skip_prefix(arg, "--disambiguate=", &arg)) {
882 repo_for_each_abbrev(the_repository, arg,
883 show_abbrev, NULL);
884 continue;
886 if (!strcmp(arg, "--bisect")) {
887 for_each_fullref_in("refs/bisect/bad", show_reference, NULL);
888 for_each_fullref_in("refs/bisect/good", anti_reference, NULL);
889 continue;
891 if (opt_with_value(arg, "--branches", &arg)) {
892 if (ref_excludes.hidden_refs_configured)
893 return error(_("--exclude-hidden cannot be used together with --branches"));
894 handle_ref_opt(arg, "refs/heads/");
895 continue;
897 if (opt_with_value(arg, "--tags", &arg)) {
898 if (ref_excludes.hidden_refs_configured)
899 return error(_("--exclude-hidden cannot be used together with --tags"));
900 handle_ref_opt(arg, "refs/tags/");
901 continue;
903 if (skip_prefix(arg, "--glob=", &arg)) {
904 handle_ref_opt(arg, NULL);
905 continue;
907 if (opt_with_value(arg, "--remotes", &arg)) {
908 if (ref_excludes.hidden_refs_configured)
909 return error(_("--exclude-hidden cannot be used together with --remotes"));
910 handle_ref_opt(arg, "refs/remotes/");
911 continue;
913 if (skip_prefix(arg, "--exclude=", &arg)) {
914 add_ref_exclusion(&ref_excludes, arg);
915 continue;
917 if (skip_prefix(arg, "--exclude-hidden=", &arg)) {
918 exclude_hidden_refs(&ref_excludes, arg);
919 continue;
921 if (!strcmp(arg, "--show-toplevel")) {
922 const char *work_tree = get_git_work_tree();
923 if (work_tree)
924 print_path(work_tree, prefix, format, DEFAULT_UNMODIFIED);
925 else
926 die(_("this operation must be run in a work tree"));
927 continue;
929 if (!strcmp(arg, "--show-superproject-working-tree")) {
930 struct strbuf superproject = STRBUF_INIT;
931 if (get_superproject_working_tree(&superproject))
932 print_path(superproject.buf, prefix, format, DEFAULT_UNMODIFIED);
933 strbuf_release(&superproject);
934 continue;
936 if (!strcmp(arg, "--show-prefix")) {
937 if (prefix)
938 puts(prefix);
939 else
940 putchar('\n');
941 continue;
943 if (!strcmp(arg, "--show-cdup")) {
944 const char *pfx = prefix;
945 if (!is_inside_work_tree()) {
946 const char *work_tree =
947 get_git_work_tree();
948 if (work_tree)
949 printf("%s\n", work_tree);
950 continue;
952 while (pfx) {
953 pfx = strchr(pfx, '/');
954 if (pfx) {
955 pfx++;
956 printf("../");
959 putchar('\n');
960 continue;
962 if (!strcmp(arg, "--git-dir") ||
963 !strcmp(arg, "--absolute-git-dir")) {
964 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
965 char *cwd;
966 int len;
967 enum format_type wanted = format;
968 if (arg[2] == 'g') { /* --git-dir */
969 if (gitdir) {
970 print_path(gitdir, prefix, format, DEFAULT_UNMODIFIED);
971 continue;
973 if (!prefix) {
974 print_path(".git", prefix, format, DEFAULT_UNMODIFIED);
975 continue;
977 } else { /* --absolute-git-dir */
978 wanted = FORMAT_CANONICAL;
979 if (!gitdir && !prefix)
980 gitdir = ".git";
981 if (gitdir) {
982 struct strbuf realpath = STRBUF_INIT;
983 strbuf_realpath(&realpath, gitdir, 1);
984 puts(realpath.buf);
985 strbuf_release(&realpath);
986 continue;
989 cwd = xgetcwd();
990 len = strlen(cwd);
991 strbuf_reset(&buf);
992 strbuf_addf(&buf, "%s%s.git", cwd, len && cwd[len-1] != '/' ? "/" : "");
993 free(cwd);
994 print_path(buf.buf, prefix, wanted, DEFAULT_CANONICAL);
995 continue;
997 if (!strcmp(arg, "--git-common-dir")) {
998 print_path(get_git_common_dir(), prefix, format, DEFAULT_RELATIVE_IF_SHARED);
999 continue;
1001 if (!strcmp(arg, "--is-inside-git-dir")) {
1002 printf("%s\n", is_inside_git_dir() ? "true"
1003 : "false");
1004 continue;
1006 if (!strcmp(arg, "--is-inside-work-tree")) {
1007 printf("%s\n", is_inside_work_tree() ? "true"
1008 : "false");
1009 continue;
1011 if (!strcmp(arg, "--is-bare-repository")) {
1012 printf("%s\n", is_bare_repository() ? "true"
1013 : "false");
1014 continue;
1016 if (!strcmp(arg, "--is-shallow-repository")) {
1017 printf("%s\n",
1018 is_repository_shallow(the_repository) ? "true"
1019 : "false");
1020 continue;
1022 if (!strcmp(arg, "--shared-index-path")) {
1023 if (repo_read_index(the_repository) < 0)
1024 die(_("Could not read the index"));
1025 if (the_index.split_index) {
1026 const struct object_id *oid = &the_index.split_index->base_oid;
1027 const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
1028 print_path(path, prefix, format, DEFAULT_RELATIVE);
1030 continue;
1032 if (skip_prefix(arg, "--since=", &arg)) {
1033 show_datestring("--max-age=", arg);
1034 continue;
1036 if (skip_prefix(arg, "--after=", &arg)) {
1037 show_datestring("--max-age=", arg);
1038 continue;
1040 if (skip_prefix(arg, "--before=", &arg)) {
1041 show_datestring("--min-age=", arg);
1042 continue;
1044 if (skip_prefix(arg, "--until=", &arg)) {
1045 show_datestring("--min-age=", arg);
1046 continue;
1048 if (opt_with_value(arg, "--show-object-format", &arg)) {
1049 const char *val = arg ? arg : "storage";
1051 if (strcmp(val, "storage") &&
1052 strcmp(val, "input") &&
1053 strcmp(val, "output"))
1054 die(_("unknown mode for --show-object-format: %s"),
1055 arg);
1056 puts(the_hash_algo->name);
1057 continue;
1059 if (!strcmp(arg, "--end-of-options")) {
1060 seen_end_of_options = 1;
1061 if (filter & (DO_FLAGS | DO_REVS))
1062 show_file(arg, 0);
1063 continue;
1065 if (show_flag(arg) && verify)
1066 die_no_single_rev(quiet);
1067 continue;
1070 /* Not a flag argument */
1071 if (try_difference(arg))
1072 continue;
1073 if (try_parent_shorthands(arg))
1074 continue;
1075 name = arg;
1076 type = NORMAL;
1077 if (*arg == '^') {
1078 name++;
1079 type = REVERSED;
1081 if (!get_oid_with_context(the_repository, name,
1082 flags, &oid, &unused)) {
1083 if (verify)
1084 revs_count++;
1085 else
1086 show_rev(type, &oid, name);
1087 continue;
1089 if (verify)
1090 die_no_single_rev(quiet);
1091 if (has_dashdash)
1092 die(_("bad revision '%s'"), arg);
1093 as_is = 1;
1094 if (!show_file(arg, output_prefix))
1095 continue;
1096 verify_filename(prefix, arg, 1);
1098 strbuf_release(&buf);
1099 if (verify) {
1100 if (revs_count == 1) {
1101 show_rev(type, &oid, name);
1102 return 0;
1103 } else if (revs_count == 0 && show_default())
1104 return 0;
1105 die_no_single_rev(quiet);
1106 } else
1107 show_default();
1108 return 0;