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