banned.h: mark `strtok()` and `strtok_r()` as banned
[git/debian.git] / builtin / rev-parse.c
blob1af2089f9bd04810cb057b94371f67e45523ad0b
1 /*
2 * rev-parse.c
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #define USE_THE_INDEX_VARIABLE
7 #include "cache.h"
8 #include "abspath.h"
9 #include "alloc.h"
10 #include "config.h"
11 #include "commit.h"
12 #include "environment.h"
13 #include "gettext.h"
14 #include "hex.h"
15 #include "refs.h"
16 #include "quote.h"
17 #include "builtin.h"
18 #include "parse-options.h"
19 #include "diff.h"
20 #include "revision.h"
21 #include "setup.h"
22 #include "split-index.h"
23 #include "submodule.h"
24 #include "commit-reach.h"
25 #include "shallow.h"
27 #define DO_REVS 1
28 #define DO_NOREV 2
29 #define DO_FLAGS 4
30 #define DO_NONFLAGS 8
31 static int filter = ~0;
33 static const char *def;
35 #define NORMAL 0
36 #define REVERSED 1
37 static int show_type = NORMAL;
39 #define SHOW_SYMBOLIC_ASIS 1
40 #define SHOW_SYMBOLIC_FULL 2
41 static int symbolic;
42 static int abbrev;
43 static int abbrev_ref;
44 static int abbrev_ref_strict;
45 static int output_sq;
47 static int stuck_long;
48 static struct ref_exclusions ref_excludes = REF_EXCLUSIONS_INIT;
51 * Some arguments are relevant "revision" arguments,
52 * others are about output format or other details.
53 * This sorts it all out.
55 static int is_rev_argument(const char *arg)
57 static const char *rev_args[] = {
58 "--all",
59 "--bisect",
60 "--dense",
61 "--branches=",
62 "--branches",
63 "--header",
64 "--ignore-missing",
65 "--max-age=",
66 "--max-count=",
67 "--min-age=",
68 "--no-merges",
69 "--min-parents=",
70 "--no-min-parents",
71 "--max-parents=",
72 "--no-max-parents",
73 "--objects",
74 "--objects-edge",
75 "--parents",
76 "--pretty",
77 "--remotes=",
78 "--remotes",
79 "--glob=",
80 "--sparse",
81 "--tags=",
82 "--tags",
83 "--topo-order",
84 "--date-order",
85 "--unpacked",
86 NULL
88 const char **p = rev_args;
90 /* accept -<digit>, like traditional "head" */
91 if ((*arg == '-') && isdigit(arg[1]))
92 return 1;
94 for (;;) {
95 const char *str = *p++;
96 int len;
97 if (!str)
98 return 0;
99 len = strlen(str);
100 if (!strcmp(arg, str) ||
101 (str[len-1] == '=' && !strncmp(arg, str, len)))
102 return 1;
106 /* Output argument as a string, either SQ or normal */
107 static void show(const char *arg)
109 if (output_sq) {
110 int sq = '\'', ch;
112 putchar(sq);
113 while ((ch = *arg++)) {
114 if (ch == sq)
115 fputs("'\\'", stdout);
116 putchar(ch);
118 putchar(sq);
119 putchar(' ');
121 else
122 puts(arg);
125 /* Like show(), but with a negation prefix according to type */
126 static void show_with_type(int type, const char *arg)
128 if (type != show_type)
129 putchar('^');
130 show(arg);
133 /* Output a revision, only if filter allows it */
134 static void show_rev(int type, const struct object_id *oid, const char *name)
136 if (!(filter & DO_REVS))
137 return;
138 def = NULL;
140 if ((symbolic || abbrev_ref) && name) {
141 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
142 struct object_id discard;
143 char *full;
145 switch (repo_dwim_ref(the_repository, name,
146 strlen(name), &discard, &full,
147 0)) {
148 case 0:
150 * Not found -- not a ref. We could
151 * emit "name" here, but symbolic-full
152 * users are interested in finding the
153 * refs spelled in full, and they would
154 * need to filter non-refs if we did so.
156 break;
157 case 1: /* happy */
158 if (abbrev_ref)
159 full = shorten_unambiguous_ref(full,
160 abbrev_ref_strict);
161 show_with_type(type, full);
162 break;
163 default: /* ambiguous */
164 error("refname '%s' is ambiguous", name);
165 break;
167 free(full);
168 } else {
169 show_with_type(type, name);
172 else if (abbrev)
173 show_with_type(type,
174 repo_find_unique_abbrev(the_repository, oid, abbrev));
175 else
176 show_with_type(type, oid_to_hex(oid));
179 /* Output a flag, only if filter allows it. */
180 static int show_flag(const char *arg)
182 if (!(filter & DO_FLAGS))
183 return 0;
184 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
185 show(arg);
186 return 1;
188 return 0;
191 static int show_default(void)
193 const char *s = def;
195 if (s) {
196 struct object_id oid;
198 def = NULL;
199 if (!repo_get_oid(the_repository, s, &oid)) {
200 show_rev(NORMAL, &oid, s);
201 return 1;
204 return 0;
207 static int show_reference(const char *refname, const struct object_id *oid,
208 int flag UNUSED, void *cb_data UNUSED)
210 if (ref_excluded(&ref_excludes, refname))
211 return 0;
212 show_rev(NORMAL, oid, refname);
213 return 0;
216 static int anti_reference(const char *refname, const struct object_id *oid,
217 int flag UNUSED, void *cb_data UNUSED)
219 show_rev(REVERSED, oid, refname);
220 return 0;
223 static int show_abbrev(const struct object_id *oid, void *cb_data)
225 show_rev(NORMAL, oid, NULL);
226 return 0;
229 static void show_datestring(const char *flag, const char *datestr)
231 char *buffer;
233 /* date handling requires both flags and revs */
234 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
235 return;
236 buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr));
237 show(buffer);
238 free(buffer);
241 static int show_file(const char *arg, int output_prefix)
243 show_default();
244 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
245 if (output_prefix) {
246 const char *prefix = startup_info->prefix;
247 char *fname = prefix_filename(prefix, arg);
248 show(fname);
249 free(fname);
250 } else
251 show(arg);
252 return 1;
254 return 0;
257 static int try_difference(const char *arg)
259 char *dotdot;
260 struct object_id start_oid;
261 struct object_id end_oid;
262 const char *end;
263 const char *start;
264 int symmetric;
265 static const char head_by_default[] = "HEAD";
267 if (!(dotdot = strstr(arg, "..")))
268 return 0;
269 end = dotdot + 2;
270 start = arg;
271 symmetric = (*end == '.');
273 *dotdot = 0;
274 end += symmetric;
276 if (!*end)
277 end = head_by_default;
278 if (dotdot == arg)
279 start = head_by_default;
281 if (start == head_by_default && end == head_by_default &&
282 !symmetric) {
284 * Just ".."? That is not a range but the
285 * pathspec for the parent directory.
287 *dotdot = '.';
288 return 0;
291 if (!repo_get_oid_committish(the_repository, start, &start_oid) && !repo_get_oid_committish(the_repository, end, &end_oid)) {
292 show_rev(NORMAL, &end_oid, end);
293 show_rev(symmetric ? NORMAL : REVERSED, &start_oid, start);
294 if (symmetric) {
295 struct commit_list *exclude;
296 struct commit *a, *b;
297 a = lookup_commit_reference(the_repository, &start_oid);
298 b = lookup_commit_reference(the_repository, &end_oid);
299 if (!a || !b) {
300 *dotdot = '.';
301 return 0;
303 exclude = repo_get_merge_bases(the_repository, a, b);
304 while (exclude) {
305 struct commit *commit = pop_commit(&exclude);
306 show_rev(REVERSED, &commit->object.oid, NULL);
309 *dotdot = '.';
310 return 1;
312 *dotdot = '.';
313 return 0;
316 static int try_parent_shorthands(const char *arg)
318 char *dotdot;
319 struct object_id oid;
320 struct commit *commit;
321 struct commit_list *parents;
322 int parent_number;
323 int include_rev = 0;
324 int include_parents = 0;
325 int exclude_parent = 0;
327 if ((dotdot = strstr(arg, "^!"))) {
328 include_rev = 1;
329 if (dotdot[2])
330 return 0;
331 } else if ((dotdot = strstr(arg, "^@"))) {
332 include_parents = 1;
333 if (dotdot[2])
334 return 0;
335 } else if ((dotdot = strstr(arg, "^-"))) {
336 include_rev = 1;
337 exclude_parent = 1;
339 if (dotdot[2]) {
340 char *end;
341 exclude_parent = strtoul(dotdot + 2, &end, 10);
342 if (*end != '\0' || !exclude_parent)
343 return 0;
345 } else
346 return 0;
348 *dotdot = 0;
349 if (repo_get_oid_committish(the_repository, arg, &oid) ||
350 !(commit = lookup_commit_reference(the_repository, &oid))) {
351 *dotdot = '^';
352 return 0;
355 if (exclude_parent &&
356 exclude_parent > commit_list_count(commit->parents)) {
357 *dotdot = '^';
358 return 0;
361 if (include_rev)
362 show_rev(NORMAL, &oid, arg);
363 for (parents = commit->parents, parent_number = 1;
364 parents;
365 parents = parents->next, parent_number++) {
366 char *name = NULL;
368 if (exclude_parent && parent_number != exclude_parent)
369 continue;
371 if (symbolic)
372 name = xstrfmt("%s^%d", arg, parent_number);
373 show_rev(include_parents ? NORMAL : REVERSED,
374 &parents->item->object.oid, name);
375 free(name);
378 *dotdot = '^';
379 return 1;
382 static int parseopt_dump(const struct option *o, const char *arg, int unset)
384 struct strbuf *parsed = o->value;
385 if (unset)
386 strbuf_addf(parsed, " --no-%s", o->long_name);
387 else if (o->short_name && (o->long_name == NULL || !stuck_long))
388 strbuf_addf(parsed, " -%c", o->short_name);
389 else
390 strbuf_addf(parsed, " --%s", o->long_name);
391 if (arg) {
392 if (!stuck_long)
393 strbuf_addch(parsed, ' ');
394 else if (o->long_name)
395 strbuf_addch(parsed, '=');
396 sq_quote_buf(parsed, arg);
398 return 0;
401 static const char *skipspaces(const char *s)
403 while (isspace(*s))
404 s++;
405 return s;
408 static char *findspace(const char *s)
410 for (; *s; s++)
411 if (isspace(*s))
412 return (char*)s;
413 return NULL;
416 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
418 static int keep_dashdash = 0, stop_at_non_option = 0;
419 static char const * const parseopt_usage[] = {
420 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
421 NULL
423 static struct option parseopt_opts[] = {
424 OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
425 N_("keep the `--` passed as an arg")),
426 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
427 N_("stop parsing after the "
428 "first non-option argument")),
429 OPT_BOOL(0, "stuck-long", &stuck_long,
430 N_("output in stuck long form")),
431 OPT_END(),
433 static const char * const flag_chars = "*=?!";
435 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
436 const char **usage = NULL;
437 struct option *opts = NULL;
438 int onb = 0, osz = 0, unb = 0, usz = 0;
440 strbuf_addstr(&parsed, "set --");
441 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
442 PARSE_OPT_KEEP_DASHDASH);
443 if (argc < 1 || strcmp(argv[0], "--"))
444 usage_with_options(parseopt_usage, parseopt_opts);
446 /* get the usage up to the first line with a -- on it */
447 for (;;) {
448 if (strbuf_getline(&sb, stdin) == EOF)
449 die(_("premature end of input"));
450 ALLOC_GROW(usage, unb + 1, usz);
451 if (!strcmp("--", sb.buf)) {
452 if (unb < 1)
453 die(_("no usage string given before the `--' separator"));
454 usage[unb] = NULL;
455 break;
457 usage[unb++] = strbuf_detach(&sb, NULL);
460 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
461 while (strbuf_getline(&sb, stdin) != EOF) {
462 const char *s;
463 char *help;
464 struct option *o;
466 if (!sb.len)
467 continue;
469 ALLOC_GROW(opts, onb + 1, osz);
470 memset(opts + onb, 0, sizeof(opts[onb]));
472 o = &opts[onb++];
473 help = findspace(sb.buf);
474 if (!help || sb.buf == help) {
475 o->type = OPTION_GROUP;
476 o->help = xstrdup(skipspaces(sb.buf));
477 continue;
480 *help = '\0';
482 o->type = OPTION_CALLBACK;
483 o->help = xstrdup(skipspaces(help+1));
484 o->value = &parsed;
485 o->flags = PARSE_OPT_NOARG;
486 o->callback = &parseopt_dump;
488 /* name(s) */
489 s = strpbrk(sb.buf, flag_chars);
490 if (!s)
491 s = help;
493 if (s == sb.buf)
494 die(_("missing opt-spec before option flags"));
496 if (s - sb.buf == 1) /* short option only */
497 o->short_name = *sb.buf;
498 else if (sb.buf[1] != ',') /* long option only */
499 o->long_name = xmemdupz(sb.buf, s - sb.buf);
500 else {
501 o->short_name = *sb.buf;
502 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
505 /* flags */
506 while (s < help) {
507 switch (*s++) {
508 case '=':
509 o->flags &= ~PARSE_OPT_NOARG;
510 continue;
511 case '?':
512 o->flags &= ~PARSE_OPT_NOARG;
513 o->flags |= PARSE_OPT_OPTARG;
514 continue;
515 case '!':
516 o->flags |= PARSE_OPT_NONEG;
517 continue;
518 case '*':
519 o->flags |= PARSE_OPT_HIDDEN;
520 continue;
522 s--;
523 break;
526 if (s < help)
527 o->argh = xmemdupz(s, help - s);
529 strbuf_release(&sb);
531 /* put an OPT_END() */
532 ALLOC_GROW(opts, onb + 1, osz);
533 memset(opts + onb, 0, sizeof(opts[onb]));
534 argc = parse_options(argc, argv, prefix, opts, usage,
535 (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
536 (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
537 PARSE_OPT_SHELL_EVAL);
539 strbuf_addstr(&parsed, " --");
540 sq_quote_argv(&parsed, argv);
541 puts(parsed.buf);
542 strbuf_release(&parsed);
543 return 0;
546 static int cmd_sq_quote(int argc, const char **argv)
548 struct strbuf buf = STRBUF_INIT;
550 if (argc)
551 sq_quote_argv(&buf, argv);
552 printf("%s\n", buf.buf);
553 strbuf_release(&buf);
555 return 0;
558 static void die_no_single_rev(int quiet)
560 if (quiet)
561 exit(1);
562 else
563 die(_("Needed a single revision"));
566 static const char builtin_rev_parse_usage[] =
567 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
568 " or: git rev-parse --sq-quote [<arg>...]\n"
569 " or: git rev-parse [<options>] [<arg>...]\n"
570 "\n"
571 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
574 * Parse "opt" or "opt=<value>", setting value respectively to either
575 * NULL or the string after "=".
577 static int opt_with_value(const char *arg, const char *opt, const char **value)
579 if (skip_prefix(arg, opt, &arg)) {
580 if (!*arg) {
581 *value = NULL;
582 return 1;
584 if (*arg++ == '=') {
585 *value = arg;
586 return 1;
589 return 0;
592 static void handle_ref_opt(const char *pattern, const char *prefix)
594 if (pattern)
595 for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
596 else
597 for_each_ref_in(prefix, show_reference, NULL);
598 clear_ref_exclusions(&ref_excludes);
601 enum format_type {
602 /* We would like a relative path. */
603 FORMAT_RELATIVE,
604 /* We would like a canonical absolute path. */
605 FORMAT_CANONICAL,
606 /* We would like the default behavior. */
607 FORMAT_DEFAULT,
610 enum default_type {
611 /* Our default is a relative path. */
612 DEFAULT_RELATIVE,
613 /* Our default is a relative path if there's a shared root. */
614 DEFAULT_RELATIVE_IF_SHARED,
615 /* Our default is a canonical absolute path. */
616 DEFAULT_CANONICAL,
617 /* Our default is not to modify the item. */
618 DEFAULT_UNMODIFIED,
621 static void print_path(const char *path, const char *prefix, enum format_type format, enum default_type def)
623 char *cwd = NULL;
625 * We don't ever produce a relative path if prefix is NULL, so set the
626 * prefix to the current directory so that we can produce a relative
627 * path whenever possible. If we're using RELATIVE_IF_SHARED mode, then
628 * we want an absolute path unless the two share a common prefix, so don't
629 * set it in that case, since doing so causes a relative path to always
630 * be produced if possible.
632 if (!prefix && (format != FORMAT_DEFAULT || def != DEFAULT_RELATIVE_IF_SHARED))
633 prefix = cwd = xgetcwd();
634 if (format == FORMAT_DEFAULT && def == DEFAULT_UNMODIFIED) {
635 puts(path);
636 } else if (format == FORMAT_RELATIVE ||
637 (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE)) {
639 * In order for relative_path to work as expected, we need to
640 * make sure that both paths are absolute paths. If we don't,
641 * we can end up with an unexpected absolute path that the user
642 * didn't want.
644 struct strbuf buf = STRBUF_INIT, realbuf = STRBUF_INIT, prefixbuf = STRBUF_INIT;
645 if (!is_absolute_path(path)) {
646 strbuf_realpath_forgiving(&realbuf, path, 1);
647 path = realbuf.buf;
649 if (!is_absolute_path(prefix)) {
650 strbuf_realpath_forgiving(&prefixbuf, prefix, 1);
651 prefix = prefixbuf.buf;
653 puts(relative_path(path, prefix, &buf));
654 strbuf_release(&buf);
655 strbuf_release(&realbuf);
656 strbuf_release(&prefixbuf);
657 } else if (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE_IF_SHARED) {
658 struct strbuf buf = STRBUF_INIT;
659 puts(relative_path(path, prefix, &buf));
660 strbuf_release(&buf);
661 } else {
662 struct strbuf buf = STRBUF_INIT;
663 strbuf_realpath_forgiving(&buf, path, 1);
664 puts(buf.buf);
665 strbuf_release(&buf);
667 free(cwd);
670 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
672 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
673 int did_repo_setup = 0;
674 int has_dashdash = 0;
675 int output_prefix = 0;
676 struct object_id oid;
677 unsigned int flags = 0;
678 const char *name = NULL;
679 struct object_context unused;
680 struct strbuf buf = STRBUF_INIT;
681 const int hexsz = the_hash_algo->hexsz;
682 int seen_end_of_options = 0;
683 enum format_type format = FORMAT_DEFAULT;
685 if (argc > 1 && !strcmp("--parseopt", argv[1]))
686 return cmd_parseopt(argc - 1, argv + 1, prefix);
688 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
689 return cmd_sq_quote(argc - 2, argv + 2);
691 if (argc > 1 && !strcmp("-h", argv[1]))
692 usage(builtin_rev_parse_usage);
694 for (i = 1; i < argc; i++) {
695 if (!strcmp(argv[i], "--")) {
696 has_dashdash = 1;
697 break;
701 /* No options; just report on whether we're in a git repo or not. */
702 if (argc == 1) {
703 setup_git_directory();
704 git_config(git_default_config, NULL);
705 return 0;
708 for (i = 1; i < argc; i++) {
709 const char *arg = argv[i];
711 if (as_is) {
712 if (show_file(arg, output_prefix) && as_is < 2)
713 verify_filename(prefix, arg, 0);
714 continue;
717 if (!seen_end_of_options) {
718 if (!strcmp(arg, "--local-env-vars")) {
719 int i;
720 for (i = 0; local_repo_env[i]; i++)
721 printf("%s\n", local_repo_env[i]);
722 continue;
724 if (!strcmp(arg, "--resolve-git-dir")) {
725 const char *gitdir = argv[++i];
726 if (!gitdir)
727 die(_("--resolve-git-dir requires an argument"));
728 gitdir = resolve_gitdir(gitdir);
729 if (!gitdir)
730 die(_("not a gitdir '%s'"), argv[i]);
731 puts(gitdir);
732 continue;
736 /* The rest of the options require a git repository. */
737 if (!did_repo_setup) {
738 prefix = setup_git_directory();
739 git_config(git_default_config, NULL);
740 did_repo_setup = 1;
742 prepare_repo_settings(the_repository);
743 the_repository->settings.command_requires_full_index = 0;
746 if (!strcmp(arg, "--")) {
747 as_is = 2;
748 /* Pass on the "--" if we show anything but files.. */
749 if (filter & (DO_FLAGS | DO_REVS))
750 show_file(arg, 0);
751 continue;
754 if (!seen_end_of_options && *arg == '-') {
755 if (!strcmp(arg, "--git-path")) {
756 if (!argv[i + 1])
757 die(_("--git-path requires an argument"));
758 strbuf_reset(&buf);
759 print_path(git_path("%s", argv[i + 1]), prefix,
760 format,
761 DEFAULT_RELATIVE_IF_SHARED);
762 i++;
763 continue;
765 if (!strcmp(arg,"-n")) {
766 if (++i >= argc)
767 die(_("-n requires an argument"));
768 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
769 show(arg);
770 show(argv[i]);
772 continue;
774 if (starts_with(arg, "-n")) {
775 if ((filter & DO_FLAGS) && (filter & DO_REVS))
776 show(arg);
777 continue;
779 if (opt_with_value(arg, "--path-format", &arg)) {
780 if (!arg)
781 die(_("--path-format requires an argument"));
782 if (!strcmp(arg, "absolute")) {
783 format = FORMAT_CANONICAL;
784 } else if (!strcmp(arg, "relative")) {
785 format = FORMAT_RELATIVE;
786 } else {
787 die(_("unknown argument to --path-format: %s"), arg);
789 continue;
791 if (!strcmp(arg, "--default")) {
792 def = argv[++i];
793 if (!def)
794 die(_("--default requires an argument"));
795 continue;
797 if (!strcmp(arg, "--prefix")) {
798 prefix = argv[++i];
799 if (!prefix)
800 die(_("--prefix requires an argument"));
801 startup_info->prefix = prefix;
802 output_prefix = 1;
803 continue;
805 if (!strcmp(arg, "--revs-only")) {
806 filter &= ~DO_NOREV;
807 continue;
809 if (!strcmp(arg, "--no-revs")) {
810 filter &= ~DO_REVS;
811 continue;
813 if (!strcmp(arg, "--flags")) {
814 filter &= ~DO_NONFLAGS;
815 continue;
817 if (!strcmp(arg, "--no-flags")) {
818 filter &= ~DO_FLAGS;
819 continue;
821 if (!strcmp(arg, "--verify")) {
822 filter &= ~(DO_FLAGS|DO_NOREV);
823 verify = 1;
824 continue;
826 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
827 quiet = 1;
828 flags |= GET_OID_QUIETLY;
829 continue;
831 if (opt_with_value(arg, "--short", &arg)) {
832 filter &= ~(DO_FLAGS|DO_NOREV);
833 verify = 1;
834 abbrev = DEFAULT_ABBREV;
835 if (!arg)
836 continue;
837 abbrev = strtoul(arg, NULL, 10);
838 if (abbrev < MINIMUM_ABBREV)
839 abbrev = MINIMUM_ABBREV;
840 else if (hexsz <= abbrev)
841 abbrev = hexsz;
842 continue;
844 if (!strcmp(arg, "--sq")) {
845 output_sq = 1;
846 continue;
848 if (!strcmp(arg, "--not")) {
849 show_type ^= REVERSED;
850 continue;
852 if (!strcmp(arg, "--symbolic")) {
853 symbolic = SHOW_SYMBOLIC_ASIS;
854 continue;
856 if (!strcmp(arg, "--symbolic-full-name")) {
857 symbolic = SHOW_SYMBOLIC_FULL;
858 continue;
860 if (opt_with_value(arg, "--abbrev-ref", &arg)) {
861 abbrev_ref = 1;
862 abbrev_ref_strict = warn_ambiguous_refs;
863 if (arg) {
864 if (!strcmp(arg, "strict"))
865 abbrev_ref_strict = 1;
866 else if (!strcmp(arg, "loose"))
867 abbrev_ref_strict = 0;
868 else
869 die(_("unknown mode for --abbrev-ref: %s"),
870 arg);
872 continue;
874 if (!strcmp(arg, "--all")) {
875 for_each_ref(show_reference, NULL);
876 clear_ref_exclusions(&ref_excludes);
877 continue;
879 if (skip_prefix(arg, "--disambiguate=", &arg)) {
880 repo_for_each_abbrev(the_repository, arg,
881 show_abbrev, NULL);
882 continue;
884 if (!strcmp(arg, "--bisect")) {
885 for_each_fullref_in("refs/bisect/bad", show_reference, NULL);
886 for_each_fullref_in("refs/bisect/good", anti_reference, NULL);
887 continue;
889 if (opt_with_value(arg, "--branches", &arg)) {
890 if (ref_excludes.hidden_refs_configured)
891 return error(_("--exclude-hidden cannot be used together with --branches"));
892 handle_ref_opt(arg, "refs/heads/");
893 continue;
895 if (opt_with_value(arg, "--tags", &arg)) {
896 if (ref_excludes.hidden_refs_configured)
897 return error(_("--exclude-hidden cannot be used together with --tags"));
898 handle_ref_opt(arg, "refs/tags/");
899 continue;
901 if (skip_prefix(arg, "--glob=", &arg)) {
902 handle_ref_opt(arg, NULL);
903 continue;
905 if (opt_with_value(arg, "--remotes", &arg)) {
906 if (ref_excludes.hidden_refs_configured)
907 return error(_("--exclude-hidden cannot be used together with --remotes"));
908 handle_ref_opt(arg, "refs/remotes/");
909 continue;
911 if (skip_prefix(arg, "--exclude=", &arg)) {
912 add_ref_exclusion(&ref_excludes, arg);
913 continue;
915 if (skip_prefix(arg, "--exclude-hidden=", &arg)) {
916 exclude_hidden_refs(&ref_excludes, arg);
917 continue;
919 if (!strcmp(arg, "--show-toplevel")) {
920 const char *work_tree = get_git_work_tree();
921 if (work_tree)
922 print_path(work_tree, prefix, format, DEFAULT_UNMODIFIED);
923 else
924 die(_("this operation must be run in a work tree"));
925 continue;
927 if (!strcmp(arg, "--show-superproject-working-tree")) {
928 struct strbuf superproject = STRBUF_INIT;
929 if (get_superproject_working_tree(&superproject))
930 print_path(superproject.buf, prefix, format, DEFAULT_UNMODIFIED);
931 strbuf_release(&superproject);
932 continue;
934 if (!strcmp(arg, "--show-prefix")) {
935 if (prefix)
936 puts(prefix);
937 else
938 putchar('\n');
939 continue;
941 if (!strcmp(arg, "--show-cdup")) {
942 const char *pfx = prefix;
943 if (!is_inside_work_tree()) {
944 const char *work_tree =
945 get_git_work_tree();
946 if (work_tree)
947 printf("%s\n", work_tree);
948 continue;
950 while (pfx) {
951 pfx = strchr(pfx, '/');
952 if (pfx) {
953 pfx++;
954 printf("../");
957 putchar('\n');
958 continue;
960 if (!strcmp(arg, "--git-dir") ||
961 !strcmp(arg, "--absolute-git-dir")) {
962 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
963 char *cwd;
964 int len;
965 enum format_type wanted = format;
966 if (arg[2] == 'g') { /* --git-dir */
967 if (gitdir) {
968 print_path(gitdir, prefix, format, DEFAULT_UNMODIFIED);
969 continue;
971 if (!prefix) {
972 print_path(".git", prefix, format, DEFAULT_UNMODIFIED);
973 continue;
975 } else { /* --absolute-git-dir */
976 wanted = FORMAT_CANONICAL;
977 if (!gitdir && !prefix)
978 gitdir = ".git";
979 if (gitdir) {
980 struct strbuf realpath = STRBUF_INIT;
981 strbuf_realpath(&realpath, gitdir, 1);
982 puts(realpath.buf);
983 strbuf_release(&realpath);
984 continue;
987 cwd = xgetcwd();
988 len = strlen(cwd);
989 strbuf_reset(&buf);
990 strbuf_addf(&buf, "%s%s.git", cwd, len && cwd[len-1] != '/' ? "/" : "");
991 free(cwd);
992 print_path(buf.buf, prefix, wanted, DEFAULT_CANONICAL);
993 continue;
995 if (!strcmp(arg, "--git-common-dir")) {
996 print_path(get_git_common_dir(), prefix, format, DEFAULT_RELATIVE_IF_SHARED);
997 continue;
999 if (!strcmp(arg, "--is-inside-git-dir")) {
1000 printf("%s\n", is_inside_git_dir() ? "true"
1001 : "false");
1002 continue;
1004 if (!strcmp(arg, "--is-inside-work-tree")) {
1005 printf("%s\n", is_inside_work_tree() ? "true"
1006 : "false");
1007 continue;
1009 if (!strcmp(arg, "--is-bare-repository")) {
1010 printf("%s\n", is_bare_repository() ? "true"
1011 : "false");
1012 continue;
1014 if (!strcmp(arg, "--is-shallow-repository")) {
1015 printf("%s\n",
1016 is_repository_shallow(the_repository) ? "true"
1017 : "false");
1018 continue;
1020 if (!strcmp(arg, "--shared-index-path")) {
1021 if (repo_read_index(the_repository) < 0)
1022 die(_("Could not read the index"));
1023 if (the_index.split_index) {
1024 const struct object_id *oid = &the_index.split_index->base_oid;
1025 const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
1026 print_path(path, prefix, format, DEFAULT_RELATIVE);
1028 continue;
1030 if (skip_prefix(arg, "--since=", &arg)) {
1031 show_datestring("--max-age=", arg);
1032 continue;
1034 if (skip_prefix(arg, "--after=", &arg)) {
1035 show_datestring("--max-age=", arg);
1036 continue;
1038 if (skip_prefix(arg, "--before=", &arg)) {
1039 show_datestring("--min-age=", arg);
1040 continue;
1042 if (skip_prefix(arg, "--until=", &arg)) {
1043 show_datestring("--min-age=", arg);
1044 continue;
1046 if (opt_with_value(arg, "--show-object-format", &arg)) {
1047 const char *val = arg ? arg : "storage";
1049 if (strcmp(val, "storage") &&
1050 strcmp(val, "input") &&
1051 strcmp(val, "output"))
1052 die(_("unknown mode for --show-object-format: %s"),
1053 arg);
1054 puts(the_hash_algo->name);
1055 continue;
1057 if (!strcmp(arg, "--end-of-options")) {
1058 seen_end_of_options = 1;
1059 if (filter & (DO_FLAGS | DO_REVS))
1060 show_file(arg, 0);
1061 continue;
1063 if (show_flag(arg) && verify)
1064 die_no_single_rev(quiet);
1065 continue;
1068 /* Not a flag argument */
1069 if (try_difference(arg))
1070 continue;
1071 if (try_parent_shorthands(arg))
1072 continue;
1073 name = arg;
1074 type = NORMAL;
1075 if (*arg == '^') {
1076 name++;
1077 type = REVERSED;
1079 if (!get_oid_with_context(the_repository, name,
1080 flags, &oid, &unused)) {
1081 if (verify)
1082 revs_count++;
1083 else
1084 show_rev(type, &oid, name);
1085 continue;
1087 if (verify)
1088 die_no_single_rev(quiet);
1089 if (has_dashdash)
1090 die(_("bad revision '%s'"), arg);
1091 as_is = 1;
1092 if (!show_file(arg, output_prefix))
1093 continue;
1094 verify_filename(prefix, arg, 1);
1096 strbuf_release(&buf);
1097 if (verify) {
1098 if (revs_count == 1) {
1099 show_rev(type, &oid, name);
1100 return 0;
1101 } else if (revs_count == 0 && show_default())
1102 return 0;
1103 die_no_single_rev(quiet);
1104 } else
1105 show_default();
1106 return 0;