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 /* Like show(), but with a negation prefix according to type */
100 static void show_with_type(int type, const char *arg)
102 if (type != show_type)
107 /* Output a revision, only if filter allows it */
108 static void show_rev(int type, const unsigned char *sha1, const char *name)
110 if (!(filter & DO_REVS))
115 if (symbolic && name) {
116 if (symbolic == SHOW_SYMBOLIC_FULL) {
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.
131 show_with_type(type, full);
133 default: /* ambiguous */
134 error("refname '%s' is ambiguous", name);
138 show_with_type(type, name);
142 show_with_type(type, find_unique_abbrev(sha1, abbrev));
144 show_with_type(type, sha1_to_hex(sha1));
147 /* Output a flag, only if filter allows it. */
148 static int show_flag(const char *arg)
150 if (!(filter & DO_FLAGS))
152 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
159 static void show_default(void)
164 unsigned char sha1[20];
167 if (!get_sha1(s, sha1)) {
168 show_rev(NORMAL, sha1, s);
174 static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
176 show_rev(NORMAL, sha1, refname);
180 static void show_datestring(const char *flag, const char *datestr)
182 static char buffer[100];
184 /* date handling requires both flags and revs */
185 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
187 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
191 static int show_file(const char *arg)
194 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
201 static int try_difference(const char *arg)
204 unsigned char sha1[20];
205 unsigned char end[20];
210 if (!(dotdot = strstr(arg, "..")))
214 symmetric = (*next == '.');
223 if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
224 show_rev(NORMAL, end, next);
225 show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
227 struct commit_list *exclude;
228 struct commit *a, *b;
229 a = lookup_commit_reference(sha1);
230 b = lookup_commit_reference(end);
231 exclude = get_merge_bases(a, b, 1);
233 struct commit_list *n = exclude->next;
235 exclude->item->object.sha1,NULL);
246 static int parseopt_dump(const struct option *o, const char *arg, int unset)
248 struct strbuf *parsed = o->value;
250 strbuf_addf(parsed, " --no-%s", o->long_name);
251 else if (o->short_name)
252 strbuf_addf(parsed, " -%c", o->short_name);
254 strbuf_addf(parsed, " --%s", o->long_name);
256 strbuf_addch(parsed, ' ');
257 sq_quote_buf(parsed, arg);
262 static const char *skipspaces(const char *s)
269 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
271 static int keep_dashdash = 0;
272 static char const * const parseopt_usage[] = {
273 "git-rev-parse --parseopt [options] -- [<args>...]",
276 static struct option parseopt_opts[] = {
277 OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash,
278 "keep the `--` passed as an arg"),
282 struct strbuf sb, parsed;
283 const char **usage = NULL;
284 struct option *opts = NULL;
285 int onb = 0, osz = 0, unb = 0, usz = 0;
287 strbuf_init(&parsed, 0);
288 strbuf_addstr(&parsed, "set --");
289 argc = parse_options(argc, argv, parseopt_opts, parseopt_usage,
290 PARSE_OPT_KEEP_DASHDASH);
291 if (argc < 1 || strcmp(argv[0], "--"))
292 usage_with_options(parseopt_usage, parseopt_opts);
295 /* get the usage up to the first line with a -- on it */
297 if (strbuf_getline(&sb, stdin, '\n') == EOF)
298 die("premature end of input");
299 ALLOC_GROW(usage, unb + 1, usz);
300 if (!strcmp("--", sb.buf)) {
302 die("no usage string given before the `--' separator");
306 usage[unb++] = strbuf_detach(&sb, NULL);
309 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
310 while (strbuf_getline(&sb, stdin, '\n') != EOF) {
317 ALLOC_GROW(opts, onb + 1, osz);
318 memset(opts + onb, 0, sizeof(opts[onb]));
321 s = strchr(sb.buf, ' ');
322 if (!s || *sb.buf == ' ') {
323 o->type = OPTION_GROUP;
324 o->help = xstrdup(skipspaces(sb.buf));
328 o->type = OPTION_CALLBACK;
329 o->help = xstrdup(skipspaces(s));
331 o->flags = PARSE_OPT_NOARG;
332 o->callback = &parseopt_dump;
333 while (s > sb.buf && strchr("*=?!", s[-1])) {
336 o->flags &= ~PARSE_OPT_NOARG;
339 o->flags &= ~PARSE_OPT_NOARG;
340 o->flags |= PARSE_OPT_OPTARG;
343 o->flags |= PARSE_OPT_NONEG;
346 o->flags |= PARSE_OPT_HIDDEN;
351 if (s - sb.buf == 1) /* short option only */
352 o->short_name = *sb.buf;
353 else if (sb.buf[1] != ',') /* long option only */
354 o->long_name = xmemdupz(sb.buf, s - sb.buf);
356 o->short_name = *sb.buf;
357 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
362 /* put an OPT_END() */
363 ALLOC_GROW(opts, onb + 1, osz);
364 memset(opts + onb, 0, sizeof(opts[onb]));
365 argc = parse_options(argc, argv, opts, usage,
366 keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0);
368 strbuf_addf(&parsed, " --");
369 sq_quote_argv(&parsed, argv, 0);
374 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
376 int i, as_is = 0, verify = 0;
377 unsigned char sha1[20];
379 if (argc > 1 && !strcmp("--parseopt", argv[1]))
380 return cmd_parseopt(argc - 1, argv + 1, prefix);
382 prefix = setup_git_directory();
383 git_config(git_default_config);
384 for (i = 1; i < argc; i++) {
385 const char *arg = argv[i];
388 if (show_file(arg) && as_is < 2)
389 verify_filename(prefix, arg);
392 if (!strcmp(arg,"-n")) {
394 die("-n requires an argument");
395 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
401 if (!prefixcmp(arg, "-n")) {
402 if ((filter & DO_FLAGS) && (filter & DO_REVS))
408 if (!strcmp(arg, "--")) {
410 /* Pass on the "--" if we show anything but files.. */
411 if (filter & (DO_FLAGS | DO_REVS))
415 if (!strcmp(arg, "--default")) {
420 if (!strcmp(arg, "--revs-only")) {
424 if (!strcmp(arg, "--no-revs")) {
428 if (!strcmp(arg, "--flags")) {
429 filter &= ~DO_NONFLAGS;
432 if (!strcmp(arg, "--no-flags")) {
436 if (!strcmp(arg, "--verify")) {
437 filter &= ~(DO_FLAGS|DO_NOREV);
441 if (!strcmp(arg, "--short") ||
442 !prefixcmp(arg, "--short=")) {
443 filter &= ~(DO_FLAGS|DO_NOREV);
445 abbrev = DEFAULT_ABBREV;
447 abbrev = strtoul(arg + 8, NULL, 10);
448 if (abbrev < MINIMUM_ABBREV)
449 abbrev = MINIMUM_ABBREV;
450 else if (40 <= abbrev)
454 if (!strcmp(arg, "--sq")) {
458 if (!strcmp(arg, "--not")) {
459 show_type ^= REVERSED;
462 if (!strcmp(arg, "--symbolic")) {
463 symbolic = SHOW_SYMBOLIC_ASIS;
466 if (!strcmp(arg, "--symbolic-full-name")) {
467 symbolic = SHOW_SYMBOLIC_FULL;
470 if (!strcmp(arg, "--all")) {
471 for_each_ref(show_reference, NULL);
474 if (!strcmp(arg, "--branches")) {
475 for_each_branch_ref(show_reference, NULL);
478 if (!strcmp(arg, "--tags")) {
479 for_each_tag_ref(show_reference, NULL);
482 if (!strcmp(arg, "--remotes")) {
483 for_each_remote_ref(show_reference, NULL);
486 if (!strcmp(arg, "--show-prefix")) {
491 if (!strcmp(arg, "--show-cdup")) {
492 const char *pfx = prefix;
493 if (!is_inside_work_tree()) {
494 const char *work_tree =
497 printf("%s\n", work_tree);
501 pfx = strchr(pfx, '/');
510 if (!strcmp(arg, "--git-dir")) {
511 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
512 static char cwd[PATH_MAX];
521 if (!getcwd(cwd, PATH_MAX))
522 die("unable to get current working directory");
523 printf("%s/.git\n", cwd);
526 if (!strcmp(arg, "--is-inside-git-dir")) {
527 printf("%s\n", is_inside_git_dir() ? "true"
531 if (!strcmp(arg, "--is-inside-work-tree")) {
532 printf("%s\n", is_inside_work_tree() ? "true"
536 if (!strcmp(arg, "--is-bare-repository")) {
537 printf("%s\n", is_bare_repository() ? "true"
541 if (!prefixcmp(arg, "--since=")) {
542 show_datestring("--max-age=", arg+8);
545 if (!prefixcmp(arg, "--after=")) {
546 show_datestring("--max-age=", arg+8);
549 if (!prefixcmp(arg, "--before=")) {
550 show_datestring("--min-age=", arg+9);
553 if (!prefixcmp(arg, "--until=")) {
554 show_datestring("--min-age=", arg+8);
557 if (show_flag(arg) && verify)
558 die("Needed a single revision");
562 /* Not a flag argument */
563 if (try_difference(arg))
565 if (!get_sha1(arg, sha1)) {
566 show_rev(NORMAL, sha1, arg);
569 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
570 show_rev(REVERSED, sha1, arg+1);
577 die("Needed a single revision");
578 verify_filename(prefix, arg);
581 if (verify && revs_count != 1)
582 die("Needed a single revision");