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;
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[] = {
70 const char **p = rev_args;
72 /* accept -<digit>, like traditional "head" */
73 if ((*arg == '-') && isdigit(arg[1]))
77 const char *str = *p++;
82 if (!strcmp(arg, str) ||
83 (str[len-1] == '=' && !strncmp(arg, str, len)))
88 /* Output argument as a string, either SQ or normal */
89 static void show(const char *arg)
95 while ((ch = *arg++)) {
97 fputs("'\\'", stdout);
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)
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))
122 if ((symbolic || abbrev_ref) && name) {
123 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
124 unsigned char discard[20];
127 switch (dwim_ref(name, strlen(name), discard, &full)) {
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.
139 full = shorten_unambiguous_ref(full,
141 show_with_type(type, full);
143 default: /* ambiguous */
144 error("refname '%s' is ambiguous", name);
148 show_with_type(type, name);
152 show_with_type(type, find_unique_abbrev(sha1, abbrev));
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))
162 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
169 static int show_default(void)
174 unsigned char sha1[20];
177 if (!get_sha1(s, sha1)) {
178 show_rev(NORMAL, sha1, s);
185 static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
187 show_rev(NORMAL, sha1, refname);
191 static int anti_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
193 show_rev(REVERSED, sha1, refname);
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))
204 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
208 static int show_file(const char *arg)
211 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
218 static int try_difference(const char *arg)
221 unsigned char sha1[20];
222 unsigned char end[20];
227 if (!(dotdot = strstr(arg, "..")))
231 symmetric = (*next == '.');
240 if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
241 show_rev(NORMAL, end, next);
242 show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
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);
250 struct commit_list *n = exclude->next;
252 exclude->item->object.sha1,NULL);
263 static int try_parent_shorthands(const char *arg)
266 unsigned char sha1[20];
267 struct commit *commit;
268 struct commit_list *parents;
271 if ((dotdot = strstr(arg, "^!")))
273 else if ((dotdot = strstr(arg, "^@")))
276 if (!dotdot || dotdot[2])
280 if (get_sha1(arg, sha1))
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);
293 static int parseopt_dump(const struct option *o, const char *arg, int unset)
295 struct strbuf *parsed = o->value;
297 strbuf_addf(parsed, " --no-%s", o->long_name);
298 else if (o->short_name)
299 strbuf_addf(parsed, " -%c", o->short_name);
301 strbuf_addf(parsed, " --%s", o->long_name);
303 strbuf_addch(parsed, ' ');
304 sq_quote_buf(parsed, arg);
309 static const char *skipspaces(const char *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>...]",
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"),
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 */
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)) {
350 die("no usage string given before the `--' separator");
354 usage[unb++] = strbuf_detach(&sb, NULL);
357 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
358 while (strbuf_getline(&sb, stdin, '\n') != EOF) {
365 ALLOC_GROW(opts, onb + 1, osz);
366 memset(opts + onb, 0, sizeof(opts[onb]));
369 s = strchr(sb.buf, ' ');
370 if (!s || *sb.buf == ' ') {
371 o->type = OPTION_GROUP;
372 o->help = xstrdup(skipspaces(sb.buf));
376 o->type = OPTION_CALLBACK;
377 o->help = xstrdup(skipspaces(s));
379 o->flags = PARSE_OPT_NOARG;
380 o->callback = &parseopt_dump;
381 while (s > sb.buf && strchr("*=?!", s[-1])) {
384 o->flags &= ~PARSE_OPT_NOARG;
387 o->flags &= ~PARSE_OPT_NOARG;
388 o->flags |= PARSE_OPT_OPTARG;
391 o->flags |= PARSE_OPT_NONEG;
394 o->flags |= PARSE_OPT_HIDDEN;
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);
404 o->short_name = *sb.buf;
405 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
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);
424 static int cmd_sq_quote(int argc, const char **argv)
426 struct strbuf buf = STRBUF_INIT;
429 sq_quote_argv(&buf, argv, 0);
430 printf("%s\n", buf.buf);
431 strbuf_release(&buf);
436 static void die_no_single_rev(int quiet)
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"
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])) {
465 for (i = 0; local_repo_env[i]; i++)
466 printf("%s\n", local_repo_env[i]);
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];
479 if (show_file(arg) && as_is < 2)
480 verify_filename(prefix, arg);
483 if (!strcmp(arg,"-n")) {
485 die("-n requires an argument");
486 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
492 if (!prefixcmp(arg, "-n")) {
493 if ((filter & DO_FLAGS) && (filter & DO_REVS))
499 if (!strcmp(arg, "--")) {
501 /* Pass on the "--" if we show anything but files.. */
502 if (filter & (DO_FLAGS | DO_REVS))
506 if (!strcmp(arg, "--default")) {
511 if (!strcmp(arg, "--revs-only")) {
515 if (!strcmp(arg, "--no-revs")) {
519 if (!strcmp(arg, "--flags")) {
520 filter &= ~DO_NONFLAGS;
523 if (!strcmp(arg, "--no-flags")) {
527 if (!strcmp(arg, "--verify")) {
528 filter &= ~(DO_FLAGS|DO_NOREV);
532 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
536 if (!strcmp(arg, "--short") ||
537 !prefixcmp(arg, "--short=")) {
538 filter &= ~(DO_FLAGS|DO_NOREV);
540 abbrev = DEFAULT_ABBREV;
542 abbrev = strtoul(arg + 8, NULL, 10);
543 if (abbrev < MINIMUM_ABBREV)
544 abbrev = MINIMUM_ABBREV;
545 else if (40 <= abbrev)
549 if (!strcmp(arg, "--sq")) {
553 if (!strcmp(arg, "--not")) {
554 show_type ^= REVERSED;
557 if (!strcmp(arg, "--symbolic")) {
558 symbolic = SHOW_SYMBOLIC_ASIS;
561 if (!strcmp(arg, "--symbolic-full-name")) {
562 symbolic = SHOW_SYMBOLIC_FULL;
565 if (!prefixcmp(arg, "--abbrev-ref") &&
566 (!arg[12] || arg[12] == '=')) {
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;
575 die("unknown mode for %s", arg);
579 if (!strcmp(arg, "--all")) {
580 for_each_ref(show_reference, NULL);
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);
588 if (!prefixcmp(arg, "--branches=")) {
589 for_each_glob_ref_in(show_reference, arg + 11,
590 "refs/heads/", NULL);
593 if (!strcmp(arg, "--branches")) {
594 for_each_branch_ref(show_reference, NULL);
597 if (!prefixcmp(arg, "--tags=")) {
598 for_each_glob_ref_in(show_reference, arg + 7,
602 if (!strcmp(arg, "--tags")) {
603 for_each_tag_ref(show_reference, NULL);
606 if (!prefixcmp(arg, "--glob=")) {
607 for_each_glob_ref(show_reference, arg + 7, NULL);
610 if (!prefixcmp(arg, "--remotes=")) {
611 for_each_glob_ref_in(show_reference, arg + 10,
612 "refs/remotes/", NULL);
615 if (!strcmp(arg, "--remotes")) {
616 for_each_remote_ref(show_reference, NULL);
619 if (!strcmp(arg, "--show-toplevel")) {
620 const char *work_tree = get_git_work_tree();
625 if (!strcmp(arg, "--show-prefix")) {
630 if (!strcmp(arg, "--show-cdup")) {
631 const char *pfx = prefix;
632 if (!is_inside_work_tree()) {
633 const char *work_tree =
636 printf("%s\n", work_tree);
640 pfx = strchr(pfx, '/');
649 if (!strcmp(arg, "--git-dir")) {
650 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
651 static char cwd[PATH_MAX];
661 if (!getcwd(cwd, PATH_MAX))
662 die_errno("unable to get current working directory");
664 printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : "");
667 if (!strcmp(arg, "--is-inside-git-dir")) {
668 printf("%s\n", is_inside_git_dir() ? "true"
672 if (!strcmp(arg, "--is-inside-work-tree")) {
673 printf("%s\n", is_inside_work_tree() ? "true"
677 if (!strcmp(arg, "--is-bare-repository")) {
678 printf("%s\n", is_bare_repository() ? "true"
682 if (!prefixcmp(arg, "--since=")) {
683 show_datestring("--max-age=", arg+8);
686 if (!prefixcmp(arg, "--after=")) {
687 show_datestring("--max-age=", arg+8);
690 if (!prefixcmp(arg, "--before=")) {
691 show_datestring("--min-age=", arg+9);
694 if (!prefixcmp(arg, "--until=")) {
695 show_datestring("--min-age=", arg+8);
698 if (show_flag(arg) && verify)
699 die_no_single_rev(quiet);
703 /* Not a flag argument */
704 if (try_difference(arg))
706 if (try_parent_shorthands(arg))
714 if (!get_sha1(name, sha1)) {
718 show_rev(type, sha1, name);
722 die_no_single_rev(quiet);
726 verify_filename(prefix, arg);
729 if (revs_count == 1) {
730 show_rev(type, sha1, name);
732 } else if (revs_count == 0 && show_default())
734 die_no_single_rev(quiet);