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
31 static int revs_count;
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[] = {
62 const char **p = rev_args;
64 /* accept -<digit>, like traditional "head" */
65 if ((*arg == '-') && isdigit(arg[1]))
69 const char *str = *p++;
74 if (!strcmp(arg, str) ||
75 (str[len-1] == '=' && !strncmp(arg, str, len)))
80 /* Output argument as a string, either SQ or normal */
81 static void show(const char *arg)
87 while ((ch = *arg++)) {
89 fputs("'\\'", stdout);
99 /* Output a revision, only if filter allows it */
100 static void show_rev(int type, const unsigned char *sha1, const char *name)
102 if (!(filter & DO_REVS))
107 if (type != show_type)
109 if (symbolic && name) {
110 if (symbolic == SHOW_SYMBOLIC_FULL) {
111 unsigned char discard[20];
114 switch (dwim_ref(name, strlen(name), discard, &full)) {
117 * Not found -- not a ref. We could
118 * emit "name" here, but symbolic-full
119 * users are interested in finding the
120 * refs spelled in full, and they would
121 * need to filter non-refs if we did so.
127 default: /* ambiguous */
128 error("refname '%s' is ambiguous", name);
136 show(find_unique_abbrev(sha1, abbrev));
138 show(sha1_to_hex(sha1));
141 /* Output a flag, only if filter allows it. */
142 static int show_flag(const char *arg)
144 if (!(filter & DO_FLAGS))
146 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
153 static void show_default(void)
158 unsigned char sha1[20];
161 if (!get_sha1(s, sha1)) {
162 show_rev(NORMAL, sha1, s);
168 static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
170 show_rev(NORMAL, sha1, refname);
174 static void show_datestring(const char *flag, const char *datestr)
176 static char buffer[100];
178 /* date handling requires both flags and revs */
179 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
181 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
185 static int show_file(const char *arg)
188 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
195 static int try_difference(const char *arg)
198 unsigned char sha1[20];
199 unsigned char end[20];
204 if (!(dotdot = strstr(arg, "..")))
208 symmetric = (*next == '.');
217 if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
218 show_rev(NORMAL, end, next);
219 show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
221 struct commit_list *exclude;
222 struct commit *a, *b;
223 a = lookup_commit_reference(sha1);
224 b = lookup_commit_reference(end);
225 exclude = get_merge_bases(a, b, 1);
227 struct commit_list *n = exclude->next;
229 exclude->item->object.sha1,NULL);
240 static int parseopt_dump(const struct option *o, const char *arg, int unset)
242 struct strbuf *parsed = o->value;
244 strbuf_addf(parsed, " --no-%s", o->long_name);
245 else if (o->short_name)
246 strbuf_addf(parsed, " -%c", o->short_name);
248 strbuf_addf(parsed, " --%s", o->long_name);
250 strbuf_addch(parsed, ' ');
251 sq_quote_buf(parsed, arg);
256 static const char *skipspaces(const char *s)
263 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
265 static int keep_dashdash = 0;
266 static char const * const parseopt_usage[] = {
267 "git-rev-parse --parseopt [options] -- [<args>...]",
270 static struct option parseopt_opts[] = {
271 OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash,
272 "keep the `--` passed as an arg"),
276 struct strbuf sb, parsed;
277 const char **usage = NULL;
278 struct option *opts = NULL;
279 int onb = 0, osz = 0, unb = 0, usz = 0;
281 strbuf_init(&parsed, 0);
282 strbuf_addstr(&parsed, "set --");
283 argc = parse_options(argc, argv, parseopt_opts, parseopt_usage,
284 PARSE_OPT_KEEP_DASHDASH);
285 if (argc < 1 || strcmp(argv[0], "--"))
286 usage_with_options(parseopt_usage, parseopt_opts);
289 /* get the usage up to the first line with a -- on it */
291 if (strbuf_getline(&sb, stdin, '\n') == EOF)
292 die("premature end of input");
293 ALLOC_GROW(usage, unb + 1, usz);
294 if (!strcmp("--", sb.buf)) {
296 die("no usage string given before the `--' separator");
300 usage[unb++] = strbuf_detach(&sb, NULL);
303 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
304 while (strbuf_getline(&sb, stdin, '\n') != EOF) {
311 ALLOC_GROW(opts, onb + 1, osz);
312 memset(opts + onb, 0, sizeof(opts[onb]));
315 s = strchr(sb.buf, ' ');
316 if (!s || *sb.buf == ' ') {
317 o->type = OPTION_GROUP;
318 o->help = xstrdup(skipspaces(sb.buf));
322 o->type = OPTION_CALLBACK;
323 o->help = xstrdup(skipspaces(s));
325 o->flags = PARSE_OPT_NOARG;
326 o->callback = &parseopt_dump;
327 while (s > sb.buf && strchr("*=?!", s[-1])) {
330 o->flags &= ~PARSE_OPT_NOARG;
333 o->flags &= ~PARSE_OPT_NOARG;
334 o->flags |= PARSE_OPT_OPTARG;
337 o->flags |= PARSE_OPT_NONEG;
340 o->flags |= PARSE_OPT_HIDDEN;
345 if (s - sb.buf == 1) /* short option only */
346 o->short_name = *sb.buf;
347 else if (sb.buf[1] != ',') /* long option only */
348 o->long_name = xmemdupz(sb.buf, s - sb.buf);
350 o->short_name = *sb.buf;
351 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
356 /* put an OPT_END() */
357 ALLOC_GROW(opts, onb + 1, osz);
358 memset(opts + onb, 0, sizeof(opts[onb]));
359 argc = parse_options(argc, argv, opts, usage,
360 keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0);
362 strbuf_addf(&parsed, " --");
363 sq_quote_argv(&parsed, argv, 0);
368 static void die_no_single_rev(int quiet)
373 die("Needed a single revision");
376 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
378 int i, as_is = 0, verify = 0, quiet = 0;
379 unsigned char sha1[20];
381 if (argc > 1 && !strcmp("--parseopt", argv[1]))
382 return cmd_parseopt(argc - 1, argv + 1, prefix);
384 prefix = setup_git_directory();
385 git_config(git_default_config);
386 for (i = 1; i < argc; i++) {
387 const char *arg = argv[i];
390 if (show_file(arg) && as_is < 2)
391 verify_filename(prefix, arg);
394 if (!strcmp(arg,"-n")) {
396 die("-n requires an argument");
397 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
403 if (!prefixcmp(arg, "-n")) {
404 if ((filter & DO_FLAGS) && (filter & DO_REVS))
410 if (!strcmp(arg, "--")) {
412 /* Pass on the "--" if we show anything but files.. */
413 if (filter & (DO_FLAGS | DO_REVS))
417 if (!strcmp(arg, "--default")) {
422 if (!strcmp(arg, "--revs-only")) {
426 if (!strcmp(arg, "--no-revs")) {
430 if (!strcmp(arg, "--flags")) {
431 filter &= ~DO_NONFLAGS;
434 if (!strcmp(arg, "--no-flags")) {
438 if (!strcmp(arg, "--verify")) {
439 filter &= ~(DO_FLAGS|DO_NOREV);
443 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
447 if (!strcmp(arg, "--short") ||
448 !prefixcmp(arg, "--short=")) {
449 filter &= ~(DO_FLAGS|DO_NOREV);
451 abbrev = DEFAULT_ABBREV;
453 abbrev = strtoul(arg + 8, NULL, 10);
454 if (abbrev < MINIMUM_ABBREV)
455 abbrev = MINIMUM_ABBREV;
456 else if (40 <= abbrev)
460 if (!strcmp(arg, "--sq")) {
464 if (!strcmp(arg, "--not")) {
465 show_type ^= REVERSED;
468 if (!strcmp(arg, "--symbolic")) {
469 symbolic = SHOW_SYMBOLIC_ASIS;
472 if (!strcmp(arg, "--symbolic-full-name")) {
473 symbolic = SHOW_SYMBOLIC_FULL;
476 if (!strcmp(arg, "--all")) {
477 for_each_ref(show_reference, NULL);
480 if (!strcmp(arg, "--branches")) {
481 for_each_branch_ref(show_reference, NULL);
484 if (!strcmp(arg, "--tags")) {
485 for_each_tag_ref(show_reference, NULL);
488 if (!strcmp(arg, "--remotes")) {
489 for_each_remote_ref(show_reference, NULL);
492 if (!strcmp(arg, "--show-prefix")) {
497 if (!strcmp(arg, "--show-cdup")) {
498 const char *pfx = prefix;
499 if (!is_inside_work_tree()) {
500 const char *work_tree =
503 printf("%s\n", work_tree);
507 pfx = strchr(pfx, '/');
516 if (!strcmp(arg, "--git-dir")) {
517 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
518 static char cwd[PATH_MAX];
527 if (!getcwd(cwd, PATH_MAX))
528 die("unable to get current working directory");
529 printf("%s/.git\n", cwd);
532 if (!strcmp(arg, "--is-inside-git-dir")) {
533 printf("%s\n", is_inside_git_dir() ? "true"
537 if (!strcmp(arg, "--is-inside-work-tree")) {
538 printf("%s\n", is_inside_work_tree() ? "true"
542 if (!strcmp(arg, "--is-bare-repository")) {
543 printf("%s\n", is_bare_repository() ? "true"
547 if (!prefixcmp(arg, "--since=")) {
548 show_datestring("--max-age=", arg+8);
551 if (!prefixcmp(arg, "--after=")) {
552 show_datestring("--max-age=", arg+8);
555 if (!prefixcmp(arg, "--before=")) {
556 show_datestring("--min-age=", arg+9);
559 if (!prefixcmp(arg, "--until=")) {
560 show_datestring("--min-age=", arg+8);
563 if (show_flag(arg) && verify)
564 die_no_single_rev(quiet);
568 /* Not a flag argument */
569 if (try_difference(arg))
571 if (!get_sha1(arg, sha1)) {
572 show_rev(NORMAL, sha1, arg);
575 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
576 show_rev(REVERSED, sha1, arg+1);
580 die_no_single_rev(quiet);
584 verify_filename(prefix, arg);
587 if (verify && revs_count != 1)
588 die_no_single_rev(quiet);