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