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