cache.h: remove this no-longer-used header
[alt-git.git] / builtin / rev-parse.c
blob8d8c870421dc037da82f43f2d8c423d58fd8bb28
1 /*
2 * rev-parse.c
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #define USE_THE_INDEX_VARIABLE
7 #include "builtin.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 "object-name.h"
18 #include "parse-options.h"
19 #include "diff.h"
20 #include "read-cache-ll.h"
21 #include "revision.h"
22 #include "setup.h"
23 #include "split-index.h"
24 #include "submodule.h"
25 #include "commit-reach.h"
26 #include "shallow.h"
28 #define DO_REVS 1
29 #define DO_NOREV 2
30 #define DO_FLAGS 4
31 #define DO_NONFLAGS 8
32 static int filter = ~0;
34 static const char *def;
36 #define NORMAL 0
37 #define REVERSED 1
38 static int show_type = NORMAL;
40 #define SHOW_SYMBOLIC_ASIS 1
41 #define SHOW_SYMBOLIC_FULL 2
42 static int symbolic;
43 static int abbrev;
44 static int abbrev_ref;
45 static int abbrev_ref_strict;
46 static int output_sq;
48 static int stuck_long;
49 static struct ref_exclusions ref_excludes = REF_EXCLUSIONS_INIT;
52 * Some arguments are relevant "revision" arguments,
53 * others are about output format or other details.
54 * This sorts it all out.
56 static int is_rev_argument(const char *arg)
58 static const char *rev_args[] = {
59 "--all",
60 "--bisect",
61 "--dense",
62 "--branches=",
63 "--branches",
64 "--header",
65 "--ignore-missing",
66 "--max-age=",
67 "--max-count=",
68 "--min-age=",
69 "--no-merges",
70 "--min-parents=",
71 "--no-min-parents",
72 "--max-parents=",
73 "--no-max-parents",
74 "--objects",
75 "--objects-edge",
76 "--parents",
77 "--pretty",
78 "--remotes=",
79 "--remotes",
80 "--glob=",
81 "--sparse",
82 "--tags=",
83 "--tags",
84 "--topo-order",
85 "--date-order",
86 "--unpacked",
87 NULL
89 const char **p = rev_args;
91 /* accept -<digit>, like traditional "head" */
92 if ((*arg == '-') && isdigit(arg[1]))
93 return 1;
95 for (;;) {
96 const char *str = *p++;
97 int len;
98 if (!str)
99 return 0;
100 len = strlen(str);
101 if (!strcmp(arg, str) ||
102 (str[len-1] == '=' && !strncmp(arg, str, len)))
103 return 1;
107 /* Output argument as a string, either SQ or normal */
108 static void show(const char *arg)
110 if (output_sq) {
111 int sq = '\'', ch;
113 putchar(sq);
114 while ((ch = *arg++)) {
115 if (ch == sq)
116 fputs("'\\'", stdout);
117 putchar(ch);
119 putchar(sq);
120 putchar(' ');
122 else
123 puts(arg);
126 /* Like show(), but with a negation prefix according to type */
127 static void show_with_type(int type, const char *arg)
129 if (type != show_type)
130 putchar('^');
131 show(arg);
134 /* Output a revision, only if filter allows it */
135 static void show_rev(int type, const struct object_id *oid, const char *name)
137 if (!(filter & DO_REVS))
138 return;
139 def = NULL;
141 if ((symbolic || abbrev_ref) && name) {
142 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
143 struct object_id discard;
144 char *full;
146 switch (repo_dwim_ref(the_repository, name,
147 strlen(name), &discard, &full,
148 0)) {
149 case 0:
151 * Not found -- not a ref. We could
152 * emit "name" here, but symbolic-full
153 * users are interested in finding the
154 * refs spelled in full, and they would
155 * need to filter non-refs if we did so.
157 break;
158 case 1: /* happy */
159 if (abbrev_ref)
160 full = shorten_unambiguous_ref(full,
161 abbrev_ref_strict);
162 show_with_type(type, full);
163 break;
164 default: /* ambiguous */
165 error("refname '%s' is ambiguous", name);
166 break;
168 free(full);
169 } else {
170 show_with_type(type, name);
173 else if (abbrev)
174 show_with_type(type,
175 repo_find_unique_abbrev(the_repository, oid, abbrev));
176 else
177 show_with_type(type, oid_to_hex(oid));
180 /* Output a flag, only if filter allows it. */
181 static int show_flag(const char *arg)
183 if (!(filter & DO_FLAGS))
184 return 0;
185 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
186 show(arg);
187 return 1;
189 return 0;
192 static int show_default(void)
194 const char *s = def;
196 if (s) {
197 struct object_id oid;
199 def = NULL;
200 if (!repo_get_oid(the_repository, s, &oid)) {
201 show_rev(NORMAL, &oid, s);
202 return 1;
205 return 0;
208 static int show_reference(const char *refname, const struct object_id *oid,
209 int flag UNUSED, void *cb_data UNUSED)
211 if (ref_excluded(&ref_excludes, refname))
212 return 0;
213 show_rev(NORMAL, oid, refname);
214 return 0;
217 static int anti_reference(const char *refname, const struct object_id *oid,
218 int flag UNUSED, void *cb_data UNUSED)
220 show_rev(REVERSED, oid, refname);
221 return 0;
224 static int show_abbrev(const struct object_id *oid, void *cb_data)
226 show_rev(NORMAL, oid, NULL);
227 return 0;
230 static void show_datestring(const char *flag, const char *datestr)
232 char *buffer;
234 /* date handling requires both flags and revs */
235 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
236 return;
237 buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr));
238 show(buffer);
239 free(buffer);
242 static int show_file(const char *arg, int output_prefix)
244 show_default();
245 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
246 if (output_prefix) {
247 const char *prefix = startup_info->prefix;
248 char *fname = prefix_filename(prefix, arg);
249 show(fname);
250 free(fname);
251 } else
252 show(arg);
253 return 1;
255 return 0;
258 static int try_difference(const char *arg)
260 char *dotdot;
261 struct object_id start_oid;
262 struct object_id end_oid;
263 const char *end;
264 const char *start;
265 int symmetric;
266 static const char head_by_default[] = "HEAD";
268 if (!(dotdot = strstr(arg, "..")))
269 return 0;
270 end = dotdot + 2;
271 start = arg;
272 symmetric = (*end == '.');
274 *dotdot = 0;
275 end += symmetric;
277 if (!*end)
278 end = head_by_default;
279 if (dotdot == arg)
280 start = head_by_default;
282 if (start == head_by_default && end == head_by_default &&
283 !symmetric) {
285 * Just ".."? That is not a range but the
286 * pathspec for the parent directory.
288 *dotdot = '.';
289 return 0;
292 if (!repo_get_oid_committish(the_repository, start, &start_oid) && !repo_get_oid_committish(the_repository, end, &end_oid)) {
293 show_rev(NORMAL, &end_oid, end);
294 show_rev(symmetric ? NORMAL : REVERSED, &start_oid, start);
295 if (symmetric) {
296 struct commit_list *exclude;
297 struct commit *a, *b;
298 a = lookup_commit_reference(the_repository, &start_oid);
299 b = lookup_commit_reference(the_repository, &end_oid);
300 if (!a || !b) {
301 *dotdot = '.';
302 return 0;
304 exclude = repo_get_merge_bases(the_repository, a, b);
305 while (exclude) {
306 struct commit *commit = pop_commit(&exclude);
307 show_rev(REVERSED, &commit->object.oid, NULL);
310 *dotdot = '.';
311 return 1;
313 *dotdot = '.';
314 return 0;
317 static int try_parent_shorthands(const char *arg)
319 char *dotdot;
320 struct object_id oid;
321 struct commit *commit;
322 struct commit_list *parents;
323 int parent_number;
324 int include_rev = 0;
325 int include_parents = 0;
326 int exclude_parent = 0;
328 if ((dotdot = strstr(arg, "^!"))) {
329 include_rev = 1;
330 if (dotdot[2])
331 return 0;
332 } else if ((dotdot = strstr(arg, "^@"))) {
333 include_parents = 1;
334 if (dotdot[2])
335 return 0;
336 } else if ((dotdot = strstr(arg, "^-"))) {
337 include_rev = 1;
338 exclude_parent = 1;
340 if (dotdot[2]) {
341 char *end;
342 exclude_parent = strtoul(dotdot + 2, &end, 10);
343 if (*end != '\0' || !exclude_parent)
344 return 0;
346 } else
347 return 0;
349 *dotdot = 0;
350 if (repo_get_oid_committish(the_repository, arg, &oid) ||
351 !(commit = lookup_commit_reference(the_repository, &oid))) {
352 *dotdot = '^';
353 return 0;
356 if (exclude_parent &&
357 exclude_parent > commit_list_count(commit->parents)) {
358 *dotdot = '^';
359 return 0;
362 if (include_rev)
363 show_rev(NORMAL, &oid, arg);
364 for (parents = commit->parents, parent_number = 1;
365 parents;
366 parents = parents->next, parent_number++) {
367 char *name = NULL;
369 if (exclude_parent && parent_number != exclude_parent)
370 continue;
372 if (symbolic)
373 name = xstrfmt("%s^%d", arg, parent_number);
374 show_rev(include_parents ? NORMAL : REVERSED,
375 &parents->item->object.oid, name);
376 free(name);
379 *dotdot = '^';
380 return 1;
383 static int parseopt_dump(const struct option *o, const char *arg, int unset)
385 struct strbuf *parsed = o->value;
386 if (unset)
387 strbuf_addf(parsed, " --no-%s", o->long_name);
388 else if (o->short_name && (o->long_name == NULL || !stuck_long))
389 strbuf_addf(parsed, " -%c", o->short_name);
390 else
391 strbuf_addf(parsed, " --%s", o->long_name);
392 if (arg) {
393 if (!stuck_long)
394 strbuf_addch(parsed, ' ');
395 else if (o->long_name)
396 strbuf_addch(parsed, '=');
397 sq_quote_buf(parsed, arg);
399 return 0;
402 static const char *skipspaces(const char *s)
404 while (isspace(*s))
405 s++;
406 return s;
409 static char *findspace(const char *s)
411 for (; *s; s++)
412 if (isspace(*s))
413 return (char*)s;
414 return NULL;
417 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
419 static int keep_dashdash = 0, stop_at_non_option = 0;
420 static char const * const parseopt_usage[] = {
421 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
422 NULL
424 static struct option parseopt_opts[] = {
425 OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
426 N_("keep the `--` passed as an arg")),
427 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
428 N_("stop parsing after the "
429 "first non-option argument")),
430 OPT_BOOL(0, "stuck-long", &stuck_long,
431 N_("output in stuck long form")),
432 OPT_END(),
434 static const char * const flag_chars = "*=?!";
436 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
437 const char **usage = NULL;
438 struct option *opts = NULL;
439 int onb = 0, osz = 0, unb = 0, usz = 0;
441 strbuf_addstr(&parsed, "set --");
442 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
443 PARSE_OPT_KEEP_DASHDASH);
444 if (argc < 1 || strcmp(argv[0], "--"))
445 usage_with_options(parseopt_usage, parseopt_opts);
447 /* get the usage up to the first line with a -- on it */
448 for (;;) {
449 if (strbuf_getline(&sb, stdin) == EOF)
450 die(_("premature end of input"));
451 ALLOC_GROW(usage, unb + 1, usz);
452 if (!strcmp("--", sb.buf)) {
453 if (unb < 1)
454 die(_("no usage string given before the `--' separator"));
455 usage[unb] = NULL;
456 break;
458 usage[unb++] = strbuf_detach(&sb, NULL);
461 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
462 while (strbuf_getline(&sb, stdin) != EOF) {
463 const char *s;
464 char *help;
465 struct option *o;
467 if (!sb.len)
468 continue;
470 ALLOC_GROW(opts, onb + 1, osz);
471 memset(opts + onb, 0, sizeof(opts[onb]));
473 o = &opts[onb++];
474 help = findspace(sb.buf);
475 if (!help || sb.buf == help) {
476 o->type = OPTION_GROUP;
477 o->help = xstrdup(skipspaces(sb.buf));
478 continue;
481 *help = '\0';
483 o->type = OPTION_CALLBACK;
484 o->help = xstrdup(skipspaces(help+1));
485 o->value = &parsed;
486 o->flags = PARSE_OPT_NOARG;
487 o->callback = &parseopt_dump;
489 /* name(s) */
490 s = strpbrk(sb.buf, flag_chars);
491 if (!s)
492 s = help;
494 if (s == sb.buf)
495 die(_("missing opt-spec before option flags"));
497 if (s - sb.buf == 1) /* short option only */
498 o->short_name = *sb.buf;
499 else if (sb.buf[1] != ',') /* long option only */
500 o->long_name = xmemdupz(sb.buf, s - sb.buf);
501 else {
502 o->short_name = *sb.buf;
503 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
506 /* flags */
507 while (s < help) {
508 switch (*s++) {
509 case '=':
510 o->flags &= ~PARSE_OPT_NOARG;
511 continue;
512 case '?':
513 o->flags &= ~PARSE_OPT_NOARG;
514 o->flags |= PARSE_OPT_OPTARG;
515 continue;
516 case '!':
517 o->flags |= PARSE_OPT_NONEG;
518 continue;
519 case '*':
520 o->flags |= PARSE_OPT_HIDDEN;
521 continue;
523 s--;
524 break;
527 if (s < help)
528 o->argh = xmemdupz(s, help - s);
530 strbuf_release(&sb);
532 /* put an OPT_END() */
533 ALLOC_GROW(opts, onb + 1, osz);
534 memset(opts + onb, 0, sizeof(opts[onb]));
535 argc = parse_options(argc, argv, prefix, opts, usage,
536 (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
537 (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
538 PARSE_OPT_SHELL_EVAL);
540 strbuf_addstr(&parsed, " --");
541 sq_quote_argv(&parsed, argv);
542 puts(parsed.buf);
543 strbuf_release(&parsed);
544 return 0;
547 static int cmd_sq_quote(int argc, const char **argv)
549 struct strbuf buf = STRBUF_INIT;
551 if (argc)
552 sq_quote_argv(&buf, argv);
553 printf("%s\n", buf.buf);
554 strbuf_release(&buf);
556 return 0;
559 static void die_no_single_rev(int quiet)
561 if (quiet)
562 exit(1);
563 else
564 die(_("Needed a single revision"));
567 static const char builtin_rev_parse_usage[] =
568 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
569 " or: git rev-parse --sq-quote [<arg>...]\n"
570 " or: git rev-parse [<options>] [<arg>...]\n"
571 "\n"
572 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
575 * Parse "opt" or "opt=<value>", setting value respectively to either
576 * NULL or the string after "=".
578 static int opt_with_value(const char *arg, const char *opt, const char **value)
580 if (skip_prefix(arg, opt, &arg)) {
581 if (!*arg) {
582 *value = NULL;
583 return 1;
585 if (*arg++ == '=') {
586 *value = arg;
587 return 1;
590 return 0;
593 static void handle_ref_opt(const char *pattern, const char *prefix)
595 if (pattern)
596 for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
597 else
598 for_each_ref_in(prefix, show_reference, NULL);
599 clear_ref_exclusions(&ref_excludes);
602 enum format_type {
603 /* We would like a relative path. */
604 FORMAT_RELATIVE,
605 /* We would like a canonical absolute path. */
606 FORMAT_CANONICAL,
607 /* We would like the default behavior. */
608 FORMAT_DEFAULT,
611 enum default_type {
612 /* Our default is a relative path. */
613 DEFAULT_RELATIVE,
614 /* Our default is a relative path if there's a shared root. */
615 DEFAULT_RELATIVE_IF_SHARED,
616 /* Our default is a canonical absolute path. */
617 DEFAULT_CANONICAL,
618 /* Our default is not to modify the item. */
619 DEFAULT_UNMODIFIED,
622 static void print_path(const char *path, const char *prefix, enum format_type format, enum default_type def)
624 char *cwd = NULL;
626 * We don't ever produce a relative path if prefix is NULL, so set the
627 * prefix to the current directory so that we can produce a relative
628 * path whenever possible. If we're using RELATIVE_IF_SHARED mode, then
629 * we want an absolute path unless the two share a common prefix, so don't
630 * set it in that case, since doing so causes a relative path to always
631 * be produced if possible.
633 if (!prefix && (format != FORMAT_DEFAULT || def != DEFAULT_RELATIVE_IF_SHARED))
634 prefix = cwd = xgetcwd();
635 if (format == FORMAT_DEFAULT && def == DEFAULT_UNMODIFIED) {
636 puts(path);
637 } else if (format == FORMAT_RELATIVE ||
638 (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE)) {
640 * In order for relative_path to work as expected, we need to
641 * make sure that both paths are absolute paths. If we don't,
642 * we can end up with an unexpected absolute path that the user
643 * didn't want.
645 struct strbuf buf = STRBUF_INIT, realbuf = STRBUF_INIT, prefixbuf = STRBUF_INIT;
646 if (!is_absolute_path(path)) {
647 strbuf_realpath_forgiving(&realbuf, path, 1);
648 path = realbuf.buf;
650 if (!is_absolute_path(prefix)) {
651 strbuf_realpath_forgiving(&prefixbuf, prefix, 1);
652 prefix = prefixbuf.buf;
654 puts(relative_path(path, prefix, &buf));
655 strbuf_release(&buf);
656 strbuf_release(&realbuf);
657 strbuf_release(&prefixbuf);
658 } else if (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE_IF_SHARED) {
659 struct strbuf buf = STRBUF_INIT;
660 puts(relative_path(path, prefix, &buf));
661 strbuf_release(&buf);
662 } else {
663 struct strbuf buf = STRBUF_INIT;
664 strbuf_realpath_forgiving(&buf, path, 1);
665 puts(buf.buf);
666 strbuf_release(&buf);
668 free(cwd);
671 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
673 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
674 int did_repo_setup = 0;
675 int has_dashdash = 0;
676 int output_prefix = 0;
677 struct object_id oid;
678 unsigned int flags = 0;
679 const char *name = NULL;
680 struct object_context unused;
681 struct strbuf buf = STRBUF_INIT;
682 const int hexsz = the_hash_algo->hexsz;
683 int seen_end_of_options = 0;
684 enum format_type format = FORMAT_DEFAULT;
686 if (argc > 1 && !strcmp("--parseopt", argv[1]))
687 return cmd_parseopt(argc - 1, argv + 1, prefix);
689 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
690 return cmd_sq_quote(argc - 2, argv + 2);
692 if (argc > 1 && !strcmp("-h", argv[1]))
693 usage(builtin_rev_parse_usage);
695 for (i = 1; i < argc; i++) {
696 if (!strcmp(argv[i], "--")) {
697 has_dashdash = 1;
698 break;
702 /* No options; just report on whether we're in a git repo or not. */
703 if (argc == 1) {
704 setup_git_directory();
705 git_config(git_default_config, NULL);
706 return 0;
709 for (i = 1; i < argc; i++) {
710 const char *arg = argv[i];
712 if (as_is) {
713 if (show_file(arg, output_prefix) && as_is < 2)
714 verify_filename(prefix, arg, 0);
715 continue;
718 if (!seen_end_of_options) {
719 if (!strcmp(arg, "--local-env-vars")) {
720 int i;
721 for (i = 0; local_repo_env[i]; i++)
722 printf("%s\n", local_repo_env[i]);
723 continue;
725 if (!strcmp(arg, "--resolve-git-dir")) {
726 const char *gitdir = argv[++i];
727 if (!gitdir)
728 die(_("--resolve-git-dir requires an argument"));
729 gitdir = resolve_gitdir(gitdir);
730 if (!gitdir)
731 die(_("not a gitdir '%s'"), argv[i]);
732 puts(gitdir);
733 continue;
737 /* The rest of the options require a git repository. */
738 if (!did_repo_setup) {
739 prefix = setup_git_directory();
740 git_config(git_default_config, NULL);
741 did_repo_setup = 1;
743 prepare_repo_settings(the_repository);
744 the_repository->settings.command_requires_full_index = 0;
747 if (!strcmp(arg, "--")) {
748 as_is = 2;
749 /* Pass on the "--" if we show anything but files.. */
750 if (filter & (DO_FLAGS | DO_REVS))
751 show_file(arg, 0);
752 continue;
755 if (!seen_end_of_options && *arg == '-') {
756 if (!strcmp(arg, "--git-path")) {
757 if (!argv[i + 1])
758 die(_("--git-path requires an argument"));
759 strbuf_reset(&buf);
760 print_path(git_path("%s", argv[i + 1]), prefix,
761 format,
762 DEFAULT_RELATIVE_IF_SHARED);
763 i++;
764 continue;
766 if (!strcmp(arg,"-n")) {
767 if (++i >= argc)
768 die(_("-n requires an argument"));
769 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
770 show(arg);
771 show(argv[i]);
773 continue;
775 if (starts_with(arg, "-n")) {
776 if ((filter & DO_FLAGS) && (filter & DO_REVS))
777 show(arg);
778 continue;
780 if (opt_with_value(arg, "--path-format", &arg)) {
781 if (!arg)
782 die(_("--path-format requires an argument"));
783 if (!strcmp(arg, "absolute")) {
784 format = FORMAT_CANONICAL;
785 } else if (!strcmp(arg, "relative")) {
786 format = FORMAT_RELATIVE;
787 } else {
788 die(_("unknown argument to --path-format: %s"), arg);
790 continue;
792 if (!strcmp(arg, "--default")) {
793 def = argv[++i];
794 if (!def)
795 die(_("--default requires an argument"));
796 continue;
798 if (!strcmp(arg, "--prefix")) {
799 prefix = argv[++i];
800 if (!prefix)
801 die(_("--prefix requires an argument"));
802 startup_info->prefix = prefix;
803 output_prefix = 1;
804 continue;
806 if (!strcmp(arg, "--revs-only")) {
807 filter &= ~DO_NOREV;
808 continue;
810 if (!strcmp(arg, "--no-revs")) {
811 filter &= ~DO_REVS;
812 continue;
814 if (!strcmp(arg, "--flags")) {
815 filter &= ~DO_NONFLAGS;
816 continue;
818 if (!strcmp(arg, "--no-flags")) {
819 filter &= ~DO_FLAGS;
820 continue;
822 if (!strcmp(arg, "--verify")) {
823 filter &= ~(DO_FLAGS|DO_NOREV);
824 verify = 1;
825 continue;
827 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
828 quiet = 1;
829 flags |= GET_OID_QUIETLY;
830 continue;
832 if (opt_with_value(arg, "--short", &arg)) {
833 filter &= ~(DO_FLAGS|DO_NOREV);
834 verify = 1;
835 abbrev = DEFAULT_ABBREV;
836 if (!arg)
837 continue;
838 abbrev = strtoul(arg, NULL, 10);
839 if (abbrev < MINIMUM_ABBREV)
840 abbrev = MINIMUM_ABBREV;
841 else if (hexsz <= abbrev)
842 abbrev = hexsz;
843 continue;
845 if (!strcmp(arg, "--sq")) {
846 output_sq = 1;
847 continue;
849 if (!strcmp(arg, "--not")) {
850 show_type ^= REVERSED;
851 continue;
853 if (!strcmp(arg, "--symbolic")) {
854 symbolic = SHOW_SYMBOLIC_ASIS;
855 continue;
857 if (!strcmp(arg, "--symbolic-full-name")) {
858 symbolic = SHOW_SYMBOLIC_FULL;
859 continue;
861 if (opt_with_value(arg, "--abbrev-ref", &arg)) {
862 abbrev_ref = 1;
863 abbrev_ref_strict = warn_ambiguous_refs;
864 if (arg) {
865 if (!strcmp(arg, "strict"))
866 abbrev_ref_strict = 1;
867 else if (!strcmp(arg, "loose"))
868 abbrev_ref_strict = 0;
869 else
870 die(_("unknown mode for --abbrev-ref: %s"),
871 arg);
873 continue;
875 if (!strcmp(arg, "--all")) {
876 for_each_ref(show_reference, NULL);
877 clear_ref_exclusions(&ref_excludes);
878 continue;
880 if (skip_prefix(arg, "--disambiguate=", &arg)) {
881 repo_for_each_abbrev(the_repository, arg,
882 show_abbrev, NULL);
883 continue;
885 if (!strcmp(arg, "--bisect")) {
886 for_each_fullref_in("refs/bisect/bad", show_reference, NULL);
887 for_each_fullref_in("refs/bisect/good", anti_reference, NULL);
888 continue;
890 if (opt_with_value(arg, "--branches", &arg)) {
891 if (ref_excludes.hidden_refs_configured)
892 return error(_("--exclude-hidden cannot be used together with --branches"));
893 handle_ref_opt(arg, "refs/heads/");
894 continue;
896 if (opt_with_value(arg, "--tags", &arg)) {
897 if (ref_excludes.hidden_refs_configured)
898 return error(_("--exclude-hidden cannot be used together with --tags"));
899 handle_ref_opt(arg, "refs/tags/");
900 continue;
902 if (skip_prefix(arg, "--glob=", &arg)) {
903 handle_ref_opt(arg, NULL);
904 continue;
906 if (opt_with_value(arg, "--remotes", &arg)) {
907 if (ref_excludes.hidden_refs_configured)
908 return error(_("--exclude-hidden cannot be used together with --remotes"));
909 handle_ref_opt(arg, "refs/remotes/");
910 continue;
912 if (skip_prefix(arg, "--exclude=", &arg)) {
913 add_ref_exclusion(&ref_excludes, arg);
914 continue;
916 if (skip_prefix(arg, "--exclude-hidden=", &arg)) {
917 exclude_hidden_refs(&ref_excludes, arg);
918 continue;
920 if (!strcmp(arg, "--show-toplevel")) {
921 const char *work_tree = get_git_work_tree();
922 if (work_tree)
923 print_path(work_tree, prefix, format, DEFAULT_UNMODIFIED);
924 else
925 die(_("this operation must be run in a work tree"));
926 continue;
928 if (!strcmp(arg, "--show-superproject-working-tree")) {
929 struct strbuf superproject = STRBUF_INIT;
930 if (get_superproject_working_tree(&superproject))
931 print_path(superproject.buf, prefix, format, DEFAULT_UNMODIFIED);
932 strbuf_release(&superproject);
933 continue;
935 if (!strcmp(arg, "--show-prefix")) {
936 if (prefix)
937 puts(prefix);
938 else
939 putchar('\n');
940 continue;
942 if (!strcmp(arg, "--show-cdup")) {
943 const char *pfx = prefix;
944 if (!is_inside_work_tree()) {
945 const char *work_tree =
946 get_git_work_tree();
947 if (work_tree)
948 printf("%s\n", work_tree);
949 continue;
951 while (pfx) {
952 pfx = strchr(pfx, '/');
953 if (pfx) {
954 pfx++;
955 printf("../");
958 putchar('\n');
959 continue;
961 if (!strcmp(arg, "--git-dir") ||
962 !strcmp(arg, "--absolute-git-dir")) {
963 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
964 char *cwd;
965 int len;
966 enum format_type wanted = format;
967 if (arg[2] == 'g') { /* --git-dir */
968 if (gitdir) {
969 print_path(gitdir, prefix, format, DEFAULT_UNMODIFIED);
970 continue;
972 if (!prefix) {
973 print_path(".git", prefix, format, DEFAULT_UNMODIFIED);
974 continue;
976 } else { /* --absolute-git-dir */
977 wanted = FORMAT_CANONICAL;
978 if (!gitdir && !prefix)
979 gitdir = ".git";
980 if (gitdir) {
981 struct strbuf realpath = STRBUF_INIT;
982 strbuf_realpath(&realpath, gitdir, 1);
983 puts(realpath.buf);
984 strbuf_release(&realpath);
985 continue;
988 cwd = xgetcwd();
989 len = strlen(cwd);
990 strbuf_reset(&buf);
991 strbuf_addf(&buf, "%s%s.git", cwd, len && cwd[len-1] != '/' ? "/" : "");
992 free(cwd);
993 print_path(buf.buf, prefix, wanted, DEFAULT_CANONICAL);
994 continue;
996 if (!strcmp(arg, "--git-common-dir")) {
997 print_path(get_git_common_dir(), prefix, format, DEFAULT_RELATIVE_IF_SHARED);
998 continue;
1000 if (!strcmp(arg, "--is-inside-git-dir")) {
1001 printf("%s\n", is_inside_git_dir() ? "true"
1002 : "false");
1003 continue;
1005 if (!strcmp(arg, "--is-inside-work-tree")) {
1006 printf("%s\n", is_inside_work_tree() ? "true"
1007 : "false");
1008 continue;
1010 if (!strcmp(arg, "--is-bare-repository")) {
1011 printf("%s\n", is_bare_repository() ? "true"
1012 : "false");
1013 continue;
1015 if (!strcmp(arg, "--is-shallow-repository")) {
1016 printf("%s\n",
1017 is_repository_shallow(the_repository) ? "true"
1018 : "false");
1019 continue;
1021 if (!strcmp(arg, "--shared-index-path")) {
1022 if (repo_read_index(the_repository) < 0)
1023 die(_("Could not read the index"));
1024 if (the_index.split_index) {
1025 const struct object_id *oid = &the_index.split_index->base_oid;
1026 const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
1027 print_path(path, prefix, format, DEFAULT_RELATIVE);
1029 continue;
1031 if (skip_prefix(arg, "--since=", &arg)) {
1032 show_datestring("--max-age=", arg);
1033 continue;
1035 if (skip_prefix(arg, "--after=", &arg)) {
1036 show_datestring("--max-age=", arg);
1037 continue;
1039 if (skip_prefix(arg, "--before=", &arg)) {
1040 show_datestring("--min-age=", arg);
1041 continue;
1043 if (skip_prefix(arg, "--until=", &arg)) {
1044 show_datestring("--min-age=", arg);
1045 continue;
1047 if (opt_with_value(arg, "--show-object-format", &arg)) {
1048 const char *val = arg ? arg : "storage";
1050 if (strcmp(val, "storage") &&
1051 strcmp(val, "input") &&
1052 strcmp(val, "output"))
1053 die(_("unknown mode for --show-object-format: %s"),
1054 arg);
1055 puts(the_hash_algo->name);
1056 continue;
1058 if (!strcmp(arg, "--end-of-options")) {
1059 seen_end_of_options = 1;
1060 if (filter & (DO_FLAGS | DO_REVS))
1061 show_file(arg, 0);
1062 continue;
1064 if (show_flag(arg) && verify)
1065 die_no_single_rev(quiet);
1066 continue;
1069 /* Not a flag argument */
1070 if (try_difference(arg))
1071 continue;
1072 if (try_parent_shorthands(arg))
1073 continue;
1074 name = arg;
1075 type = NORMAL;
1076 if (*arg == '^') {
1077 name++;
1078 type = REVERSED;
1080 if (!get_oid_with_context(the_repository, name,
1081 flags, &oid, &unused)) {
1082 if (verify)
1083 revs_count++;
1084 else
1085 show_rev(type, &oid, name);
1086 continue;
1088 if (verify)
1089 die_no_single_rev(quiet);
1090 if (has_dashdash)
1091 die(_("bad revision '%s'"), arg);
1092 as_is = 1;
1093 if (!show_file(arg, output_prefix))
1094 continue;
1095 verify_filename(prefix, arg, 1);
1097 strbuf_release(&buf);
1098 if (verify) {
1099 if (revs_count == 1) {
1100 show_rev(type, &oid, name);
1101 return 0;
1102 } else if (revs_count == 0 && show_default())
1103 return 0;
1104 die_no_single_rev(quiet);
1105 } else
1106 show_default();
1107 return 0;