hash.h: move some oid-related declarations from cache.h
[git/debian.git] / builtin / rev-parse.c
blobfd4f59ff2b9e7e628d731390f907667b75a9d7bd
1 /*
2 * rev-parse.c
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #define USE_THE_INDEX_VARIABLE
7 #include "cache.h"
8 #include "alloc.h"
9 #include "config.h"
10 #include "commit.h"
11 #include "refs.h"
12 #include "quote.h"
13 #include "builtin.h"
14 #include "parse-options.h"
15 #include "diff.h"
16 #include "revision.h"
17 #include "split-index.h"
18 #include "submodule.h"
19 #include "commit-reach.h"
20 #include "shallow.h"
22 #define DO_REVS 1
23 #define DO_NOREV 2
24 #define DO_FLAGS 4
25 #define DO_NONFLAGS 8
26 static int filter = ~0;
28 static const char *def;
30 #define NORMAL 0
31 #define REVERSED 1
32 static int show_type = NORMAL;
34 #define SHOW_SYMBOLIC_ASIS 1
35 #define SHOW_SYMBOLIC_FULL 2
36 static int symbolic;
37 static int abbrev;
38 static int abbrev_ref;
39 static int abbrev_ref_strict;
40 static int output_sq;
42 static int stuck_long;
43 static struct ref_exclusions ref_excludes = REF_EXCLUSIONS_INIT;
46 * Some arguments are relevant "revision" arguments,
47 * others are about output format or other details.
48 * This sorts it all out.
50 static int is_rev_argument(const char *arg)
52 static const char *rev_args[] = {
53 "--all",
54 "--bisect",
55 "--dense",
56 "--branches=",
57 "--branches",
58 "--header",
59 "--ignore-missing",
60 "--max-age=",
61 "--max-count=",
62 "--min-age=",
63 "--no-merges",
64 "--min-parents=",
65 "--no-min-parents",
66 "--max-parents=",
67 "--no-max-parents",
68 "--objects",
69 "--objects-edge",
70 "--parents",
71 "--pretty",
72 "--remotes=",
73 "--remotes",
74 "--glob=",
75 "--sparse",
76 "--tags=",
77 "--tags",
78 "--topo-order",
79 "--date-order",
80 "--unpacked",
81 NULL
83 const char **p = rev_args;
85 /* accept -<digit>, like traditional "head" */
86 if ((*arg == '-') && isdigit(arg[1]))
87 return 1;
89 for (;;) {
90 const char *str = *p++;
91 int len;
92 if (!str)
93 return 0;
94 len = strlen(str);
95 if (!strcmp(arg, str) ||
96 (str[len-1] == '=' && !strncmp(arg, str, len)))
97 return 1;
101 /* Output argument as a string, either SQ or normal */
102 static void show(const char *arg)
104 if (output_sq) {
105 int sq = '\'', ch;
107 putchar(sq);
108 while ((ch = *arg++)) {
109 if (ch == sq)
110 fputs("'\\'", stdout);
111 putchar(ch);
113 putchar(sq);
114 putchar(' ');
116 else
117 puts(arg);
120 /* Like show(), but with a negation prefix according to type */
121 static void show_with_type(int type, const char *arg)
123 if (type != show_type)
124 putchar('^');
125 show(arg);
128 /* Output a revision, only if filter allows it */
129 static void show_rev(int type, const struct object_id *oid, const char *name)
131 if (!(filter & DO_REVS))
132 return;
133 def = NULL;
135 if ((symbolic || abbrev_ref) && name) {
136 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
137 struct object_id discard;
138 char *full;
140 switch (dwim_ref(name, strlen(name), &discard, &full, 0)) {
141 case 0:
143 * Not found -- not a ref. We could
144 * emit "name" here, but symbolic-full
145 * users are interested in finding the
146 * refs spelled in full, and they would
147 * need to filter non-refs if we did so.
149 break;
150 case 1: /* happy */
151 if (abbrev_ref)
152 full = shorten_unambiguous_ref(full,
153 abbrev_ref_strict);
154 show_with_type(type, full);
155 break;
156 default: /* ambiguous */
157 error("refname '%s' is ambiguous", name);
158 break;
160 free(full);
161 } else {
162 show_with_type(type, name);
165 else if (abbrev)
166 show_with_type(type, find_unique_abbrev(oid, abbrev));
167 else
168 show_with_type(type, oid_to_hex(oid));
171 /* Output a flag, only if filter allows it. */
172 static int show_flag(const char *arg)
174 if (!(filter & DO_FLAGS))
175 return 0;
176 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
177 show(arg);
178 return 1;
180 return 0;
183 static int show_default(void)
185 const char *s = def;
187 if (s) {
188 struct object_id oid;
190 def = NULL;
191 if (!get_oid(s, &oid)) {
192 show_rev(NORMAL, &oid, s);
193 return 1;
196 return 0;
199 static int show_reference(const char *refname, const struct object_id *oid,
200 int flag UNUSED, void *cb_data UNUSED)
202 if (ref_excluded(&ref_excludes, refname))
203 return 0;
204 show_rev(NORMAL, oid, refname);
205 return 0;
208 static int anti_reference(const char *refname, const struct object_id *oid,
209 int flag UNUSED, void *cb_data UNUSED)
211 show_rev(REVERSED, oid, refname);
212 return 0;
215 static int show_abbrev(const struct object_id *oid, void *cb_data)
217 show_rev(NORMAL, oid, NULL);
218 return 0;
221 static void show_datestring(const char *flag, const char *datestr)
223 char *buffer;
225 /* date handling requires both flags and revs */
226 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
227 return;
228 buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr));
229 show(buffer);
230 free(buffer);
233 static int show_file(const char *arg, int output_prefix)
235 show_default();
236 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
237 if (output_prefix) {
238 const char *prefix = startup_info->prefix;
239 char *fname = prefix_filename(prefix, arg);
240 show(fname);
241 free(fname);
242 } else
243 show(arg);
244 return 1;
246 return 0;
249 static int try_difference(const char *arg)
251 char *dotdot;
252 struct object_id start_oid;
253 struct object_id end_oid;
254 const char *end;
255 const char *start;
256 int symmetric;
257 static const char head_by_default[] = "HEAD";
259 if (!(dotdot = strstr(arg, "..")))
260 return 0;
261 end = dotdot + 2;
262 start = arg;
263 symmetric = (*end == '.');
265 *dotdot = 0;
266 end += symmetric;
268 if (!*end)
269 end = head_by_default;
270 if (dotdot == arg)
271 start = head_by_default;
273 if (start == head_by_default && end == head_by_default &&
274 !symmetric) {
276 * Just ".."? That is not a range but the
277 * pathspec for the parent directory.
279 *dotdot = '.';
280 return 0;
283 if (!get_oid_committish(start, &start_oid) && !get_oid_committish(end, &end_oid)) {
284 show_rev(NORMAL, &end_oid, end);
285 show_rev(symmetric ? NORMAL : REVERSED, &start_oid, start);
286 if (symmetric) {
287 struct commit_list *exclude;
288 struct commit *a, *b;
289 a = lookup_commit_reference(the_repository, &start_oid);
290 b = lookup_commit_reference(the_repository, &end_oid);
291 if (!a || !b) {
292 *dotdot = '.';
293 return 0;
295 exclude = get_merge_bases(a, b);
296 while (exclude) {
297 struct commit *commit = pop_commit(&exclude);
298 show_rev(REVERSED, &commit->object.oid, NULL);
301 *dotdot = '.';
302 return 1;
304 *dotdot = '.';
305 return 0;
308 static int try_parent_shorthands(const char *arg)
310 char *dotdot;
311 struct object_id oid;
312 struct commit *commit;
313 struct commit_list *parents;
314 int parent_number;
315 int include_rev = 0;
316 int include_parents = 0;
317 int exclude_parent = 0;
319 if ((dotdot = strstr(arg, "^!"))) {
320 include_rev = 1;
321 if (dotdot[2])
322 return 0;
323 } else if ((dotdot = strstr(arg, "^@"))) {
324 include_parents = 1;
325 if (dotdot[2])
326 return 0;
327 } else if ((dotdot = strstr(arg, "^-"))) {
328 include_rev = 1;
329 exclude_parent = 1;
331 if (dotdot[2]) {
332 char *end;
333 exclude_parent = strtoul(dotdot + 2, &end, 10);
334 if (*end != '\0' || !exclude_parent)
335 return 0;
337 } else
338 return 0;
340 *dotdot = 0;
341 if (get_oid_committish(arg, &oid) ||
342 !(commit = lookup_commit_reference(the_repository, &oid))) {
343 *dotdot = '^';
344 return 0;
347 if (exclude_parent &&
348 exclude_parent > commit_list_count(commit->parents)) {
349 *dotdot = '^';
350 return 0;
353 if (include_rev)
354 show_rev(NORMAL, &oid, arg);
355 for (parents = commit->parents, parent_number = 1;
356 parents;
357 parents = parents->next, parent_number++) {
358 char *name = NULL;
360 if (exclude_parent && parent_number != exclude_parent)
361 continue;
363 if (symbolic)
364 name = xstrfmt("%s^%d", arg, parent_number);
365 show_rev(include_parents ? NORMAL : REVERSED,
366 &parents->item->object.oid, name);
367 free(name);
370 *dotdot = '^';
371 return 1;
374 static int parseopt_dump(const struct option *o, const char *arg, int unset)
376 struct strbuf *parsed = o->value;
377 if (unset)
378 strbuf_addf(parsed, " --no-%s", o->long_name);
379 else if (o->short_name && (o->long_name == NULL || !stuck_long))
380 strbuf_addf(parsed, " -%c", o->short_name);
381 else
382 strbuf_addf(parsed, " --%s", o->long_name);
383 if (arg) {
384 if (!stuck_long)
385 strbuf_addch(parsed, ' ');
386 else if (o->long_name)
387 strbuf_addch(parsed, '=');
388 sq_quote_buf(parsed, arg);
390 return 0;
393 static const char *skipspaces(const char *s)
395 while (isspace(*s))
396 s++;
397 return s;
400 static char *findspace(const char *s)
402 for (; *s; s++)
403 if (isspace(*s))
404 return (char*)s;
405 return NULL;
408 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
410 static int keep_dashdash = 0, stop_at_non_option = 0;
411 static char const * const parseopt_usage[] = {
412 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
413 NULL
415 static struct option parseopt_opts[] = {
416 OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
417 N_("keep the `--` passed as an arg")),
418 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
419 N_("stop parsing after the "
420 "first non-option argument")),
421 OPT_BOOL(0, "stuck-long", &stuck_long,
422 N_("output in stuck long form")),
423 OPT_END(),
425 static const char * const flag_chars = "*=?!";
427 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
428 const char **usage = NULL;
429 struct option *opts = NULL;
430 int onb = 0, osz = 0, unb = 0, usz = 0;
432 strbuf_addstr(&parsed, "set --");
433 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
434 PARSE_OPT_KEEP_DASHDASH);
435 if (argc < 1 || strcmp(argv[0], "--"))
436 usage_with_options(parseopt_usage, parseopt_opts);
438 /* get the usage up to the first line with a -- on it */
439 for (;;) {
440 if (strbuf_getline(&sb, stdin) == EOF)
441 die(_("premature end of input"));
442 ALLOC_GROW(usage, unb + 1, usz);
443 if (!strcmp("--", sb.buf)) {
444 if (unb < 1)
445 die(_("no usage string given before the `--' separator"));
446 usage[unb] = NULL;
447 break;
449 usage[unb++] = strbuf_detach(&sb, NULL);
452 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
453 while (strbuf_getline(&sb, stdin) != EOF) {
454 const char *s;
455 char *help;
456 struct option *o;
458 if (!sb.len)
459 continue;
461 ALLOC_GROW(opts, onb + 1, osz);
462 memset(opts + onb, 0, sizeof(opts[onb]));
464 o = &opts[onb++];
465 help = findspace(sb.buf);
466 if (!help || sb.buf == help) {
467 o->type = OPTION_GROUP;
468 o->help = xstrdup(skipspaces(sb.buf));
469 continue;
472 *help = '\0';
474 o->type = OPTION_CALLBACK;
475 o->help = xstrdup(skipspaces(help+1));
476 o->value = &parsed;
477 o->flags = PARSE_OPT_NOARG;
478 o->callback = &parseopt_dump;
480 /* name(s) */
481 s = strpbrk(sb.buf, flag_chars);
482 if (!s)
483 s = help;
485 if (s == sb.buf)
486 die(_("missing opt-spec before option flags"));
488 if (s - sb.buf == 1) /* short option only */
489 o->short_name = *sb.buf;
490 else if (sb.buf[1] != ',') /* long option only */
491 o->long_name = xmemdupz(sb.buf, s - sb.buf);
492 else {
493 o->short_name = *sb.buf;
494 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
497 /* flags */
498 while (s < help) {
499 switch (*s++) {
500 case '=':
501 o->flags &= ~PARSE_OPT_NOARG;
502 continue;
503 case '?':
504 o->flags &= ~PARSE_OPT_NOARG;
505 o->flags |= PARSE_OPT_OPTARG;
506 continue;
507 case '!':
508 o->flags |= PARSE_OPT_NONEG;
509 continue;
510 case '*':
511 o->flags |= PARSE_OPT_HIDDEN;
512 continue;
514 s--;
515 break;
518 if (s < help)
519 o->argh = xmemdupz(s, help - s);
521 strbuf_release(&sb);
523 /* put an OPT_END() */
524 ALLOC_GROW(opts, onb + 1, osz);
525 memset(opts + onb, 0, sizeof(opts[onb]));
526 argc = parse_options(argc, argv, prefix, opts, usage,
527 (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
528 (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
529 PARSE_OPT_SHELL_EVAL);
531 strbuf_addstr(&parsed, " --");
532 sq_quote_argv(&parsed, argv);
533 puts(parsed.buf);
534 strbuf_release(&parsed);
535 return 0;
538 static int cmd_sq_quote(int argc, const char **argv)
540 struct strbuf buf = STRBUF_INIT;
542 if (argc)
543 sq_quote_argv(&buf, argv);
544 printf("%s\n", buf.buf);
545 strbuf_release(&buf);
547 return 0;
550 static void die_no_single_rev(int quiet)
552 if (quiet)
553 exit(1);
554 else
555 die(_("Needed a single revision"));
558 static const char builtin_rev_parse_usage[] =
559 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
560 " or: git rev-parse --sq-quote [<arg>...]\n"
561 " or: git rev-parse [<options>] [<arg>...]\n"
562 "\n"
563 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
566 * Parse "opt" or "opt=<value>", setting value respectively to either
567 * NULL or the string after "=".
569 static int opt_with_value(const char *arg, const char *opt, const char **value)
571 if (skip_prefix(arg, opt, &arg)) {
572 if (!*arg) {
573 *value = NULL;
574 return 1;
576 if (*arg++ == '=') {
577 *value = arg;
578 return 1;
581 return 0;
584 static void handle_ref_opt(const char *pattern, const char *prefix)
586 if (pattern)
587 for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
588 else
589 for_each_ref_in(prefix, show_reference, NULL);
590 clear_ref_exclusions(&ref_excludes);
593 enum format_type {
594 /* We would like a relative path. */
595 FORMAT_RELATIVE,
596 /* We would like a canonical absolute path. */
597 FORMAT_CANONICAL,
598 /* We would like the default behavior. */
599 FORMAT_DEFAULT,
602 enum default_type {
603 /* Our default is a relative path. */
604 DEFAULT_RELATIVE,
605 /* Our default is a relative path if there's a shared root. */
606 DEFAULT_RELATIVE_IF_SHARED,
607 /* Our default is a canonical absolute path. */
608 DEFAULT_CANONICAL,
609 /* Our default is not to modify the item. */
610 DEFAULT_UNMODIFIED,
613 static void print_path(const char *path, const char *prefix, enum format_type format, enum default_type def)
615 char *cwd = NULL;
617 * We don't ever produce a relative path if prefix is NULL, so set the
618 * prefix to the current directory so that we can produce a relative
619 * path whenever possible. If we're using RELATIVE_IF_SHARED mode, then
620 * we want an absolute path unless the two share a common prefix, so don't
621 * set it in that case, since doing so causes a relative path to always
622 * be produced if possible.
624 if (!prefix && (format != FORMAT_DEFAULT || def != DEFAULT_RELATIVE_IF_SHARED))
625 prefix = cwd = xgetcwd();
626 if (format == FORMAT_DEFAULT && def == DEFAULT_UNMODIFIED) {
627 puts(path);
628 } else if (format == FORMAT_RELATIVE ||
629 (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE)) {
631 * In order for relative_path to work as expected, we need to
632 * make sure that both paths are absolute paths. If we don't,
633 * we can end up with an unexpected absolute path that the user
634 * didn't want.
636 struct strbuf buf = STRBUF_INIT, realbuf = STRBUF_INIT, prefixbuf = STRBUF_INIT;
637 if (!is_absolute_path(path)) {
638 strbuf_realpath_forgiving(&realbuf, path, 1);
639 path = realbuf.buf;
641 if (!is_absolute_path(prefix)) {
642 strbuf_realpath_forgiving(&prefixbuf, prefix, 1);
643 prefix = prefixbuf.buf;
645 puts(relative_path(path, prefix, &buf));
646 strbuf_release(&buf);
647 strbuf_release(&realbuf);
648 strbuf_release(&prefixbuf);
649 } else if (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE_IF_SHARED) {
650 struct strbuf buf = STRBUF_INIT;
651 puts(relative_path(path, prefix, &buf));
652 strbuf_release(&buf);
653 } else {
654 struct strbuf buf = STRBUF_INIT;
655 strbuf_realpath_forgiving(&buf, path, 1);
656 puts(buf.buf);
657 strbuf_release(&buf);
659 free(cwd);
662 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
664 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
665 int did_repo_setup = 0;
666 int has_dashdash = 0;
667 int output_prefix = 0;
668 struct object_id oid;
669 unsigned int flags = 0;
670 const char *name = NULL;
671 struct object_context unused;
672 struct strbuf buf = STRBUF_INIT;
673 const int hexsz = the_hash_algo->hexsz;
674 int seen_end_of_options = 0;
675 enum format_type format = FORMAT_DEFAULT;
677 if (argc > 1 && !strcmp("--parseopt", argv[1]))
678 return cmd_parseopt(argc - 1, argv + 1, prefix);
680 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
681 return cmd_sq_quote(argc - 2, argv + 2);
683 if (argc > 1 && !strcmp("-h", argv[1]))
684 usage(builtin_rev_parse_usage);
686 for (i = 1; i < argc; i++) {
687 if (!strcmp(argv[i], "--")) {
688 has_dashdash = 1;
689 break;
693 /* No options; just report on whether we're in a git repo or not. */
694 if (argc == 1) {
695 setup_git_directory();
696 git_config(git_default_config, NULL);
697 return 0;
700 for (i = 1; i < argc; i++) {
701 const char *arg = argv[i];
703 if (as_is) {
704 if (show_file(arg, output_prefix) && as_is < 2)
705 verify_filename(prefix, arg, 0);
706 continue;
709 if (!seen_end_of_options) {
710 if (!strcmp(arg, "--local-env-vars")) {
711 int i;
712 for (i = 0; local_repo_env[i]; i++)
713 printf("%s\n", local_repo_env[i]);
714 continue;
716 if (!strcmp(arg, "--resolve-git-dir")) {
717 const char *gitdir = argv[++i];
718 if (!gitdir)
719 die(_("--resolve-git-dir requires an argument"));
720 gitdir = resolve_gitdir(gitdir);
721 if (!gitdir)
722 die(_("not a gitdir '%s'"), argv[i]);
723 puts(gitdir);
724 continue;
728 /* The rest of the options require a git repository. */
729 if (!did_repo_setup) {
730 prefix = setup_git_directory();
731 git_config(git_default_config, NULL);
732 did_repo_setup = 1;
734 prepare_repo_settings(the_repository);
735 the_repository->settings.command_requires_full_index = 0;
738 if (!strcmp(arg, "--")) {
739 as_is = 2;
740 /* Pass on the "--" if we show anything but files.. */
741 if (filter & (DO_FLAGS | DO_REVS))
742 show_file(arg, 0);
743 continue;
746 if (!seen_end_of_options && *arg == '-') {
747 if (!strcmp(arg, "--git-path")) {
748 if (!argv[i + 1])
749 die(_("--git-path requires an argument"));
750 strbuf_reset(&buf);
751 print_path(git_path("%s", argv[i + 1]), prefix,
752 format,
753 DEFAULT_RELATIVE_IF_SHARED);
754 i++;
755 continue;
757 if (!strcmp(arg,"-n")) {
758 if (++i >= argc)
759 die(_("-n requires an argument"));
760 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
761 show(arg);
762 show(argv[i]);
764 continue;
766 if (starts_with(arg, "-n")) {
767 if ((filter & DO_FLAGS) && (filter & DO_REVS))
768 show(arg);
769 continue;
771 if (opt_with_value(arg, "--path-format", &arg)) {
772 if (!arg)
773 die(_("--path-format requires an argument"));
774 if (!strcmp(arg, "absolute")) {
775 format = FORMAT_CANONICAL;
776 } else if (!strcmp(arg, "relative")) {
777 format = FORMAT_RELATIVE;
778 } else {
779 die(_("unknown argument to --path-format: %s"), arg);
781 continue;
783 if (!strcmp(arg, "--default")) {
784 def = argv[++i];
785 if (!def)
786 die(_("--default requires an argument"));
787 continue;
789 if (!strcmp(arg, "--prefix")) {
790 prefix = argv[++i];
791 if (!prefix)
792 die(_("--prefix requires an argument"));
793 startup_info->prefix = prefix;
794 output_prefix = 1;
795 continue;
797 if (!strcmp(arg, "--revs-only")) {
798 filter &= ~DO_NOREV;
799 continue;
801 if (!strcmp(arg, "--no-revs")) {
802 filter &= ~DO_REVS;
803 continue;
805 if (!strcmp(arg, "--flags")) {
806 filter &= ~DO_NONFLAGS;
807 continue;
809 if (!strcmp(arg, "--no-flags")) {
810 filter &= ~DO_FLAGS;
811 continue;
813 if (!strcmp(arg, "--verify")) {
814 filter &= ~(DO_FLAGS|DO_NOREV);
815 verify = 1;
816 continue;
818 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
819 quiet = 1;
820 flags |= GET_OID_QUIETLY;
821 continue;
823 if (opt_with_value(arg, "--short", &arg)) {
824 filter &= ~(DO_FLAGS|DO_NOREV);
825 verify = 1;
826 abbrev = DEFAULT_ABBREV;
827 if (!arg)
828 continue;
829 abbrev = strtoul(arg, NULL, 10);
830 if (abbrev < MINIMUM_ABBREV)
831 abbrev = MINIMUM_ABBREV;
832 else if (hexsz <= abbrev)
833 abbrev = hexsz;
834 continue;
836 if (!strcmp(arg, "--sq")) {
837 output_sq = 1;
838 continue;
840 if (!strcmp(arg, "--not")) {
841 show_type ^= REVERSED;
842 continue;
844 if (!strcmp(arg, "--symbolic")) {
845 symbolic = SHOW_SYMBOLIC_ASIS;
846 continue;
848 if (!strcmp(arg, "--symbolic-full-name")) {
849 symbolic = SHOW_SYMBOLIC_FULL;
850 continue;
852 if (opt_with_value(arg, "--abbrev-ref", &arg)) {
853 abbrev_ref = 1;
854 abbrev_ref_strict = warn_ambiguous_refs;
855 if (arg) {
856 if (!strcmp(arg, "strict"))
857 abbrev_ref_strict = 1;
858 else if (!strcmp(arg, "loose"))
859 abbrev_ref_strict = 0;
860 else
861 die(_("unknown mode for --abbrev-ref: %s"),
862 arg);
864 continue;
866 if (!strcmp(arg, "--all")) {
867 for_each_ref(show_reference, NULL);
868 clear_ref_exclusions(&ref_excludes);
869 continue;
871 if (skip_prefix(arg, "--disambiguate=", &arg)) {
872 for_each_abbrev(arg, show_abbrev, NULL);
873 continue;
875 if (!strcmp(arg, "--bisect")) {
876 for_each_fullref_in("refs/bisect/bad", show_reference, NULL);
877 for_each_fullref_in("refs/bisect/good", anti_reference, NULL);
878 continue;
880 if (opt_with_value(arg, "--branches", &arg)) {
881 if (ref_excludes.hidden_refs_configured)
882 return error(_("--exclude-hidden cannot be used together with --branches"));
883 handle_ref_opt(arg, "refs/heads/");
884 continue;
886 if (opt_with_value(arg, "--tags", &arg)) {
887 if (ref_excludes.hidden_refs_configured)
888 return error(_("--exclude-hidden cannot be used together with --tags"));
889 handle_ref_opt(arg, "refs/tags/");
890 continue;
892 if (skip_prefix(arg, "--glob=", &arg)) {
893 handle_ref_opt(arg, NULL);
894 continue;
896 if (opt_with_value(arg, "--remotes", &arg)) {
897 if (ref_excludes.hidden_refs_configured)
898 return error(_("--exclude-hidden cannot be used together with --remotes"));
899 handle_ref_opt(arg, "refs/remotes/");
900 continue;
902 if (skip_prefix(arg, "--exclude=", &arg)) {
903 add_ref_exclusion(&ref_excludes, arg);
904 continue;
906 if (skip_prefix(arg, "--exclude-hidden=", &arg)) {
907 exclude_hidden_refs(&ref_excludes, arg);
908 continue;
910 if (!strcmp(arg, "--show-toplevel")) {
911 const char *work_tree = get_git_work_tree();
912 if (work_tree)
913 print_path(work_tree, prefix, format, DEFAULT_UNMODIFIED);
914 else
915 die(_("this operation must be run in a work tree"));
916 continue;
918 if (!strcmp(arg, "--show-superproject-working-tree")) {
919 struct strbuf superproject = STRBUF_INIT;
920 if (get_superproject_working_tree(&superproject))
921 print_path(superproject.buf, prefix, format, DEFAULT_UNMODIFIED);
922 strbuf_release(&superproject);
923 continue;
925 if (!strcmp(arg, "--show-prefix")) {
926 if (prefix)
927 puts(prefix);
928 else
929 putchar('\n');
930 continue;
932 if (!strcmp(arg, "--show-cdup")) {
933 const char *pfx = prefix;
934 if (!is_inside_work_tree()) {
935 const char *work_tree =
936 get_git_work_tree();
937 if (work_tree)
938 printf("%s\n", work_tree);
939 continue;
941 while (pfx) {
942 pfx = strchr(pfx, '/');
943 if (pfx) {
944 pfx++;
945 printf("../");
948 putchar('\n');
949 continue;
951 if (!strcmp(arg, "--git-dir") ||
952 !strcmp(arg, "--absolute-git-dir")) {
953 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
954 char *cwd;
955 int len;
956 enum format_type wanted = format;
957 if (arg[2] == 'g') { /* --git-dir */
958 if (gitdir) {
959 print_path(gitdir, prefix, format, DEFAULT_UNMODIFIED);
960 continue;
962 if (!prefix) {
963 print_path(".git", prefix, format, DEFAULT_UNMODIFIED);
964 continue;
966 } else { /* --absolute-git-dir */
967 wanted = FORMAT_CANONICAL;
968 if (!gitdir && !prefix)
969 gitdir = ".git";
970 if (gitdir) {
971 struct strbuf realpath = STRBUF_INIT;
972 strbuf_realpath(&realpath, gitdir, 1);
973 puts(realpath.buf);
974 strbuf_release(&realpath);
975 continue;
978 cwd = xgetcwd();
979 len = strlen(cwd);
980 strbuf_reset(&buf);
981 strbuf_addf(&buf, "%s%s.git", cwd, len && cwd[len-1] != '/' ? "/" : "");
982 free(cwd);
983 print_path(buf.buf, prefix, wanted, DEFAULT_CANONICAL);
984 continue;
986 if (!strcmp(arg, "--git-common-dir")) {
987 print_path(get_git_common_dir(), prefix, format, DEFAULT_RELATIVE_IF_SHARED);
988 continue;
990 if (!strcmp(arg, "--is-inside-git-dir")) {
991 printf("%s\n", is_inside_git_dir() ? "true"
992 : "false");
993 continue;
995 if (!strcmp(arg, "--is-inside-work-tree")) {
996 printf("%s\n", is_inside_work_tree() ? "true"
997 : "false");
998 continue;
1000 if (!strcmp(arg, "--is-bare-repository")) {
1001 printf("%s\n", is_bare_repository() ? "true"
1002 : "false");
1003 continue;
1005 if (!strcmp(arg, "--is-shallow-repository")) {
1006 printf("%s\n",
1007 is_repository_shallow(the_repository) ? "true"
1008 : "false");
1009 continue;
1011 if (!strcmp(arg, "--shared-index-path")) {
1012 if (repo_read_index(the_repository) < 0)
1013 die(_("Could not read the index"));
1014 if (the_index.split_index) {
1015 const struct object_id *oid = &the_index.split_index->base_oid;
1016 const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
1017 print_path(path, prefix, format, DEFAULT_RELATIVE);
1019 continue;
1021 if (skip_prefix(arg, "--since=", &arg)) {
1022 show_datestring("--max-age=", arg);
1023 continue;
1025 if (skip_prefix(arg, "--after=", &arg)) {
1026 show_datestring("--max-age=", arg);
1027 continue;
1029 if (skip_prefix(arg, "--before=", &arg)) {
1030 show_datestring("--min-age=", arg);
1031 continue;
1033 if (skip_prefix(arg, "--until=", &arg)) {
1034 show_datestring("--min-age=", arg);
1035 continue;
1037 if (opt_with_value(arg, "--show-object-format", &arg)) {
1038 const char *val = arg ? arg : "storage";
1040 if (strcmp(val, "storage") &&
1041 strcmp(val, "input") &&
1042 strcmp(val, "output"))
1043 die(_("unknown mode for --show-object-format: %s"),
1044 arg);
1045 puts(the_hash_algo->name);
1046 continue;
1048 if (!strcmp(arg, "--end-of-options")) {
1049 seen_end_of_options = 1;
1050 if (filter & (DO_FLAGS | DO_REVS))
1051 show_file(arg, 0);
1052 continue;
1054 if (show_flag(arg) && verify)
1055 die_no_single_rev(quiet);
1056 continue;
1059 /* Not a flag argument */
1060 if (try_difference(arg))
1061 continue;
1062 if (try_parent_shorthands(arg))
1063 continue;
1064 name = arg;
1065 type = NORMAL;
1066 if (*arg == '^') {
1067 name++;
1068 type = REVERSED;
1070 if (!get_oid_with_context(the_repository, name,
1071 flags, &oid, &unused)) {
1072 if (verify)
1073 revs_count++;
1074 else
1075 show_rev(type, &oid, name);
1076 continue;
1078 if (verify)
1079 die_no_single_rev(quiet);
1080 if (has_dashdash)
1081 die(_("bad revision '%s'"), arg);
1082 as_is = 1;
1083 if (!show_file(arg, output_prefix))
1084 continue;
1085 verify_filename(prefix, arg, 1);
1087 strbuf_release(&buf);
1088 if (verify) {
1089 if (revs_count == 1) {
1090 show_rev(type, &oid, name);
1091 return 0;
1092 } else if (revs_count == 0 && show_default())
1093 return 0;
1094 die_no_single_rev(quiet);
1095 } else
1096 show_default();
1097 return 0;