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