reftable/merged: handle subiter cleanup on close only
[alt-git.git] / builtin / rev-parse.c
blobd08987646a0a533ad81480ae4667b577a335f9f7
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 "config.h"
10 #include "commit.h"
11 #include "environment.h"
12 #include "gettext.h"
13 #include "hash.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 "path.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 char *old = full;
162 full = shorten_unambiguous_ref(full,
163 abbrev_ref_strict);
164 free(old);
166 show_with_type(type, full);
167 break;
168 default: /* ambiguous */
169 error("refname '%s' is ambiguous", name);
170 break;
172 free(full);
173 } else {
174 show_with_type(type, name);
177 else if (abbrev)
178 show_with_type(type,
179 repo_find_unique_abbrev(the_repository, oid, abbrev));
180 else
181 show_with_type(type, oid_to_hex(oid));
184 /* Output a flag, only if filter allows it. */
185 static int show_flag(const char *arg)
187 if (!(filter & DO_FLAGS))
188 return 0;
189 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
190 show(arg);
191 return 1;
193 return 0;
196 static int show_default(void)
198 const char *s = def;
200 if (s) {
201 struct object_id oid;
203 def = NULL;
204 if (!repo_get_oid(the_repository, s, &oid)) {
205 show_rev(NORMAL, &oid, s);
206 return 1;
209 return 0;
212 static int show_reference(const char *refname, const struct object_id *oid,
213 int flag UNUSED, void *cb_data UNUSED)
215 if (ref_excluded(&ref_excludes, refname))
216 return 0;
217 show_rev(NORMAL, oid, refname);
218 return 0;
221 static int anti_reference(const char *refname, const struct object_id *oid,
222 int flag UNUSED, void *cb_data UNUSED)
224 show_rev(REVERSED, oid, refname);
225 return 0;
228 static int show_abbrev(const struct object_id *oid, void *cb_data UNUSED)
230 show_rev(NORMAL, oid, NULL);
231 return 0;
234 static void show_datestring(const char *flag, const char *datestr)
236 char *buffer;
238 /* date handling requires both flags and revs */
239 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
240 return;
241 buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr));
242 show(buffer);
243 free(buffer);
246 static int show_file(const char *arg, int output_prefix)
248 show_default();
249 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
250 if (output_prefix) {
251 const char *prefix = startup_info->prefix;
252 char *fname = prefix_filename(prefix, arg);
253 show(fname);
254 free(fname);
255 } else
256 show(arg);
257 return 1;
259 return 0;
262 static int try_difference(const char *arg)
264 char *dotdot;
265 struct object_id start_oid;
266 struct object_id end_oid;
267 const char *end;
268 const char *start;
269 int symmetric;
270 static const char head_by_default[] = "HEAD";
272 if (!(dotdot = strstr(arg, "..")))
273 return 0;
274 end = dotdot + 2;
275 start = arg;
276 symmetric = (*end == '.');
278 *dotdot = 0;
279 end += symmetric;
281 if (!*end)
282 end = head_by_default;
283 if (dotdot == arg)
284 start = head_by_default;
286 if (start == head_by_default && end == head_by_default &&
287 !symmetric) {
289 * Just ".."? That is not a range but the
290 * pathspec for the parent directory.
292 *dotdot = '.';
293 return 0;
296 if (!repo_get_oid_committish(the_repository, start, &start_oid) && !repo_get_oid_committish(the_repository, end, &end_oid)) {
297 show_rev(NORMAL, &end_oid, end);
298 show_rev(symmetric ? NORMAL : REVERSED, &start_oid, start);
299 if (symmetric) {
300 struct commit_list *exclude;
301 struct commit *a, *b;
302 a = lookup_commit_reference(the_repository, &start_oid);
303 b = lookup_commit_reference(the_repository, &end_oid);
304 if (!a || !b) {
305 *dotdot = '.';
306 return 0;
308 exclude = repo_get_merge_bases(the_repository, a, b);
309 while (exclude) {
310 struct commit *commit = pop_commit(&exclude);
311 show_rev(REVERSED, &commit->object.oid, NULL);
314 *dotdot = '.';
315 return 1;
317 *dotdot = '.';
318 return 0;
321 static int try_parent_shorthands(const char *arg)
323 char *dotdot;
324 struct object_id oid;
325 struct commit *commit;
326 struct commit_list *parents;
327 int parent_number;
328 int include_rev = 0;
329 int include_parents = 0;
330 int exclude_parent = 0;
332 if ((dotdot = strstr(arg, "^!"))) {
333 include_rev = 1;
334 if (dotdot[2])
335 return 0;
336 } else if ((dotdot = strstr(arg, "^@"))) {
337 include_parents = 1;
338 if (dotdot[2])
339 return 0;
340 } else if ((dotdot = strstr(arg, "^-"))) {
341 include_rev = 1;
342 exclude_parent = 1;
344 if (dotdot[2]) {
345 char *end;
346 exclude_parent = strtoul(dotdot + 2, &end, 10);
347 if (*end != '\0' || !exclude_parent)
348 return 0;
350 } else
351 return 0;
353 *dotdot = 0;
354 if (repo_get_oid_committish(the_repository, arg, &oid) ||
355 !(commit = lookup_commit_reference(the_repository, &oid))) {
356 *dotdot = '^';
357 return 0;
360 if (exclude_parent &&
361 exclude_parent > commit_list_count(commit->parents)) {
362 *dotdot = '^';
363 return 0;
366 if (include_rev)
367 show_rev(NORMAL, &oid, arg);
368 for (parents = commit->parents, parent_number = 1;
369 parents;
370 parents = parents->next, parent_number++) {
371 char *name = NULL;
373 if (exclude_parent && parent_number != exclude_parent)
374 continue;
376 if (symbolic)
377 name = xstrfmt("%s^%d", arg, parent_number);
378 show_rev(include_parents ? NORMAL : REVERSED,
379 &parents->item->object.oid, name);
380 free(name);
383 *dotdot = '^';
384 return 1;
387 static int parseopt_dump(const struct option *o, const char *arg, int unset)
389 struct strbuf *parsed = o->value;
390 if (unset)
391 strbuf_addf(parsed, " --no-%s", o->long_name);
392 else if (o->short_name && (o->long_name == NULL || !stuck_long))
393 strbuf_addf(parsed, " -%c", o->short_name);
394 else
395 strbuf_addf(parsed, " --%s", o->long_name);
396 if (arg) {
397 if (!stuck_long)
398 strbuf_addch(parsed, ' ');
399 else if (o->long_name)
400 strbuf_addch(parsed, '=');
401 sq_quote_buf(parsed, arg);
403 return 0;
406 static const char *skipspaces(const char *s)
408 while (isspace(*s))
409 s++;
410 return s;
413 static char *findspace(const char *s)
415 for (; *s; s++)
416 if (isspace(*s))
417 return (char*)s;
418 return NULL;
421 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
423 static int keep_dashdash = 0, stop_at_non_option = 0;
424 static char const * const parseopt_usage[] = {
425 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
426 NULL
428 static struct option parseopt_opts[] = {
429 OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
430 N_("keep the `--` passed as an arg")),
431 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
432 N_("stop parsing after the "
433 "first non-option argument")),
434 OPT_BOOL(0, "stuck-long", &stuck_long,
435 N_("output in stuck long form")),
436 OPT_END(),
438 static const char * const flag_chars = "*=?!";
440 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
441 const char **usage = NULL;
442 struct option *opts = NULL;
443 int onb = 0, osz = 0, unb = 0, usz = 0;
445 strbuf_addstr(&parsed, "set --");
446 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
447 PARSE_OPT_KEEP_DASHDASH);
448 if (argc < 1 || strcmp(argv[0], "--"))
449 usage_with_options(parseopt_usage, parseopt_opts);
451 /* get the usage up to the first line with a -- on it */
452 for (;;) {
453 if (strbuf_getline(&sb, stdin) == EOF)
454 die(_("premature end of input"));
455 ALLOC_GROW(usage, unb + 1, usz);
456 if (!strcmp("--", sb.buf)) {
457 if (unb < 1)
458 die(_("no usage string given before the `--' separator"));
459 usage[unb] = NULL;
460 break;
462 usage[unb++] = strbuf_detach(&sb, NULL);
465 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
466 while (strbuf_getline(&sb, stdin) != EOF) {
467 const char *s;
468 char *help;
469 struct option *o;
471 if (!sb.len)
472 continue;
474 ALLOC_GROW(opts, onb + 1, osz);
475 memset(opts + onb, 0, sizeof(opts[onb]));
477 o = &opts[onb++];
478 help = findspace(sb.buf);
479 if (!help || sb.buf == help) {
480 o->type = OPTION_GROUP;
481 o->help = xstrdup(skipspaces(sb.buf));
482 continue;
485 *help = '\0';
487 o->type = OPTION_CALLBACK;
488 o->help = xstrdup(skipspaces(help+1));
489 o->value = &parsed;
490 o->flags = PARSE_OPT_NOARG;
491 o->callback = &parseopt_dump;
493 /* name(s) */
494 s = strpbrk(sb.buf, flag_chars);
495 if (!s)
496 s = help;
498 if (s == sb.buf)
499 die(_("missing opt-spec before option flags"));
501 if (s - sb.buf == 1) /* short option only */
502 o->short_name = *sb.buf;
503 else if (sb.buf[1] != ',') /* long option only */
504 o->long_name = xmemdupz(sb.buf, s - sb.buf);
505 else {
506 o->short_name = *sb.buf;
507 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
510 /* flags */
511 while (s < help) {
512 switch (*s++) {
513 case '=':
514 o->flags &= ~PARSE_OPT_NOARG;
515 continue;
516 case '?':
517 o->flags &= ~PARSE_OPT_NOARG;
518 o->flags |= PARSE_OPT_OPTARG;
519 continue;
520 case '!':
521 o->flags |= PARSE_OPT_NONEG;
522 continue;
523 case '*':
524 o->flags |= PARSE_OPT_HIDDEN;
525 continue;
527 s--;
528 break;
531 if (s < help)
532 o->argh = xmemdupz(s, help - s);
534 strbuf_release(&sb);
536 /* put an OPT_END() */
537 ALLOC_GROW(opts, onb + 1, osz);
538 memset(opts + onb, 0, sizeof(opts[onb]));
539 argc = parse_options(argc, argv, prefix, opts, usage,
540 (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
541 (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
542 PARSE_OPT_SHELL_EVAL);
544 strbuf_addstr(&parsed, " --");
545 sq_quote_argv(&parsed, argv);
546 puts(parsed.buf);
547 strbuf_release(&parsed);
548 return 0;
551 static int cmd_sq_quote(int argc, const char **argv)
553 struct strbuf buf = STRBUF_INIT;
555 if (argc)
556 sq_quote_argv(&buf, argv);
557 printf("%s\n", buf.buf);
558 strbuf_release(&buf);
560 return 0;
563 static void die_no_single_rev(int quiet)
565 if (quiet)
566 exit(1);
567 else
568 die(_("Needed a single revision"));
571 static const char builtin_rev_parse_usage[] =
572 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
573 " or: git rev-parse --sq-quote [<arg>...]\n"
574 " or: git rev-parse [<options>] [<arg>...]\n"
575 "\n"
576 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
579 * Parse "opt" or "opt=<value>", setting value respectively to either
580 * NULL or the string after "=".
582 static int opt_with_value(const char *arg, const char *opt, const char **value)
584 if (skip_prefix(arg, opt, &arg)) {
585 if (!*arg) {
586 *value = NULL;
587 return 1;
589 if (*arg++ == '=') {
590 *value = arg;
591 return 1;
594 return 0;
597 static void handle_ref_opt(const char *pattern, const char *prefix)
599 if (pattern)
600 for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
601 else
602 for_each_ref_in(prefix, show_reference, NULL);
603 clear_ref_exclusions(&ref_excludes);
606 enum format_type {
607 /* We would like a relative path. */
608 FORMAT_RELATIVE,
609 /* We would like a canonical absolute path. */
610 FORMAT_CANONICAL,
611 /* We would like the default behavior. */
612 FORMAT_DEFAULT,
615 enum default_type {
616 /* Our default is a relative path. */
617 DEFAULT_RELATIVE,
618 /* Our default is a relative path if there's a shared root. */
619 DEFAULT_RELATIVE_IF_SHARED,
620 /* Our default is a canonical absolute path. */
621 DEFAULT_CANONICAL,
622 /* Our default is not to modify the item. */
623 DEFAULT_UNMODIFIED,
626 static void print_path(const char *path, const char *prefix, enum format_type format, enum default_type def)
628 char *cwd = NULL;
630 * We don't ever produce a relative path if prefix is NULL, so set the
631 * prefix to the current directory so that we can produce a relative
632 * path whenever possible. If we're using RELATIVE_IF_SHARED mode, then
633 * we want an absolute path unless the two share a common prefix, so don't
634 * set it in that case, since doing so causes a relative path to always
635 * be produced if possible.
637 if (!prefix && (format != FORMAT_DEFAULT || def != DEFAULT_RELATIVE_IF_SHARED))
638 prefix = cwd = xgetcwd();
639 if (format == FORMAT_DEFAULT && def == DEFAULT_UNMODIFIED) {
640 puts(path);
641 } else if (format == FORMAT_RELATIVE ||
642 (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE)) {
644 * In order for relative_path to work as expected, we need to
645 * make sure that both paths are absolute paths. If we don't,
646 * we can end up with an unexpected absolute path that the user
647 * didn't want.
649 struct strbuf buf = STRBUF_INIT, realbuf = STRBUF_INIT, prefixbuf = STRBUF_INIT;
650 if (!is_absolute_path(path)) {
651 strbuf_realpath_forgiving(&realbuf, path, 1);
652 path = realbuf.buf;
654 if (!is_absolute_path(prefix)) {
655 strbuf_realpath_forgiving(&prefixbuf, prefix, 1);
656 prefix = prefixbuf.buf;
658 puts(relative_path(path, prefix, &buf));
659 strbuf_release(&buf);
660 strbuf_release(&realbuf);
661 strbuf_release(&prefixbuf);
662 } else if (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE_IF_SHARED) {
663 struct strbuf buf = STRBUF_INIT;
664 puts(relative_path(path, prefix, &buf));
665 strbuf_release(&buf);
666 } else {
667 struct strbuf buf = STRBUF_INIT;
668 strbuf_realpath_forgiving(&buf, path, 1);
669 puts(buf.buf);
670 strbuf_release(&buf);
672 free(cwd);
675 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
677 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
678 int did_repo_setup = 0;
679 int has_dashdash = 0;
680 int output_prefix = 0;
681 struct object_id oid;
682 unsigned int flags = 0;
683 const char *name = NULL;
684 struct object_context unused;
685 struct strbuf buf = STRBUF_INIT;
686 const int hexsz = the_hash_algo->hexsz;
687 int seen_end_of_options = 0;
688 enum format_type format = FORMAT_DEFAULT;
690 if (argc > 1 && !strcmp("--parseopt", argv[1]))
691 return cmd_parseopt(argc - 1, argv + 1, prefix);
693 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
694 return cmd_sq_quote(argc - 2, argv + 2);
696 if (argc > 1 && !strcmp("-h", argv[1]))
697 usage(builtin_rev_parse_usage);
699 for (i = 1; i < argc; i++) {
700 if (!strcmp(argv[i], "--")) {
701 has_dashdash = 1;
702 break;
706 /* No options; just report on whether we're in a git repo or not. */
707 if (argc == 1) {
708 setup_git_directory();
709 git_config(git_default_config, NULL);
710 return 0;
713 for (i = 1; i < argc; i++) {
714 const char *arg = argv[i];
716 if (as_is) {
717 if (show_file(arg, output_prefix) && as_is < 2)
718 verify_filename(prefix, arg, 0);
719 continue;
722 if (!seen_end_of_options) {
723 if (!strcmp(arg, "--local-env-vars")) {
724 int i;
725 for (i = 0; local_repo_env[i]; i++)
726 printf("%s\n", local_repo_env[i]);
727 continue;
729 if (!strcmp(arg, "--resolve-git-dir")) {
730 const char *gitdir = argv[++i];
731 if (!gitdir)
732 die(_("--resolve-git-dir requires an argument"));
733 gitdir = resolve_gitdir(gitdir);
734 if (!gitdir)
735 die(_("not a gitdir '%s'"), argv[i]);
736 puts(gitdir);
737 continue;
741 /* The rest of the options require a git repository. */
742 if (!did_repo_setup) {
743 prefix = setup_git_directory();
744 git_config(git_default_config, NULL);
745 did_repo_setup = 1;
747 prepare_repo_settings(the_repository);
748 the_repository->settings.command_requires_full_index = 0;
751 if (!strcmp(arg, "--")) {
752 as_is = 2;
753 /* Pass on the "--" if we show anything but files.. */
754 if (filter & (DO_FLAGS | DO_REVS))
755 show_file(arg, 0);
756 continue;
759 if (!seen_end_of_options && *arg == '-') {
760 if (!strcmp(arg, "--git-path")) {
761 if (!argv[i + 1])
762 die(_("--git-path requires an argument"));
763 strbuf_reset(&buf);
764 print_path(git_path("%s", argv[i + 1]), prefix,
765 format,
766 DEFAULT_RELATIVE_IF_SHARED);
767 i++;
768 continue;
770 if (!strcmp(arg,"-n")) {
771 if (++i >= argc)
772 die(_("-n requires an argument"));
773 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
774 show(arg);
775 show(argv[i]);
777 continue;
779 if (starts_with(arg, "-n")) {
780 if ((filter & DO_FLAGS) && (filter & DO_REVS))
781 show(arg);
782 continue;
784 if (opt_with_value(arg, "--path-format", &arg)) {
785 if (!arg)
786 die(_("--path-format requires an argument"));
787 if (!strcmp(arg, "absolute")) {
788 format = FORMAT_CANONICAL;
789 } else if (!strcmp(arg, "relative")) {
790 format = FORMAT_RELATIVE;
791 } else {
792 die(_("unknown argument to --path-format: %s"), arg);
794 continue;
796 if (!strcmp(arg, "--default")) {
797 def = argv[++i];
798 if (!def)
799 die(_("--default requires an argument"));
800 continue;
802 if (!strcmp(arg, "--prefix")) {
803 prefix = argv[++i];
804 if (!prefix)
805 die(_("--prefix requires an argument"));
806 startup_info->prefix = prefix;
807 output_prefix = 1;
808 continue;
810 if (!strcmp(arg, "--revs-only")) {
811 filter &= ~DO_NOREV;
812 continue;
814 if (!strcmp(arg, "--no-revs")) {
815 filter &= ~DO_REVS;
816 continue;
818 if (!strcmp(arg, "--flags")) {
819 filter &= ~DO_NONFLAGS;
820 continue;
822 if (!strcmp(arg, "--no-flags")) {
823 filter &= ~DO_FLAGS;
824 continue;
826 if (!strcmp(arg, "--verify")) {
827 filter &= ~(DO_FLAGS|DO_NOREV);
828 verify = 1;
829 continue;
831 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
832 quiet = 1;
833 flags |= GET_OID_QUIETLY;
834 continue;
836 if (opt_with_value(arg, "--short", &arg)) {
837 filter &= ~(DO_FLAGS|DO_NOREV);
838 verify = 1;
839 abbrev = DEFAULT_ABBREV;
840 if (!arg)
841 continue;
842 abbrev = strtoul(arg, NULL, 10);
843 if (abbrev < MINIMUM_ABBREV)
844 abbrev = MINIMUM_ABBREV;
845 else if (hexsz <= abbrev)
846 abbrev = hexsz;
847 continue;
849 if (!strcmp(arg, "--sq")) {
850 output_sq = 1;
851 continue;
853 if (!strcmp(arg, "--not")) {
854 show_type ^= REVERSED;
855 continue;
857 if (!strcmp(arg, "--symbolic")) {
858 symbolic = SHOW_SYMBOLIC_ASIS;
859 continue;
861 if (!strcmp(arg, "--symbolic-full-name")) {
862 symbolic = SHOW_SYMBOLIC_FULL;
863 continue;
865 if (opt_with_value(arg, "--abbrev-ref", &arg)) {
866 abbrev_ref = 1;
867 abbrev_ref_strict = warn_ambiguous_refs;
868 if (arg) {
869 if (!strcmp(arg, "strict"))
870 abbrev_ref_strict = 1;
871 else if (!strcmp(arg, "loose"))
872 abbrev_ref_strict = 0;
873 else
874 die(_("unknown mode for --abbrev-ref: %s"),
875 arg);
877 continue;
879 if (!strcmp(arg, "--all")) {
880 for_each_ref(show_reference, NULL);
881 clear_ref_exclusions(&ref_excludes);
882 continue;
884 if (skip_prefix(arg, "--disambiguate=", &arg)) {
885 repo_for_each_abbrev(the_repository, arg,
886 show_abbrev, NULL);
887 continue;
889 if (!strcmp(arg, "--bisect")) {
890 for_each_fullref_in("refs/bisect/bad", show_reference, NULL);
891 for_each_fullref_in("refs/bisect/good", anti_reference, NULL);
892 continue;
894 if (opt_with_value(arg, "--branches", &arg)) {
895 if (ref_excludes.hidden_refs_configured)
896 return error(_("options '%s' and '%s' cannot be used together"),
897 "--exclude-hidden", "--branches");
898 handle_ref_opt(arg, "refs/heads/");
899 continue;
901 if (opt_with_value(arg, "--tags", &arg)) {
902 if (ref_excludes.hidden_refs_configured)
903 return error(_("options '%s' and '%s' cannot be used together"),
904 "--exclude-hidden", "--tags");
905 handle_ref_opt(arg, "refs/tags/");
906 continue;
908 if (skip_prefix(arg, "--glob=", &arg)) {
909 handle_ref_opt(arg, NULL);
910 continue;
912 if (opt_with_value(arg, "--remotes", &arg)) {
913 if (ref_excludes.hidden_refs_configured)
914 return error(_("options '%s' and '%s' cannot be used together"),
915 "--exclude-hidden", "--remotes");
916 handle_ref_opt(arg, "refs/remotes/");
917 continue;
919 if (skip_prefix(arg, "--exclude=", &arg)) {
920 add_ref_exclusion(&ref_excludes, arg);
921 continue;
923 if (skip_prefix(arg, "--exclude-hidden=", &arg)) {
924 exclude_hidden_refs(&ref_excludes, arg);
925 continue;
927 if (!strcmp(arg, "--show-toplevel")) {
928 const char *work_tree = get_git_work_tree();
929 if (work_tree)
930 print_path(work_tree, prefix, format, DEFAULT_UNMODIFIED);
931 else
932 die(_("this operation must be run in a work tree"));
933 continue;
935 if (!strcmp(arg, "--show-superproject-working-tree")) {
936 struct strbuf superproject = STRBUF_INIT;
937 if (get_superproject_working_tree(&superproject))
938 print_path(superproject.buf, prefix, format, DEFAULT_UNMODIFIED);
939 strbuf_release(&superproject);
940 continue;
942 if (!strcmp(arg, "--show-prefix")) {
943 if (prefix)
944 puts(prefix);
945 else
946 putchar('\n');
947 continue;
949 if (!strcmp(arg, "--show-cdup")) {
950 const char *pfx = prefix;
951 if (!is_inside_work_tree()) {
952 const char *work_tree =
953 get_git_work_tree();
954 if (work_tree)
955 printf("%s\n", work_tree);
956 continue;
958 while (pfx) {
959 pfx = strchr(pfx, '/');
960 if (pfx) {
961 pfx++;
962 printf("../");
965 putchar('\n');
966 continue;
968 if (!strcmp(arg, "--git-dir") ||
969 !strcmp(arg, "--absolute-git-dir")) {
970 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
971 char *cwd;
972 int len;
973 enum format_type wanted = format;
974 if (arg[2] == 'g') { /* --git-dir */
975 if (gitdir) {
976 print_path(gitdir, prefix, format, DEFAULT_UNMODIFIED);
977 continue;
979 if (!prefix) {
980 print_path(".git", prefix, format, DEFAULT_UNMODIFIED);
981 continue;
983 } else { /* --absolute-git-dir */
984 wanted = FORMAT_CANONICAL;
985 if (!gitdir && !prefix)
986 gitdir = ".git";
987 if (gitdir) {
988 struct strbuf realpath = STRBUF_INIT;
989 strbuf_realpath(&realpath, gitdir, 1);
990 puts(realpath.buf);
991 strbuf_release(&realpath);
992 continue;
995 cwd = xgetcwd();
996 len = strlen(cwd);
997 strbuf_reset(&buf);
998 strbuf_addf(&buf, "%s%s.git", cwd, len && cwd[len-1] != '/' ? "/" : "");
999 free(cwd);
1000 print_path(buf.buf, prefix, wanted, DEFAULT_CANONICAL);
1001 continue;
1003 if (!strcmp(arg, "--git-common-dir")) {
1004 print_path(get_git_common_dir(), prefix, format, DEFAULT_RELATIVE_IF_SHARED);
1005 continue;
1007 if (!strcmp(arg, "--is-inside-git-dir")) {
1008 printf("%s\n", is_inside_git_dir() ? "true"
1009 : "false");
1010 continue;
1012 if (!strcmp(arg, "--is-inside-work-tree")) {
1013 printf("%s\n", is_inside_work_tree() ? "true"
1014 : "false");
1015 continue;
1017 if (!strcmp(arg, "--is-bare-repository")) {
1018 printf("%s\n", is_bare_repository() ? "true"
1019 : "false");
1020 continue;
1022 if (!strcmp(arg, "--is-shallow-repository")) {
1023 printf("%s\n",
1024 is_repository_shallow(the_repository) ? "true"
1025 : "false");
1026 continue;
1028 if (!strcmp(arg, "--shared-index-path")) {
1029 if (repo_read_index(the_repository) < 0)
1030 die(_("Could not read the index"));
1031 if (the_index.split_index) {
1032 const struct object_id *oid = &the_index.split_index->base_oid;
1033 const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
1034 print_path(path, prefix, format, DEFAULT_RELATIVE);
1036 continue;
1038 if (skip_prefix(arg, "--since=", &arg)) {
1039 show_datestring("--max-age=", arg);
1040 continue;
1042 if (skip_prefix(arg, "--after=", &arg)) {
1043 show_datestring("--max-age=", arg);
1044 continue;
1046 if (skip_prefix(arg, "--before=", &arg)) {
1047 show_datestring("--min-age=", arg);
1048 continue;
1050 if (skip_prefix(arg, "--until=", &arg)) {
1051 show_datestring("--min-age=", arg);
1052 continue;
1054 if (opt_with_value(arg, "--show-object-format", &arg)) {
1055 const char *val = arg ? arg : "storage";
1057 if (strcmp(val, "storage") &&
1058 strcmp(val, "input") &&
1059 strcmp(val, "output"))
1060 die(_("unknown mode for --show-object-format: %s"),
1061 arg);
1062 puts(the_hash_algo->name);
1063 continue;
1065 if (!strcmp(arg, "--show-ref-format")) {
1066 puts(ref_storage_format_to_name(the_repository->ref_storage_format));
1067 continue;
1069 if (!strcmp(arg, "--end-of-options")) {
1070 seen_end_of_options = 1;
1071 if (filter & (DO_FLAGS | DO_REVS))
1072 show_file(arg, 0);
1073 continue;
1075 if (show_flag(arg) && verify)
1076 die_no_single_rev(quiet);
1077 continue;
1080 /* Not a flag argument */
1081 if (try_difference(arg))
1082 continue;
1083 if (try_parent_shorthands(arg))
1084 continue;
1085 name = arg;
1086 type = NORMAL;
1087 if (*arg == '^') {
1088 name++;
1089 type = REVERSED;
1091 if (!get_oid_with_context(the_repository, name,
1092 flags, &oid, &unused)) {
1093 if (verify)
1094 revs_count++;
1095 else
1096 show_rev(type, &oid, name);
1097 continue;
1099 if (verify)
1100 die_no_single_rev(quiet);
1101 if (has_dashdash)
1102 die(_("bad revision '%s'"), arg);
1103 as_is = 1;
1104 if (!show_file(arg, output_prefix))
1105 continue;
1106 verify_filename(prefix, arg, 1);
1108 strbuf_release(&buf);
1109 if (verify) {
1110 if (revs_count == 1) {
1111 show_rev(type, &oid, name);
1112 return 0;
1113 } else if (revs_count == 0 && show_default())
1114 return 0;
1115 die_no_single_rev(quiet);
1116 } else
1117 show_default();
1118 return 0;