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