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
[] = {
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
))
114 if ((symbolic
|| abbrev_ref
) && name
) {
115 if (symbolic
== SHOW_SYMBOLIC_FULL
|| abbrev_ref
) {
116 unsigned char discard
[20];
119 switch (dwim_ref(name
, strlen(name
), discard
, &full
)) {
122 * Not found -- not a ref. We could
123 * emit "name" here, but symbolic-full
124 * users are interested in finding the
125 * refs spelled in full, and they would
126 * need to filter non-refs if we did so.
131 full
= shorten_unambiguous_ref(full
,
133 show_with_type(type
, full
);
135 default: /* ambiguous */
136 error("refname '%s' is ambiguous", name
);
140 show_with_type(type
, name
);
144 show_with_type(type
, find_unique_abbrev(sha1
, abbrev
));
146 show_with_type(type
, sha1_to_hex(sha1
));
149 /* Output a flag, only if filter allows it. */
150 static int show_flag(const char *arg
)
152 if (!(filter
& DO_FLAGS
))
154 if (filter
& (is_rev_argument(arg
) ? DO_REVS
: DO_NOREV
)) {
161 static int show_default(void)
166 unsigned char sha1
[20];
169 if (!get_sha1(s
, sha1
)) {
170 show_rev(NORMAL
, sha1
, s
);
177 static int show_reference(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
179 show_rev(NORMAL
, sha1
, refname
);
183 static int anti_reference(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
185 show_rev(REVERSED
, sha1
, refname
);
189 static void show_datestring(const char *flag
, const char *datestr
)
191 static char buffer
[100];
193 /* date handling requires both flags and revs */
194 if ((filter
& (DO_FLAGS
| DO_REVS
)) != (DO_FLAGS
| DO_REVS
))
196 snprintf(buffer
, sizeof(buffer
), "%s%lu", flag
, approxidate(datestr
));
200 static int show_file(const char *arg
)
203 if ((filter
& (DO_NONFLAGS
|DO_NOREV
)) == (DO_NONFLAGS
|DO_NOREV
)) {
210 static int try_difference(const char *arg
)
213 unsigned char sha1
[20];
214 unsigned char end
[20];
219 if (!(dotdot
= strstr(arg
, "..")))
223 symmetric
= (*next
== '.');
232 if (!get_sha1(this, sha1
) && !get_sha1(next
, end
)) {
233 show_rev(NORMAL
, end
, next
);
234 show_rev(symmetric
? NORMAL
: REVERSED
, sha1
, this);
236 struct commit_list
*exclude
;
237 struct commit
*a
, *b
;
238 a
= lookup_commit_reference(sha1
);
239 b
= lookup_commit_reference(end
);
240 exclude
= get_merge_bases(a
, b
, 1);
242 struct commit_list
*n
= exclude
->next
;
244 exclude
->item
->object
.sha1
,NULL
);
255 static int try_parent_shorthands(const char *arg
)
258 unsigned char sha1
[20];
259 struct commit
*commit
;
260 struct commit_list
*parents
;
263 if ((dotdot
= strstr(arg
, "^!")))
265 else if ((dotdot
= strstr(arg
, "^@")))
268 if (!dotdot
|| dotdot
[2])
272 if (get_sha1(arg
, sha1
))
276 show_rev(NORMAL
, sha1
, arg
);
277 commit
= lookup_commit_reference(sha1
);
278 for (parents
= commit
->parents
; parents
; parents
= parents
->next
)
279 show_rev(parents_only
? NORMAL
: REVERSED
,
280 parents
->item
->object
.sha1
, arg
);
285 static int parseopt_dump(const struct option
*o
, const char *arg
, int unset
)
287 struct strbuf
*parsed
= o
->value
;
289 strbuf_addf(parsed
, " --no-%s", o
->long_name
);
290 else if (o
->short_name
)
291 strbuf_addf(parsed
, " -%c", o
->short_name
);
293 strbuf_addf(parsed
, " --%s", o
->long_name
);
295 strbuf_addch(parsed
, ' ');
296 sq_quote_buf(parsed
, arg
);
301 static const char *skipspaces(const char *s
)
308 static int cmd_parseopt(int argc
, const char **argv
, const char *prefix
)
310 static int keep_dashdash
= 0, stop_at_non_option
= 0;
311 static char const * const parseopt_usage
[] = {
312 "git rev-parse --parseopt [options] -- [<args>...]",
315 static struct option parseopt_opts
[] = {
316 OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash
,
317 "keep the `--` passed as an arg"),
318 OPT_BOOLEAN(0, "stop-at-non-option", &stop_at_non_option
,
319 "stop parsing after the "
320 "first non-option argument"),
324 struct strbuf sb
= STRBUF_INIT
, parsed
= STRBUF_INIT
;
325 const char **usage
= NULL
;
326 struct option
*opts
= NULL
;
327 int onb
= 0, osz
= 0, unb
= 0, usz
= 0;
329 strbuf_addstr(&parsed
, "set --");
330 argc
= parse_options(argc
, argv
, prefix
, parseopt_opts
, parseopt_usage
,
331 PARSE_OPT_KEEP_DASHDASH
);
332 if (argc
< 1 || strcmp(argv
[0], "--"))
333 usage_with_options(parseopt_usage
, parseopt_opts
);
335 /* get the usage up to the first line with a -- on it */
337 if (strbuf_getline(&sb
, stdin
, '\n') == EOF
)
338 die("premature end of input");
339 ALLOC_GROW(usage
, unb
+ 1, usz
);
340 if (!strcmp("--", sb
.buf
)) {
342 die("no usage string given before the `--' separator");
346 usage
[unb
++] = strbuf_detach(&sb
, NULL
);
349 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
350 while (strbuf_getline(&sb
, stdin
, '\n') != EOF
) {
357 ALLOC_GROW(opts
, onb
+ 1, osz
);
358 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
361 s
= strchr(sb
.buf
, ' ');
362 if (!s
|| *sb
.buf
== ' ') {
363 o
->type
= OPTION_GROUP
;
364 o
->help
= xstrdup(skipspaces(sb
.buf
));
368 o
->type
= OPTION_CALLBACK
;
369 o
->help
= xstrdup(skipspaces(s
));
371 o
->flags
= PARSE_OPT_NOARG
;
372 o
->callback
= &parseopt_dump
;
373 while (s
> sb
.buf
&& strchr("*=?!", s
[-1])) {
376 o
->flags
&= ~PARSE_OPT_NOARG
;
379 o
->flags
&= ~PARSE_OPT_NOARG
;
380 o
->flags
|= PARSE_OPT_OPTARG
;
383 o
->flags
|= PARSE_OPT_NONEG
;
386 o
->flags
|= PARSE_OPT_HIDDEN
;
391 if (s
- sb
.buf
== 1) /* short option only */
392 o
->short_name
= *sb
.buf
;
393 else if (sb
.buf
[1] != ',') /* long option only */
394 o
->long_name
= xmemdupz(sb
.buf
, s
- sb
.buf
);
396 o
->short_name
= *sb
.buf
;
397 o
->long_name
= xmemdupz(sb
.buf
+ 2, s
- sb
.buf
- 2);
402 /* put an OPT_END() */
403 ALLOC_GROW(opts
, onb
+ 1, osz
);
404 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
405 argc
= parse_options(argc
, argv
, prefix
, opts
, usage
,
406 keep_dashdash
? PARSE_OPT_KEEP_DASHDASH
: 0 |
407 stop_at_non_option
? PARSE_OPT_STOP_AT_NON_OPTION
: 0);
409 strbuf_addf(&parsed
, " --");
410 sq_quote_argv(&parsed
, argv
, 0);
415 static int cmd_sq_quote(int argc
, const char **argv
)
417 struct strbuf buf
= STRBUF_INIT
;
420 sq_quote_argv(&buf
, argv
, 0);
421 printf("%s\n", buf
.buf
);
422 strbuf_release(&buf
);
427 static void die_no_single_rev(int quiet
)
432 die("Needed a single revision");
435 int cmd_rev_parse(int argc
, const char **argv
, const char *prefix
)
437 int i
, as_is
= 0, verify
= 0, quiet
= 0, revs_count
= 0, type
= 0;
438 unsigned char sha1
[20];
439 const char *name
= NULL
;
441 if (argc
> 1 && !strcmp("--parseopt", argv
[1]))
442 return cmd_parseopt(argc
- 1, argv
+ 1, prefix
);
444 if (argc
> 1 && !strcmp("--sq-quote", argv
[1]))
445 return cmd_sq_quote(argc
- 2, argv
+ 2);
447 prefix
= setup_git_directory();
448 git_config(git_default_config
, NULL
);
449 for (i
= 1; i
< argc
; i
++) {
450 const char *arg
= argv
[i
];
453 if (show_file(arg
) && as_is
< 2)
454 verify_filename(prefix
, arg
);
457 if (!strcmp(arg
,"-n")) {
459 die("-n requires an argument");
460 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
)) {
466 if (!prefixcmp(arg
, "-n")) {
467 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
))
473 if (!strcmp(arg
, "--")) {
475 /* Pass on the "--" if we show anything but files.. */
476 if (filter
& (DO_FLAGS
| DO_REVS
))
480 if (!strcmp(arg
, "--default")) {
485 if (!strcmp(arg
, "--revs-only")) {
489 if (!strcmp(arg
, "--no-revs")) {
493 if (!strcmp(arg
, "--flags")) {
494 filter
&= ~DO_NONFLAGS
;
497 if (!strcmp(arg
, "--no-flags")) {
501 if (!strcmp(arg
, "--verify")) {
502 filter
&= ~(DO_FLAGS
|DO_NOREV
);
506 if (!strcmp(arg
, "--quiet") || !strcmp(arg
, "-q")) {
510 if (!strcmp(arg
, "--short") ||
511 !prefixcmp(arg
, "--short=")) {
512 filter
&= ~(DO_FLAGS
|DO_NOREV
);
514 abbrev
= DEFAULT_ABBREV
;
516 abbrev
= strtoul(arg
+ 8, NULL
, 10);
517 if (abbrev
< MINIMUM_ABBREV
)
518 abbrev
= MINIMUM_ABBREV
;
519 else if (40 <= abbrev
)
523 if (!strcmp(arg
, "--sq")) {
527 if (!strcmp(arg
, "--not")) {
528 show_type
^= REVERSED
;
531 if (!strcmp(arg
, "--symbolic")) {
532 symbolic
= SHOW_SYMBOLIC_ASIS
;
535 if (!strcmp(arg
, "--symbolic-full-name")) {
536 symbolic
= SHOW_SYMBOLIC_FULL
;
539 if (!prefixcmp(arg
, "--abbrev-ref") &&
540 (!arg
[12] || arg
[12] == '=')) {
542 abbrev_ref_strict
= warn_ambiguous_refs
;
543 if (arg
[12] == '=') {
544 if (!strcmp(arg
+ 13, "strict"))
545 abbrev_ref_strict
= 1;
546 else if (!strcmp(arg
+ 13, "loose"))
547 abbrev_ref_strict
= 0;
549 die("unknown mode for %s", arg
);
553 if (!strcmp(arg
, "--all")) {
554 for_each_ref(show_reference
, NULL
);
557 if (!strcmp(arg
, "--bisect")) {
558 for_each_ref_in("refs/bisect/bad", show_reference
, NULL
);
559 for_each_ref_in("refs/bisect/good", anti_reference
, NULL
);
562 if (!strcmp(arg
, "--branches")) {
563 for_each_branch_ref(show_reference
, NULL
);
566 if (!strcmp(arg
, "--tags")) {
567 for_each_tag_ref(show_reference
, NULL
);
570 if (!strcmp(arg
, "--remotes")) {
571 for_each_remote_ref(show_reference
, NULL
);
574 if (!strcmp(arg
, "--show-prefix")) {
579 if (!strcmp(arg
, "--show-cdup")) {
580 const char *pfx
= prefix
;
581 if (!is_inside_work_tree()) {
582 const char *work_tree
=
585 printf("%s\n", work_tree
);
589 pfx
= strchr(pfx
, '/');
598 if (!strcmp(arg
, "--git-dir")) {
599 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
600 static char cwd
[PATH_MAX
];
609 if (!getcwd(cwd
, PATH_MAX
))
610 die_errno("unable to get current working directory");
611 printf("%s/.git\n", cwd
);
614 if (!strcmp(arg
, "--is-inside-git-dir")) {
615 printf("%s\n", is_inside_git_dir() ? "true"
619 if (!strcmp(arg
, "--is-inside-work-tree")) {
620 printf("%s\n", is_inside_work_tree() ? "true"
624 if (!strcmp(arg
, "--is-bare-repository")) {
625 printf("%s\n", is_bare_repository() ? "true"
629 if (!prefixcmp(arg
, "--since=")) {
630 show_datestring("--max-age=", arg
+8);
633 if (!prefixcmp(arg
, "--after=")) {
634 show_datestring("--max-age=", arg
+8);
637 if (!prefixcmp(arg
, "--before=")) {
638 show_datestring("--min-age=", arg
+9);
641 if (!prefixcmp(arg
, "--until=")) {
642 show_datestring("--min-age=", arg
+8);
645 if (show_flag(arg
) && verify
)
646 die_no_single_rev(quiet
);
650 /* Not a flag argument */
651 if (try_difference(arg
))
653 if (try_parent_shorthands(arg
))
661 if (!get_sha1(name
, sha1
)) {
665 show_rev(type
, sha1
, name
);
669 die_no_single_rev(quiet
);
673 verify_filename(prefix
, arg
);
676 if (revs_count
== 1) {
677 show_rev(type
, sha1
, name
);
679 } else if (revs_count
== 0 && show_default())
681 die_no_single_rev(quiet
);