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