]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-rev-parse.c
Merge branch 'sb/opt-filename'
[thirdparty/git.git] / builtin-rev-parse.c
CommitLineData
178cb243
LT
1/*
2 * rev-parse.c
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
a8be83fe 7#include "commit.h"
960bba0d 8#include "refs.h"
c1babb1d 9#include "quote.h"
895f10c3 10#include "builtin.h"
21d47835 11#include "parse-options.h"
a8be83fe 12
4866ccf0
JH
13#define DO_REVS 1
14#define DO_NOREV 2
15#define DO_FLAGS 4
16#define DO_NONFLAGS 8
17static int filter = ~0;
18
96f1e58f 19static const char *def;
023d66ed 20
042a4ed7
LT
21#define NORMAL 0
22#define REVERSED 1
23static int show_type = NORMAL;
a6d97d49
JH
24
25#define SHOW_SYMBOLIC_ASIS 1
26#define SHOW_SYMBOLIC_FULL 2
96f1e58f
DR
27static int symbolic;
28static int abbrev;
a45d3469
BW
29static int abbrev_ref;
30static int abbrev_ref_strict;
96f1e58f 31static int output_sq;
4866ccf0 32
921d865e
LT
33/*
34 * Some arguments are relevant "revision" arguments,
35 * others are about output format or other details.
36 * This sorts it all out.
37 */
38static int is_rev_argument(const char *arg)
39{
40 static const char *rev_args[] = {
e091eb93 41 "--all",
4866ccf0 42 "--bisect",
5a83f3be 43 "--dense",
a62be77f 44 "--branches",
4866ccf0 45 "--header",
921d865e 46 "--max-age=",
4866ccf0 47 "--max-count=",
4866ccf0 48 "--min-age=",
5ccfb758 49 "--no-merges",
4866ccf0 50 "--objects",
c6496575 51 "--objects-edge",
4866ccf0
JH
52 "--parents",
53 "--pretty",
a62be77f 54 "--remotes",
5a83f3be 55 "--sparse",
a62be77f 56 "--tags",
4866ccf0 57 "--topo-order",
4c8725f1 58 "--date-order",
4866ccf0 59 "--unpacked",
921d865e
LT
60 NULL
61 };
62 const char **p = rev_args;
63
8233340c
EW
64 /* accept -<digit>, like traditional "head" */
65 if ((*arg == '-') && isdigit(arg[1]))
66 return 1;
67
921d865e
LT
68 for (;;) {
69 const char *str = *p++;
70 int len;
71 if (!str)
72 return 0;
73 len = strlen(str);
4866ccf0
JH
74 if (!strcmp(arg, str) ||
75 (str[len-1] == '=' && !strncmp(arg, str, len)))
921d865e
LT
76 return 1;
77 }
78}
79
4866ccf0 80/* Output argument as a string, either SQ or normal */
5bb2c65a
JH
81static void show(const char *arg)
82{
83 if (output_sq) {
84 int sq = '\'', ch;
85
86 putchar(sq);
87 while ((ch = *arg++)) {
88 if (ch == sq)
89 fputs("'\\'", stdout);
90 putchar(ch);
91 }
92 putchar(sq);
93 putchar(' ');
94 }
95 else
96 puts(arg);
97}
98
e00f3790
JS
99/* Like show(), but with a negation prefix according to type */
100static void show_with_type(int type, const char *arg)
101{
102 if (type != show_type)
103 putchar('^');
104 show(arg);
105}
106
4866ccf0 107/* Output a revision, only if filter allows it */
30b96fce 108static void show_rev(int type, const unsigned char *sha1, const char *name)
023d66ed 109{
4866ccf0 110 if (!(filter & DO_REVS))
023d66ed 111 return;
4866ccf0 112 def = NULL;
5bb2c65a 113
a45d3469
BW
114 if ((symbolic || abbrev_ref) && name) {
115 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
a6d97d49
JH
116 unsigned char discard[20];
117 char *full;
118
119 switch (dwim_ref(name, strlen(name), discard, &full)) {
120 case 0:
121 /*
122 * Not found -- not a ref. We could
123 * emit "name" here, but symbolic-full
124 * users are interested in finding the
125 * refs spelled in full, and they would
126 * need to filter non-refs if we did so.
127 */
128 break;
129 case 1: /* happy */
a45d3469
BW
130 if (abbrev_ref)
131 full = shorten_unambiguous_ref(full,
132 abbrev_ref_strict);
e00f3790 133 show_with_type(type, full);
a6d97d49
JH
134 break;
135 default: /* ambiguous */
136 error("refname '%s' is ambiguous", name);
137 break;
138 }
139 } else {
e00f3790 140 show_with_type(type, name);
a6d97d49
JH
141 }
142 }
d5012508 143 else if (abbrev)
e00f3790 144 show_with_type(type, find_unique_abbrev(sha1, abbrev));
30b96fce 145 else
e00f3790 146 show_with_type(type, sha1_to_hex(sha1));
023d66ed
LT
147}
148
4866ccf0 149/* Output a flag, only if filter allows it. */
16cee38a 150static int show_flag(const char *arg)
023d66ed 151{
4866ccf0 152 if (!(filter & DO_FLAGS))
9523a4c2
LT
153 return 0;
154 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
0360e99d 155 show(arg);
9523a4c2
LT
156 return 1;
157 }
158 return 0;
023d66ed
LT
159}
160
dfd1b749 161static int show_default(void)
023d66ed 162{
16cee38a 163 const char *s = def;
023d66ed
LT
164
165 if (s) {
166 unsigned char sha1[20];
167
168 def = NULL;
9938af6a 169 if (!get_sha1(s, sha1)) {
30b96fce 170 show_rev(NORMAL, sha1, s);
dfd1b749 171 return 1;
023d66ed 172 }
023d66ed 173 }
dfd1b749 174 return 0;
023d66ed
LT
175}
176
8da19775 177static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
960bba0d 178{
30b96fce 179 show_rev(NORMAL, sha1, refname);
960bba0d
LT
180 return 0;
181}
182
c1babb1d
LT
183static void show_datestring(const char *flag, const char *datestr)
184{
c1babb1d 185 static char buffer[100];
c1babb1d
LT
186
187 /* date handling requires both flags and revs */
188 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
189 return;
3c07b1d1 190 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
c1babb1d
LT
191 show(buffer);
192}
193
9ad0a933 194static int show_file(const char *arg)
7a3dd472 195{
7b34c2fa 196 show_default();
9ad0a933 197 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
7a3dd472 198 show(arg);
9ad0a933
LT
199 return 1;
200 }
201 return 0;
7a3dd472
LT
202}
203
b7d936b2 204static int try_difference(const char *arg)
3dd4e732
SB
205{
206 char *dotdot;
207 unsigned char sha1[20];
208 unsigned char end[20];
209 const char *next;
210 const char *this;
211 int symmetric;
212
213 if (!(dotdot = strstr(arg, "..")))
214 return 0;
215 next = dotdot + 2;
216 this = arg;
217 symmetric = (*next == '.');
218
219 *dotdot = 0;
220 next += symmetric;
221
222 if (!*next)
223 next = "HEAD";
224 if (dotdot == arg)
225 this = "HEAD";
226 if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
227 show_rev(NORMAL, end, next);
228 show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
229 if (symmetric) {
230 struct commit_list *exclude;
231 struct commit *a, *b;
232 a = lookup_commit_reference(sha1);
233 b = lookup_commit_reference(end);
234 exclude = get_merge_bases(a, b, 1);
235 while (exclude) {
236 struct commit_list *n = exclude->next;
237 show_rev(REVERSED,
238 exclude->item->object.sha1,NULL);
239 free(exclude);
240 exclude = n;
241 }
242 }
243 return 1;
244 }
245 *dotdot = '.';
246 return 0;
247}
248
2122f8b9
BS
249static int try_parent_shorthands(const char *arg)
250{
251 char *dotdot;
252 unsigned char sha1[20];
253 struct commit *commit;
254 struct commit_list *parents;
255 int parents_only;
256
257 if ((dotdot = strstr(arg, "^!")))
258 parents_only = 0;
259 else if ((dotdot = strstr(arg, "^@")))
260 parents_only = 1;
261
262 if (!dotdot || dotdot[2])
263 return 0;
264
265 *dotdot = 0;
266 if (get_sha1(arg, sha1))
267 return 0;
268
269 if (!parents_only)
270 show_rev(NORMAL, sha1, arg);
271 commit = lookup_commit_reference(sha1);
272 for (parents = commit->parents; parents; parents = parents->next)
273 show_rev(parents_only ? NORMAL : REVERSED,
274 parents->item->object.sha1, arg);
275
276 return 1;
277}
278
21d47835
PH
279static int parseopt_dump(const struct option *o, const char *arg, int unset)
280{
281 struct strbuf *parsed = o->value;
282 if (unset)
283 strbuf_addf(parsed, " --no-%s", o->long_name);
284 else if (o->short_name)
285 strbuf_addf(parsed, " -%c", o->short_name);
286 else
287 strbuf_addf(parsed, " --%s", o->long_name);
288 if (arg) {
289 strbuf_addch(parsed, ' ');
290 sq_quote_buf(parsed, arg);
291 }
292 return 0;
293}
294
295static const char *skipspaces(const char *s)
296{
297 while (isspace(*s))
298 s++;
299 return s;
300}
301
302static int cmd_parseopt(int argc, const char **argv, const char *prefix)
303{
304 static int keep_dashdash = 0;
305 static char const * const parseopt_usage[] = {
1b1dd23f 306 "git rev-parse --parseopt [options] -- [<args>...]",
21d47835
PH
307 NULL
308 };
309 static struct option parseopt_opts[] = {
310 OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash,
311 "keep the `--` passed as an arg"),
312 OPT_END(),
313 };
314
f285a2d7 315 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
21d47835
PH
316 const char **usage = NULL;
317 struct option *opts = NULL;
318 int onb = 0, osz = 0, unb = 0, usz = 0;
319
21d47835 320 strbuf_addstr(&parsed, "set --");
37782920 321 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
21d47835
PH
322 PARSE_OPT_KEEP_DASHDASH);
323 if (argc < 1 || strcmp(argv[0], "--"))
324 usage_with_options(parseopt_usage, parseopt_opts);
325
21d47835
PH
326 /* get the usage up to the first line with a -- on it */
327 for (;;) {
328 if (strbuf_getline(&sb, stdin, '\n') == EOF)
329 die("premature end of input");
330 ALLOC_GROW(usage, unb + 1, usz);
331 if (!strcmp("--", sb.buf)) {
332 if (unb < 1)
333 die("no usage string given before the `--' separator");
334 usage[unb] = NULL;
335 break;
336 }
337 usage[unb++] = strbuf_detach(&sb, NULL);
338 }
339
340 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
341 while (strbuf_getline(&sb, stdin, '\n') != EOF) {
342 const char *s;
343 struct option *o;
344
345 if (!sb.len)
346 continue;
347
348 ALLOC_GROW(opts, onb + 1, osz);
349 memset(opts + onb, 0, sizeof(opts[onb]));
350
351 o = &opts[onb++];
352 s = strchr(sb.buf, ' ');
353 if (!s || *sb.buf == ' ') {
354 o->type = OPTION_GROUP;
e1033436 355 o->help = xstrdup(skipspaces(sb.buf));
21d47835
PH
356 continue;
357 }
358
359 o->type = OPTION_CALLBACK;
360 o->help = xstrdup(skipspaces(s));
361 o->value = &parsed;
ff962a3f 362 o->flags = PARSE_OPT_NOARG;
21d47835 363 o->callback = &parseopt_dump;
ff962a3f
PH
364 while (s > sb.buf && strchr("*=?!", s[-1])) {
365 switch (*--s) {
366 case '=':
367 o->flags &= ~PARSE_OPT_NOARG;
368 break;
369 case '?':
370 o->flags &= ~PARSE_OPT_NOARG;
371 o->flags |= PARSE_OPT_OPTARG;
372 break;
373 case '!':
374 o->flags |= PARSE_OPT_NONEG;
375 break;
376 case '*':
377 o->flags |= PARSE_OPT_HIDDEN;
378 break;
379 }
21d47835
PH
380 }
381
382 if (s - sb.buf == 1) /* short option only */
383 o->short_name = *sb.buf;
384 else if (sb.buf[1] != ',') /* long option only */
385 o->long_name = xmemdupz(sb.buf, s - sb.buf);
386 else {
387 o->short_name = *sb.buf;
388 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
389 }
390 }
391 strbuf_release(&sb);
392
393 /* put an OPT_END() */
394 ALLOC_GROW(opts, onb + 1, osz);
395 memset(opts + onb, 0, sizeof(opts[onb]));
37782920 396 argc = parse_options(argc, argv, prefix, opts, usage,
21d47835
PH
397 keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0);
398
399 strbuf_addf(&parsed, " --");
b319ce4c 400 sq_quote_argv(&parsed, argv, 0);
21d47835
PH
401 puts(parsed.buf);
402 return 0;
403}
404
50325377
CC
405static int cmd_sq_quote(int argc, const char **argv)
406{
407 struct strbuf buf = STRBUF_INIT;
408
409 if (argc)
410 sq_quote_argv(&buf, argv, 0);
411 printf("%s\n", buf.buf);
412 strbuf_release(&buf);
413
414 return 0;
415}
416
b1b35969
CC
417static void die_no_single_rev(int quiet)
418{
419 if (quiet)
420 exit(1);
421 else
422 die("Needed a single revision");
423}
424
a633fca0 425int cmd_rev_parse(int argc, const char **argv, const char *prefix)
178cb243 426{
dfd1b749 427 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
178cb243 428 unsigned char sha1[20];
dfd1b749 429 const char *name = NULL;
a62be77f 430
21d47835
PH
431 if (argc > 1 && !strcmp("--parseopt", argv[1]))
432 return cmd_parseopt(argc - 1, argv + 1, prefix);
433
50325377
CC
434 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
435 return cmd_sq_quote(argc - 2, argv + 2);
436
5410a02a 437 prefix = setup_git_directory();
ef90d6d4 438 git_config(git_default_config, NULL);
178cb243 439 for (i = 1; i < argc; i++) {
16cee38a 440 const char *arg = argv[i];
fb18a2ed 441
178cb243 442 if (as_is) {
fb18a2ed 443 if (show_file(arg) && as_is < 2)
e23d0b4a 444 verify_filename(prefix, arg);
178cb243
LT
445 continue;
446 }
3af06987
EW
447 if (!strcmp(arg,"-n")) {
448 if (++i >= argc)
449 die("-n requires an argument");
450 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
451 show(arg);
452 show(argv[i]);
453 }
454 continue;
455 }
1968d77d 456 if (!prefixcmp(arg, "-n")) {
3af06987
EW
457 if ((filter & DO_FLAGS) && (filter & DO_REVS))
458 show(arg);
459 continue;
460 }
461
178cb243
LT
462 if (*arg == '-') {
463 if (!strcmp(arg, "--")) {
fb18a2ed 464 as_is = 2;
a08b6505
LT
465 /* Pass on the "--" if we show anything but files.. */
466 if (filter & (DO_FLAGS | DO_REVS))
467 show_file(arg);
4866ccf0 468 continue;
178cb243
LT
469 }
470 if (!strcmp(arg, "--default")) {
178cb243
LT
471 def = argv[i+1];
472 i++;
473 continue;
474 }
8ebb0184 475 if (!strcmp(arg, "--revs-only")) {
4866ccf0 476 filter &= ~DO_NOREV;
8ebb0184
LT
477 continue;
478 }
479 if (!strcmp(arg, "--no-revs")) {
4866ccf0 480 filter &= ~DO_REVS;
8ebb0184
LT
481 continue;
482 }
f79b65aa 483 if (!strcmp(arg, "--flags")) {
4866ccf0 484 filter &= ~DO_NONFLAGS;
f79b65aa
LT
485 continue;
486 }
487 if (!strcmp(arg, "--no-flags")) {
4866ccf0 488 filter &= ~DO_FLAGS;
f79b65aa
LT
489 continue;
490 }
023d66ed 491 if (!strcmp(arg, "--verify")) {
4866ccf0
JH
492 filter &= ~(DO_FLAGS|DO_NOREV);
493 verify = 1;
023d66ed 494 continue;
921d865e 495 }
b1b35969
CC
496 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
497 quiet = 1;
498 continue;
499 }
62a604ba 500 if (!strcmp(arg, "--short") ||
cc44c765 501 !prefixcmp(arg, "--short=")) {
d5012508
JH
502 filter &= ~(DO_FLAGS|DO_NOREV);
503 verify = 1;
504 abbrev = DEFAULT_ABBREV;
44de0da4
JF
505 if (arg[7] == '=')
506 abbrev = strtoul(arg + 8, NULL, 10);
1dc4fb84
JH
507 if (abbrev < MINIMUM_ABBREV)
508 abbrev = MINIMUM_ABBREV;
509 else if (40 <= abbrev)
510 abbrev = 40;
d5012508
JH
511 continue;
512 }
5bb2c65a
JH
513 if (!strcmp(arg, "--sq")) {
514 output_sq = 1;
515 continue;
516 }
042a4ed7
LT
517 if (!strcmp(arg, "--not")) {
518 show_type ^= REVERSED;
519 continue;
520 }
30b96fce 521 if (!strcmp(arg, "--symbolic")) {
a6d97d49
JH
522 symbolic = SHOW_SYMBOLIC_ASIS;
523 continue;
524 }
525 if (!strcmp(arg, "--symbolic-full-name")) {
526 symbolic = SHOW_SYMBOLIC_FULL;
30b96fce
JH
527 continue;
528 }
a45d3469
BW
529 if (!prefixcmp(arg, "--abbrev-ref") &&
530 (!arg[12] || arg[12] == '=')) {
531 abbrev_ref = 1;
532 abbrev_ref_strict = warn_ambiguous_refs;
533 if (arg[12] == '=') {
534 if (!strcmp(arg + 13, "strict"))
535 abbrev_ref_strict = 1;
536 else if (!strcmp(arg + 13, "loose"))
537 abbrev_ref_strict = 0;
538 else
539 die("unknown mode for %s", arg);
540 }
541 continue;
542 }
960bba0d 543 if (!strcmp(arg, "--all")) {
cb5d709f 544 for_each_ref(show_reference, NULL);
960bba0d
LT
545 continue;
546 }
a62be77f 547 if (!strcmp(arg, "--branches")) {
cb5d709f 548 for_each_branch_ref(show_reference, NULL);
a62be77f
SE
549 continue;
550 }
551 if (!strcmp(arg, "--tags")) {
cb5d709f 552 for_each_tag_ref(show_reference, NULL);
a62be77f
SE
553 continue;
554 }
555 if (!strcmp(arg, "--remotes")) {
cb5d709f 556 for_each_remote_ref(show_reference, NULL);
a62be77f
SE
557 continue;
558 }
d288a700 559 if (!strcmp(arg, "--show-prefix")) {
4866ccf0
JH
560 if (prefix)
561 puts(prefix);
d288a700
LT
562 continue;
563 }
5f94c730
JH
564 if (!strcmp(arg, "--show-cdup")) {
565 const char *pfx = prefix;
e90fdc39
JS
566 if (!is_inside_work_tree()) {
567 const char *work_tree =
568 get_git_work_tree();
569 if (work_tree)
570 printf("%s\n", work_tree);
571 continue;
572 }
5f94c730
JH
573 while (pfx) {
574 pfx = strchr(pfx, '/');
575 if (pfx) {
576 pfx++;
577 printf("../");
578 }
579 }
580 putchar('\n');
581 continue;
582 }
a8783eeb
LT
583 if (!strcmp(arg, "--git-dir")) {
584 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
585 static char cwd[PATH_MAX];
586 if (gitdir) {
587 puts(gitdir);
588 continue;
589 }
590 if (!prefix) {
591 puts(".git");
592 continue;
593 }
594 if (!getcwd(cwd, PATH_MAX))
595 die("unable to get current working directory");
596 printf("%s/.git\n", cwd);
597 continue;
598 }
6d9ba67b
JS
599 if (!strcmp(arg, "--is-inside-git-dir")) {
600 printf("%s\n", is_inside_git_dir() ? "true"
601 : "false");
602 continue;
603 }
892c41b9
ML
604 if (!strcmp(arg, "--is-inside-work-tree")) {
605 printf("%s\n", is_inside_work_tree() ? "true"
606 : "false");
607 continue;
608 }
493c774e
ML
609 if (!strcmp(arg, "--is-bare-repository")) {
610 printf("%s\n", is_bare_repository() ? "true"
611 : "false");
612 continue;
613 }
cc44c765 614 if (!prefixcmp(arg, "--since=")) {
c1babb1d
LT
615 show_datestring("--max-age=", arg+8);
616 continue;
617 }
cc44c765 618 if (!prefixcmp(arg, "--after=")) {
c1babb1d
LT
619 show_datestring("--max-age=", arg+8);
620 continue;
621 }
cc44c765 622 if (!prefixcmp(arg, "--before=")) {
c1babb1d
LT
623 show_datestring("--min-age=", arg+9);
624 continue;
625 }
cc44c765 626 if (!prefixcmp(arg, "--until=")) {
c1babb1d
LT
627 show_datestring("--min-age=", arg+8);
628 continue;
629 }
9523a4c2 630 if (show_flag(arg) && verify)
b1b35969 631 die_no_single_rev(quiet);
178cb243
LT
632 continue;
633 }
4866ccf0
JH
634
635 /* Not a flag argument */
3dd4e732
SB
636 if (try_difference(arg))
637 continue;
2122f8b9
BS
638 if (try_parent_shorthands(arg))
639 continue;
dfd1b749
CC
640 name = arg;
641 type = NORMAL;
642 if (*arg == '^') {
643 name++;
644 type = REVERSED;
800644c5 645 }
dfd1b749
CC
646 if (!get_sha1(name, sha1)) {
647 if (verify)
648 revs_count++;
649 else
650 show_rev(type, sha1, name);
800644c5
LT
651 continue;
652 }
75ecfce3
CC
653 if (verify)
654 die_no_single_rev(quiet);
9ad0a933
LT
655 as_is = 1;
656 if (!show_file(arg))
657 continue;
e23d0b4a 658 verify_filename(prefix, arg);
023d66ed 659 }
dfd1b749
CC
660 if (verify) {
661 if (revs_count == 1) {
662 show_rev(type, sha1, name);
663 return 0;
664 } else if (revs_count == 0 && show_default())
665 return 0;
b1b35969 666 die_no_single_rev(quiet);
dfd1b749
CC
667 } else
668 show_default();
178cb243
LT
669 return 0;
670}