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[] = {
63 const char **p = rev_args;
65 /* accept -<digit>, like traditional "head" */
66 if ((*arg == '-') && isdigit(arg[1]))
70 const char *str = *p++;
75 if (!strcmp(arg, str) ||
76 (str[len-1] == '=' && !strncmp(arg, str, len)))
81 /* Output argument as a string, either SQ or normal */
82 static void show(const char *arg)
88 while ((ch = *arg++)) {
90 fputs("'\\'", stdout);
100 /* Like show(), but with a negation prefix according to type */
101 static void show_with_type(int type, const char *arg)
103 if (type != show_type)
108 /* Output a revision, only if filter allows it */
109 static void show_rev(int type, const unsigned char *sha1, const char *name)
111 if (!(filter & DO_REVS))
115 if ((symbolic || abbrev_ref) && name) {
116 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
117 unsigned char discard[20];
120 switch (dwim_ref(name, strlen(name), discard, &full)) {
123 * Not found -- not a ref. We could
124 * emit "name" here, but symbolic-full
125 * users are interested in finding the
126 * refs spelled in full, and they would
127 * need to filter non-refs if we did so.
132 full = shorten_unambiguous_ref(full,
134 show_with_type(type, full);
136 default: /* ambiguous */
137 error("refname '%s' is ambiguous", name);
141 show_with_type(type, name);
145 show_with_type(type, find_unique_abbrev(sha1, abbrev));
147 show_with_type(type, sha1_to_hex(sha1));
150 /* Output a flag, only if filter allows it. */
151 static int show_flag(const char *arg)
153 if (!(filter & DO_FLAGS))
155 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
162 static int show_default(void)
167 unsigned char sha1[20];
170 if (!get_sha1(s, sha1)) {
171 show_rev(NORMAL, sha1, s);
178 static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
180 show_rev(NORMAL, sha1, refname);
184 static int anti_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
186 show_rev(REVERSED, sha1, refname);
190 static void show_datestring(const char *flag, const char *datestr)
192 static char buffer[100];
194 /* date handling requires both flags and revs */
195 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
197 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
201 static int show_file(const char *arg)
204 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
211 static int try_difference(const char *arg)
214 unsigned char sha1[20];
215 unsigned char end[20];
220 if (!(dotdot = strstr(arg, "..")))
224 symmetric = (*next == '.');
233 if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
234 show_rev(NORMAL, end, next);
235 show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
237 struct commit_list *exclude;
238 struct commit *a, *b;
239 a = lookup_commit_reference(sha1);
240 b = lookup_commit_reference(end);
241 exclude = get_merge_bases(a, b, 1);
243 struct commit_list *n = exclude->next;
245 exclude->item->object.sha1,NULL);
256 static int try_parent_shorthands(const char *arg)
259 unsigned char sha1[20];
260 struct commit *commit;
261 struct commit_list *parents;
264 if ((dotdot = strstr(arg, "^!")))
266 else if ((dotdot = strstr(arg, "^@")))
269 if (!dotdot || dotdot[2])
273 if (get_sha1(arg, sha1))
277 show_rev(NORMAL, sha1, arg);
278 commit = lookup_commit_reference(sha1);
279 for (parents = commit->parents; parents; parents = parents->next)
280 show_rev(parents_only ? NORMAL : REVERSED,
281 parents->item->object.sha1, arg);
286 static int parseopt_dump(const struct option *o, const char *arg, int unset)
288 struct strbuf *parsed = o->value;
290 strbuf_addf(parsed, " --no-%s", o->long_name);
291 else if (o->short_name)
292 strbuf_addf(parsed, " -%c", o->short_name);
294 strbuf_addf(parsed, " --%s", o->long_name);
296 strbuf_addch(parsed, ' ');
297 sq_quote_buf(parsed, arg);
302 static const char *skipspaces(const char *s)
309 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
311 static int keep_dashdash = 0, stop_at_non_option = 0;
312 static char const * const parseopt_usage[] = {
313 "git rev-parse --parseopt [options] -- [<args>...]",
316 static struct option parseopt_opts[] = {
317 OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash,
318 "keep the `--` passed as an arg"),
319 OPT_BOOLEAN(0, "stop-at-non-option", &stop_at_non_option,
320 "stop parsing after the "
321 "first non-option argument"),
325 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
326 const char **usage = NULL;
327 struct option *opts = NULL;
328 int onb = 0, osz = 0, unb = 0, usz = 0;
330 strbuf_addstr(&parsed, "set --");
331 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
332 PARSE_OPT_KEEP_DASHDASH);
333 if (argc < 1 || strcmp(argv[0], "--"))
334 usage_with_options(parseopt_usage, parseopt_opts);
336 /* get the usage up to the first line with a -- on it */
338 if (strbuf_getline(&sb, stdin, '\n') == EOF)
339 die("premature end of input");
340 ALLOC_GROW(usage, unb + 1, usz);
341 if (!strcmp("--", sb.buf)) {
343 die("no usage string given before the `--' separator");
347 usage[unb++] = strbuf_detach(&sb, NULL);
350 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
351 while (strbuf_getline(&sb, stdin, '\n') != EOF) {
358 ALLOC_GROW(opts, onb + 1, osz);
359 memset(opts + onb, 0, sizeof(opts[onb]));
362 s = strchr(sb.buf, ' ');
363 if (!s || *sb.buf == ' ') {
364 o->type = OPTION_GROUP;
365 o->help = xstrdup(skipspaces(sb.buf));
369 o->type = OPTION_CALLBACK;
370 o->help = xstrdup(skipspaces(s));
372 o->flags = PARSE_OPT_NOARG;
373 o->callback = &parseopt_dump;
374 while (s > sb.buf && strchr("*=?!", s[-1])) {
377 o->flags &= ~PARSE_OPT_NOARG;
380 o->flags &= ~PARSE_OPT_NOARG;
381 o->flags |= PARSE_OPT_OPTARG;
384 o->flags |= PARSE_OPT_NONEG;
387 o->flags |= PARSE_OPT_HIDDEN;
392 if (s - sb.buf == 1) /* short option only */
393 o->short_name = *sb.buf;
394 else if (sb.buf[1] != ',') /* long option only */
395 o->long_name = xmemdupz(sb.buf, s - sb.buf);
397 o->short_name = *sb.buf;
398 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
403 /* put an OPT_END() */
404 ALLOC_GROW(opts, onb + 1, osz);
405 memset(opts + onb, 0, sizeof(opts[onb]));
406 argc = parse_options(argc, argv, prefix, opts, usage,
407 keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0 |
408 stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0);
410 strbuf_addf(&parsed, " --");
411 sq_quote_argv(&parsed, argv, 0);
416 static int cmd_sq_quote(int argc, const char **argv)
418 struct strbuf buf = STRBUF_INIT;
421 sq_quote_argv(&buf, argv, 0);
422 printf("%s\n", buf.buf);
423 strbuf_release(&buf);
428 static void die_no_single_rev(int quiet)
433 die("Needed a single revision");
436 static const char builtin_rev_parse_usage[] =
437 "git rev-parse --parseopt [options] -- [<args>...]\n"
438 " or: git rev-parse --sq-quote [<arg>...]\n"
439 " or: git rev-parse [options] [<arg>...]\n"
441 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.";
443 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
445 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
446 unsigned char sha1[20];
447 const char *name = NULL;
449 if (argc > 1 && !strcmp("--parseopt", argv[1]))
450 return cmd_parseopt(argc - 1, argv + 1, prefix);
452 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
453 return cmd_sq_quote(argc - 2, argv + 2);
455 if (argc > 1 && !strcmp("-h", argv[1]))
456 usage(builtin_rev_parse_usage);
458 prefix = setup_git_directory();
459 git_config(git_default_config, NULL);
460 for (i = 1; i < argc; i++) {
461 const char *arg = argv[i];
464 if (show_file(arg) && as_is < 2)
465 verify_filename(prefix, arg);
468 if (!strcmp(arg,"-n")) {
470 die("-n requires an argument");
471 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
477 if (!prefixcmp(arg, "-n")) {
478 if ((filter & DO_FLAGS) && (filter & DO_REVS))
484 if (!strcmp(arg, "--")) {
486 /* Pass on the "--" if we show anything but files.. */
487 if (filter & (DO_FLAGS | DO_REVS))
491 if (!strcmp(arg, "--default")) {
496 if (!strcmp(arg, "--revs-only")) {
500 if (!strcmp(arg, "--no-revs")) {
504 if (!strcmp(arg, "--flags")) {
505 filter &= ~DO_NONFLAGS;
508 if (!strcmp(arg, "--no-flags")) {
512 if (!strcmp(arg, "--verify")) {
513 filter &= ~(DO_FLAGS|DO_NOREV);
517 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
521 if (!strcmp(arg, "--short") ||
522 !prefixcmp(arg, "--short=")) {
523 filter &= ~(DO_FLAGS|DO_NOREV);
525 abbrev = DEFAULT_ABBREV;
527 abbrev = strtoul(arg + 8, NULL, 10);
528 if (abbrev < MINIMUM_ABBREV)
529 abbrev = MINIMUM_ABBREV;
530 else if (40 <= abbrev)
534 if (!strcmp(arg, "--sq")) {
538 if (!strcmp(arg, "--not")) {
539 show_type ^= REVERSED;
542 if (!strcmp(arg, "--symbolic")) {
543 symbolic = SHOW_SYMBOLIC_ASIS;
546 if (!strcmp(arg, "--symbolic-full-name")) {
547 symbolic = SHOW_SYMBOLIC_FULL;
550 if (!prefixcmp(arg, "--abbrev-ref") &&
551 (!arg[12] || arg[12] == '=')) {
553 abbrev_ref_strict = warn_ambiguous_refs;
554 if (arg[12] == '=') {
555 if (!strcmp(arg + 13, "strict"))
556 abbrev_ref_strict = 1;
557 else if (!strcmp(arg + 13, "loose"))
558 abbrev_ref_strict = 0;
560 die("unknown mode for %s", arg);
564 if (!strcmp(arg, "--all")) {
565 for_each_ref(show_reference, NULL);
568 if (!strcmp(arg, "--bisect")) {
569 for_each_ref_in("refs/bisect/bad", show_reference, NULL);
570 for_each_ref_in("refs/bisect/good", anti_reference, NULL);
573 if (!strcmp(arg, "--branches")) {
574 for_each_branch_ref(show_reference, NULL);
577 if (!strcmp(arg, "--tags")) {
578 for_each_tag_ref(show_reference, NULL);
581 if (!prefixcmp(arg, "--glob=")) {
582 for_each_glob_ref(show_reference, arg + 7, NULL);
585 if (!strcmp(arg, "--remotes")) {
586 for_each_remote_ref(show_reference, NULL);
589 if (!strcmp(arg, "--show-prefix")) {
594 if (!strcmp(arg, "--show-cdup")) {
595 const char *pfx = prefix;
596 if (!is_inside_work_tree()) {
597 const char *work_tree =
600 printf("%s\n", work_tree);
604 pfx = strchr(pfx, '/');
613 if (!strcmp(arg, "--git-dir")) {
614 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
615 static char cwd[PATH_MAX];
624 if (!getcwd(cwd, PATH_MAX))
625 die_errno("unable to get current working directory");
626 printf("%s/.git\n", cwd);
629 if (!strcmp(arg, "--is-inside-git-dir")) {
630 printf("%s\n", is_inside_git_dir() ? "true"
634 if (!strcmp(arg, "--is-inside-work-tree")) {
635 printf("%s\n", is_inside_work_tree() ? "true"
639 if (!strcmp(arg, "--is-bare-repository")) {
640 printf("%s\n", is_bare_repository() ? "true"
644 if (!prefixcmp(arg, "--since=")) {
645 show_datestring("--max-age=", arg+8);
648 if (!prefixcmp(arg, "--after=")) {
649 show_datestring("--max-age=", arg+8);
652 if (!prefixcmp(arg, "--before=")) {
653 show_datestring("--min-age=", arg+9);
656 if (!prefixcmp(arg, "--until=")) {
657 show_datestring("--min-age=", arg+8);
660 if (show_flag(arg) && verify)
661 die_no_single_rev(quiet);
665 /* Not a flag argument */
666 if (try_difference(arg))
668 if (try_parent_shorthands(arg))
676 if (!get_sha1(name, sha1)) {
680 show_rev(type, sha1, name);
684 die_no_single_rev(quiet);
688 verify_filename(prefix, arg);
691 if (revs_count == 1) {
692 show_rev(type, sha1, name);
694 } else if (revs_count == 0 && show_default())
696 die_no_single_rev(quiet);