ci: fix setup of custom path for GitLab CI
[alt-git.git] / builtin / rev-parse.c
blob624182e507e5af9ac34a67f18d7a5dcf6fe2e58c
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"
28 #include "object-file-convert.h"
30 #define DO_REVS 1
31 #define DO_NOREV 2
32 #define DO_FLAGS 4
33 #define DO_NONFLAGS 8
34 static int filter = ~0;
36 static const char *def;
38 #define NORMAL 0
39 #define REVERSED 1
40 static int show_type = NORMAL;
42 #define SHOW_SYMBOLIC_ASIS 1
43 #define SHOW_SYMBOLIC_FULL 2
44 static int symbolic;
45 static int abbrev;
46 static int abbrev_ref;
47 static int abbrev_ref_strict;
48 static int output_sq;
50 static int stuck_long;
51 static struct ref_exclusions ref_excludes = REF_EXCLUSIONS_INIT;
54 * Some arguments are relevant "revision" arguments,
55 * others are about output format or other details.
56 * This sorts it all out.
58 static int is_rev_argument(const char *arg)
60 static const char *rev_args[] = {
61 "--all",
62 "--bisect",
63 "--dense",
64 "--branches=",
65 "--branches",
66 "--header",
67 "--ignore-missing",
68 "--max-age=",
69 "--max-count=",
70 "--min-age=",
71 "--no-merges",
72 "--min-parents=",
73 "--no-min-parents",
74 "--max-parents=",
75 "--no-max-parents",
76 "--objects",
77 "--objects-edge",
78 "--parents",
79 "--pretty",
80 "--remotes=",
81 "--remotes",
82 "--glob=",
83 "--sparse",
84 "--tags=",
85 "--tags",
86 "--topo-order",
87 "--date-order",
88 "--unpacked",
89 NULL
91 const char **p = rev_args;
93 /* accept -<digit>, like traditional "head" */
94 if ((*arg == '-') && isdigit(arg[1]))
95 return 1;
97 for (;;) {
98 const char *str = *p++;
99 int len;
100 if (!str)
101 return 0;
102 len = strlen(str);
103 if (!strcmp(arg, str) ||
104 (str[len-1] == '=' && !strncmp(arg, str, len)))
105 return 1;
109 /* Output argument as a string, either SQ or normal */
110 static void show(const char *arg)
112 if (output_sq) {
113 int sq = '\'', ch;
115 putchar(sq);
116 while ((ch = *arg++)) {
117 if (ch == sq)
118 fputs("'\\'", stdout);
119 putchar(ch);
121 putchar(sq);
122 putchar(' ');
124 else
125 puts(arg);
128 /* Like show(), but with a negation prefix according to type */
129 static void show_with_type(int type, const char *arg)
131 if (type != show_type)
132 putchar('^');
133 show(arg);
136 /* Output a revision, only if filter allows it */
137 static void show_rev(int type, const struct object_id *oid, const char *name)
139 if (!(filter & DO_REVS))
140 return;
141 def = NULL;
143 if ((symbolic || abbrev_ref) && name) {
144 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
145 struct object_id discard;
146 char *full;
148 switch (repo_dwim_ref(the_repository, name,
149 strlen(name), &discard, &full,
150 0)) {
151 case 0:
153 * Not found -- not a ref. We could
154 * emit "name" here, but symbolic-full
155 * users are interested in finding the
156 * refs spelled in full, and they would
157 * need to filter non-refs if we did so.
159 break;
160 case 1: /* happy */
161 if (abbrev_ref) {
162 char *old = full;
163 full = shorten_unambiguous_ref(full,
164 abbrev_ref_strict);
165 free(old);
167 show_with_type(type, full);
168 break;
169 default: /* ambiguous */
170 error("refname '%s' is ambiguous", name);
171 break;
173 free(full);
174 } else {
175 show_with_type(type, name);
178 else if (abbrev)
179 show_with_type(type,
180 repo_find_unique_abbrev(the_repository, oid, abbrev));
181 else
182 show_with_type(type, oid_to_hex(oid));
185 /* Output a flag, only if filter allows it. */
186 static int show_flag(const char *arg)
188 if (!(filter & DO_FLAGS))
189 return 0;
190 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
191 show(arg);
192 return 1;
194 return 0;
197 static int show_default(void)
199 const char *s = def;
201 if (s) {
202 struct object_id oid;
204 def = NULL;
205 if (!repo_get_oid(the_repository, s, &oid)) {
206 show_rev(NORMAL, &oid, s);
207 return 1;
210 return 0;
213 static int show_reference(const char *refname, const struct object_id *oid,
214 int flag UNUSED, void *cb_data UNUSED)
216 if (ref_excluded(&ref_excludes, refname))
217 return 0;
218 show_rev(NORMAL, oid, refname);
219 return 0;
222 static int anti_reference(const char *refname, const struct object_id *oid,
223 int flag UNUSED, void *cb_data UNUSED)
225 show_rev(REVERSED, oid, refname);
226 return 0;
229 static int show_abbrev(const struct object_id *oid, void *cb_data UNUSED)
231 show_rev(NORMAL, oid, NULL);
232 return 0;
235 static void show_datestring(const char *flag, const char *datestr)
237 char *buffer;
239 /* date handling requires both flags and revs */
240 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
241 return;
242 buffer = xstrfmt("%s%"PRItime, flag, approxidate(datestr));
243 show(buffer);
244 free(buffer);
247 static int show_file(const char *arg, int output_prefix)
249 show_default();
250 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
251 if (output_prefix) {
252 const char *prefix = startup_info->prefix;
253 char *fname = prefix_filename(prefix, arg);
254 show(fname);
255 free(fname);
256 } else
257 show(arg);
258 return 1;
260 return 0;
263 static int try_difference(const char *arg)
265 char *dotdot;
266 struct object_id start_oid;
267 struct object_id end_oid;
268 const char *end;
269 const char *start;
270 int symmetric;
271 static const char head_by_default[] = "HEAD";
273 if (!(dotdot = strstr(arg, "..")))
274 return 0;
275 end = dotdot + 2;
276 start = arg;
277 symmetric = (*end == '.');
279 *dotdot = 0;
280 end += symmetric;
282 if (!*end)
283 end = head_by_default;
284 if (dotdot == arg)
285 start = head_by_default;
287 if (start == head_by_default && end == head_by_default &&
288 !symmetric) {
290 * Just ".."? That is not a range but the
291 * pathspec for the parent directory.
293 *dotdot = '.';
294 return 0;
297 if (!repo_get_oid_committish(the_repository, start, &start_oid) && !repo_get_oid_committish(the_repository, end, &end_oid)) {
298 show_rev(NORMAL, &end_oid, end);
299 show_rev(symmetric ? NORMAL : REVERSED, &start_oid, start);
300 if (symmetric) {
301 struct commit_list *exclude = NULL;
302 struct commit *a, *b;
303 a = lookup_commit_reference(the_repository, &start_oid);
304 b = lookup_commit_reference(the_repository, &end_oid);
305 if (!a || !b) {
306 *dotdot = '.';
307 return 0;
309 if (repo_get_merge_bases(the_repository, a, b, &exclude) < 0)
310 exit(128);
311 while (exclude) {
312 struct commit *commit = pop_commit(&exclude);
313 show_rev(REVERSED, &commit->object.oid, NULL);
316 *dotdot = '.';
317 return 1;
319 *dotdot = '.';
320 return 0;
323 static int try_parent_shorthands(const char *arg)
325 char *dotdot;
326 struct object_id oid;
327 struct commit *commit;
328 struct commit_list *parents;
329 int parent_number;
330 int include_rev = 0;
331 int include_parents = 0;
332 int exclude_parent = 0;
334 if ((dotdot = strstr(arg, "^!"))) {
335 include_rev = 1;
336 if (dotdot[2])
337 return 0;
338 } else if ((dotdot = strstr(arg, "^@"))) {
339 include_parents = 1;
340 if (dotdot[2])
341 return 0;
342 } else if ((dotdot = strstr(arg, "^-"))) {
343 include_rev = 1;
344 exclude_parent = 1;
346 if (dotdot[2]) {
347 char *end;
348 exclude_parent = strtoul(dotdot + 2, &end, 10);
349 if (*end != '\0' || !exclude_parent)
350 return 0;
352 } else
353 return 0;
355 *dotdot = 0;
356 if (repo_get_oid_committish(the_repository, arg, &oid) ||
357 !(commit = lookup_commit_reference(the_repository, &oid))) {
358 *dotdot = '^';
359 return 0;
362 if (exclude_parent &&
363 exclude_parent > commit_list_count(commit->parents)) {
364 *dotdot = '^';
365 return 0;
368 if (include_rev)
369 show_rev(NORMAL, &oid, arg);
370 for (parents = commit->parents, parent_number = 1;
371 parents;
372 parents = parents->next, parent_number++) {
373 char *name = NULL;
375 if (exclude_parent && parent_number != exclude_parent)
376 continue;
378 if (symbolic)
379 name = xstrfmt("%s^%d", arg, parent_number);
380 show_rev(include_parents ? NORMAL : REVERSED,
381 &parents->item->object.oid, name);
382 free(name);
385 *dotdot = '^';
386 return 1;
389 static int parseopt_dump(const struct option *o, const char *arg, int unset)
391 struct strbuf *parsed = o->value;
392 if (unset)
393 strbuf_addf(parsed, " --no-%s", o->long_name);
394 else if (o->short_name && (o->long_name == NULL || !stuck_long))
395 strbuf_addf(parsed, " -%c", o->short_name);
396 else
397 strbuf_addf(parsed, " --%s", o->long_name);
398 if (arg) {
399 if (!stuck_long)
400 strbuf_addch(parsed, ' ');
401 else if (o->long_name)
402 strbuf_addch(parsed, '=');
403 sq_quote_buf(parsed, arg);
405 return 0;
408 static const char *skipspaces(const char *s)
410 while (isspace(*s))
411 s++;
412 return s;
415 static char *findspace(const char *s)
417 for (; *s; s++)
418 if (isspace(*s))
419 return (char*)s;
420 return NULL;
423 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
425 static int keep_dashdash = 0, stop_at_non_option = 0;
426 static char const * const parseopt_usage[] = {
427 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
428 NULL
430 static struct option parseopt_opts[] = {
431 OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
432 N_("keep the `--` passed as an arg")),
433 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
434 N_("stop parsing after the "
435 "first non-option argument")),
436 OPT_BOOL(0, "stuck-long", &stuck_long,
437 N_("output in stuck long form")),
438 OPT_END(),
440 static const char * const flag_chars = "*=?!";
442 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
443 const char **usage = NULL;
444 struct option *opts = NULL;
445 int onb = 0, osz = 0, unb = 0, usz = 0;
447 strbuf_addstr(&parsed, "set --");
448 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
449 PARSE_OPT_KEEP_DASHDASH);
450 if (argc < 1 || strcmp(argv[0], "--"))
451 usage_with_options(parseopt_usage, parseopt_opts);
453 /* get the usage up to the first line with a -- on it */
454 for (;;) {
455 if (strbuf_getline(&sb, stdin) == EOF)
456 die(_("premature end of input"));
457 ALLOC_GROW(usage, unb + 1, usz);
458 if (!strcmp("--", sb.buf)) {
459 if (unb < 1)
460 die(_("no usage string given before the `--' separator"));
461 usage[unb] = NULL;
462 break;
464 usage[unb++] = strbuf_detach(&sb, NULL);
467 /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
468 while (strbuf_getline(&sb, stdin) != EOF) {
469 const char *s;
470 char *help;
471 struct option *o;
473 if (!sb.len)
474 continue;
476 ALLOC_GROW(opts, onb + 1, osz);
477 memset(opts + onb, 0, sizeof(opts[onb]));
479 o = &opts[onb++];
480 help = findspace(sb.buf);
481 if (!help || sb.buf == help) {
482 o->type = OPTION_GROUP;
483 o->help = xstrdup(skipspaces(sb.buf));
484 continue;
487 *help = '\0';
489 o->type = OPTION_CALLBACK;
490 o->help = xstrdup(skipspaces(help+1));
491 o->value = &parsed;
492 o->flags = PARSE_OPT_NOARG;
493 o->callback = &parseopt_dump;
495 /* name(s) */
496 s = strpbrk(sb.buf, flag_chars);
497 if (!s)
498 s = help;
500 if (s == sb.buf)
501 die(_("missing opt-spec before option flags"));
503 if (s - sb.buf == 1) /* short option only */
504 o->short_name = *sb.buf;
505 else if (sb.buf[1] != ',') /* long option only */
506 o->long_name = xmemdupz(sb.buf, s - sb.buf);
507 else {
508 o->short_name = *sb.buf;
509 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
512 /* flags */
513 while (s < help) {
514 switch (*s++) {
515 case '=':
516 o->flags &= ~PARSE_OPT_NOARG;
517 continue;
518 case '?':
519 o->flags &= ~PARSE_OPT_NOARG;
520 o->flags |= PARSE_OPT_OPTARG;
521 continue;
522 case '!':
523 o->flags |= PARSE_OPT_NONEG;
524 continue;
525 case '*':
526 o->flags |= PARSE_OPT_HIDDEN;
527 continue;
529 s--;
530 break;
533 if (s < help)
534 o->argh = xmemdupz(s, help - s);
536 strbuf_release(&sb);
538 /* put an OPT_END() */
539 ALLOC_GROW(opts, onb + 1, osz);
540 memset(opts + onb, 0, sizeof(opts[onb]));
541 argc = parse_options(argc, argv, prefix, opts, usage,
542 (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
543 (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
544 PARSE_OPT_SHELL_EVAL);
546 strbuf_addstr(&parsed, " --");
547 sq_quote_argv(&parsed, argv);
548 puts(parsed.buf);
549 strbuf_release(&parsed);
550 return 0;
553 static int cmd_sq_quote(int argc, const char **argv)
555 struct strbuf buf = STRBUF_INIT;
557 if (argc)
558 sq_quote_argv(&buf, argv);
559 printf("%s\n", buf.buf);
560 strbuf_release(&buf);
562 return 0;
565 static void die_no_single_rev(int quiet)
567 if (quiet)
568 exit(1);
569 else
570 die(_("Needed a single revision"));
573 static const char builtin_rev_parse_usage[] =
574 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
575 " or: git rev-parse --sq-quote [<arg>...]\n"
576 " or: git rev-parse [<options>] [<arg>...]\n"
577 "\n"
578 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
581 * Parse "opt" or "opt=<value>", setting value respectively to either
582 * NULL or the string after "=".
584 static int opt_with_value(const char *arg, const char *opt, const char **value)
586 if (skip_prefix(arg, opt, &arg)) {
587 if (!*arg) {
588 *value = NULL;
589 return 1;
591 if (*arg++ == '=') {
592 *value = arg;
593 return 1;
596 return 0;
599 static void handle_ref_opt(const char *pattern, const char *prefix)
601 if (pattern)
602 for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
603 else
604 for_each_ref_in(prefix, show_reference, NULL);
605 clear_ref_exclusions(&ref_excludes);
608 enum format_type {
609 /* We would like a relative path. */
610 FORMAT_RELATIVE,
611 /* We would like a canonical absolute path. */
612 FORMAT_CANONICAL,
613 /* We would like the default behavior. */
614 FORMAT_DEFAULT,
617 enum default_type {
618 /* Our default is a relative path. */
619 DEFAULT_RELATIVE,
620 /* Our default is a relative path if there's a shared root. */
621 DEFAULT_RELATIVE_IF_SHARED,
622 /* Our default is a canonical absolute path. */
623 DEFAULT_CANONICAL,
624 /* Our default is not to modify the item. */
625 DEFAULT_UNMODIFIED,
628 static void print_path(const char *path, const char *prefix, enum format_type format, enum default_type def)
630 char *cwd = NULL;
632 * We don't ever produce a relative path if prefix is NULL, so set the
633 * prefix to the current directory so that we can produce a relative
634 * path whenever possible. If we're using RELATIVE_IF_SHARED mode, then
635 * we want an absolute path unless the two share a common prefix, so don't
636 * set it in that case, since doing so causes a relative path to always
637 * be produced if possible.
639 if (!prefix && (format != FORMAT_DEFAULT || def != DEFAULT_RELATIVE_IF_SHARED))
640 prefix = cwd = xgetcwd();
641 if (format == FORMAT_DEFAULT && def == DEFAULT_UNMODIFIED) {
642 puts(path);
643 } else if (format == FORMAT_RELATIVE ||
644 (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE)) {
646 * In order for relative_path to work as expected, we need to
647 * make sure that both paths are absolute paths. If we don't,
648 * we can end up with an unexpected absolute path that the user
649 * didn't want.
651 struct strbuf buf = STRBUF_INIT, realbuf = STRBUF_INIT, prefixbuf = STRBUF_INIT;
652 if (!is_absolute_path(path)) {
653 strbuf_realpath_forgiving(&realbuf, path, 1);
654 path = realbuf.buf;
656 if (!is_absolute_path(prefix)) {
657 strbuf_realpath_forgiving(&prefixbuf, prefix, 1);
658 prefix = prefixbuf.buf;
660 puts(relative_path(path, prefix, &buf));
661 strbuf_release(&buf);
662 strbuf_release(&realbuf);
663 strbuf_release(&prefixbuf);
664 } else if (format == FORMAT_DEFAULT && def == DEFAULT_RELATIVE_IF_SHARED) {
665 struct strbuf buf = STRBUF_INIT;
666 puts(relative_path(path, prefix, &buf));
667 strbuf_release(&buf);
668 } else {
669 struct strbuf buf = STRBUF_INIT;
670 strbuf_realpath_forgiving(&buf, path, 1);
671 puts(buf.buf);
672 strbuf_release(&buf);
674 free(cwd);
677 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
679 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
680 const struct git_hash_algo *output_algo = NULL;
681 const struct git_hash_algo *compat = NULL;
682 int did_repo_setup = 0;
683 int has_dashdash = 0;
684 int output_prefix = 0;
685 struct object_id oid;
686 unsigned int flags = 0;
687 const char *name = NULL;
688 struct object_context unused;
689 struct strbuf buf = STRBUF_INIT;
690 const int hexsz = the_hash_algo->hexsz;
691 int seen_end_of_options = 0;
692 enum format_type format = FORMAT_DEFAULT;
694 if (argc > 1 && !strcmp("--parseopt", argv[1]))
695 return cmd_parseopt(argc - 1, argv + 1, prefix);
697 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
698 return cmd_sq_quote(argc - 2, argv + 2);
700 if (argc > 1 && !strcmp("-h", argv[1]))
701 usage(builtin_rev_parse_usage);
703 for (i = 1; i < argc; i++) {
704 if (!strcmp(argv[i], "--")) {
705 has_dashdash = 1;
706 break;
710 /* No options; just report on whether we're in a git repo or not. */
711 if (argc == 1) {
712 setup_git_directory();
713 git_config(git_default_config, NULL);
714 return 0;
717 for (i = 1; i < argc; i++) {
718 const char *arg = argv[i];
720 if (as_is) {
721 if (show_file(arg, output_prefix) && as_is < 2)
722 verify_filename(prefix, arg, 0);
723 continue;
726 if (!seen_end_of_options) {
727 if (!strcmp(arg, "--local-env-vars")) {
728 int i;
729 for (i = 0; local_repo_env[i]; i++)
730 printf("%s\n", local_repo_env[i]);
731 continue;
733 if (!strcmp(arg, "--resolve-git-dir")) {
734 const char *gitdir = argv[++i];
735 if (!gitdir)
736 die(_("--resolve-git-dir requires an argument"));
737 gitdir = resolve_gitdir(gitdir);
738 if (!gitdir)
739 die(_("not a gitdir '%s'"), argv[i]);
740 puts(gitdir);
741 continue;
745 /* The rest of the options require a git repository. */
746 if (!did_repo_setup) {
747 prefix = setup_git_directory();
748 git_config(git_default_config, NULL);
749 did_repo_setup = 1;
751 prepare_repo_settings(the_repository);
752 the_repository->settings.command_requires_full_index = 0;
753 compat = the_repository->compat_hash_algo;
756 if (!strcmp(arg, "--")) {
757 as_is = 2;
758 /* Pass on the "--" if we show anything but files.. */
759 if (filter & (DO_FLAGS | DO_REVS))
760 show_file(arg, 0);
761 continue;
764 if (!seen_end_of_options && *arg == '-') {
765 if (!strcmp(arg, "--git-path")) {
766 if (!argv[i + 1])
767 die(_("--git-path requires an argument"));
768 strbuf_reset(&buf);
769 print_path(git_path("%s", argv[i + 1]), prefix,
770 format,
771 DEFAULT_RELATIVE_IF_SHARED);
772 i++;
773 continue;
775 if (!strcmp(arg,"-n")) {
776 if (++i >= argc)
777 die(_("-n requires an argument"));
778 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
779 show(arg);
780 show(argv[i]);
782 continue;
784 if (starts_with(arg, "-n")) {
785 if ((filter & DO_FLAGS) && (filter & DO_REVS))
786 show(arg);
787 continue;
789 if (opt_with_value(arg, "--path-format", &arg)) {
790 if (!arg)
791 die(_("--path-format requires an argument"));
792 if (!strcmp(arg, "absolute")) {
793 format = FORMAT_CANONICAL;
794 } else if (!strcmp(arg, "relative")) {
795 format = FORMAT_RELATIVE;
796 } else {
797 die(_("unknown argument to --path-format: %s"), arg);
799 continue;
801 if (!strcmp(arg, "--default")) {
802 def = argv[++i];
803 if (!def)
804 die(_("--default requires an argument"));
805 continue;
807 if (!strcmp(arg, "--prefix")) {
808 prefix = argv[++i];
809 if (!prefix)
810 die(_("--prefix requires an argument"));
811 startup_info->prefix = prefix;
812 output_prefix = 1;
813 continue;
815 if (!strcmp(arg, "--revs-only")) {
816 filter &= ~DO_NOREV;
817 continue;
819 if (!strcmp(arg, "--no-revs")) {
820 filter &= ~DO_REVS;
821 continue;
823 if (!strcmp(arg, "--flags")) {
824 filter &= ~DO_NONFLAGS;
825 continue;
827 if (!strcmp(arg, "--no-flags")) {
828 filter &= ~DO_FLAGS;
829 continue;
831 if (!strcmp(arg, "--verify")) {
832 filter &= ~(DO_FLAGS|DO_NOREV);
833 verify = 1;
834 continue;
836 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
837 quiet = 1;
838 flags |= GET_OID_QUIETLY;
839 continue;
841 if (opt_with_value(arg, "--output-object-format", &arg)) {
842 if (!arg)
843 die(_("no object format specified"));
844 if (!strcmp(arg, the_hash_algo->name) ||
845 !strcmp(arg, "storage")) {
846 flags |= GET_OID_HASH_ANY;
847 output_algo = the_hash_algo;
848 continue;
850 else if (compat && !strcmp(arg, compat->name)) {
851 flags |= GET_OID_HASH_ANY;
852 output_algo = compat;
853 continue;
855 else die(_("unsupported object format: %s"), arg);
857 if (opt_with_value(arg, "--short", &arg)) {
858 filter &= ~(DO_FLAGS|DO_NOREV);
859 verify = 1;
860 abbrev = DEFAULT_ABBREV;
861 if (!arg)
862 continue;
863 abbrev = strtoul(arg, NULL, 10);
864 if (abbrev < MINIMUM_ABBREV)
865 abbrev = MINIMUM_ABBREV;
866 else if (hexsz <= abbrev)
867 abbrev = hexsz;
868 continue;
870 if (!strcmp(arg, "--sq")) {
871 output_sq = 1;
872 continue;
874 if (!strcmp(arg, "--not")) {
875 show_type ^= REVERSED;
876 continue;
878 if (!strcmp(arg, "--symbolic")) {
879 symbolic = SHOW_SYMBOLIC_ASIS;
880 continue;
882 if (!strcmp(arg, "--symbolic-full-name")) {
883 symbolic = SHOW_SYMBOLIC_FULL;
884 continue;
886 if (opt_with_value(arg, "--abbrev-ref", &arg)) {
887 abbrev_ref = 1;
888 abbrev_ref_strict = warn_ambiguous_refs;
889 if (arg) {
890 if (!strcmp(arg, "strict"))
891 abbrev_ref_strict = 1;
892 else if (!strcmp(arg, "loose"))
893 abbrev_ref_strict = 0;
894 else
895 die(_("unknown mode for --abbrev-ref: %s"),
896 arg);
898 continue;
900 if (!strcmp(arg, "--all")) {
901 for_each_ref(show_reference, NULL);
902 clear_ref_exclusions(&ref_excludes);
903 continue;
905 if (skip_prefix(arg, "--disambiguate=", &arg)) {
906 repo_for_each_abbrev(the_repository, arg, the_hash_algo,
907 show_abbrev, NULL);
908 continue;
910 if (!strcmp(arg, "--bisect")) {
911 for_each_fullref_in("refs/bisect/bad", show_reference, NULL);
912 for_each_fullref_in("refs/bisect/good", anti_reference, NULL);
913 continue;
915 if (opt_with_value(arg, "--branches", &arg)) {
916 if (ref_excludes.hidden_refs_configured)
917 return error(_("options '%s' and '%s' cannot be used together"),
918 "--exclude-hidden", "--branches");
919 handle_ref_opt(arg, "refs/heads/");
920 continue;
922 if (opt_with_value(arg, "--tags", &arg)) {
923 if (ref_excludes.hidden_refs_configured)
924 return error(_("options '%s' and '%s' cannot be used together"),
925 "--exclude-hidden", "--tags");
926 handle_ref_opt(arg, "refs/tags/");
927 continue;
929 if (skip_prefix(arg, "--glob=", &arg)) {
930 handle_ref_opt(arg, NULL);
931 continue;
933 if (opt_with_value(arg, "--remotes", &arg)) {
934 if (ref_excludes.hidden_refs_configured)
935 return error(_("options '%s' and '%s' cannot be used together"),
936 "--exclude-hidden", "--remotes");
937 handle_ref_opt(arg, "refs/remotes/");
938 continue;
940 if (skip_prefix(arg, "--exclude=", &arg)) {
941 add_ref_exclusion(&ref_excludes, arg);
942 continue;
944 if (skip_prefix(arg, "--exclude-hidden=", &arg)) {
945 exclude_hidden_refs(&ref_excludes, arg);
946 continue;
948 if (!strcmp(arg, "--show-toplevel")) {
949 const char *work_tree = get_git_work_tree();
950 if (work_tree)
951 print_path(work_tree, prefix, format, DEFAULT_UNMODIFIED);
952 else
953 die(_("this operation must be run in a work tree"));
954 continue;
956 if (!strcmp(arg, "--show-superproject-working-tree")) {
957 struct strbuf superproject = STRBUF_INIT;
958 if (get_superproject_working_tree(&superproject))
959 print_path(superproject.buf, prefix, format, DEFAULT_UNMODIFIED);
960 strbuf_release(&superproject);
961 continue;
963 if (!strcmp(arg, "--show-prefix")) {
964 if (prefix)
965 puts(prefix);
966 else
967 putchar('\n');
968 continue;
970 if (!strcmp(arg, "--show-cdup")) {
971 const char *pfx = prefix;
972 if (!is_inside_work_tree()) {
973 const char *work_tree =
974 get_git_work_tree();
975 if (work_tree)
976 printf("%s\n", work_tree);
977 continue;
979 while (pfx) {
980 pfx = strchr(pfx, '/');
981 if (pfx) {
982 pfx++;
983 printf("../");
986 putchar('\n');
987 continue;
989 if (!strcmp(arg, "--git-dir") ||
990 !strcmp(arg, "--absolute-git-dir")) {
991 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
992 char *cwd;
993 int len;
994 enum format_type wanted = format;
995 if (arg[2] == 'g') { /* --git-dir */
996 if (gitdir) {
997 print_path(gitdir, prefix, format, DEFAULT_UNMODIFIED);
998 continue;
1000 if (!prefix) {
1001 print_path(".git", prefix, format, DEFAULT_UNMODIFIED);
1002 continue;
1004 } else { /* --absolute-git-dir */
1005 wanted = FORMAT_CANONICAL;
1006 if (!gitdir && !prefix)
1007 gitdir = ".git";
1008 if (gitdir) {
1009 struct strbuf realpath = STRBUF_INIT;
1010 strbuf_realpath(&realpath, gitdir, 1);
1011 puts(realpath.buf);
1012 strbuf_release(&realpath);
1013 continue;
1016 cwd = xgetcwd();
1017 len = strlen(cwd);
1018 strbuf_reset(&buf);
1019 strbuf_addf(&buf, "%s%s.git", cwd, len && cwd[len-1] != '/' ? "/" : "");
1020 free(cwd);
1021 print_path(buf.buf, prefix, wanted, DEFAULT_CANONICAL);
1022 continue;
1024 if (!strcmp(arg, "--git-common-dir")) {
1025 print_path(get_git_common_dir(), prefix, format, DEFAULT_RELATIVE_IF_SHARED);
1026 continue;
1028 if (!strcmp(arg, "--is-inside-git-dir")) {
1029 printf("%s\n", is_inside_git_dir() ? "true"
1030 : "false");
1031 continue;
1033 if (!strcmp(arg, "--is-inside-work-tree")) {
1034 printf("%s\n", is_inside_work_tree() ? "true"
1035 : "false");
1036 continue;
1038 if (!strcmp(arg, "--is-bare-repository")) {
1039 printf("%s\n", is_bare_repository() ? "true"
1040 : "false");
1041 continue;
1043 if (!strcmp(arg, "--is-shallow-repository")) {
1044 printf("%s\n",
1045 is_repository_shallow(the_repository) ? "true"
1046 : "false");
1047 continue;
1049 if (!strcmp(arg, "--shared-index-path")) {
1050 if (repo_read_index(the_repository) < 0)
1051 die(_("Could not read the index"));
1052 if (the_index.split_index) {
1053 const struct object_id *oid = &the_index.split_index->base_oid;
1054 const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
1055 print_path(path, prefix, format, DEFAULT_RELATIVE);
1057 continue;
1059 if (skip_prefix(arg, "--since=", &arg)) {
1060 show_datestring("--max-age=", arg);
1061 continue;
1063 if (skip_prefix(arg, "--after=", &arg)) {
1064 show_datestring("--max-age=", arg);
1065 continue;
1067 if (skip_prefix(arg, "--before=", &arg)) {
1068 show_datestring("--min-age=", arg);
1069 continue;
1071 if (skip_prefix(arg, "--until=", &arg)) {
1072 show_datestring("--min-age=", arg);
1073 continue;
1075 if (opt_with_value(arg, "--show-object-format", &arg)) {
1076 const char *val = arg ? arg : "storage";
1078 if (strcmp(val, "storage") &&
1079 strcmp(val, "input") &&
1080 strcmp(val, "output"))
1081 die(_("unknown mode for --show-object-format: %s"),
1082 arg);
1083 puts(the_hash_algo->name);
1084 continue;
1086 if (!strcmp(arg, "--show-ref-format")) {
1087 puts(ref_storage_format_to_name(the_repository->ref_storage_format));
1088 continue;
1090 if (!strcmp(arg, "--end-of-options")) {
1091 seen_end_of_options = 1;
1092 if (filter & (DO_FLAGS | DO_REVS))
1093 show_file(arg, 0);
1094 continue;
1096 if (show_flag(arg) && verify)
1097 die_no_single_rev(quiet);
1098 continue;
1101 /* Not a flag argument */
1102 if (try_difference(arg))
1103 continue;
1104 if (try_parent_shorthands(arg))
1105 continue;
1106 name = arg;
1107 type = NORMAL;
1108 if (*arg == '^') {
1109 name++;
1110 type = REVERSED;
1112 if (!get_oid_with_context(the_repository, name,
1113 flags, &oid, &unused)) {
1114 if (output_algo)
1115 repo_oid_to_algop(the_repository, &oid,
1116 output_algo, &oid);
1117 if (verify)
1118 revs_count++;
1119 else
1120 show_rev(type, &oid, name);
1121 continue;
1123 if (verify)
1124 die_no_single_rev(quiet);
1125 if (has_dashdash)
1126 die(_("bad revision '%s'"), arg);
1127 as_is = 1;
1128 if (!show_file(arg, output_prefix))
1129 continue;
1130 verify_filename(prefix, arg, 1);
1132 strbuf_release(&buf);
1133 if (verify) {
1134 if (revs_count == 1) {
1135 show_rev(type, &oid, name);
1136 return 0;
1137 } else if (revs_count == 0 && show_default())
1138 return 0;
1139 die_no_single_rev(quiet);
1140 } else
1141 show_default();
1142 return 0;