4 * Copyright (C) Linus Torvalds, 2005
11 #include "parse-options.h"
19 static int filter
= ~0;
21 static const char *def
;
25 static int show_type
= NORMAL
;
27 #define SHOW_SYMBOLIC_ASIS 1
28 #define SHOW_SYMBOLIC_FULL 2
31 static int abbrev_ref
;
32 static int abbrev_ref_strict
;
35 static struct string_list
*ref_excludes
;
38 * Some arguments are relevant "revision" arguments,
39 * others are about output format or other details.
40 * This sorts it all out.
42 static int is_rev_argument(const char *arg
)
44 static const char *rev_args
[] = {
75 const char **p
= rev_args
;
77 /* accept -<digit>, like traditional "head" */
78 if ((*arg
== '-') && isdigit(arg
[1]))
82 const char *str
= *p
++;
87 if (!strcmp(arg
, str
) ||
88 (str
[len
-1] == '=' && !strncmp(arg
, str
, len
)))
93 /* Output argument as a string, either SQ or normal */
94 static void show(const char *arg
)
100 while ((ch
= *arg
++)) {
102 fputs("'\\'", stdout
);
112 /* Like show(), but with a negation prefix according to type */
113 static void show_with_type(int type
, const char *arg
)
115 if (type
!= show_type
)
120 /* Output a revision, only if filter allows it */
121 static void show_rev(int type
, const unsigned char *sha1
, const char *name
)
123 if (!(filter
& DO_REVS
))
127 if ((symbolic
|| abbrev_ref
) && name
) {
128 if (symbolic
== SHOW_SYMBOLIC_FULL
|| abbrev_ref
) {
129 unsigned char discard
[20];
132 switch (dwim_ref(name
, strlen(name
), discard
, &full
)) {
135 * Not found -- not a ref. We could
136 * emit "name" here, but symbolic-full
137 * users are interested in finding the
138 * refs spelled in full, and they would
139 * need to filter non-refs if we did so.
144 full
= shorten_unambiguous_ref(full
,
146 show_with_type(type
, full
);
148 default: /* ambiguous */
149 error("refname '%s' is ambiguous", name
);
153 show_with_type(type
, name
);
157 show_with_type(type
, find_unique_abbrev(sha1
, abbrev
));
159 show_with_type(type
, sha1_to_hex(sha1
));
162 /* Output a flag, only if filter allows it. */
163 static int show_flag(const char *arg
)
165 if (!(filter
& DO_FLAGS
))
167 if (filter
& (is_rev_argument(arg
) ? DO_REVS
: DO_NOREV
)) {
174 static int show_default(void)
179 unsigned char sha1
[20];
182 if (!get_sha1(s
, sha1
)) {
183 show_rev(NORMAL
, sha1
, s
);
190 static int show_reference(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
192 if (ref_excluded(ref_excludes
, refname
))
194 show_rev(NORMAL
, sha1
, refname
);
198 static int anti_reference(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
200 show_rev(REVERSED
, sha1
, refname
);
204 static int show_abbrev(const unsigned char *sha1
, void *cb_data
)
206 show_rev(NORMAL
, sha1
, NULL
);
210 static void show_datestring(const char *flag
, const char *datestr
)
212 static char buffer
[100];
214 /* date handling requires both flags and revs */
215 if ((filter
& (DO_FLAGS
| DO_REVS
)) != (DO_FLAGS
| DO_REVS
))
217 snprintf(buffer
, sizeof(buffer
), "%s%lu", flag
, approxidate(datestr
));
221 static int show_file(const char *arg
, int output_prefix
)
224 if ((filter
& (DO_NONFLAGS
|DO_NOREV
)) == (DO_NONFLAGS
|DO_NOREV
)) {
226 const char *prefix
= startup_info
->prefix
;
227 show(prefix_filename(prefix
,
228 prefix
? strlen(prefix
) : 0,
237 static int try_difference(const char *arg
)
240 unsigned char sha1
[20];
241 unsigned char end
[20];
245 static const char head_by_default
[] = "HEAD";
247 if (!(dotdot
= strstr(arg
, "..")))
251 symmetric
= (*next
== '.');
257 next
= head_by_default
;
259 this = head_by_default
;
261 if (this == head_by_default
&& next
== head_by_default
&&
264 * Just ".."? That is not a range but the
265 * pathspec for the parent directory.
271 if (!get_sha1_committish(this, sha1
) && !get_sha1_committish(next
, end
)) {
272 show_rev(NORMAL
, end
, next
);
273 show_rev(symmetric
? NORMAL
: REVERSED
, sha1
, this);
275 struct commit_list
*exclude
;
276 struct commit
*a
, *b
;
277 a
= lookup_commit_reference(sha1
);
278 b
= lookup_commit_reference(end
);
279 exclude
= get_merge_bases(a
, b
, 1);
281 struct commit_list
*n
= exclude
->next
;
283 exclude
->item
->object
.sha1
,NULL
);
294 static int try_parent_shorthands(const char *arg
)
297 unsigned char sha1
[20];
298 struct commit
*commit
;
299 struct commit_list
*parents
;
302 if ((dotdot
= strstr(arg
, "^!")))
304 else if ((dotdot
= strstr(arg
, "^@")))
307 if (!dotdot
|| dotdot
[2])
311 if (get_sha1_committish(arg
, sha1
))
315 show_rev(NORMAL
, sha1
, arg
);
316 commit
= lookup_commit_reference(sha1
);
317 for (parents
= commit
->parents
; parents
; parents
= parents
->next
)
318 show_rev(parents_only
? NORMAL
: REVERSED
,
319 parents
->item
->object
.sha1
, arg
);
324 static int parseopt_dump(const struct option
*o
, const char *arg
, int unset
)
326 struct strbuf
*parsed
= o
->value
;
328 strbuf_addf(parsed
, " --no-%s", o
->long_name
);
329 else if (o
->short_name
)
330 strbuf_addf(parsed
, " -%c", o
->short_name
);
332 strbuf_addf(parsed
, " --%s", o
->long_name
);
334 strbuf_addch(parsed
, ' ');
335 sq_quote_buf(parsed
, arg
);
340 static const char *skipspaces(const char *s
)
347 static int cmd_parseopt(int argc
, const char **argv
, const char *prefix
)
349 static int keep_dashdash
= 0, stop_at_non_option
= 0;
350 static char const * const parseopt_usage
[] = {
351 N_("git rev-parse --parseopt [options] -- [<args>...]"),
354 static struct option parseopt_opts
[] = {
355 OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash
,
356 N_("keep the `--` passed as an arg")),
357 OPT_BOOLEAN(0, "stop-at-non-option", &stop_at_non_option
,
358 N_("stop parsing after the "
359 "first non-option argument")),
363 struct strbuf sb
= STRBUF_INIT
, parsed
= STRBUF_INIT
;
364 const char **usage
= NULL
;
365 struct option
*opts
= NULL
;
366 int onb
= 0, osz
= 0, unb
= 0, usz
= 0;
368 strbuf_addstr(&parsed
, "set --");
369 argc
= parse_options(argc
, argv
, prefix
, parseopt_opts
, parseopt_usage
,
370 PARSE_OPT_KEEP_DASHDASH
);
371 if (argc
< 1 || strcmp(argv
[0], "--"))
372 usage_with_options(parseopt_usage
, parseopt_opts
);
374 /* get the usage up to the first line with a -- on it */
376 if (strbuf_getline(&sb
, stdin
, '\n') == EOF
)
377 die("premature end of input");
378 ALLOC_GROW(usage
, unb
+ 1, usz
);
379 if (!strcmp("--", sb
.buf
)) {
381 die("no usage string given before the `--' separator");
385 usage
[unb
++] = strbuf_detach(&sb
, NULL
);
388 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
389 while (strbuf_getline(&sb
, stdin
, '\n') != EOF
) {
396 ALLOC_GROW(opts
, onb
+ 1, osz
);
397 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
400 s
= strchr(sb
.buf
, ' ');
401 if (!s
|| *sb
.buf
== ' ') {
402 o
->type
= OPTION_GROUP
;
403 o
->help
= xstrdup(skipspaces(sb
.buf
));
407 o
->type
= OPTION_CALLBACK
;
408 o
->help
= xstrdup(skipspaces(s
));
410 o
->flags
= PARSE_OPT_NOARG
;
411 o
->callback
= &parseopt_dump
;
412 while (s
> sb
.buf
&& strchr("*=?!", s
[-1])) {
415 o
->flags
&= ~PARSE_OPT_NOARG
;
418 o
->flags
&= ~PARSE_OPT_NOARG
;
419 o
->flags
|= PARSE_OPT_OPTARG
;
422 o
->flags
|= PARSE_OPT_NONEG
;
425 o
->flags
|= PARSE_OPT_HIDDEN
;
430 if (s
- sb
.buf
== 1) /* short option only */
431 o
->short_name
= *sb
.buf
;
432 else if (sb
.buf
[1] != ',') /* long option only */
433 o
->long_name
= xmemdupz(sb
.buf
, s
- sb
.buf
);
435 o
->short_name
= *sb
.buf
;
436 o
->long_name
= xmemdupz(sb
.buf
+ 2, s
- sb
.buf
- 2);
441 /* put an OPT_END() */
442 ALLOC_GROW(opts
, onb
+ 1, osz
);
443 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
444 argc
= parse_options(argc
, argv
, prefix
, opts
, usage
,
445 (keep_dashdash
? PARSE_OPT_KEEP_DASHDASH
: 0) |
446 (stop_at_non_option
? PARSE_OPT_STOP_AT_NON_OPTION
: 0) |
447 PARSE_OPT_SHELL_EVAL
);
449 strbuf_addf(&parsed
, " --");
450 sq_quote_argv(&parsed
, argv
, 0);
455 static int cmd_sq_quote(int argc
, const char **argv
)
457 struct strbuf buf
= STRBUF_INIT
;
460 sq_quote_argv(&buf
, argv
, 0);
461 printf("%s\n", buf
.buf
);
462 strbuf_release(&buf
);
467 static void die_no_single_rev(int quiet
)
472 die("Needed a single revision");
475 static const char builtin_rev_parse_usage
[] =
476 N_("git rev-parse --parseopt [options] -- [<args>...]\n"
477 " or: git rev-parse --sq-quote [<arg>...]\n"
478 " or: git rev-parse [options] [<arg>...]\n"
480 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
482 int cmd_rev_parse(int argc
, const char **argv
, const char *prefix
)
484 int i
, as_is
= 0, verify
= 0, quiet
= 0, revs_count
= 0, type
= 0;
485 int output_prefix
= 0;
486 unsigned char sha1
[20];
487 const char *name
= NULL
;
489 if (argc
> 1 && !strcmp("--parseopt", argv
[1]))
490 return cmd_parseopt(argc
- 1, argv
+ 1, prefix
);
492 if (argc
> 1 && !strcmp("--sq-quote", argv
[1]))
493 return cmd_sq_quote(argc
- 2, argv
+ 2);
495 if (argc
== 2 && !strcmp("--local-env-vars", argv
[1])) {
497 for (i
= 0; local_repo_env
[i
]; i
++)
498 printf("%s\n", local_repo_env
[i
]);
502 if (argc
> 2 && !strcmp(argv
[1], "--resolve-git-dir")) {
503 const char *gitdir
= resolve_gitdir(argv
[2]);
505 die("not a gitdir '%s'", argv
[2]);
510 if (argc
> 1 && !strcmp("-h", argv
[1]))
511 usage(builtin_rev_parse_usage
);
513 prefix
= setup_git_directory();
514 git_config(git_default_config
, NULL
);
515 for (i
= 1; i
< argc
; i
++) {
516 const char *arg
= argv
[i
];
519 if (show_file(arg
, output_prefix
) && as_is
< 2)
520 verify_filename(prefix
, arg
, 0);
523 if (!strcmp(arg
,"-n")) {
525 die("-n requires an argument");
526 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
)) {
532 if (!prefixcmp(arg
, "-n")) {
533 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
))
539 if (!strcmp(arg
, "--")) {
541 /* Pass on the "--" if we show anything but files.. */
542 if (filter
& (DO_FLAGS
| DO_REVS
))
546 if (!strcmp(arg
, "--default")) {
551 if (!strcmp(arg
, "--prefix")) {
553 startup_info
->prefix
= prefix
;
558 if (!strcmp(arg
, "--revs-only")) {
562 if (!strcmp(arg
, "--no-revs")) {
566 if (!strcmp(arg
, "--flags")) {
567 filter
&= ~DO_NONFLAGS
;
570 if (!strcmp(arg
, "--no-flags")) {
574 if (!strcmp(arg
, "--verify")) {
575 filter
&= ~(DO_FLAGS
|DO_NOREV
);
579 if (!strcmp(arg
, "--quiet") || !strcmp(arg
, "-q")) {
583 if (!strcmp(arg
, "--short") ||
584 !prefixcmp(arg
, "--short=")) {
585 filter
&= ~(DO_FLAGS
|DO_NOREV
);
587 abbrev
= DEFAULT_ABBREV
;
589 abbrev
= strtoul(arg
+ 8, NULL
, 10);
590 if (abbrev
< MINIMUM_ABBREV
)
591 abbrev
= MINIMUM_ABBREV
;
592 else if (40 <= abbrev
)
596 if (!strcmp(arg
, "--sq")) {
600 if (!strcmp(arg
, "--not")) {
601 show_type
^= REVERSED
;
604 if (!strcmp(arg
, "--symbolic")) {
605 symbolic
= SHOW_SYMBOLIC_ASIS
;
608 if (!strcmp(arg
, "--symbolic-full-name")) {
609 symbolic
= SHOW_SYMBOLIC_FULL
;
612 if (!prefixcmp(arg
, "--abbrev-ref") &&
613 (!arg
[12] || arg
[12] == '=')) {
615 abbrev_ref_strict
= warn_ambiguous_refs
;
616 if (arg
[12] == '=') {
617 if (!strcmp(arg
+ 13, "strict"))
618 abbrev_ref_strict
= 1;
619 else if (!strcmp(arg
+ 13, "loose"))
620 abbrev_ref_strict
= 0;
622 die("unknown mode for %s", arg
);
626 if (!strcmp(arg
, "--all")) {
627 for_each_ref(show_reference
, NULL
);
630 if (!prefixcmp(arg
, "--disambiguate=")) {
631 for_each_abbrev(arg
+ 15, show_abbrev
, NULL
);
634 if (!strcmp(arg
, "--bisect")) {
635 for_each_ref_in("refs/bisect/bad", show_reference
, NULL
);
636 for_each_ref_in("refs/bisect/good", anti_reference
, NULL
);
639 if (!prefixcmp(arg
, "--branches=")) {
640 for_each_glob_ref_in(show_reference
, arg
+ 11,
641 "refs/heads/", NULL
);
642 clear_ref_exclusion(&ref_excludes
);
645 if (!strcmp(arg
, "--branches")) {
646 for_each_branch_ref(show_reference
, NULL
);
647 clear_ref_exclusion(&ref_excludes
);
650 if (!prefixcmp(arg
, "--tags=")) {
651 for_each_glob_ref_in(show_reference
, arg
+ 7,
653 clear_ref_exclusion(&ref_excludes
);
656 if (!strcmp(arg
, "--tags")) {
657 for_each_tag_ref(show_reference
, NULL
);
658 clear_ref_exclusion(&ref_excludes
);
661 if (!prefixcmp(arg
, "--glob=")) {
662 for_each_glob_ref(show_reference
, arg
+ 7, NULL
);
663 clear_ref_exclusion(&ref_excludes
);
666 if (!prefixcmp(arg
, "--remotes=")) {
667 for_each_glob_ref_in(show_reference
, arg
+ 10,
668 "refs/remotes/", NULL
);
669 clear_ref_exclusion(&ref_excludes
);
672 if (!strcmp(arg
, "--remotes")) {
673 for_each_remote_ref(show_reference
, NULL
);
674 clear_ref_exclusion(&ref_excludes
);
677 if (!prefixcmp(arg
, "--exclude=")) {
678 add_ref_exclusion(&ref_excludes
, arg
+ 10);
681 if (!strcmp(arg
, "--show-toplevel")) {
682 const char *work_tree
= get_git_work_tree();
687 if (!strcmp(arg
, "--show-prefix")) {
694 if (!strcmp(arg
, "--show-cdup")) {
695 const char *pfx
= prefix
;
696 if (!is_inside_work_tree()) {
697 const char *work_tree
=
700 printf("%s\n", work_tree
);
704 pfx
= strchr(pfx
, '/');
713 if (!strcmp(arg
, "--git-dir")) {
714 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
715 static char cwd
[PATH_MAX
];
725 if (!getcwd(cwd
, PATH_MAX
))
726 die_errno("unable to get current working directory");
728 printf("%s%s.git\n", cwd
, len
&& cwd
[len
-1] != '/' ? "/" : "");
731 if (!strcmp(arg
, "--is-inside-git-dir")) {
732 printf("%s\n", is_inside_git_dir() ? "true"
736 if (!strcmp(arg
, "--is-inside-work-tree")) {
737 printf("%s\n", is_inside_work_tree() ? "true"
741 if (!strcmp(arg
, "--is-bare-repository")) {
742 printf("%s\n", is_bare_repository() ? "true"
746 if (!prefixcmp(arg
, "--since=")) {
747 show_datestring("--max-age=", arg
+8);
750 if (!prefixcmp(arg
, "--after=")) {
751 show_datestring("--max-age=", arg
+8);
754 if (!prefixcmp(arg
, "--before=")) {
755 show_datestring("--min-age=", arg
+9);
758 if (!prefixcmp(arg
, "--until=")) {
759 show_datestring("--min-age=", arg
+8);
762 if (show_flag(arg
) && verify
)
763 die_no_single_rev(quiet
);
767 /* Not a flag argument */
768 if (try_difference(arg
))
770 if (try_parent_shorthands(arg
))
778 if (!get_sha1(name
, sha1
)) {
782 show_rev(type
, sha1
, name
);
786 die_no_single_rev(quiet
);
788 if (!show_file(arg
, output_prefix
))
790 verify_filename(prefix
, arg
, 1);
793 if (revs_count
== 1) {
794 show_rev(type
, sha1
, name
);
796 } else if (revs_count
== 0 && show_default())
798 die_no_single_rev(quiet
);