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
32 * Some arguments are relevant "revision" arguments,
33 * others are about output format or other details.
34 * This sorts it all out.
36 static int is_rev_argument(const char *arg)
38 static const char *rev_args[] = {
60 const char **p = rev_args;
62 /* accept -<digit>, like traditional "head" */
63 if ((*arg == '-') && isdigit(arg[1]))
67 const char *str = *p++;
72 if (!strcmp(arg, str) ||
73 (str[len-1] == '=' && !strncmp(arg, str, len)))
78 /* Output argument as a string, either SQ or normal */
79 static void show(const char *arg)
85 while ((ch = *arg++)) {
87 fputs("'\\'", stdout);
97 /* Like show(), but with a negation prefix according to type */
98 static void show_with_type(int type, const char *arg)
100 if (type != show_type)
105 /* Output a revision, only if filter allows it */
106 static void show_rev(int type, const unsigned char *sha1, const char *name)
108 if (!(filter & DO_REVS))
112 if (symbolic && name) {
113 if (symbolic == SHOW_SYMBOLIC_FULL) {
114 unsigned char discard[20];
117 switch (dwim_ref(name, strlen(name), discard, &full)) {
120 * Not found -- not a ref. We could
121 * emit "name" here, but symbolic-full
122 * users are interested in finding the
123 * refs spelled in full, and they would
124 * need to filter non-refs if we did so.
128 show_with_type(type, full);
130 default: /* ambiguous */
131 error("refname '%s' is ambiguous", name);
135 show_with_type(type, name);
139 show_with_type(type, find_unique_abbrev(sha1, abbrev));
141 show_with_type(type, sha1_to_hex(sha1));
144 /* Output a flag, only if filter allows it. */
145 static int show_flag(const char *arg)
147 if (!(filter & DO_FLAGS))
149 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
156 static int show_default(void)
161 unsigned char sha1[20];
164 if (!get_sha1(s, sha1)) {
165 show_rev(NORMAL, sha1, s);
172 static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
174 show_rev(NORMAL, sha1, refname);
178 static void show_datestring(const char *flag, const char *datestr)
180 static char buffer[100];
182 /* date handling requires both flags and revs */
183 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
185 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
189 static int show_file(const char *arg)
192 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
199 static int try_difference(const char *arg)
202 unsigned char sha1[20];
203 unsigned char end[20];
208 if (!(dotdot = strstr(arg, "..")))
212 symmetric = (*next == '.');
221 if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
222 show_rev(NORMAL, end, next);
223 show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
225 struct commit_list *exclude;
226 struct commit *a, *b;
227 a = lookup_commit_reference(sha1);
228 b = lookup_commit_reference(end);
229 exclude = get_merge_bases(a, b, 1);
231 struct commit_list *n = exclude->next;
233 exclude->item->object.sha1,NULL);
244 static int parseopt_dump(const struct option *o, const char *arg, int unset)
246 struct strbuf *parsed = o->value;
248 strbuf_addf(parsed, " --no-%s", o->long_name);
249 else if (o->short_name)
250 strbuf_addf(parsed, " -%c", o->short_name);
252 strbuf_addf(parsed, " --%s", o->long_name);
254 strbuf_addch(parsed, ' ');
255 sq_quote_buf(parsed, arg);
260 static const char *skipspaces(const char *s)
267 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
269 static int keep_dashdash = 0;
270 static char const * const parseopt_usage[] = {
271 "git-rev-parse --parseopt [options] -- [<args>...]",
274 static struct option parseopt_opts[] = {
275 OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash,
276 "keep the `--` passed as an arg"),
280 struct strbuf sb, parsed;
281 const char **usage = NULL;
282 struct option *opts = NULL;
283 int onb = 0, osz = 0, unb = 0, usz = 0;
285 strbuf_init(&parsed, 0);
286 strbuf_addstr(&parsed, "set --");
287 argc = parse_options(argc, argv, parseopt_opts, parseopt_usage,
288 PARSE_OPT_KEEP_DASHDASH);
289 if (argc < 1 || strcmp(argv[0], "--"))
290 usage_with_options(parseopt_usage, parseopt_opts);
293 /* get the usage up to the first line with a -- on it */
295 if (strbuf_getline(&sb, stdin, '\n') == EOF)
296 die("premature end of input");
297 ALLOC_GROW(usage, unb + 1, usz);
298 if (!strcmp("--", sb.buf)) {
300 die("no usage string given before the `--' separator");
304 usage[unb++] = strbuf_detach(&sb, NULL);
307 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
308 while (strbuf_getline(&sb, stdin, '\n') != EOF) {
315 ALLOC_GROW(opts, onb + 1, osz);
316 memset(opts + onb, 0, sizeof(opts[onb]));
319 s = strchr(sb.buf, ' ');
320 if (!s || *sb.buf == ' ') {
321 o->type = OPTION_GROUP;
322 o->help = xstrdup(skipspaces(sb.buf));
326 o->type = OPTION_CALLBACK;
327 o->help = xstrdup(skipspaces(s));
329 o->flags = PARSE_OPT_NOARG;
330 o->callback = &parseopt_dump;
331 while (s > sb.buf && strchr("*=?!", s[-1])) {
334 o->flags &= ~PARSE_OPT_NOARG;
337 o->flags &= ~PARSE_OPT_NOARG;
338 o->flags |= PARSE_OPT_OPTARG;
341 o->flags |= PARSE_OPT_NONEG;
344 o->flags |= PARSE_OPT_HIDDEN;
349 if (s - sb.buf == 1) /* short option only */
350 o->short_name = *sb.buf;
351 else if (sb.buf[1] != ',') /* long option only */
352 o->long_name = xmemdupz(sb.buf, s - sb.buf);
354 o->short_name = *sb.buf;
355 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
360 /* put an OPT_END() */
361 ALLOC_GROW(opts, onb + 1, osz);
362 memset(opts + onb, 0, sizeof(opts[onb]));
363 argc = parse_options(argc, argv, opts, usage,
364 keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0);
366 strbuf_addf(&parsed, " --");
367 sq_quote_argv(&parsed, argv, 0);
372 static void die_no_single_rev(int quiet)
377 die("Needed a single revision");
380 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
382 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
383 unsigned char sha1[20];
384 const char *name = NULL;
386 if (argc > 1 && !strcmp("--parseopt", argv[1]))
387 return cmd_parseopt(argc - 1, argv + 1, prefix);
389 prefix = setup_git_directory();
390 git_config(git_default_config, NULL);
391 for (i = 1; i < argc; i++) {
392 const char *arg = argv[i];
395 if (show_file(arg) && as_is < 2)
396 verify_filename(prefix, arg);
399 if (!strcmp(arg,"-n")) {
401 die("-n requires an argument");
402 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
408 if (!prefixcmp(arg, "-n")) {
409 if ((filter & DO_FLAGS) && (filter & DO_REVS))
415 if (!strcmp(arg, "--")) {
417 /* Pass on the "--" if we show anything but files.. */
418 if (filter & (DO_FLAGS | DO_REVS))
422 if (!strcmp(arg, "--default")) {
427 if (!strcmp(arg, "--revs-only")) {
431 if (!strcmp(arg, "--no-revs")) {
435 if (!strcmp(arg, "--flags")) {
436 filter &= ~DO_NONFLAGS;
439 if (!strcmp(arg, "--no-flags")) {
443 if (!strcmp(arg, "--verify")) {
444 filter &= ~(DO_FLAGS|DO_NOREV);
448 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
452 if (!strcmp(arg, "--short") ||
453 !prefixcmp(arg, "--short=")) {
454 filter &= ~(DO_FLAGS|DO_NOREV);
456 abbrev = DEFAULT_ABBREV;
458 abbrev = strtoul(arg + 8, NULL, 10);
459 if (abbrev < MINIMUM_ABBREV)
460 abbrev = MINIMUM_ABBREV;
461 else if (40 <= abbrev)
465 if (!strcmp(arg, "--sq")) {
469 if (!strcmp(arg, "--not")) {
470 show_type ^= REVERSED;
473 if (!strcmp(arg, "--symbolic")) {
474 symbolic = SHOW_SYMBOLIC_ASIS;
477 if (!strcmp(arg, "--symbolic-full-name")) {
478 symbolic = SHOW_SYMBOLIC_FULL;
481 if (!strcmp(arg, "--all")) {
482 for_each_ref(show_reference, NULL);
485 if (!strcmp(arg, "--branches")) {
486 for_each_branch_ref(show_reference, NULL);
489 if (!strcmp(arg, "--tags")) {
490 for_each_tag_ref(show_reference, NULL);
493 if (!strcmp(arg, "--remotes")) {
494 for_each_remote_ref(show_reference, NULL);
497 if (!strcmp(arg, "--show-prefix")) {
502 if (!strcmp(arg, "--show-cdup")) {
503 const char *pfx = prefix;
504 if (!is_inside_work_tree()) {
505 const char *work_tree =
508 printf("%s\n", work_tree);
512 pfx = strchr(pfx, '/');
521 if (!strcmp(arg, "--git-dir")) {
522 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
523 static char cwd[PATH_MAX];
532 if (!getcwd(cwd, PATH_MAX))
533 die("unable to get current working directory");
534 printf("%s/.git\n", cwd);
537 if (!strcmp(arg, "--is-inside-git-dir")) {
538 printf("%s\n", is_inside_git_dir() ? "true"
542 if (!strcmp(arg, "--is-inside-work-tree")) {
543 printf("%s\n", is_inside_work_tree() ? "true"
547 if (!strcmp(arg, "--is-bare-repository")) {
548 printf("%s\n", is_bare_repository() ? "true"
552 if (!prefixcmp(arg, "--since=")) {
553 show_datestring("--max-age=", arg+8);
556 if (!prefixcmp(arg, "--after=")) {
557 show_datestring("--max-age=", arg+8);
560 if (!prefixcmp(arg, "--before=")) {
561 show_datestring("--min-age=", arg+9);
564 if (!prefixcmp(arg, "--until=")) {
565 show_datestring("--min-age=", arg+8);
568 if (show_flag(arg) && verify)
569 die_no_single_rev(quiet);
573 /* Not a flag argument */
574 if (try_difference(arg))
582 if (!get_sha1(name, sha1)) {
586 show_rev(type, sha1, name);
590 die_no_single_rev(quiet);
594 verify_filename(prefix, arg);
597 if (revs_count == 1) {
598 show_rev(type, sha1, name);
600 } else if (revs_count == 0 && show_default())
602 die_no_single_rev(quiet);