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