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