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