config: Give error message when not changing a multivar
[git/dscho.git] / builtin / rev-parse.c
blobadb1cae4f27ad3993aa2418f7b0fd355d19cdab9
1 /*
2 * rev-parse.c
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "commit.h"
8 #include "refs.h"
9 #include "quote.h"
10 #include "builtin.h"
11 #include "parse-options.h"
13 #define DO_REVS 1
14 #define DO_NOREV 2
15 #define DO_FLAGS 4
16 #define DO_NONFLAGS 8
17 static int filter = ~0;
19 static const char *def;
21 #define NORMAL 0
22 #define REVERSED 1
23 static int show_type = NORMAL;
25 #define SHOW_SYMBOLIC_ASIS 1
26 #define SHOW_SYMBOLIC_FULL 2
27 static int symbolic;
28 static int abbrev;
29 static int abbrev_ref;
30 static int abbrev_ref_strict;
31 static int output_sq;
34 * Some arguments are relevant "revision" arguments,
35 * others are about output format or other details.
36 * This sorts it all out.
38 static int is_rev_argument(const char *arg)
40 static const char *rev_args[] = {
41 "--all",
42 "--bisect",
43 "--dense",
44 "--branches=",
45 "--branches",
46 "--header",
47 "--max-age=",
48 "--max-count=",
49 "--min-age=",
50 "--no-merges",
51 "--min-parents=",
52 "--no-min-parents",
53 "--max-parents=",
54 "--no-max-parents",
55 "--objects",
56 "--objects-edge",
57 "--parents",
58 "--pretty",
59 "--remotes=",
60 "--remotes",
61 "--glob=",
62 "--sparse",
63 "--tags=",
64 "--tags",
65 "--topo-order",
66 "--date-order",
67 "--unpacked",
68 NULL
70 const char **p = rev_args;
72 /* accept -<digit>, like traditional "head" */
73 if ((*arg == '-') && isdigit(arg[1]))
74 return 1;
76 for (;;) {
77 const char *str = *p++;
78 int len;
79 if (!str)
80 return 0;
81 len = strlen(str);
82 if (!strcmp(arg, str) ||
83 (str[len-1] == '=' && !strncmp(arg, str, len)))
84 return 1;
88 /* Output argument as a string, either SQ or normal */
89 static void show(const char *arg)
91 if (output_sq) {
92 int sq = '\'', ch;
94 putchar(sq);
95 while ((ch = *arg++)) {
96 if (ch == sq)
97 fputs("'\\'", stdout);
98 putchar(ch);
100 putchar(sq);
101 putchar(' ');
103 else
104 puts(arg);
107 /* Like show(), but with a negation prefix according to type */
108 static void show_with_type(int type, const char *arg)
110 if (type != show_type)
111 putchar('^');
112 show(arg);
115 /* Output a revision, only if filter allows it */
116 static void show_rev(int type, const unsigned char *sha1, const char *name)
118 if (!(filter & DO_REVS))
119 return;
120 def = NULL;
122 if ((symbolic || abbrev_ref) && name) {
123 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
124 unsigned char discard[20];
125 char *full;
127 switch (dwim_ref(name, strlen(name), discard, &full)) {
128 case 0:
130 * Not found -- not a ref. We could
131 * emit "name" here, but symbolic-full
132 * users are interested in finding the
133 * refs spelled in full, and they would
134 * need to filter non-refs if we did so.
136 break;
137 case 1: /* happy */
138 if (abbrev_ref)
139 full = shorten_unambiguous_ref(full,
140 abbrev_ref_strict);
141 show_with_type(type, full);
142 break;
143 default: /* ambiguous */
144 error("refname '%s' is ambiguous", name);
145 break;
147 } else {
148 show_with_type(type, name);
151 else if (abbrev)
152 show_with_type(type, find_unique_abbrev(sha1, abbrev));
153 else
154 show_with_type(type, sha1_to_hex(sha1));
157 /* Output a flag, only if filter allows it. */
158 static int show_flag(const char *arg)
160 if (!(filter & DO_FLAGS))
161 return 0;
162 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
163 show(arg);
164 return 1;
166 return 0;
169 static int show_default(void)
171 const char *s = def;
173 if (s) {
174 unsigned char sha1[20];
176 def = NULL;
177 if (!get_sha1(s, sha1)) {
178 show_rev(NORMAL, sha1, s);
179 return 1;
182 return 0;
185 static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
187 show_rev(NORMAL, sha1, refname);
188 return 0;
191 static int anti_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
193 show_rev(REVERSED, sha1, refname);
194 return 0;
197 static void show_datestring(const char *flag, const char *datestr)
199 static char buffer[100];
201 /* date handling requires both flags and revs */
202 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
203 return;
204 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
205 show(buffer);
208 static int show_file(const char *arg)
210 show_default();
211 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
212 show(arg);
213 return 1;
215 return 0;
218 static int try_difference(const char *arg)
220 char *dotdot;
221 unsigned char sha1[20];
222 unsigned char end[20];
223 const char *next;
224 const char *this;
225 int symmetric;
227 if (!(dotdot = strstr(arg, "..")))
228 return 0;
229 next = dotdot + 2;
230 this = arg;
231 symmetric = (*next == '.');
233 *dotdot = 0;
234 next += symmetric;
236 if (!*next)
237 next = "HEAD";
238 if (dotdot == arg)
239 this = "HEAD";
240 if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
241 show_rev(NORMAL, end, next);
242 show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
243 if (symmetric) {
244 struct commit_list *exclude;
245 struct commit *a, *b;
246 a = lookup_commit_reference(sha1);
247 b = lookup_commit_reference(end);
248 exclude = get_merge_bases(a, b, 1);
249 while (exclude) {
250 struct commit_list *n = exclude->next;
251 show_rev(REVERSED,
252 exclude->item->object.sha1,NULL);
253 free(exclude);
254 exclude = n;
257 return 1;
259 *dotdot = '.';
260 return 0;
263 static int try_parent_shorthands(const char *arg)
265 char *dotdot;
266 unsigned char sha1[20];
267 struct commit *commit;
268 struct commit_list *parents;
269 int parents_only;
271 if ((dotdot = strstr(arg, "^!")))
272 parents_only = 0;
273 else if ((dotdot = strstr(arg, "^@")))
274 parents_only = 1;
276 if (!dotdot || dotdot[2])
277 return 0;
279 *dotdot = 0;
280 if (get_sha1(arg, sha1))
281 return 0;
283 if (!parents_only)
284 show_rev(NORMAL, sha1, arg);
285 commit = lookup_commit_reference(sha1);
286 for (parents = commit->parents; parents; parents = parents->next)
287 show_rev(parents_only ? NORMAL : REVERSED,
288 parents->item->object.sha1, arg);
290 return 1;
293 static int parseopt_dump(const struct option *o, const char *arg, int unset)
295 struct strbuf *parsed = o->value;
296 if (unset)
297 strbuf_addf(parsed, " --no-%s", o->long_name);
298 else if (o->short_name)
299 strbuf_addf(parsed, " -%c", o->short_name);
300 else
301 strbuf_addf(parsed, " --%s", o->long_name);
302 if (arg) {
303 strbuf_addch(parsed, ' ');
304 sq_quote_buf(parsed, arg);
306 return 0;
309 static const char *skipspaces(const char *s)
311 while (isspace(*s))
312 s++;
313 return s;
316 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
318 static int keep_dashdash = 0, stop_at_non_option = 0;
319 static char const * const parseopt_usage[] = {
320 "git rev-parse --parseopt [options] -- [<args>...]",
321 NULL
323 static struct option parseopt_opts[] = {
324 OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash,
325 "keep the `--` passed as an arg"),
326 OPT_BOOLEAN(0, "stop-at-non-option", &stop_at_non_option,
327 "stop parsing after the "
328 "first non-option argument"),
329 OPT_END(),
332 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
333 const char **usage = NULL;
334 struct option *opts = NULL;
335 int onb = 0, osz = 0, unb = 0, usz = 0;
337 strbuf_addstr(&parsed, "set --");
338 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
339 PARSE_OPT_KEEP_DASHDASH);
340 if (argc < 1 || strcmp(argv[0], "--"))
341 usage_with_options(parseopt_usage, parseopt_opts);
343 /* get the usage up to the first line with a -- on it */
344 for (;;) {
345 if (strbuf_getline(&sb, stdin, '\n') == EOF)
346 die("premature end of input");
347 ALLOC_GROW(usage, unb + 1, usz);
348 if (!strcmp("--", sb.buf)) {
349 if (unb < 1)
350 die("no usage string given before the `--' separator");
351 usage[unb] = NULL;
352 break;
354 usage[unb++] = strbuf_detach(&sb, NULL);
357 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
358 while (strbuf_getline(&sb, stdin, '\n') != EOF) {
359 const char *s;
360 struct option *o;
362 if (!sb.len)
363 continue;
365 ALLOC_GROW(opts, onb + 1, osz);
366 memset(opts + onb, 0, sizeof(opts[onb]));
368 o = &opts[onb++];
369 s = strchr(sb.buf, ' ');
370 if (!s || *sb.buf == ' ') {
371 o->type = OPTION_GROUP;
372 o->help = xstrdup(skipspaces(sb.buf));
373 continue;
376 o->type = OPTION_CALLBACK;
377 o->help = xstrdup(skipspaces(s));
378 o->value = &parsed;
379 o->flags = PARSE_OPT_NOARG;
380 o->callback = &parseopt_dump;
381 while (s > sb.buf && strchr("*=?!", s[-1])) {
382 switch (*--s) {
383 case '=':
384 o->flags &= ~PARSE_OPT_NOARG;
385 break;
386 case '?':
387 o->flags &= ~PARSE_OPT_NOARG;
388 o->flags |= PARSE_OPT_OPTARG;
389 break;
390 case '!':
391 o->flags |= PARSE_OPT_NONEG;
392 break;
393 case '*':
394 o->flags |= PARSE_OPT_HIDDEN;
395 break;
399 if (s - sb.buf == 1) /* short option only */
400 o->short_name = *sb.buf;
401 else if (sb.buf[1] != ',') /* long option only */
402 o->long_name = xmemdupz(sb.buf, s - sb.buf);
403 else {
404 o->short_name = *sb.buf;
405 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
408 strbuf_release(&sb);
410 /* put an OPT_END() */
411 ALLOC_GROW(opts, onb + 1, osz);
412 memset(opts + onb, 0, sizeof(opts[onb]));
413 argc = parse_options(argc, argv, prefix, opts, usage,
414 (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
415 (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
416 PARSE_OPT_SHELL_EVAL);
418 strbuf_addf(&parsed, " --");
419 sq_quote_argv(&parsed, argv, 0);
420 puts(parsed.buf);
421 return 0;
424 static int cmd_sq_quote(int argc, const char **argv)
426 struct strbuf buf = STRBUF_INIT;
428 if (argc)
429 sq_quote_argv(&buf, argv, 0);
430 printf("%s\n", buf.buf);
431 strbuf_release(&buf);
433 return 0;
436 static void die_no_single_rev(int quiet)
438 if (quiet)
439 exit(1);
440 else
441 die("Needed a single revision");
444 static const char builtin_rev_parse_usage[] =
445 "git rev-parse --parseopt [options] -- [<args>...]\n"
446 " or: git rev-parse --sq-quote [<arg>...]\n"
447 " or: git rev-parse [options] [<arg>...]\n"
448 "\n"
449 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.";
451 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
453 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
454 unsigned char sha1[20];
455 const char *name = NULL;
457 if (argc > 1 && !strcmp("--parseopt", argv[1]))
458 return cmd_parseopt(argc - 1, argv + 1, prefix);
460 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
461 return cmd_sq_quote(argc - 2, argv + 2);
463 if (argc == 2 && !strcmp("--local-env-vars", argv[1])) {
464 int i;
465 for (i = 0; local_repo_env[i]; i++)
466 printf("%s\n", local_repo_env[i]);
467 return 0;
470 if (argc > 1 && !strcmp("-h", argv[1]))
471 usage(builtin_rev_parse_usage);
473 prefix = setup_git_directory();
474 git_config(git_default_config, NULL);
475 for (i = 1; i < argc; i++) {
476 const char *arg = argv[i];
478 if (as_is) {
479 if (show_file(arg) && as_is < 2)
480 verify_filename(prefix, arg);
481 continue;
483 if (!strcmp(arg,"-n")) {
484 if (++i >= argc)
485 die("-n requires an argument");
486 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
487 show(arg);
488 show(argv[i]);
490 continue;
492 if (!prefixcmp(arg, "-n")) {
493 if ((filter & DO_FLAGS) && (filter & DO_REVS))
494 show(arg);
495 continue;
498 if (*arg == '-') {
499 if (!strcmp(arg, "--")) {
500 as_is = 2;
501 /* Pass on the "--" if we show anything but files.. */
502 if (filter & (DO_FLAGS | DO_REVS))
503 show_file(arg);
504 continue;
506 if (!strcmp(arg, "--default")) {
507 def = argv[i+1];
508 i++;
509 continue;
511 if (!strcmp(arg, "--revs-only")) {
512 filter &= ~DO_NOREV;
513 continue;
515 if (!strcmp(arg, "--no-revs")) {
516 filter &= ~DO_REVS;
517 continue;
519 if (!strcmp(arg, "--flags")) {
520 filter &= ~DO_NONFLAGS;
521 continue;
523 if (!strcmp(arg, "--no-flags")) {
524 filter &= ~DO_FLAGS;
525 continue;
527 if (!strcmp(arg, "--verify")) {
528 filter &= ~(DO_FLAGS|DO_NOREV);
529 verify = 1;
530 continue;
532 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
533 quiet = 1;
534 continue;
536 if (!strcmp(arg, "--short") ||
537 !prefixcmp(arg, "--short=")) {
538 filter &= ~(DO_FLAGS|DO_NOREV);
539 verify = 1;
540 abbrev = DEFAULT_ABBREV;
541 if (arg[7] == '=')
542 abbrev = strtoul(arg + 8, NULL, 10);
543 if (abbrev < MINIMUM_ABBREV)
544 abbrev = MINIMUM_ABBREV;
545 else if (40 <= abbrev)
546 abbrev = 40;
547 continue;
549 if (!strcmp(arg, "--sq")) {
550 output_sq = 1;
551 continue;
553 if (!strcmp(arg, "--not")) {
554 show_type ^= REVERSED;
555 continue;
557 if (!strcmp(arg, "--symbolic")) {
558 symbolic = SHOW_SYMBOLIC_ASIS;
559 continue;
561 if (!strcmp(arg, "--symbolic-full-name")) {
562 symbolic = SHOW_SYMBOLIC_FULL;
563 continue;
565 if (!prefixcmp(arg, "--abbrev-ref") &&
566 (!arg[12] || arg[12] == '=')) {
567 abbrev_ref = 1;
568 abbrev_ref_strict = warn_ambiguous_refs;
569 if (arg[12] == '=') {
570 if (!strcmp(arg + 13, "strict"))
571 abbrev_ref_strict = 1;
572 else if (!strcmp(arg + 13, "loose"))
573 abbrev_ref_strict = 0;
574 else
575 die("unknown mode for %s", arg);
577 continue;
579 if (!strcmp(arg, "--all")) {
580 for_each_ref(show_reference, NULL);
581 continue;
583 if (!strcmp(arg, "--bisect")) {
584 for_each_ref_in("refs/bisect/bad", show_reference, NULL);
585 for_each_ref_in("refs/bisect/good", anti_reference, NULL);
586 continue;
588 if (!prefixcmp(arg, "--branches=")) {
589 for_each_glob_ref_in(show_reference, arg + 11,
590 "refs/heads/", NULL);
591 continue;
593 if (!strcmp(arg, "--branches")) {
594 for_each_branch_ref(show_reference, NULL);
595 continue;
597 if (!prefixcmp(arg, "--tags=")) {
598 for_each_glob_ref_in(show_reference, arg + 7,
599 "refs/tags/", NULL);
600 continue;
602 if (!strcmp(arg, "--tags")) {
603 for_each_tag_ref(show_reference, NULL);
604 continue;
606 if (!prefixcmp(arg, "--glob=")) {
607 for_each_glob_ref(show_reference, arg + 7, NULL);
608 continue;
610 if (!prefixcmp(arg, "--remotes=")) {
611 for_each_glob_ref_in(show_reference, arg + 10,
612 "refs/remotes/", NULL);
613 continue;
615 if (!strcmp(arg, "--remotes")) {
616 for_each_remote_ref(show_reference, NULL);
617 continue;
619 if (!strcmp(arg, "--show-toplevel")) {
620 const char *work_tree = get_git_work_tree();
621 if (work_tree)
622 puts(work_tree);
623 continue;
625 if (!strcmp(arg, "--show-prefix")) {
626 if (prefix)
627 puts(prefix);
628 continue;
630 if (!strcmp(arg, "--show-cdup")) {
631 const char *pfx = prefix;
632 if (!is_inside_work_tree()) {
633 const char *work_tree =
634 get_git_work_tree();
635 if (work_tree)
636 printf("%s\n", work_tree);
637 continue;
639 while (pfx) {
640 pfx = strchr(pfx, '/');
641 if (pfx) {
642 pfx++;
643 printf("../");
646 putchar('\n');
647 continue;
649 if (!strcmp(arg, "--git-dir")) {
650 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
651 static char cwd[PATH_MAX];
652 int len;
653 if (gitdir) {
654 puts(gitdir);
655 continue;
657 if (!prefix) {
658 puts(".git");
659 continue;
661 if (!getcwd(cwd, PATH_MAX))
662 die_errno("unable to get current working directory");
663 len = strlen(cwd);
664 printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : "");
665 continue;
667 if (!strcmp(arg, "--is-inside-git-dir")) {
668 printf("%s\n", is_inside_git_dir() ? "true"
669 : "false");
670 continue;
672 if (!strcmp(arg, "--is-inside-work-tree")) {
673 printf("%s\n", is_inside_work_tree() ? "true"
674 : "false");
675 continue;
677 if (!strcmp(arg, "--is-bare-repository")) {
678 printf("%s\n", is_bare_repository() ? "true"
679 : "false");
680 continue;
682 if (!prefixcmp(arg, "--since=")) {
683 show_datestring("--max-age=", arg+8);
684 continue;
686 if (!prefixcmp(arg, "--after=")) {
687 show_datestring("--max-age=", arg+8);
688 continue;
690 if (!prefixcmp(arg, "--before=")) {
691 show_datestring("--min-age=", arg+9);
692 continue;
694 if (!prefixcmp(arg, "--until=")) {
695 show_datestring("--min-age=", arg+8);
696 continue;
698 if (show_flag(arg) && verify)
699 die_no_single_rev(quiet);
700 continue;
703 /* Not a flag argument */
704 if (try_difference(arg))
705 continue;
706 if (try_parent_shorthands(arg))
707 continue;
708 name = arg;
709 type = NORMAL;
710 if (*arg == '^') {
711 name++;
712 type = REVERSED;
714 if (!get_sha1(name, sha1)) {
715 if (verify)
716 revs_count++;
717 else
718 show_rev(type, sha1, name);
719 continue;
721 if (verify)
722 die_no_single_rev(quiet);
723 as_is = 1;
724 if (!show_file(arg))
725 continue;
726 verify_filename(prefix, arg);
728 if (verify) {
729 if (revs_count == 1) {
730 show_rev(type, sha1, name);
731 return 0;
732 } else if (revs_count == 0 && show_default())
733 return 0;
734 die_no_single_rev(quiet);
735 } else
736 show_default();
737 return 0;