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;
28 static int revs_count;
31 * Some arguments are relevant "revision" arguments,
32 * others are about output format or other details.
33 * This sorts it all out.
35 static int is_rev_argument(const char *arg)
37 static const char *rev_args[] = {
59 const char **p = rev_args;
61 /* accept -<digit>, like traditional "head" */
62 if ((*arg == '-') && isdigit(arg[1]))
66 const char *str = *p++;
71 if (!strcmp(arg, str) ||
72 (str[len-1] == '=' && !strncmp(arg, str, len)))
77 /* Output argument as a string, either SQ or normal */
78 static void show(const char *arg)
84 while ((ch = *arg++)) {
86 fputs("'\\'", stdout);
96 /* Output a revision, only if filter allows it */
97 static void show_rev(int type, const unsigned char *sha1, const char *name)
99 if (!(filter & DO_REVS))
104 if (type != show_type)
106 if (symbolic && name)
109 show(find_unique_abbrev(sha1, abbrev));
111 show(sha1_to_hex(sha1));
114 /* Output a flag, only if filter allows it. */
115 static int show_flag(const char *arg)
117 if (!(filter & DO_FLAGS))
119 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
126 static void show_default(void)
131 unsigned char sha1[20];
134 if (!get_sha1(s, sha1)) {
135 show_rev(NORMAL, sha1, s);
141 static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
143 show_rev(NORMAL, sha1, refname);
147 static void show_datestring(const char *flag, const char *datestr)
149 static char buffer[100];
151 /* date handling requires both flags and revs */
152 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
154 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
158 static int show_file(const char *arg)
161 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
168 static int try_difference(const char *arg)
171 unsigned char sha1[20];
172 unsigned char end[20];
177 if (!(dotdot = strstr(arg, "..")))
181 symmetric = (*next == '.');
190 if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
191 show_rev(NORMAL, end, next);
192 show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
194 struct commit_list *exclude;
195 struct commit *a, *b;
196 a = lookup_commit_reference(sha1);
197 b = lookup_commit_reference(end);
198 exclude = get_merge_bases(a, b, 1);
200 struct commit_list *n = exclude->next;
202 exclude->item->object.sha1,NULL);
213 static int parseopt_dump(const struct option *o, const char *arg, int unset)
215 struct strbuf *parsed = o->value;
217 strbuf_addf(parsed, " --no-%s", o->long_name);
218 else if (o->short_name)
219 strbuf_addf(parsed, " -%c", o->short_name);
221 strbuf_addf(parsed, " --%s", o->long_name);
223 strbuf_addch(parsed, ' ');
224 sq_quote_buf(parsed, arg);
229 static const char *skipspaces(const char *s)
236 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
238 static int keep_dashdash = 0;
239 static char const * const parseopt_usage[] = {
240 "git-rev-parse --parseopt [options] -- [<args>...]",
243 static struct option parseopt_opts[] = {
244 OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash,
245 "keep the `--` passed as an arg"),
249 struct strbuf sb, parsed;
250 const char **usage = NULL;
251 struct option *opts = NULL;
252 int onb = 0, osz = 0, unb = 0, usz = 0;
254 strbuf_init(&parsed, 0);
255 strbuf_addstr(&parsed, "set --");
256 argc = parse_options(argc, argv, parseopt_opts, parseopt_usage,
257 PARSE_OPT_KEEP_DASHDASH);
258 if (argc < 1 || strcmp(argv[0], "--"))
259 usage_with_options(parseopt_usage, parseopt_opts);
262 /* get the usage up to the first line with a -- on it */
264 if (strbuf_getline(&sb, stdin, '\n') == EOF)
265 die("premature end of input");
266 ALLOC_GROW(usage, unb + 1, usz);
267 if (!strcmp("--", sb.buf)) {
269 die("no usage string given before the `--' separator");
273 usage[unb++] = strbuf_detach(&sb, NULL);
276 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
277 while (strbuf_getline(&sb, stdin, '\n') != EOF) {
284 ALLOC_GROW(opts, onb + 1, osz);
285 memset(opts + onb, 0, sizeof(opts[onb]));
288 s = strchr(sb.buf, ' ');
289 if (!s || *sb.buf == ' ') {
290 o->type = OPTION_GROUP;
291 o->help = xstrdup(skipspaces(s));
295 o->type = OPTION_CALLBACK;
296 o->help = xstrdup(skipspaces(s));
298 o->callback = &parseopt_dump;
304 o->flags = PARSE_OPT_OPTARG;
308 o->flags = PARSE_OPT_NOARG;
312 if (s - sb.buf == 1) /* short option only */
313 o->short_name = *sb.buf;
314 else if (sb.buf[1] != ',') /* long option only */
315 o->long_name = xmemdupz(sb.buf, s - sb.buf);
317 o->short_name = *sb.buf;
318 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
323 /* put an OPT_END() */
324 ALLOC_GROW(opts, onb + 1, osz);
325 memset(opts + onb, 0, sizeof(opts[onb]));
326 argc = parse_options(argc, argv, opts, usage,
327 keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0);
329 strbuf_addf(&parsed, " --");
330 sq_quote_argv(&parsed, argv, argc, 0);
335 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
337 int i, as_is = 0, verify = 0;
338 unsigned char sha1[20];
340 if (argc > 1 && !strcmp("--parseopt", argv[1]))
341 return cmd_parseopt(argc - 1, argv + 1, prefix);
343 prefix = setup_git_directory();
344 git_config(git_default_config);
345 for (i = 1; i < argc; i++) {
346 const char *arg = argv[i];
349 if (show_file(arg) && as_is < 2)
350 verify_filename(prefix, arg);
353 if (!strcmp(arg,"-n")) {
355 die("-n requires an argument");
356 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
362 if (!prefixcmp(arg, "-n")) {
363 if ((filter & DO_FLAGS) && (filter & DO_REVS))
369 if (!strcmp(arg, "--")) {
371 /* Pass on the "--" if we show anything but files.. */
372 if (filter & (DO_FLAGS | DO_REVS))
376 if (!strcmp(arg, "--default")) {
381 if (!strcmp(arg, "--revs-only")) {
385 if (!strcmp(arg, "--no-revs")) {
389 if (!strcmp(arg, "--flags")) {
390 filter &= ~DO_NONFLAGS;
393 if (!strcmp(arg, "--no-flags")) {
397 if (!strcmp(arg, "--verify")) {
398 filter &= ~(DO_FLAGS|DO_NOREV);
402 if (!strcmp(arg, "--short") ||
403 !prefixcmp(arg, "--short=")) {
404 filter &= ~(DO_FLAGS|DO_NOREV);
406 abbrev = DEFAULT_ABBREV;
408 abbrev = strtoul(arg + 8, NULL, 10);
409 if (abbrev < MINIMUM_ABBREV)
410 abbrev = MINIMUM_ABBREV;
411 else if (40 <= abbrev)
415 if (!strcmp(arg, "--sq")) {
419 if (!strcmp(arg, "--not")) {
420 show_type ^= REVERSED;
423 if (!strcmp(arg, "--symbolic")) {
427 if (!strcmp(arg, "--all")) {
428 for_each_ref(show_reference, NULL);
431 if (!strcmp(arg, "--branches")) {
432 for_each_branch_ref(show_reference, NULL);
435 if (!strcmp(arg, "--tags")) {
436 for_each_tag_ref(show_reference, NULL);
439 if (!strcmp(arg, "--remotes")) {
440 for_each_remote_ref(show_reference, NULL);
443 if (!strcmp(arg, "--show-prefix")) {
448 if (!strcmp(arg, "--show-cdup")) {
449 const char *pfx = prefix;
450 if (!is_inside_work_tree()) {
451 const char *work_tree =
454 printf("%s\n", work_tree);
458 pfx = strchr(pfx, '/');
467 if (!strcmp(arg, "--git-dir")) {
468 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
469 static char cwd[PATH_MAX];
478 if (!getcwd(cwd, PATH_MAX))
479 die("unable to get current working directory");
480 printf("%s/.git\n", cwd);
483 if (!strcmp(arg, "--is-inside-git-dir")) {
484 printf("%s\n", is_inside_git_dir() ? "true"
488 if (!strcmp(arg, "--is-inside-work-tree")) {
489 printf("%s\n", is_inside_work_tree() ? "true"
493 if (!strcmp(arg, "--is-bare-repository")) {
494 printf("%s\n", is_bare_repository() ? "true"
498 if (!prefixcmp(arg, "--since=")) {
499 show_datestring("--max-age=", arg+8);
502 if (!prefixcmp(arg, "--after=")) {
503 show_datestring("--max-age=", arg+8);
506 if (!prefixcmp(arg, "--before=")) {
507 show_datestring("--min-age=", arg+9);
510 if (!prefixcmp(arg, "--until=")) {
511 show_datestring("--min-age=", arg+8);
514 if (show_flag(arg) && verify)
515 die("Needed a single revision");
519 /* Not a flag argument */
520 if (try_difference(arg))
522 if (!get_sha1(arg, sha1)) {
523 show_rev(NORMAL, sha1, arg);
526 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
527 show_rev(REVERSED, sha1, arg+1);
534 die("Needed a single revision");
535 verify_filename(prefix, arg);
538 if (verify && revs_count != 1)
539 die("Needed a single revision");