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
[] = {
66 const char **p
= rev_args
;
68 /* accept -<digit>, like traditional "head" */
69 if ((*arg
== '-') && isdigit(arg
[1]))
73 const char *str
= *p
++;
78 if (!strcmp(arg
, str
) ||
79 (str
[len
-1] == '=' && !strncmp(arg
, str
, len
)))
84 /* Output argument as a string, either SQ or normal */
85 static void show(const char *arg
)
91 while ((ch
= *arg
++)) {
93 fputs("'\\'", stdout
);
103 /* Like show(), but with a negation prefix according to type */
104 static void show_with_type(int type
, const char *arg
)
106 if (type
!= show_type
)
111 /* Output a revision, only if filter allows it */
112 static void show_rev(int type
, const unsigned char *sha1
, const char *name
)
114 if (!(filter
& DO_REVS
))
118 if ((symbolic
|| abbrev_ref
) && name
) {
119 if (symbolic
== SHOW_SYMBOLIC_FULL
|| abbrev_ref
) {
120 unsigned char discard
[20];
123 switch (dwim_ref(name
, strlen(name
), discard
, &full
)) {
126 * Not found -- not a ref. We could
127 * emit "name" here, but symbolic-full
128 * users are interested in finding the
129 * refs spelled in full, and they would
130 * need to filter non-refs if we did so.
135 full
= shorten_unambiguous_ref(full
,
137 show_with_type(type
, full
);
139 default: /* ambiguous */
140 error("refname '%s' is ambiguous", name
);
144 show_with_type(type
, name
);
148 show_with_type(type
, find_unique_abbrev(sha1
, abbrev
));
150 show_with_type(type
, sha1_to_hex(sha1
));
153 /* Output a flag, only if filter allows it. */
154 static int show_flag(const char *arg
)
156 if (!(filter
& DO_FLAGS
))
158 if (filter
& (is_rev_argument(arg
) ? DO_REVS
: DO_NOREV
)) {
165 static int show_default(void)
170 unsigned char sha1
[20];
173 if (!get_sha1(s
, sha1
)) {
174 show_rev(NORMAL
, sha1
, s
);
181 static int show_reference(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
183 show_rev(NORMAL
, sha1
, refname
);
187 static int anti_reference(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
189 show_rev(REVERSED
, sha1
, refname
);
193 static void show_datestring(const char *flag
, const char *datestr
)
195 static char buffer
[100];
197 /* date handling requires both flags and revs */
198 if ((filter
& (DO_FLAGS
| DO_REVS
)) != (DO_FLAGS
| DO_REVS
))
200 snprintf(buffer
, sizeof(buffer
), "%s%lu", flag
, approxidate(datestr
));
204 static int show_file(const char *arg
)
207 if ((filter
& (DO_NONFLAGS
|DO_NOREV
)) == (DO_NONFLAGS
|DO_NOREV
)) {
214 static int try_difference(const char *arg
)
217 unsigned char sha1
[20];
218 unsigned char end
[20];
223 if (!(dotdot
= strstr(arg
, "..")))
227 symmetric
= (*next
== '.');
236 if (!get_sha1(this, sha1
) && !get_sha1(next
, end
)) {
237 show_rev(NORMAL
, end
, next
);
238 show_rev(symmetric
? NORMAL
: REVERSED
, sha1
, this);
240 struct commit_list
*exclude
;
241 struct commit
*a
, *b
;
242 a
= lookup_commit_reference(sha1
);
243 b
= lookup_commit_reference(end
);
244 exclude
= get_merge_bases(a
, b
, 1);
246 struct commit_list
*n
= exclude
->next
;
248 exclude
->item
->object
.sha1
,NULL
);
259 static int try_parent_shorthands(const char *arg
)
262 unsigned char sha1
[20];
263 struct commit
*commit
;
264 struct commit_list
*parents
;
267 if ((dotdot
= strstr(arg
, "^!")))
269 else if ((dotdot
= strstr(arg
, "^@")))
272 if (!dotdot
|| dotdot
[2])
276 if (get_sha1(arg
, sha1
))
280 show_rev(NORMAL
, sha1
, arg
);
281 commit
= lookup_commit_reference(sha1
);
282 for (parents
= commit
->parents
; parents
; parents
= parents
->next
)
283 show_rev(parents_only
? NORMAL
: REVERSED
,
284 parents
->item
->object
.sha1
, arg
);
289 static int parseopt_dump(const struct option
*o
, const char *arg
, int unset
)
291 struct strbuf
*parsed
= o
->value
;
293 strbuf_addf(parsed
, " --no-%s", o
->long_name
);
294 else if (o
->short_name
)
295 strbuf_addf(parsed
, " -%c", o
->short_name
);
297 strbuf_addf(parsed
, " --%s", o
->long_name
);
299 strbuf_addch(parsed
, ' ');
300 sq_quote_buf(parsed
, arg
);
305 static const char *skipspaces(const char *s
)
312 static int cmd_parseopt(int argc
, const char **argv
, const char *prefix
)
314 static int keep_dashdash
= 0, stop_at_non_option
= 0;
315 static char const * const parseopt_usage
[] = {
316 "git rev-parse --parseopt [options] -- [<args>...]",
319 static struct option parseopt_opts
[] = {
320 OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash
,
321 "keep the `--` passed as an arg"),
322 OPT_BOOLEAN(0, "stop-at-non-option", &stop_at_non_option
,
323 "stop parsing after the "
324 "first non-option argument"),
328 struct strbuf sb
= STRBUF_INIT
, parsed
= STRBUF_INIT
;
329 const char **usage
= NULL
;
330 struct option
*opts
= NULL
;
331 int onb
= 0, osz
= 0, unb
= 0, usz
= 0;
333 strbuf_addstr(&parsed
, "set --");
334 argc
= parse_options(argc
, argv
, prefix
, parseopt_opts
, parseopt_usage
,
335 PARSE_OPT_KEEP_DASHDASH
);
336 if (argc
< 1 || strcmp(argv
[0], "--"))
337 usage_with_options(parseopt_usage
, parseopt_opts
);
339 /* get the usage up to the first line with a -- on it */
341 if (strbuf_getline(&sb
, stdin
, '\n') == EOF
)
342 die("premature end of input");
343 ALLOC_GROW(usage
, unb
+ 1, usz
);
344 if (!strcmp("--", sb
.buf
)) {
346 die("no usage string given before the `--' separator");
350 usage
[unb
++] = strbuf_detach(&sb
, NULL
);
353 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
354 while (strbuf_getline(&sb
, stdin
, '\n') != EOF
) {
361 ALLOC_GROW(opts
, onb
+ 1, osz
);
362 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
365 s
= strchr(sb
.buf
, ' ');
366 if (!s
|| *sb
.buf
== ' ') {
367 o
->type
= OPTION_GROUP
;
368 o
->help
= xstrdup(skipspaces(sb
.buf
));
372 o
->type
= OPTION_CALLBACK
;
373 o
->help
= xstrdup(skipspaces(s
));
375 o
->flags
= PARSE_OPT_NOARG
;
376 o
->callback
= &parseopt_dump
;
377 while (s
> sb
.buf
&& strchr("*=?!", s
[-1])) {
380 o
->flags
&= ~PARSE_OPT_NOARG
;
383 o
->flags
&= ~PARSE_OPT_NOARG
;
384 o
->flags
|= PARSE_OPT_OPTARG
;
387 o
->flags
|= PARSE_OPT_NONEG
;
390 o
->flags
|= PARSE_OPT_HIDDEN
;
395 if (s
- sb
.buf
== 1) /* short option only */
396 o
->short_name
= *sb
.buf
;
397 else if (sb
.buf
[1] != ',') /* long option only */
398 o
->long_name
= xmemdupz(sb
.buf
, s
- sb
.buf
);
400 o
->short_name
= *sb
.buf
;
401 o
->long_name
= xmemdupz(sb
.buf
+ 2, s
- sb
.buf
- 2);
406 /* put an OPT_END() */
407 ALLOC_GROW(opts
, onb
+ 1, osz
);
408 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
409 argc
= parse_options(argc
, argv
, prefix
, opts
, usage
,
410 (keep_dashdash
? PARSE_OPT_KEEP_DASHDASH
: 0) |
411 (stop_at_non_option
? PARSE_OPT_STOP_AT_NON_OPTION
: 0) |
412 PARSE_OPT_SHELL_EVAL
);
414 strbuf_addf(&parsed
, " --");
415 sq_quote_argv(&parsed
, argv
, 0);
420 static int cmd_sq_quote(int argc
, const char **argv
)
422 struct strbuf buf
= STRBUF_INIT
;
425 sq_quote_argv(&buf
, argv
, 0);
426 printf("%s\n", buf
.buf
);
427 strbuf_release(&buf
);
432 static void die_no_single_rev(int quiet
)
437 die("Needed a single revision");
440 static const char builtin_rev_parse_usage
[] =
441 "git rev-parse --parseopt [options] -- [<args>...]\n"
442 " or: git rev-parse --sq-quote [<arg>...]\n"
443 " or: git rev-parse [options] [<arg>...]\n"
445 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.";
447 int cmd_rev_parse(int argc
, const char **argv
, const char *prefix
)
449 int i
, as_is
= 0, verify
= 0, quiet
= 0, revs_count
= 0, type
= 0;
450 unsigned char sha1
[20];
451 const char *name
= NULL
;
453 if (argc
> 1 && !strcmp("--parseopt", argv
[1]))
454 return cmd_parseopt(argc
- 1, argv
+ 1, prefix
);
456 if (argc
> 1 && !strcmp("--sq-quote", argv
[1]))
457 return cmd_sq_quote(argc
- 2, argv
+ 2);
459 if (argc
== 2 && !strcmp("--local-env-vars", argv
[1])) {
461 for (i
= 0; local_repo_env
[i
]; i
++)
462 printf("%s\n", local_repo_env
[i
]);
466 if (argc
> 1 && !strcmp("-h", argv
[1]))
467 usage(builtin_rev_parse_usage
);
469 prefix
= setup_git_directory();
470 git_config(git_default_config
, NULL
);
471 for (i
= 1; i
< argc
; i
++) {
472 const char *arg
= argv
[i
];
475 if (show_file(arg
) && as_is
< 2)
476 verify_filename(prefix
, arg
);
479 if (!strcmp(arg
,"-n")) {
481 die("-n requires an argument");
482 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
)) {
488 if (!prefixcmp(arg
, "-n")) {
489 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
))
495 if (!strcmp(arg
, "--")) {
497 /* Pass on the "--" if we show anything but files.. */
498 if (filter
& (DO_FLAGS
| DO_REVS
))
502 if (!strcmp(arg
, "--default")) {
507 if (!strcmp(arg
, "--revs-only")) {
511 if (!strcmp(arg
, "--no-revs")) {
515 if (!strcmp(arg
, "--flags")) {
516 filter
&= ~DO_NONFLAGS
;
519 if (!strcmp(arg
, "--no-flags")) {
523 if (!strcmp(arg
, "--verify")) {
524 filter
&= ~(DO_FLAGS
|DO_NOREV
);
528 if (!strcmp(arg
, "--quiet") || !strcmp(arg
, "-q")) {
532 if (!strcmp(arg
, "--short") ||
533 !prefixcmp(arg
, "--short=")) {
534 filter
&= ~(DO_FLAGS
|DO_NOREV
);
536 abbrev
= DEFAULT_ABBREV
;
538 abbrev
= strtoul(arg
+ 8, NULL
, 10);
539 if (abbrev
< MINIMUM_ABBREV
)
540 abbrev
= MINIMUM_ABBREV
;
541 else if (40 <= abbrev
)
545 if (!strcmp(arg
, "--sq")) {
549 if (!strcmp(arg
, "--not")) {
550 show_type
^= REVERSED
;
553 if (!strcmp(arg
, "--symbolic")) {
554 symbolic
= SHOW_SYMBOLIC_ASIS
;
557 if (!strcmp(arg
, "--symbolic-full-name")) {
558 symbolic
= SHOW_SYMBOLIC_FULL
;
561 if (!prefixcmp(arg
, "--abbrev-ref") &&
562 (!arg
[12] || arg
[12] == '=')) {
564 abbrev_ref_strict
= warn_ambiguous_refs
;
565 if (arg
[12] == '=') {
566 if (!strcmp(arg
+ 13, "strict"))
567 abbrev_ref_strict
= 1;
568 else if (!strcmp(arg
+ 13, "loose"))
569 abbrev_ref_strict
= 0;
571 die("unknown mode for %s", arg
);
575 if (!strcmp(arg
, "--all")) {
576 for_each_ref(show_reference
, NULL
);
579 if (!strcmp(arg
, "--bisect")) {
580 for_each_ref_in("refs/bisect/bad", show_reference
, NULL
);
581 for_each_ref_in("refs/bisect/good", anti_reference
, NULL
);
584 if (!prefixcmp(arg
, "--branches=")) {
585 for_each_glob_ref_in(show_reference
, arg
+ 11,
586 "refs/heads/", NULL
);
589 if (!strcmp(arg
, "--branches")) {
590 for_each_branch_ref(show_reference
, NULL
);
593 if (!prefixcmp(arg
, "--tags=")) {
594 for_each_glob_ref_in(show_reference
, arg
+ 7,
598 if (!strcmp(arg
, "--tags")) {
599 for_each_tag_ref(show_reference
, NULL
);
602 if (!prefixcmp(arg
, "--glob=")) {
603 for_each_glob_ref(show_reference
, arg
+ 7, NULL
);
606 if (!prefixcmp(arg
, "--remotes=")) {
607 for_each_glob_ref_in(show_reference
, arg
+ 10,
608 "refs/remotes/", NULL
);
611 if (!strcmp(arg
, "--remotes")) {
612 for_each_remote_ref(show_reference
, NULL
);
615 if (!strcmp(arg
, "--show-toplevel")) {
616 const char *work_tree
= get_git_work_tree();
621 if (!strcmp(arg
, "--show-prefix")) {
626 if (!strcmp(arg
, "--show-cdup")) {
627 const char *pfx
= prefix
;
628 if (!is_inside_work_tree()) {
629 const char *work_tree
=
632 printf("%s\n", work_tree
);
636 pfx
= strchr(pfx
, '/');
645 if (!strcmp(arg
, "--git-dir")) {
646 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
647 static char cwd
[PATH_MAX
];
657 if (!getcwd(cwd
, PATH_MAX
))
658 die_errno("unable to get current working directory");
660 printf("%s%s.git\n", cwd
, len
&& cwd
[len
-1] != '/' ? "/" : "");
663 if (!strcmp(arg
, "--is-inside-git-dir")) {
664 printf("%s\n", is_inside_git_dir() ? "true"
668 if (!strcmp(arg
, "--is-inside-work-tree")) {
669 printf("%s\n", is_inside_work_tree() ? "true"
673 if (!strcmp(arg
, "--is-bare-repository")) {
674 printf("%s\n", is_bare_repository() ? "true"
678 if (!prefixcmp(arg
, "--since=")) {
679 show_datestring("--max-age=", arg
+8);
682 if (!prefixcmp(arg
, "--after=")) {
683 show_datestring("--max-age=", arg
+8);
686 if (!prefixcmp(arg
, "--before=")) {
687 show_datestring("--min-age=", arg
+9);
690 if (!prefixcmp(arg
, "--until=")) {
691 show_datestring("--min-age=", arg
+8);
694 if (show_flag(arg
) && verify
)
695 die_no_single_rev(quiet
);
699 /* Not a flag argument */
700 if (try_difference(arg
))
702 if (try_parent_shorthands(arg
))
710 if (!get_sha1(name
, sha1
)) {
714 show_rev(type
, sha1
, name
);
718 die_no_single_rev(quiet
);
722 verify_filename(prefix
, arg
);
725 if (revs_count
== 1) {
726 show_rev(type
, sha1
, name
);
728 } else if (revs_count
== 0 && show_default())
730 die_no_single_rev(quiet
);