Merge branch 'jt/partial-clone-proto-v2'
[git] / builtin / rev-parse.c
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
22 static int filter = ~0;
23
24 static const char *def;
25
26 #define NORMAL 0
27 #define REVERSED 1
28 static int show_type = NORMAL;
29
30 #define SHOW_SYMBOLIC_ASIS 1
31 #define SHOW_SYMBOLIC_FULL 2
32 static int symbolic;
33 static int abbrev;
34 static int abbrev_ref;
35 static int abbrev_ref_strict;
36 static int output_sq;
37
38 static int stuck_long;
39 static 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  */
46 static 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 */
98 static 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 */
117 static 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 */
125 static 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, &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, abbrev));
163         else
164                 show_with_type(type, oid_to_hex(oid));
165 }
166
167 /* Output a flag, only if filter allows it. */
168 static 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
179 static 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
195 static 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
203 static 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
209 static int show_abbrev(const struct object_id *oid, void *cb_data)
210 {
211         show_rev(NORMAL, oid, NULL);
212         return 0;
213 }
214
215 static 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
227 static 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
243 static int try_difference(const char *arg)
244 {
245         char *dotdot;
246         struct object_id start_oid;
247         struct object_id end_oid;
248         const char *end;
249         const char *start;
250         int symmetric;
251         static const char head_by_default[] = "HEAD";
252
253         if (!(dotdot = strstr(arg, "..")))
254                 return 0;
255         end = dotdot + 2;
256         start = arg;
257         symmetric = (*end == '.');
258
259         *dotdot = 0;
260         end += symmetric;
261
262         if (!*end)
263                 end = head_by_default;
264         if (dotdot == arg)
265                 start = head_by_default;
266
267         if (start == head_by_default && end == 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_oid_committish(start, &start_oid) && !get_oid_committish(end, &end_oid)) {
278                 show_rev(NORMAL, &end_oid, end);
279                 show_rev(symmetric ? NORMAL : REVERSED, &start_oid, start);
280                 if (symmetric) {
281                         struct commit_list *exclude;
282                         struct commit *a, *b;
283                         a = lookup_commit_reference(&start_oid);
284                         b = lookup_commit_reference(&end_oid);
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
298 static 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_oid_committish(arg, &oid)) {
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
364 static 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
383 static const char *skipspaces(const char *s)
384 {
385         while (isspace(*s))
386                 s++;
387         return s;
388 }
389
390 static char *findspace(const char *s)
391 {
392         for (; *s; s++)
393                 if (isspace(*s))
394                         return (char*)s;
395         return NULL;
396 }
397
398 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
399 {
400         static int keep_dashdash = 0, stop_at_non_option = 0;
401         static char const * const parseopt_usage[] = {
402                 N_("git rev-parse --parseopt [<options>] -- [<args>...]"),
403                 NULL
404         };
405         static struct option parseopt_opts[] = {
406                 OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
407                                         N_("keep the `--` passed as an arg")),
408                 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
409                                         N_("stop parsing after the "
410                                            "first non-option argument")),
411                 OPT_BOOL(0, "stuck-long", &stuck_long,
412                                         N_("output in stuck long form")),
413                 OPT_END(),
414         };
415         static const char * const flag_chars = "*=?!";
416
417         struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
418         const char **usage = NULL;
419         struct option *opts = NULL;
420         int onb = 0, osz = 0, unb = 0, usz = 0;
421
422         strbuf_addstr(&parsed, "set --");
423         argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
424                              PARSE_OPT_KEEP_DASHDASH);
425         if (argc < 1 || strcmp(argv[0], "--"))
426                 usage_with_options(parseopt_usage, parseopt_opts);
427
428         /* get the usage up to the first line with a -- on it */
429         for (;;) {
430                 if (strbuf_getline(&sb, stdin) == EOF)
431                         die("premature end of input");
432                 ALLOC_GROW(usage, unb + 1, usz);
433                 if (!strcmp("--", sb.buf)) {
434                         if (unb < 1)
435                                 die("no usage string given before the `--' separator");
436                         usage[unb] = NULL;
437                         break;
438                 }
439                 usage[unb++] = strbuf_detach(&sb, NULL);
440         }
441
442         /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
443         while (strbuf_getline(&sb, stdin) != EOF) {
444                 const char *s;
445                 char *help;
446                 struct option *o;
447
448                 if (!sb.len)
449                         continue;
450
451                 ALLOC_GROW(opts, onb + 1, osz);
452                 memset(opts + onb, 0, sizeof(opts[onb]));
453
454                 o = &opts[onb++];
455                 help = findspace(sb.buf);
456                 if (!help || sb.buf == help) {
457                         o->type = OPTION_GROUP;
458                         o->help = xstrdup(skipspaces(sb.buf));
459                         continue;
460                 }
461
462                 *help = '\0';
463
464                 o->type = OPTION_CALLBACK;
465                 o->help = xstrdup(skipspaces(help+1));
466                 o->value = &parsed;
467                 o->flags = PARSE_OPT_NOARG;
468                 o->callback = &parseopt_dump;
469
470                 /* name(s) */
471                 s = strpbrk(sb.buf, flag_chars);
472                 if (s == NULL)
473                         s = help;
474
475                 if (s - sb.buf == 1) /* short option only */
476                         o->short_name = *sb.buf;
477                 else if (sb.buf[1] != ',') /* long option only */
478                         o->long_name = xmemdupz(sb.buf, s - sb.buf);
479                 else {
480                         o->short_name = *sb.buf;
481                         o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
482                 }
483
484                 /* flags */
485                 while (s < help) {
486                         switch (*s++) {
487                         case '=':
488                                 o->flags &= ~PARSE_OPT_NOARG;
489                                 continue;
490                         case '?':
491                                 o->flags &= ~PARSE_OPT_NOARG;
492                                 o->flags |= PARSE_OPT_OPTARG;
493                                 continue;
494                         case '!':
495                                 o->flags |= PARSE_OPT_NONEG;
496                                 continue;
497                         case '*':
498                                 o->flags |= PARSE_OPT_HIDDEN;
499                                 continue;
500                         }
501                         s--;
502                         break;
503                 }
504
505                 if (s < help)
506                         o->argh = xmemdupz(s, help - s);
507         }
508         strbuf_release(&sb);
509
510         /* put an OPT_END() */
511         ALLOC_GROW(opts, onb + 1, osz);
512         memset(opts + onb, 0, sizeof(opts[onb]));
513         argc = parse_options(argc, argv, prefix, opts, usage,
514                         (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
515                         (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
516                         PARSE_OPT_SHELL_EVAL);
517
518         strbuf_addstr(&parsed, " --");
519         sq_quote_argv(&parsed, argv);
520         puts(parsed.buf);
521         return 0;
522 }
523
524 static int cmd_sq_quote(int argc, const char **argv)
525 {
526         struct strbuf buf = STRBUF_INIT;
527
528         if (argc)
529                 sq_quote_argv(&buf, argv);
530         printf("%s\n", buf.buf);
531         strbuf_release(&buf);
532
533         return 0;
534 }
535
536 static void die_no_single_rev(int quiet)
537 {
538         if (quiet)
539                 exit(1);
540         else
541                 die("Needed a single revision");
542 }
543
544 static const char builtin_rev_parse_usage[] =
545 N_("git rev-parse --parseopt [<options>] -- [<args>...]\n"
546    "   or: git rev-parse --sq-quote [<arg>...]\n"
547    "   or: git rev-parse [<options>] [<arg>...]\n"
548    "\n"
549    "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
550
551 /*
552  * Parse "opt" or "opt=<value>", setting value respectively to either
553  * NULL or the string after "=".
554  */
555 static int opt_with_value(const char *arg, const char *opt, const char **value)
556 {
557         if (skip_prefix(arg, opt, &arg)) {
558                 if (!*arg) {
559                         *value = NULL;
560                         return 1;
561                 }
562                 if (*arg++ == '=') {
563                         *value = arg;
564                         return 1;
565                 }
566         }
567         return 0;
568 }
569
570 static void handle_ref_opt(const char *pattern, const char *prefix)
571 {
572         if (pattern)
573                 for_each_glob_ref_in(show_reference, pattern, prefix, NULL);
574         else
575                 for_each_ref_in(prefix, show_reference, NULL);
576         clear_ref_exclusion(&ref_excludes);
577 }
578
579 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
580 {
581         int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
582         int did_repo_setup = 0;
583         int has_dashdash = 0;
584         int output_prefix = 0;
585         struct object_id oid;
586         unsigned int flags = 0;
587         const char *name = NULL;
588         struct object_context unused;
589         struct strbuf buf = STRBUF_INIT;
590
591         if (argc > 1 && !strcmp("--parseopt", argv[1]))
592                 return cmd_parseopt(argc - 1, argv + 1, prefix);
593
594         if (argc > 1 && !strcmp("--sq-quote", argv[1]))
595                 return cmd_sq_quote(argc - 2, argv + 2);
596
597         if (argc > 1 && !strcmp("-h", argv[1]))
598                 usage(builtin_rev_parse_usage);
599
600         for (i = 1; i < argc; i++) {
601                 if (!strcmp(argv[i], "--")) {
602                         has_dashdash = 1;
603                         break;
604                 }
605         }
606
607         /* No options; just report on whether we're in a git repo or not. */
608         if (argc == 1) {
609                 setup_git_directory();
610                 git_config(git_default_config, NULL);
611                 return 0;
612         }
613
614         for (i = 1; i < argc; i++) {
615                 const char *arg = argv[i];
616
617                 if (!strcmp(arg, "--local-env-vars")) {
618                         int i;
619                         for (i = 0; local_repo_env[i]; i++)
620                                 printf("%s\n", local_repo_env[i]);
621                         continue;
622                 }
623                 if (!strcmp(arg, "--resolve-git-dir")) {
624                         const char *gitdir = argv[++i];
625                         if (!gitdir)
626                                 die("--resolve-git-dir requires an argument");
627                         gitdir = resolve_gitdir(gitdir);
628                         if (!gitdir)
629                                 die("not a gitdir '%s'", argv[i]);
630                         puts(gitdir);
631                         continue;
632                 }
633
634                 /* The rest of the options require a git repository. */
635                 if (!did_repo_setup) {
636                         prefix = setup_git_directory();
637                         git_config(git_default_config, NULL);
638                         did_repo_setup = 1;
639                 }
640
641                 if (!strcmp(arg, "--git-path")) {
642                         if (!argv[i + 1])
643                                 die("--git-path requires an argument");
644                         strbuf_reset(&buf);
645                         puts(relative_path(git_path("%s", argv[i + 1]),
646                                            prefix, &buf));
647                         i++;
648                         continue;
649                 }
650                 if (as_is) {
651                         if (show_file(arg, output_prefix) && as_is < 2)
652                                 verify_filename(prefix, arg, 0);
653                         continue;
654                 }
655                 if (!strcmp(arg,"-n")) {
656                         if (++i >= argc)
657                                 die("-n requires an argument");
658                         if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
659                                 show(arg);
660                                 show(argv[i]);
661                         }
662                         continue;
663                 }
664                 if (starts_with(arg, "-n")) {
665                         if ((filter & DO_FLAGS) && (filter & DO_REVS))
666                                 show(arg);
667                         continue;
668                 }
669
670                 if (*arg == '-') {
671                         if (!strcmp(arg, "--")) {
672                                 as_is = 2;
673                                 /* Pass on the "--" if we show anything but files.. */
674                                 if (filter & (DO_FLAGS | DO_REVS))
675                                         show_file(arg, 0);
676                                 continue;
677                         }
678                         if (!strcmp(arg, "--default")) {
679                                 def = argv[++i];
680                                 if (!def)
681                                         die("--default requires an argument");
682                                 continue;
683                         }
684                         if (!strcmp(arg, "--prefix")) {
685                                 prefix = argv[++i];
686                                 if (!prefix)
687                                         die("--prefix requires an argument");
688                                 startup_info->prefix = prefix;
689                                 output_prefix = 1;
690                                 continue;
691                         }
692                         if (!strcmp(arg, "--revs-only")) {
693                                 filter &= ~DO_NOREV;
694                                 continue;
695                         }
696                         if (!strcmp(arg, "--no-revs")) {
697                                 filter &= ~DO_REVS;
698                                 continue;
699                         }
700                         if (!strcmp(arg, "--flags")) {
701                                 filter &= ~DO_NONFLAGS;
702                                 continue;
703                         }
704                         if (!strcmp(arg, "--no-flags")) {
705                                 filter &= ~DO_FLAGS;
706                                 continue;
707                         }
708                         if (!strcmp(arg, "--verify")) {
709                                 filter &= ~(DO_FLAGS|DO_NOREV);
710                                 verify = 1;
711                                 continue;
712                         }
713                         if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
714                                 quiet = 1;
715                                 flags |= GET_OID_QUIETLY;
716                                 continue;
717                         }
718                         if (opt_with_value(arg, "--short", &arg)) {
719                                 filter &= ~(DO_FLAGS|DO_NOREV);
720                                 verify = 1;
721                                 abbrev = DEFAULT_ABBREV;
722                                 if (!arg)
723                                         continue;
724                                 abbrev = strtoul(arg, NULL, 10);
725                                 if (abbrev < MINIMUM_ABBREV)
726                                         abbrev = MINIMUM_ABBREV;
727                                 else if (40 <= abbrev)
728                                         abbrev = 40;
729                                 continue;
730                         }
731                         if (!strcmp(arg, "--sq")) {
732                                 output_sq = 1;
733                                 continue;
734                         }
735                         if (!strcmp(arg, "--not")) {
736                                 show_type ^= REVERSED;
737                                 continue;
738                         }
739                         if (!strcmp(arg, "--symbolic")) {
740                                 symbolic = SHOW_SYMBOLIC_ASIS;
741                                 continue;
742                         }
743                         if (!strcmp(arg, "--symbolic-full-name")) {
744                                 symbolic = SHOW_SYMBOLIC_FULL;
745                                 continue;
746                         }
747                         if (opt_with_value(arg, "--abbrev-ref", &arg)) {
748                                 abbrev_ref = 1;
749                                 abbrev_ref_strict = warn_ambiguous_refs;
750                                 if (arg) {
751                                         if (!strcmp(arg, "strict"))
752                                                 abbrev_ref_strict = 1;
753                                         else if (!strcmp(arg, "loose"))
754                                                 abbrev_ref_strict = 0;
755                                         else
756                                                 die("unknown mode for --abbrev-ref: %s",
757                                                     arg);
758                                 }
759                                 continue;
760                         }
761                         if (!strcmp(arg, "--all")) {
762                                 for_each_ref(show_reference, NULL);
763                                 continue;
764                         }
765                         if (skip_prefix(arg, "--disambiguate=", &arg)) {
766                                 for_each_abbrev(arg, show_abbrev, NULL);
767                                 continue;
768                         }
769                         if (!strcmp(arg, "--bisect")) {
770                                 for_each_fullref_in("refs/bisect/bad", show_reference, NULL, 0);
771                                 for_each_fullref_in("refs/bisect/good", anti_reference, NULL, 0);
772                                 continue;
773                         }
774                         if (opt_with_value(arg, "--branches", &arg)) {
775                                 handle_ref_opt(arg, "refs/heads/");
776                                 continue;
777                         }
778                         if (opt_with_value(arg, "--tags", &arg)) {
779                                 handle_ref_opt(arg, "refs/tags/");
780                                 continue;
781                         }
782                         if (skip_prefix(arg, "--glob=", &arg)) {
783                                 handle_ref_opt(arg, NULL);
784                                 continue;
785                         }
786                         if (opt_with_value(arg, "--remotes", &arg)) {
787                                 handle_ref_opt(arg, "refs/remotes/");
788                                 continue;
789                         }
790                         if (skip_prefix(arg, "--exclude=", &arg)) {
791                                 add_ref_exclusion(&ref_excludes, arg);
792                                 continue;
793                         }
794                         if (!strcmp(arg, "--show-toplevel")) {
795                                 const char *work_tree = get_git_work_tree();
796                                 if (work_tree)
797                                         puts(work_tree);
798                                 continue;
799                         }
800                         if (!strcmp(arg, "--show-superproject-working-tree")) {
801                                 const char *superproject = get_superproject_working_tree();
802                                 if (superproject)
803                                         puts(superproject);
804                                 continue;
805                         }
806                         if (!strcmp(arg, "--show-prefix")) {
807                                 if (prefix)
808                                         puts(prefix);
809                                 else
810                                         putchar('\n');
811                                 continue;
812                         }
813                         if (!strcmp(arg, "--show-cdup")) {
814                                 const char *pfx = prefix;
815                                 if (!is_inside_work_tree()) {
816                                         const char *work_tree =
817                                                 get_git_work_tree();
818                                         if (work_tree)
819                                                 printf("%s\n", work_tree);
820                                         continue;
821                                 }
822                                 while (pfx) {
823                                         pfx = strchr(pfx, '/');
824                                         if (pfx) {
825                                                 pfx++;
826                                                 printf("../");
827                                         }
828                                 }
829                                 putchar('\n');
830                                 continue;
831                         }
832                         if (!strcmp(arg, "--git-dir") ||
833                             !strcmp(arg, "--absolute-git-dir")) {
834                                 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
835                                 char *cwd;
836                                 int len;
837                                 if (arg[2] == 'g') {    /* --git-dir */
838                                         if (gitdir) {
839                                                 puts(gitdir);
840                                                 continue;
841                                         }
842                                         if (!prefix) {
843                                                 puts(".git");
844                                                 continue;
845                                         }
846                                 } else {                /* --absolute-git-dir */
847                                         if (!gitdir && !prefix)
848                                                 gitdir = ".git";
849                                         if (gitdir) {
850                                                 puts(real_path(gitdir));
851                                                 continue;
852                                         }
853                                 }
854                                 cwd = xgetcwd();
855                                 len = strlen(cwd);
856                                 printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : "");
857                                 free(cwd);
858                                 continue;
859                         }
860                         if (!strcmp(arg, "--git-common-dir")) {
861                                 strbuf_reset(&buf);
862                                 puts(relative_path(get_git_common_dir(),
863                                                    prefix, &buf));
864                                 continue;
865                         }
866                         if (!strcmp(arg, "--is-inside-git-dir")) {
867                                 printf("%s\n", is_inside_git_dir() ? "true"
868                                                 : "false");
869                                 continue;
870                         }
871                         if (!strcmp(arg, "--is-inside-work-tree")) {
872                                 printf("%s\n", is_inside_work_tree() ? "true"
873                                                 : "false");
874                                 continue;
875                         }
876                         if (!strcmp(arg, "--is-bare-repository")) {
877                                 printf("%s\n", is_bare_repository() ? "true"
878                                                 : "false");
879                                 continue;
880                         }
881                         if (!strcmp(arg, "--is-shallow-repository")) {
882                                 printf("%s\n", is_repository_shallow() ? "true"
883                                                 : "false");
884                                 continue;
885                         }
886                         if (!strcmp(arg, "--shared-index-path")) {
887                                 if (read_cache() < 0)
888                                         die(_("Could not read the index"));
889                                 if (the_index.split_index) {
890                                         const struct object_id *oid = &the_index.split_index->base_oid;
891                                         const char *path = git_path("sharedindex.%s", oid_to_hex(oid));
892                                         strbuf_reset(&buf);
893                                         puts(relative_path(path, prefix, &buf));
894                                 }
895                                 continue;
896                         }
897                         if (skip_prefix(arg, "--since=", &arg)) {
898                                 show_datestring("--max-age=", arg);
899                                 continue;
900                         }
901                         if (skip_prefix(arg, "--after=", &arg)) {
902                                 show_datestring("--max-age=", arg);
903                                 continue;
904                         }
905                         if (skip_prefix(arg, "--before=", &arg)) {
906                                 show_datestring("--min-age=", arg);
907                                 continue;
908                         }
909                         if (skip_prefix(arg, "--until=", &arg)) {
910                                 show_datestring("--min-age=", arg);
911                                 continue;
912                         }
913                         if (show_flag(arg) && verify)
914                                 die_no_single_rev(quiet);
915                         continue;
916                 }
917
918                 /* Not a flag argument */
919                 if (try_difference(arg))
920                         continue;
921                 if (try_parent_shorthands(arg))
922                         continue;
923                 name = arg;
924                 type = NORMAL;
925                 if (*arg == '^') {
926                         name++;
927                         type = REVERSED;
928                 }
929                 if (!get_oid_with_context(name, flags, &oid, &unused)) {
930                         if (verify)
931                                 revs_count++;
932                         else
933                                 show_rev(type, &oid, name);
934                         continue;
935                 }
936                 if (verify)
937                         die_no_single_rev(quiet);
938                 if (has_dashdash)
939                         die("bad revision '%s'", arg);
940                 as_is = 1;
941                 if (!show_file(arg, output_prefix))
942                         continue;
943                 verify_filename(prefix, arg, 1);
944         }
945         strbuf_release(&buf);
946         if (verify) {
947                 if (revs_count == 1) {
948                         show_rev(type, &oid, name);
949                         return 0;
950                 } else if (revs_count == 0 && show_default())
951                         return 0;
952                 die_no_single_rev(quiet);
953         } else
954                 show_default();
955         return 0;
956 }