4 * Copyright (C) Linus Torvalds, 2005
11 #include "parse-options.h"
17 static int filter = ~0;
19 static const char *def;
23 static int show_type = NORMAL;
25 #define SHOW_SYMBOLIC_ASIS 1
26 #define SHOW_SYMBOLIC_FULL 2
29 static int abbrev_ref;
30 static int abbrev_ref_strict;
33 static int stuck_long;
36 * Some arguments are relevant "revision" arguments,
37 * others are about output format or other details.
38 * This sorts it all out.
40 static int is_rev_argument(const char *arg)
42 static const char *rev_args[] = {
73 const char **p = rev_args;
75 /* accept -<digit>, like traditional "head" */
76 if ((*arg == '-') && isdigit(arg[1]))
80 const char *str = *p++;
85 if (!strcmp(arg, str) ||
86 (str[len-1] == '=' && !strncmp(arg, str, len)))
91 /* Output argument as a string, either SQ or normal */
92 static void show(const char *arg)
98 while ((ch = *arg++)) {
100 fputs("'\\'", stdout);
110 /* Like show(), but with a negation prefix according to type */
111 static void show_with_type(int type, const char *arg)
113 if (type != show_type)
118 /* Output a revision, only if filter allows it */
119 static void show_rev(int type, const unsigned char *sha1, const char *name)
121 if (!(filter & DO_REVS))
125 if ((symbolic || abbrev_ref) && name) {
126 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
127 unsigned char discard[20];
130 switch (dwim_ref(name, strlen(name), discard, &full)) {
133 * Not found -- not a ref. We could
134 * emit "name" here, but symbolic-full
135 * users are interested in finding the
136 * refs spelled in full, and they would
137 * need to filter non-refs if we did so.
142 full = shorten_unambiguous_ref(full,
144 show_with_type(type, full);
146 default: /* ambiguous */
147 error("refname '%s' is ambiguous", name);
151 show_with_type(type, name);
155 show_with_type(type, find_unique_abbrev(sha1, abbrev));
157 show_with_type(type, sha1_to_hex(sha1));
160 /* Output a flag, only if filter allows it. */
161 static int show_flag(const char *arg)
163 if (!(filter & DO_FLAGS))
165 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
172 static int show_default(void)
177 unsigned char sha1[20];
180 if (!get_sha1(s, sha1)) {
181 show_rev(NORMAL, sha1, s);
188 static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
190 show_rev(NORMAL, sha1, refname);
194 static int anti_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
196 show_rev(REVERSED, sha1, refname);
200 static int show_abbrev(const unsigned char *sha1, void *cb_data)
202 show_rev(NORMAL, sha1, NULL);
206 static void show_datestring(const char *flag, const char *datestr)
208 static char buffer[100];
210 /* date handling requires both flags and revs */
211 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
213 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
217 static int show_file(const char *arg, int output_prefix)
220 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
222 const char *prefix = startup_info->prefix;
223 show(prefix_filename(prefix,
224 prefix ? strlen(prefix) : 0,
233 static int try_difference(const char *arg)
236 unsigned char sha1[20];
237 unsigned char end[20];
241 static const char head_by_default[] = "HEAD";
243 if (!(dotdot = strstr(arg, "..")))
247 symmetric = (*next == '.');
253 next = head_by_default;
255 this = head_by_default;
257 if (this == head_by_default && next == head_by_default &&
260 * Just ".."? That is not a range but the
261 * pathspec for the parent directory.
267 if (!get_sha1_committish(this, sha1) && !get_sha1_committish(next, end)) {
268 show_rev(NORMAL, end, next);
269 show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
271 struct commit_list *exclude;
272 struct commit *a, *b;
273 a = lookup_commit_reference(sha1);
274 b = lookup_commit_reference(end);
275 exclude = get_merge_bases(a, b, 1);
277 struct commit_list *n = exclude->next;
279 exclude->item->object.sha1,NULL);
290 static int try_parent_shorthands(const char *arg)
293 unsigned char sha1[20];
294 struct commit *commit;
295 struct commit_list *parents;
298 if ((dotdot = strstr(arg, "^!")))
300 else if ((dotdot = strstr(arg, "^@")))
303 if (!dotdot || dotdot[2])
307 if (get_sha1_committish(arg, sha1))
311 show_rev(NORMAL, sha1, arg);
312 commit = lookup_commit_reference(sha1);
313 for (parents = commit->parents; parents; parents = parents->next)
314 show_rev(parents_only ? NORMAL : REVERSED,
315 parents->item->object.sha1, arg);
320 static int parseopt_dump(const struct option *o, const char *arg, int unset)
322 struct strbuf *parsed = o->value;
324 strbuf_addf(parsed, " --no-%s", o->long_name);
325 else if (o->short_name && (o->long_name == NULL || !stuck_long))
326 strbuf_addf(parsed, " -%c", o->short_name);
328 strbuf_addf(parsed, " --%s", o->long_name);
331 strbuf_addch(parsed, ' ');
332 else if (o->long_name)
333 strbuf_addch(parsed, '=');
334 sq_quote_buf(parsed, arg);
339 static const char *skipspaces(const char *s)
346 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
348 static int keep_dashdash = 0, stop_at_non_option = 0;
349 static char const * const parseopt_usage[] = {
350 N_("git rev-parse --parseopt [options] -- [<args>...]"),
353 static struct option parseopt_opts[] = {
354 OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
355 N_("keep the `--` passed as an arg")),
356 OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
357 N_("stop parsing after the "
358 "first non-option argument")),
359 OPT_BOOL(0, "stuck-long", &stuck_long,
360 N_("output in stuck long form")),
364 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
365 const char **usage = NULL;
366 struct option *opts = NULL;
367 int onb = 0, osz = 0, unb = 0, usz = 0;
369 strbuf_addstr(&parsed, "set --");
370 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
371 PARSE_OPT_KEEP_DASHDASH);
372 if (argc < 1 || strcmp(argv[0], "--"))
373 usage_with_options(parseopt_usage, parseopt_opts);
375 /* get the usage up to the first line with a -- on it */
377 if (strbuf_getline(&sb, stdin, '\n') == EOF)
378 die("premature end of input");
379 ALLOC_GROW(usage, unb + 1, usz);
380 if (!strcmp("--", sb.buf)) {
382 die("no usage string given before the `--' separator");
386 usage[unb++] = strbuf_detach(&sb, NULL);
389 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
390 while (strbuf_getline(&sb, stdin, '\n') != EOF) {
397 ALLOC_GROW(opts, onb + 1, osz);
398 memset(opts + onb, 0, sizeof(opts[onb]));
401 s = strchr(sb.buf, ' ');
402 if (!s || *sb.buf == ' ') {
403 o->type = OPTION_GROUP;
404 o->help = xstrdup(skipspaces(sb.buf));
408 o->type = OPTION_CALLBACK;
409 o->help = xstrdup(skipspaces(s));
411 o->flags = PARSE_OPT_NOARG;
412 o->callback = &parseopt_dump;
413 while (s > sb.buf && strchr("*=?!", s[-1])) {
416 o->flags &= ~PARSE_OPT_NOARG;
419 o->flags &= ~PARSE_OPT_NOARG;
420 o->flags |= PARSE_OPT_OPTARG;
423 o->flags |= PARSE_OPT_NONEG;
426 o->flags |= PARSE_OPT_HIDDEN;
431 if (s - sb.buf == 1) /* short option only */
432 o->short_name = *sb.buf;
433 else if (sb.buf[1] != ',') /* long option only */
434 o->long_name = xmemdupz(sb.buf, s - sb.buf);
436 o->short_name = *sb.buf;
437 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
442 /* put an OPT_END() */
443 ALLOC_GROW(opts, onb + 1, osz);
444 memset(opts + onb, 0, sizeof(opts[onb]));
445 argc = parse_options(argc, argv, prefix, opts, usage,
446 (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
447 (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
448 PARSE_OPT_SHELL_EVAL);
450 strbuf_addf(&parsed, " --");
451 sq_quote_argv(&parsed, argv, 0);
456 static int cmd_sq_quote(int argc, const char **argv)
458 struct strbuf buf = STRBUF_INIT;
461 sq_quote_argv(&buf, argv, 0);
462 printf("%s\n", buf.buf);
463 strbuf_release(&buf);
468 static void die_no_single_rev(int quiet)
473 die("Needed a single revision");
476 static const char builtin_rev_parse_usage[] =
477 N_("git rev-parse --parseopt [options] -- [<args>...]\n"
478 " or: git rev-parse --sq-quote [<arg>...]\n"
479 " or: git rev-parse [options] [<arg>...]\n"
481 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
483 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
485 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
486 int output_prefix = 0;
487 unsigned char sha1[20];
488 const char *name = NULL;
490 if (argc > 1 && !strcmp("--parseopt", argv[1]))
491 return cmd_parseopt(argc - 1, argv + 1, prefix);
493 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
494 return cmd_sq_quote(argc - 2, argv + 2);
496 if (argc > 1 && !strcmp("-h", argv[1]))
497 usage(builtin_rev_parse_usage);
499 prefix = setup_git_directory();
500 git_config(git_default_config, NULL);
501 for (i = 1; i < argc; i++) {
502 const char *arg = argv[i];
505 if (show_file(arg, output_prefix) && as_is < 2)
506 verify_filename(prefix, arg, 0);
509 if (!strcmp(arg,"-n")) {
511 die("-n requires an argument");
512 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
518 if (!prefixcmp(arg, "-n")) {
519 if ((filter & DO_FLAGS) && (filter & DO_REVS))
525 if (!strcmp(arg, "--")) {
527 /* Pass on the "--" if we show anything but files.. */
528 if (filter & (DO_FLAGS | DO_REVS))
532 if (!strcmp(arg, "--default")) {
537 if (!strcmp(arg, "--prefix")) {
539 startup_info->prefix = prefix;
544 if (!strcmp(arg, "--revs-only")) {
548 if (!strcmp(arg, "--no-revs")) {
552 if (!strcmp(arg, "--flags")) {
553 filter &= ~DO_NONFLAGS;
556 if (!strcmp(arg, "--no-flags")) {
560 if (!strcmp(arg, "--verify")) {
561 filter &= ~(DO_FLAGS|DO_NOREV);
565 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
569 if (!strcmp(arg, "--short") ||
570 !prefixcmp(arg, "--short=")) {
571 filter &= ~(DO_FLAGS|DO_NOREV);
573 abbrev = DEFAULT_ABBREV;
575 abbrev = strtoul(arg + 8, NULL, 10);
576 if (abbrev < MINIMUM_ABBREV)
577 abbrev = MINIMUM_ABBREV;
578 else if (40 <= abbrev)
582 if (!strcmp(arg, "--sq")) {
586 if (!strcmp(arg, "--not")) {
587 show_type ^= REVERSED;
590 if (!strcmp(arg, "--symbolic")) {
591 symbolic = SHOW_SYMBOLIC_ASIS;
594 if (!strcmp(arg, "--symbolic-full-name")) {
595 symbolic = SHOW_SYMBOLIC_FULL;
598 if (!prefixcmp(arg, "--abbrev-ref") &&
599 (!arg[12] || arg[12] == '=')) {
601 abbrev_ref_strict = warn_ambiguous_refs;
602 if (arg[12] == '=') {
603 if (!strcmp(arg + 13, "strict"))
604 abbrev_ref_strict = 1;
605 else if (!strcmp(arg + 13, "loose"))
606 abbrev_ref_strict = 0;
608 die("unknown mode for %s", arg);
612 if (!strcmp(arg, "--all")) {
613 for_each_ref(show_reference, NULL);
616 if (!prefixcmp(arg, "--disambiguate=")) {
617 for_each_abbrev(arg + 15, show_abbrev, NULL);
620 if (!strcmp(arg, "--bisect")) {
621 for_each_ref_in("refs/bisect/bad", show_reference, NULL);
622 for_each_ref_in("refs/bisect/good", anti_reference, NULL);
625 if (!prefixcmp(arg, "--branches=")) {
626 for_each_glob_ref_in(show_reference, arg + 11,
627 "refs/heads/", NULL);
630 if (!strcmp(arg, "--branches")) {
631 for_each_branch_ref(show_reference, NULL);
634 if (!prefixcmp(arg, "--tags=")) {
635 for_each_glob_ref_in(show_reference, arg + 7,
639 if (!strcmp(arg, "--tags")) {
640 for_each_tag_ref(show_reference, NULL);
643 if (!prefixcmp(arg, "--glob=")) {
644 for_each_glob_ref(show_reference, arg + 7, NULL);
647 if (!prefixcmp(arg, "--remotes=")) {
648 for_each_glob_ref_in(show_reference, arg + 10,
649 "refs/remotes/", NULL);
652 if (!strcmp(arg, "--remotes")) {
653 for_each_remote_ref(show_reference, NULL);
656 if (!strcmp(arg, "--local-env-vars")) {
658 for (i = 0; local_repo_env[i]; i++)
659 printf("%s\n", local_repo_env[i]);
662 if (!strcmp(arg, "--show-toplevel")) {
663 const char *work_tree = get_git_work_tree();
668 if (!strcmp(arg, "--show-prefix")) {
675 if (!strcmp(arg, "--show-cdup")) {
676 const char *pfx = prefix;
677 if (!is_inside_work_tree()) {
678 const char *work_tree =
681 printf("%s\n", work_tree);
685 pfx = strchr(pfx, '/');
694 if (!strcmp(arg, "--git-dir")) {
695 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
696 static char cwd[PATH_MAX];
706 if (!getcwd(cwd, PATH_MAX))
707 die_errno("unable to get current working directory");
709 printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : "");
712 if (!strcmp(arg, "--resolve-git-dir")) {
713 const char *gitdir = resolve_gitdir(argv[i+1]);
715 die("not a gitdir '%s'", argv[i+1]);
719 if (!strcmp(arg, "--is-inside-git-dir")) {
720 printf("%s\n", is_inside_git_dir() ? "true"
724 if (!strcmp(arg, "--is-inside-work-tree")) {
725 printf("%s\n", is_inside_work_tree() ? "true"
729 if (!strcmp(arg, "--is-bare-repository")) {
730 printf("%s\n", is_bare_repository() ? "true"
734 if (!prefixcmp(arg, "--since=")) {
735 show_datestring("--max-age=", arg+8);
738 if (!prefixcmp(arg, "--after=")) {
739 show_datestring("--max-age=", arg+8);
742 if (!prefixcmp(arg, "--before=")) {
743 show_datestring("--min-age=", arg+9);
746 if (!prefixcmp(arg, "--until=")) {
747 show_datestring("--min-age=", arg+8);
750 if (show_flag(arg) && verify)
751 die_no_single_rev(quiet);
755 /* Not a flag argument */
756 if (try_difference(arg))
758 if (try_parent_shorthands(arg))
766 if (!get_sha1(name, sha1)) {
770 show_rev(type, sha1, name);
774 die_no_single_rev(quiet);
776 if (!show_file(arg, output_prefix))
778 verify_filename(prefix, arg, 1);
781 if (revs_count == 1) {
782 show_rev(type, sha1, name);
784 } else if (revs_count == 0 && show_default())
786 die_no_single_rev(quiet);