The eleventh batch
[git/gitster.git] / builtin / rev-parse.c
blob8401b4d7ab64f06b55fb055899d2eb36dc3810a2
1 /*
2 * rev-parse.c
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #define USE_THE_REPOSITORY_VARIABLE
7 #include "builtin.h"
9 #include "abspath.h"
10 #include "config.h"
11 #include "commit.h"
12 #include "environment.h"
13 #include "gettext.h"
14 #include "hash.h"
15 #include "hex.h"
16 #include "refs.h"
17 #include "quote.h"
18 #include "object-name.h"
19 #include "parse-options.h"
20 #include "path.h"
21 #include "diff.h"
22 #include "read-cache-ll.h"
23 #include "repo-settings.h"
24 #include "repository.h"
25 #include "revision.h"
26 #include "setup.h"
27 #include "split-index.h"
28 #include "submodule.h"
29 #include "commit-reach.h"
30 #include "shallow.h"
31 #include "object-file-convert.h"
33 #define DO_REVS 1
34 #define DO_NOREV 2
35 #define DO_FLAGS 4
36 #define DO_NONFLAGS 8
37 static int filter = ~0;
39 static const char *def;
41 #define NORMAL 0
42 #define REVERSED 1
43 static int show_type = NORMAL;
45 #define SHOW_SYMBOLIC_ASIS 1
46 #define SHOW_SYMBOLIC_FULL 2
47 static int symbolic;
48 static int abbrev;
49 static int abbrev_ref;
50 static int abbrev_ref_strict;
51 static int output_sq;
53 static int stuck_long;
54 static struct ref_exclusions ref_excludes = REF_EXCLUSIONS_INIT;
57 * Some arguments are relevant "revision" arguments,
58 * others are about output format or other details.
59 * This sorts it all out.
61 static int is_rev_argument(const char *arg)
63 static const char *rev_args[] = {
64 "--all",
65 "--bisect",
66 "--dense",
67 "--branches=",
68 "--branches",
69 "--header",
70 "--ignore-missing",
71 "--max-age=",
72 "--max-count=",
73 "--min-age=",
74 "--no-merges",
75 "--min-parents=",
76 "--no-min-parents",
77 "--max-parents=",
78 "--no-max-parents",
79 "--objects",
80 "--objects-edge",
81 "--parents",
82 "--pretty",
83 "--remotes=",
84 "--remotes",
85 "--glob=",
86 "--sparse",
87 "--tags=",
88 "--tags",
89 "--topo-order",
90 "--date-order",
91 "--unpacked",
92 NULL
94 const char **p = rev_args;
96 /* accept -<digit>, like traditional "head" */
97 if ((*arg == '-') && isdigit(arg[1]))
98 return 1;
100 for (;;) {
101 const char *str = *p++;
102 int len;
103 if (!str)
104 return 0;
105 len = strlen(str);
106 if (!strcmp(arg, str) ||
107 (str[len-1] == '=' && !strncmp(arg, str, len)))
108 return 1;
112 /* Output argument as a string, either SQ or normal */
113 static void show(const char *arg)
115 if (output_sq) {
116 int sq = '\'', ch;
118 putchar(sq);
119 while ((ch = *arg++)) {
120 if (ch == sq)
121 fputs("'\\'", stdout);
122 putchar(ch);
124 putchar(sq);
125 putchar(' ');
127 else
128 puts(arg);
131 /* Like show(), but with a negation prefix according to type */
132 static void show_with_type(int type, const char *arg)
134 if (type != show_type)
135 putchar('^');
136 show(arg);
139 /* Output a revision, only if filter allows it */
140 static void show_rev(int type, const struct object_id *oid, const char *name)
142 if (!(filter & DO_REVS))
143 return;
144 def = NULL;
146 if ((symbolic || abbrev_ref) && name) {
147 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
148 struct object_id discard;
149 char *full;
151 switch (repo_dwim_ref(the_repository, name,
152 strlen(name), &discard, &full,
153 0)) {
154 case 0:
156 * Not found -- not a ref. We could
157 * emit "name" here, but symbolic-full
158 * users are interested in finding the
159 * refs spelled in full, and they would
160 * need to filter non-refs if we did so.
162 break;
163 case 1: /* happy */
164 if (abbrev_ref) {
165 char *old = full;
166 full = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),
167 full,
168 abbrev_ref_strict);
169 free(old);
171 show_with_type(type, full);
172 break;
173 default: /* ambiguous */
174 error("refname '%s' is ambiguous", name);
175 break;
177 free(full);
178 } else {
179 show_with_type(type, name);
182 else if (abbrev)
183 show_with_type(type,
184 repo_find_unique_abbrev(the_repository, oid, abbrev));
185 else
186 show_with_type(type, oid_to_hex(oid));
189 /* Output a flag, only if filter allows it. */
190 static int show_flag(const char *arg)
192 if (!(filter & DO_FLAGS))
193 return 0;
194 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
195 show(arg);
196 return 1;
198 return 0;
201 static int show_default(void)
203 const char *s = def;
205 if (s) {
206 struct object_id oid;
208 def = NULL;
209 if (!repo_get_oid(the_repository, s, &oid)) {
210 show_rev(NORMAL, &oid, s);
211 return 1;
214 return 0;
217 static int show_reference(const char *refname, const char *referent UNUSED, const struct object_id *oid,
218 int flag UNUSED, void *cb_data UNUSED)
220 if (ref_excluded(&ref_excludes, refname))
221 return 0;
222 show_rev(NORMAL, oid, refname);
223 return 0;
226 static int anti_reference(const char *refname, const char *referent UNUSED, const struct object_id *oid,
227 int flag UNUSED, void *cb_data UNUSED)
229 show_rev(REVERSED, oid, refname);
230 return 0;
233 static int show_abbrev(const struct object_id *oid, void *cb_data UNUSED)
235 show_rev(NORMAL, oid, NULL);
236 return 0;
239 static void show_datestring(const char *flag, const char *datestr)
241 char *buffer;
243 /* date handling requires both flags and revs */
244 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
245 return;
246 buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr));
247 show(buffer);
248 free(buffer);
251 static int show_file(const char *arg, int output_prefix)
253 show_default();
254 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
255 if (output_prefix) {
256 const char *prefix = startup_info->prefix;
257 char *fname = prefix_filename(prefix, arg);
258 show(fname);
259 free(fname);
260 } else
261 show(arg);
262 return 1;
264 return 0;
267 static int try_difference(const char *arg)
269 char *dotdot;
270 struct object_id start_oid;
271 struct object_id end_oid;
272 const char *end;
273 const char *start;
274 int symmetric;
275 static const char head_by_default[] = "HEAD";
277 if (!(dotdot = strstr(arg, "..")))
278 return 0;
279 end = dotdot + 2;
280 start = arg;
281 symmetric = (*end == '.');
283 *dotdot = 0;
284 end += symmetric;
286 if (!*end)
287 end = head_by_default;
288 if (dotdot == arg)
289 start = head_by_default;
291 if (start == head_by_default && end == head_by_default &&
292 !symmetric) {
294 * Just ".."? That is not a range but the
295 * pathspec for the parent directory.
297 *dotdot = '.';
298 return 0;
301 if (!repo_get_oid_committish(the_repository, start, &start_oid) && !repo_get_oid_committish(the_repository, end, &end_oid)) {
302 show_rev(NORMAL, &end_oid, end);
303 show_rev(symmetric ? NORMAL : REVERSED, &start_oid, start);
304 if (symmetric) {
305 struct commit_list *exclude = NULL;
306 struct commit *a, *b;
307 a = lookup_commit_reference(the_repository, &start_oid);
308 b = lookup_commit_reference(the_repository, &end_oid);
309 if (!a || !b) {
310 *dotdot = '.';
311 return 0;
313 if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0)
314 exit(128);
315 while (exclude) {
316 struct commit *commit = pop_commit(&exclude);
317 show_rev(REVERSED, &commit->object.oid, NULL);
320 *dotdot = '.';
321 return 1;
323 *dotdot = '.';
324 return 0;
327 static int try_parent_shorthands(const char *arg)
329 char *dotdot;
330 struct object_id oid;
331 struct commit *commit;
332 struct commit_list *parents;
333 int parent_number;
334 int include_rev = 0;
335 int include_parents = 0;
336 int exclude_parent = 0;
338 if ((dotdot = strstr(arg, "^!"))) {
339 include_rev = 1;
340 if (dotdot[2])
341 return 0;
342 } else if ((dotdot = strstr(arg, "^@"))) {
343 include_parents = 1;
344 if (dotdot[2])
345 return 0;
346 } else if ((dotdot = strstr(arg, "^-"))) {
347 include_rev = 1;
348 exclude_parent = 1;
350 if (dotdot[2]) {
351 char *end;
352 exclude_parent = strtoul(dotdot + 2, &end, 10);
353 if (*end != '\0' || !exclude_parent)
354 return 0;
356 } else
357 return 0;
359 *dotdot = 0;
360 if (repo_get_oid_committish(the_repository, arg, &oid) ||
361 !(commit = lookup_commit_reference(the_repository, &oid))) {
362 *dotdot = '^';
363 return 0;
366 if (exclude_parent &&
367 exclude_parent > commit_list_count(commit->parents)) {
368 *dotdot = '^';
369 return 0;
372 if (include_rev)
373 show_rev(NORMAL, &oid, arg);
374 for (parents = commit->parents, parent_number = 1;
375 parents;
376 parents = parents->next, parent_number++) {
377 char *name = NULL;
379 if (exclude_parent && parent_number != exclude_parent)
380 continue;
382 if (symbolic)
383 name = xstrfmt("%s^%d", arg, parent_number);
384 show_rev(include_parents ? NORMAL : REVERSED,
385 &parents->item->object.oid, name);
386 free(name);
389 *dotdot = '^';
390 return 1;
393 static int parseopt_dump(const struct option *o, const char *arg, int unset)
395 struct strbuf *parsed = o->value;
396 if (unset)
397 strbuf_addf(parsed, " --no-%s", o->long_name);
398 else if (o->short_name && (o->long_name == NULL || !stuck_long))
399 strbuf_addf(parsed, " -%c", o->short_name);
400 else
401 strbuf_addf(parsed, " --%s", o->long_name);
402 if (arg) {
403 if (!stuck_long)
404 strbuf_addch(parsed, ' ');
405 else if (o->long_name)
406 strbuf_addch(parsed, '=');
407 sq_quote_buf(parsed, arg);
409 return 0;
412 static const char *skipspaces(const char *s)
414 while (isspace(*s))
415 s++;
416 return s;
419 static char *findspace(const char *s)
421 for (; *s; s++)
422 if (isspace(*s))
423 return (char*)s;
424 return NULL;
427 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
429 int keep_dashdash = 0, stop_at_non_option = 0;
430 char const * const parseopt_usage[] = {
431 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
432 NULL
434 struct option parseopt_opts[] = {
435 OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
436 N_("keep the `--` passed as an arg")),
437 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
438 N_("stop parsing after the "
439 "first non-option argument")),
440 OPT_BOOL(0, "stuck-long", &stuck_long,
441 N_("output in stuck long form")),
442 OPT_END(),
444 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
445 struct strvec longnames = STRVEC_INIT;
446 struct strvec usage = STRVEC_INIT;
447 struct option *opts = NULL;
448 size_t opts_nr = 0, opts_alloc = 0;
450 strbuf_addstr(&parsed, "set --");
451 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
452 PARSE_OPT_KEEP_DASHDASH);
453 if (argc < 1 || strcmp(argv[0], "--"))
454 usage_with_options(parseopt_usage, parseopt_opts);
456 /* get the usage up to the first line with a -- on it */
457 for (;;) {
458 strbuf_reset(&sb);
459 if (strbuf_getline(&sb, stdin) == EOF)
460 die(_("premature end of input"));
461 if (!strcmp("--", sb.buf)) {
462 if (!usage.nr)
463 die(_("no usage string given before the `--' separator"));
464 break;
467 strvec_push(&usage, sb.buf);
470 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
471 while (strbuf_getline(&sb, stdin) != EOF) {
472 const char *s;
473 char *help;
474 struct option *o;
476 if (!sb.len)
477 continue;
479 ALLOC_GROW(opts, opts_nr + 1, opts_alloc);
480 memset(opts + opts_nr, 0, sizeof(*opts));
482 o = &opts[opts_nr++];
483 help = findspace(sb.buf);
484 if (!help || sb.buf == help) {
485 o->type = OPTION_GROUP;
486 o->help = xstrdup(skipspaces(sb.buf));
487 continue;
490 *help = '\0';
492 o->type = OPTION_CALLBACK;
493 o->help = xstrdup(skipspaces(help+1));
494 o->value = &parsed;
495 o->flags = PARSE_OPT_NOARG;
496 o->callback = &parseopt_dump;
498 /* name(s) */
499 s = strpbrk(sb.buf, "*=?!");
500 if (!s)
501 s = help;
503 if (s == sb.buf)
504 die(_("missing opt-spec before option flags"));
506 if (s - sb.buf == 1) { /* short option only */
507 o->short_name = *sb.buf;
508 } else if (sb.buf[1] != ',') { /* long option only */
509 o->long_name = strvec_pushf(&longnames, "%.*s",
510 (int)(s - sb.buf), sb.buf);
511 } else {
512 o->short_name = *sb.buf;
513 o->long_name = strvec_pushf(&longnames, "%.*s",
514 (int)(s - sb.buf - 2), sb.buf + 2);
517 /* flags */
518 while (s < help) {
519 switch (*s++) {
520 case '=':
521 o->flags &= ~PARSE_OPT_NOARG;
522 continue;
523 case '?':
524 o->flags &= ~PARSE_OPT_NOARG;
525 o->flags |= PARSE_OPT_OPTARG;
526 continue;
527 case '!':
528 o->flags |= PARSE_OPT_NONEG;
529 continue;
530 case '*':
531 o->flags |= PARSE_OPT_HIDDEN;
532 continue;
534 s--;
535 break;
538 if (s < help)
539 o->argh = xmemdupz(s, help - s);
541 strbuf_release(&sb);
543 /* put an OPT_END() */
544 ALLOC_GROW(opts, opts_nr + 1, opts_alloc);
545 memset(opts + opts_nr, 0, sizeof(*opts));
546 argc = parse_options(argc, argv, prefix, opts, usage.v,
547 (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
548 (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
549 PARSE_OPT_SHELL_EVAL);
551 strbuf_addstr(&parsed, " --");
552 sq_quote_argv(&parsed, argv);
553 puts(parsed.buf);
555 strbuf_release(&parsed);
556 strbuf_release(&sb);
557 strvec_clear(&longnames);
558 strvec_clear(&usage);
559 for (size_t i = 0; i < opts_nr; i++) {
560 free((char *) opts[i].help);
561 free((char *) opts[i].argh);
563 free(opts);
564 return 0;
567 static int cmd_sq_quote(int argc, const char **argv)
569 struct strbuf buf = STRBUF_INIT;
571 if (argc)
572 sq_quote_argv(&buf, argv);
573 printf("%s\n", buf.buf);
574 strbuf_release(&buf);
576 return 0;
579 static void die_no_single_rev(int quiet)
581 if (quiet)
582 exit(1);
583 else
584 die(_("Needed a single revision"));
587 static const char builtin_rev_parse_usage[] =
588 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
589 " or: git rev-parse --sq-quote [<arg>...]\n"
590 " or: git rev-parse [<options>] [<arg>...]\n"
591 "\n"
592 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
595 * Parse "opt" or "opt=<value>", setting value respectively to either
596 * NULL or the string after "=".
598 static int opt_with_value(const char *arg, const char *opt, const char **value)
600 if (skip_prefix(arg, opt, &arg)) {
601 if (!*arg) {
602 *value = NULL;
603 return 1;
605 if (*arg++ == '=') {
606 *value = arg;
607 return 1;
610 return 0;
613 static void handle_ref_opt(const char *pattern, const char *prefix)
615 if (pattern)
616 refs_for_each_glob_ref_in(get_main_ref_store(the_repository),
617 show_reference, pattern, prefix,
618 NULL);
619 else
620 refs_for_each_ref_in(get_main_ref_store(the_repository),
621 prefix, show_reference, NULL);
622 clear_ref_exclusions(&ref_excludes);
625 enum format_type {
626 /* We would like a relative path. */
627 FORMAT_RELATIVE,
628 /* We would like a canonical absolute path. */
629 FORMAT_CANONICAL,
630 /* We would like the default behavior. */
631 FORMAT_DEFAULT,
634 enum default_type {
635 /* Our default is a relative path. */
636 DEFAULT_RELATIVE,
637 /* Our default is a relative path if there's a shared root. */
638 DEFAULT_RELATIVE_IF_SHARED,
639 /* Our default is a canonical absolute path. */
640 DEFAULT_CANONICAL,
641 /* Our default is not to modify the item. */
642 DEFAULT_UNMODIFIED,
645 static void print_path(const char *path, const char *prefix, enum format_type format, enum default_type def)
647 char *cwd = NULL;
649 * We don't ever produce a relative path if prefix is NULL, so set the
650 * prefix to the current directory so that we can produce a relative
651 * path whenever possible. If we're using RELATIVE_IF_SHARED mode, then
652 * we want an absolute path unless the two share a common prefix, so don't
653 * set it in that case, since doing so causes a relative path to always
654 * be produced if possible.
656 if (!prefix && (format != FORMAT_DEFAULT || def != DEFAULT_RELATIVE_IF_SHARED))
657 prefix = cwd = xgetcwd();
658 if (format == FORMAT_DEFAULT && def == DEFAULT_UNMODIFIED) {
659 puts(path);
660 } else if (format == FORMAT_RELATIVE ||
661 (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE)) {
663 * In order for relative_path to work as expected, we need to
664 * make sure that both paths are absolute paths. If we don't,
665 * we can end up with an unexpected absolute path that the user
666 * didn't want.
668 struct strbuf buf = STRBUF_INIT, realbuf = STRBUF_INIT, prefixbuf = STRBUF_INIT;
669 if (!is_absolute_path(path)) {
670 strbuf_realpath_forgiving(&realbuf, path, 1);
671 path = realbuf.buf;
673 if (!is_absolute_path(prefix)) {
674 strbuf_realpath_forgiving(&prefixbuf, prefix, 1);
675 prefix = prefixbuf.buf;
677 puts(relative_path(path, prefix, &buf));
678 strbuf_release(&buf);
679 strbuf_release(&realbuf);
680 strbuf_release(&prefixbuf);
681 } else if (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE_IF_SHARED) {
682 struct strbuf buf = STRBUF_INIT;
683 puts(relative_path(path, prefix, &buf));
684 strbuf_release(&buf);
685 } else {
686 struct strbuf buf = STRBUF_INIT;
687 strbuf_realpath_forgiving(&buf, path, 1);
688 puts(buf.buf);
689 strbuf_release(&buf);
691 free(cwd);
694 int cmd_rev_parse(int argc,
695 const char **argv,
696 const char *prefix,
697 struct repository *repo UNUSED)
699 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
700 const struct git_hash_algo *output_algo = NULL;
701 const struct git_hash_algo *compat = NULL;
702 int did_repo_setup = 0;
703 int has_dashdash = 0;
704 int output_prefix = 0;
705 struct object_id oid;
706 unsigned int flags = 0;
707 const char *name = NULL;
708 struct object_context unused;
709 struct strbuf buf = STRBUF_INIT;
710 int seen_end_of_options = 0;
711 enum format_type format = FORMAT_DEFAULT;
713 if (argc > 1 && !strcmp("--parseopt", argv[1]))
714 return cmd_parseopt(argc - 1, argv + 1, prefix);
716 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
717 return cmd_sq_quote(argc - 2, argv + 2);
719 if (argc > 1 && !strcmp("-h", argv[1]))
720 usage(builtin_rev_parse_usage);
722 for (i = 1; i < argc; i++) {
723 if (!strcmp(argv[i], "--")) {
724 has_dashdash = 1;
725 break;
729 /* No options; just report on whether we're in a git repo or not. */
730 if (argc == 1) {
731 setup_git_directory();
732 git_config(git_default_config, NULL);
733 return 0;
736 for (i = 1; i < argc; i++) {
737 const char *arg = argv[i];
739 if (as_is) {
740 if (show_file(arg, output_prefix) && as_is < 2)
741 verify_filename(prefix, arg, 0);
742 continue;
745 if (!seen_end_of_options) {
746 if (!strcmp(arg, "--local-env-vars")) {
747 int i;
748 for (i = 0; local_repo_env[i]; i++)
749 printf("%s\n", local_repo_env[i]);
750 continue;
752 if (!strcmp(arg, "--resolve-git-dir")) {
753 const char *gitdir = argv[++i];
754 if (!gitdir)
755 die(_("--resolve-git-dir requires an argument"));
756 gitdir = resolve_gitdir(gitdir);
757 if (!gitdir)
758 die(_("not a gitdir '%s'"), argv[i]);
759 puts(gitdir);
760 continue;
764 /* The rest of the options require a git repository. */
765 if (!did_repo_setup) {
766 prefix = setup_git_directory();
767 git_config(git_default_config, NULL);
768 did_repo_setup = 1;
770 prepare_repo_settings(the_repository);
771 the_repository->settings.command_requires_full_index = 0;
772 compat = the_repository->compat_hash_algo;
775 if (!strcmp(arg, "--")) {
776 as_is = 2;
777 /* Pass on the "--" if we show anything but files.. */
778 if (filter & (DO_FLAGS | DO_REVS))
779 show_file(arg, 0);
780 continue;
783 if (!seen_end_of_options && *arg == '-') {
784 if (!strcmp(arg, "--git-path")) {
785 if (!argv[i + 1])
786 die(_("--git-path requires an argument"));
787 strbuf_reset(&buf);
788 print_path(git_path("%s", argv[i + 1]), prefix,
789 format,
790 DEFAULT_RELATIVE_IF_SHARED);
791 i++;
792 continue;
794 if (!strcmp(arg,"-n")) {
795 if (++i >= argc)
796 die(_("-n requires an argument"));
797 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
798 show(arg);
799 show(argv[i]);
801 continue;
803 if (starts_with(arg, "-n")) {
804 if ((filter & DO_FLAGS) && (filter & DO_REVS))
805 show(arg);
806 continue;
808 if (opt_with_value(arg, "--path-format", &arg)) {
809 if (!arg)
810 die(_("--path-format requires an argument"));
811 if (!strcmp(arg, "absolute")) {
812 format = FORMAT_CANONICAL;
813 } else if (!strcmp(arg, "relative")) {
814 format = FORMAT_RELATIVE;
815 } else {
816 die(_("unknown argument to --path-format: %s"), arg);
818 continue;
820 if (!strcmp(arg, "--default")) {
821 def = argv[++i];
822 if (!def)
823 die(_("--default requires an argument"));
824 continue;
826 if (!strcmp(arg, "--prefix")) {
827 prefix = argv[++i];
828 if (!prefix)
829 die(_("--prefix requires an argument"));
830 startup_info->prefix = prefix;
831 output_prefix = 1;
832 continue;
834 if (!strcmp(arg, "--revs-only")) {
835 filter &= ~DO_NOREV;
836 continue;
838 if (!strcmp(arg, "--no-revs")) {
839 filter &= ~DO_REVS;
840 continue;
842 if (!strcmp(arg, "--flags")) {
843 filter &= ~DO_NONFLAGS;
844 continue;
846 if (!strcmp(arg, "--no-flags")) {
847 filter &= ~DO_FLAGS;
848 continue;
850 if (!strcmp(arg, "--verify")) {
851 filter &= ~(DO_FLAGS|DO_NOREV);
852 verify = 1;
853 continue;
855 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
856 quiet = 1;
857 flags |= GET_OID_QUIETLY;
858 continue;
860 if (opt_with_value(arg, "--output-object-format", &arg)) {
861 if (!arg)
862 die(_("no object format specified"));
863 if (!strcmp(arg, the_hash_algo->name) ||
864 !strcmp(arg, "storage")) {
865 flags |= GET_OID_HASH_ANY;
866 output_algo = the_hash_algo;
867 continue;
869 else if (compat && !strcmp(arg, compat->name)) {
870 flags |= GET_OID_HASH_ANY;
871 output_algo = compat;
872 continue;
874 else die(_("unsupported object format: %s"), arg);
876 if (opt_with_value(arg, "--short", &arg)) {
877 filter &= ~(DO_FLAGS|DO_NOREV);
878 verify = 1;
879 abbrev = DEFAULT_ABBREV;
880 if (!arg)
881 continue;
882 abbrev = strtoul(arg, NULL, 10);
883 if (abbrev < MINIMUM_ABBREV)
884 abbrev = MINIMUM_ABBREV;
885 else if ((int)the_hash_algo->hexsz <= abbrev)
886 abbrev = the_hash_algo->hexsz;
887 continue;
889 if (!strcmp(arg, "--sq")) {
890 output_sq = 1;
891 continue;
893 if (!strcmp(arg, "--not")) {
894 show_type ^= REVERSED;
895 continue;
897 if (!strcmp(arg, "--symbolic")) {
898 symbolic = SHOW_SYMBOLIC_ASIS;
899 continue;
901 if (!strcmp(arg, "--symbolic-full-name")) {
902 symbolic = SHOW_SYMBOLIC_FULL;
903 continue;
905 if (opt_with_value(arg, "--abbrev-ref", &arg)) {
906 abbrev_ref = 1;
907 abbrev_ref_strict =
908 repo_settings_get_warn_ambiguous_refs(the_repository);
909 if (arg) {
910 if (!strcmp(arg, "strict"))
911 abbrev_ref_strict = 1;
912 else if (!strcmp(arg, "loose"))
913 abbrev_ref_strict = 0;
914 else
915 die(_("unknown mode for --abbrev-ref: %s"),
916 arg);
918 continue;
920 if (!strcmp(arg, "--all")) {
921 refs_for_each_ref(get_main_ref_store(the_repository),
922 show_reference, NULL);
923 clear_ref_exclusions(&ref_excludes);
924 continue;
926 if (skip_prefix(arg, "--disambiguate=", &arg)) {
927 repo_for_each_abbrev(the_repository, arg, the_hash_algo,
928 show_abbrev, NULL);
929 continue;
931 if (!strcmp(arg, "--bisect")) {
932 refs_for_each_fullref_in(get_main_ref_store(the_repository),
933 "refs/bisect/bad",
934 NULL, show_reference,
935 NULL);
936 refs_for_each_fullref_in(get_main_ref_store(the_repository),
937 "refs/bisect/good",
938 NULL, anti_reference,
939 NULL);
940 continue;
942 if (opt_with_value(arg, "--branches", &arg)) {
943 if (ref_excludes.hidden_refs_configured)
944 return error(_("options '%s' and '%s' cannot be used together"),
945 "--exclude-hidden", "--branches");
946 handle_ref_opt(arg, "refs/heads/");
947 continue;
949 if (opt_with_value(arg, "--tags", &arg)) {
950 if (ref_excludes.hidden_refs_configured)
951 return error(_("options '%s' and '%s' cannot be used together"),
952 "--exclude-hidden", "--tags");
953 handle_ref_opt(arg, "refs/tags/");
954 continue;
956 if (skip_prefix(arg, "--glob=", &arg)) {
957 handle_ref_opt(arg, NULL);
958 continue;
960 if (opt_with_value(arg, "--remotes", &arg)) {
961 if (ref_excludes.hidden_refs_configured)
962 return error(_("options '%s' and '%s' cannot be used together"),
963 "--exclude-hidden", "--remotes");
964 handle_ref_opt(arg, "refs/remotes/");
965 continue;
967 if (skip_prefix(arg, "--exclude=", &arg)) {
968 add_ref_exclusion(&ref_excludes, arg);
969 continue;
971 if (skip_prefix(arg, "--exclude-hidden=", &arg)) {
972 exclude_hidden_refs(&ref_excludes, arg);
973 continue;
975 if (!strcmp(arg, "--show-toplevel")) {
976 const char *work_tree = repo_get_work_tree(the_repository);
977 if (work_tree)
978 print_path(work_tree, prefix, format, DEFAULT_UNMODIFIED);
979 else
980 die(_("this operation must be run in a work tree"));
981 continue;
983 if (!strcmp(arg, "--show-superproject-working-tree")) {
984 struct strbuf superproject = STRBUF_INIT;
985 if (get_superproject_working_tree(&superproject))
986 print_path(superproject.buf, prefix, format, DEFAULT_UNMODIFIED);
987 strbuf_release(&superproject);
988 continue;
990 if (!strcmp(arg, "--show-prefix")) {
991 if (prefix)
992 puts(prefix);
993 else
994 putchar('\n');
995 continue;
997 if (!strcmp(arg, "--show-cdup")) {
998 const char *pfx = prefix;
999 if (!is_inside_work_tree()) {
1000 const char *work_tree =
1001 repo_get_work_tree(the_repository);
1002 if (work_tree)
1003 printf("%s\n", work_tree);
1004 continue;
1006 while (pfx) {
1007 pfx = strchr(pfx, '/');
1008 if (pfx) {
1009 pfx++;
1010 printf("../");
1013 putchar('\n');
1014 continue;
1016 if (!strcmp(arg, "--git-dir") ||
1017 !strcmp(arg, "--absolute-git-dir")) {
1018 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
1019 char *cwd;
1020 int len;
1021 enum format_type wanted = format;
1022 if (arg[2] == 'g') { /* --git-dir */
1023 if (gitdir) {
1024 print_path(gitdir, prefix, format, DEFAULT_UNMODIFIED);
1025 continue;
1027 if (!prefix) {
1028 print_path(".git", prefix, format, DEFAULT_UNMODIFIED);
1029 continue;
1031 } else { /* --absolute-git-dir */
1032 wanted = FORMAT_CANONICAL;
1033 if (!gitdir && !prefix)
1034 gitdir = ".git";
1035 if (gitdir) {
1036 struct strbuf realpath = STRBUF_INIT;
1037 strbuf_realpath(&realpath, gitdir, 1);
1038 puts(realpath.buf);
1039 strbuf_release(&realpath);
1040 continue;
1043 cwd = xgetcwd();
1044 len = strlen(cwd);
1045 strbuf_reset(&buf);
1046 strbuf_addf(&buf, "%s%s.git", cwd, len && cwd[len-1] != '/' ? "/" : "");
1047 free(cwd);
1048 print_path(buf.buf, prefix, wanted, DEFAULT_CANONICAL);
1049 continue;
1051 if (!strcmp(arg, "--git-common-dir")) {
1052 print_path(repo_get_common_dir(the_repository), prefix, format, DEFAULT_RELATIVE_IF_SHARED);
1053 continue;
1055 if (!strcmp(arg, "--is-inside-git-dir")) {
1056 printf("%s\n", is_inside_git_dir() ? "true"
1057 : "false");
1058 continue;
1060 if (!strcmp(arg, "--is-inside-work-tree")) {
1061 printf("%s\n", is_inside_work_tree() ? "true"
1062 : "false");
1063 continue;
1065 if (!strcmp(arg, "--is-bare-repository")) {
1066 printf("%s\n", is_bare_repository() ? "true"
1067 : "false");
1068 continue;
1070 if (!strcmp(arg, "--is-shallow-repository")) {
1071 printf("%s\n",
1072 is_repository_shallow(the_repository) ? "true"
1073 : "false");
1074 continue;
1076 if (!strcmp(arg, "--shared-index-path")) {
1077 if (repo_read_index(the_repository) < 0)
1078 die(_("Could not read the index"));
1079 if (the_repository->index->split_index) {
1080 const struct object_id *oid = &the_repository->index->split_index->base_oid;
1081 const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
1082 print_path(path, prefix, format, DEFAULT_RELATIVE);
1084 continue;
1086 if (skip_prefix(arg, "--since=", &arg)) {
1087 show_datestring("--max-age=", arg);
1088 continue;
1090 if (skip_prefix(arg, "--after=", &arg)) {
1091 show_datestring("--max-age=", arg);
1092 continue;
1094 if (skip_prefix(arg, "--before=", &arg)) {
1095 show_datestring("--min-age=", arg);
1096 continue;
1098 if (skip_prefix(arg, "--until=", &arg)) {
1099 show_datestring("--min-age=", arg);
1100 continue;
1102 if (opt_with_value(arg, "--show-object-format", &arg)) {
1103 const char *val = arg ? arg : "storage";
1105 if (strcmp(val, "storage") &&
1106 strcmp(val, "input") &&
1107 strcmp(val, "output"))
1108 die(_("unknown mode for --show-object-format: %s"),
1109 arg);
1110 puts(the_hash_algo->name);
1111 continue;
1113 if (!strcmp(arg, "--show-ref-format")) {
1114 puts(ref_storage_format_to_name(the_repository->ref_storage_format));
1115 continue;
1117 if (!strcmp(arg, "--end-of-options")) {
1118 seen_end_of_options = 1;
1119 if (filter & (DO_FLAGS | DO_REVS))
1120 show_file(arg, 0);
1121 continue;
1123 if (show_flag(arg) && verify)
1124 die_no_single_rev(quiet);
1125 continue;
1128 /* Not a flag argument */
1129 if (try_difference(arg))
1130 continue;
1131 if (try_parent_shorthands(arg))
1132 continue;
1133 name = arg;
1134 type = NORMAL;
1135 if (*arg == '^') {
1136 name++;
1137 type = REVERSED;
1139 if (!get_oid_with_context(the_repository, name,
1140 flags, &oid, &unused)) {
1141 object_context_release(&unused);
1142 if (output_algo)
1143 repo_oid_to_algop(the_repository, &oid,
1144 output_algo, &oid);
1145 if (verify)
1146 revs_count++;
1147 else
1148 show_rev(type, &oid, name);
1149 continue;
1151 object_context_release(&unused);
1152 if (verify)
1153 die_no_single_rev(quiet);
1154 if (has_dashdash)
1155 die(_("bad revision '%s'"), arg);
1156 as_is = 1;
1157 if (!show_file(arg, output_prefix))
1158 continue;
1159 verify_filename(prefix, arg, 1);
1161 strbuf_release(&buf);
1162 if (verify) {
1163 if (revs_count == 1) {
1164 show_rev(type, &oid, name);
1165 return 0;
1166 } else if (revs_count == 0 && show_default())
1167 return 0;
1168 die_no_single_rev(quiet);
1169 } else
1170 show_default();
1171 return 0;