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