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