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