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