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