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
32 * Some arguments are relevant "revision" arguments,
33 * others are about output format or other details.
34 * This sorts it all out.
36 static int is_rev_argument(const char *arg
)
38 static const char *rev_args
[] = {
60 const char **p
= rev_args
;
62 /* accept -<digit>, like traditional "head" */
63 if ((*arg
== '-') && isdigit(arg
[1]))
67 const char *str
= *p
++;
72 if (!strcmp(arg
, str
) ||
73 (str
[len
-1] == '=' && !strncmp(arg
, str
, len
)))
78 /* Output argument as a string, either SQ or normal */
79 static void show(const char *arg
)
85 while ((ch
= *arg
++)) {
87 fputs("'\\'", stdout
);
97 /* Like show(), but with a negation prefix according to type */
98 static void show_with_type(int type
, const char *arg
)
100 if (type
!= show_type
)
105 /* Output a revision, only if filter allows it */
106 static void show_rev(int type
, const unsigned char *sha1
, const char *name
)
108 if (!(filter
& DO_REVS
))
112 if (symbolic
&& name
) {
113 if (symbolic
== SHOW_SYMBOLIC_FULL
) {
114 unsigned char discard
[20];
117 switch (dwim_ref(name
, strlen(name
), discard
, &full
)) {
120 * Not found -- not a ref. We could
121 * emit "name" here, but symbolic-full
122 * users are interested in finding the
123 * refs spelled in full, and they would
124 * need to filter non-refs if we did so.
128 show_with_type(type
, full
);
130 default: /* ambiguous */
131 error("refname '%s' is ambiguous", name
);
135 show_with_type(type
, name
);
139 show_with_type(type
, find_unique_abbrev(sha1
, abbrev
));
141 show_with_type(type
, sha1_to_hex(sha1
));
144 /* Output a flag, only if filter allows it. */
145 static int show_flag(const char *arg
)
147 if (!(filter
& DO_FLAGS
))
149 if (filter
& (is_rev_argument(arg
) ? DO_REVS
: DO_NOREV
)) {
156 static int show_default(void)
161 unsigned char sha1
[20];
164 if (!get_sha1(s
, sha1
)) {
165 show_rev(NORMAL
, sha1
, s
);
172 static int show_reference(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
174 show_rev(NORMAL
, sha1
, refname
);
178 static void show_datestring(const char *flag
, const char *datestr
)
180 static char buffer
[100];
182 /* date handling requires both flags and revs */
183 if ((filter
& (DO_FLAGS
| DO_REVS
)) != (DO_FLAGS
| DO_REVS
))
185 snprintf(buffer
, sizeof(buffer
), "%s%lu", flag
, approxidate(datestr
));
189 static int show_file(const char *arg
)
192 if ((filter
& (DO_NONFLAGS
|DO_NOREV
)) == (DO_NONFLAGS
|DO_NOREV
)) {
199 static int try_difference(const char *arg
)
202 unsigned char sha1
[20];
203 unsigned char end
[20];
208 if (!(dotdot
= strstr(arg
, "..")))
212 symmetric
= (*next
== '.');
221 if (!get_sha1(this, sha1
) && !get_sha1(next
, end
)) {
222 show_rev(NORMAL
, end
, next
);
223 show_rev(symmetric
? NORMAL
: REVERSED
, sha1
, this);
225 struct commit_list
*exclude
;
226 struct commit
*a
, *b
;
227 a
= lookup_commit_reference(sha1
);
228 b
= lookup_commit_reference(end
);
229 exclude
= get_merge_bases(a
, b
, 1);
231 struct commit_list
*n
= exclude
->next
;
233 exclude
->item
->object
.sha1
,NULL
);
244 static int try_parent_shorthands(const char *arg
)
247 unsigned char sha1
[20];
248 struct commit
*commit
;
249 struct commit_list
*parents
;
252 if ((dotdot
= strstr(arg
, "^!")))
254 else if ((dotdot
= strstr(arg
, "^@")))
257 if (!dotdot
|| dotdot
[2])
261 if (get_sha1(arg
, sha1
))
265 show_rev(NORMAL
, sha1
, arg
);
266 commit
= lookup_commit_reference(sha1
);
267 for (parents
= commit
->parents
; parents
; parents
= parents
->next
)
268 show_rev(parents_only
? NORMAL
: REVERSED
,
269 parents
->item
->object
.sha1
, arg
);
274 static int parseopt_dump(const struct option
*o
, const char *arg
, int unset
)
276 struct strbuf
*parsed
= o
->value
;
278 strbuf_addf(parsed
, " --no-%s", o
->long_name
);
279 else if (o
->short_name
)
280 strbuf_addf(parsed
, " -%c", o
->short_name
);
282 strbuf_addf(parsed
, " --%s", o
->long_name
);
284 strbuf_addch(parsed
, ' ');
285 sq_quote_buf(parsed
, arg
);
290 static const char *skipspaces(const char *s
)
297 static int cmd_parseopt(int argc
, const char **argv
, const char *prefix
)
299 static int keep_dashdash
= 0;
300 static char const * const parseopt_usage
[] = {
301 "git rev-parse --parseopt [options] -- [<args>...]",
304 static struct option parseopt_opts
[] = {
305 OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash
,
306 "keep the `--` passed as an arg"),
310 struct strbuf sb
= STRBUF_INIT
, parsed
= STRBUF_INIT
;
311 const char **usage
= NULL
;
312 struct option
*opts
= NULL
;
313 int onb
= 0, osz
= 0, unb
= 0, usz
= 0;
315 strbuf_addstr(&parsed
, "set --");
316 argc
= parse_options(argc
, argv
, parseopt_opts
, parseopt_usage
,
317 PARSE_OPT_KEEP_DASHDASH
);
318 if (argc
< 1 || strcmp(argv
[0], "--"))
319 usage_with_options(parseopt_usage
, parseopt_opts
);
321 /* get the usage up to the first line with a -- on it */
323 if (strbuf_getline(&sb
, stdin
, '\n') == EOF
)
324 die("premature end of input");
325 ALLOC_GROW(usage
, unb
+ 1, usz
);
326 if (!strcmp("--", sb
.buf
)) {
328 die("no usage string given before the `--' separator");
332 usage
[unb
++] = strbuf_detach(&sb
, NULL
);
335 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
336 while (strbuf_getline(&sb
, stdin
, '\n') != EOF
) {
343 ALLOC_GROW(opts
, onb
+ 1, osz
);
344 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
347 s
= strchr(sb
.buf
, ' ');
348 if (!s
|| *sb
.buf
== ' ') {
349 o
->type
= OPTION_GROUP
;
350 o
->help
= xstrdup(skipspaces(sb
.buf
));
354 o
->type
= OPTION_CALLBACK
;
355 o
->help
= xstrdup(skipspaces(s
));
357 o
->flags
= PARSE_OPT_NOARG
;
358 o
->callback
= &parseopt_dump
;
359 while (s
> sb
.buf
&& strchr("*=?!", s
[-1])) {
362 o
->flags
&= ~PARSE_OPT_NOARG
;
365 o
->flags
&= ~PARSE_OPT_NOARG
;
366 o
->flags
|= PARSE_OPT_OPTARG
;
369 o
->flags
|= PARSE_OPT_NONEG
;
372 o
->flags
|= PARSE_OPT_HIDDEN
;
377 if (s
- sb
.buf
== 1) /* short option only */
378 o
->short_name
= *sb
.buf
;
379 else if (sb
.buf
[1] != ',') /* long option only */
380 o
->long_name
= xmemdupz(sb
.buf
, s
- sb
.buf
);
382 o
->short_name
= *sb
.buf
;
383 o
->long_name
= xmemdupz(sb
.buf
+ 2, s
- sb
.buf
- 2);
388 /* put an OPT_END() */
389 ALLOC_GROW(opts
, onb
+ 1, osz
);
390 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
391 argc
= parse_options(argc
, argv
, opts
, usage
,
392 keep_dashdash
? PARSE_OPT_KEEP_DASHDASH
: 0);
394 strbuf_addf(&parsed
, " --");
395 sq_quote_argv(&parsed
, argv
, 0);
400 static void die_no_single_rev(int quiet
)
405 die("Needed a single revision");
408 int cmd_rev_parse(int argc
, const char **argv
, const char *prefix
)
410 int i
, as_is
= 0, verify
= 0, quiet
= 0, revs_count
= 0, type
= 0;
411 unsigned char sha1
[20];
412 const char *name
= NULL
;
414 if (argc
> 1 && !strcmp("--parseopt", argv
[1]))
415 return cmd_parseopt(argc
- 1, argv
+ 1, prefix
);
417 prefix
= setup_git_directory();
418 git_config(git_default_config
, NULL
);
419 for (i
= 1; i
< argc
; i
++) {
420 const char *arg
= argv
[i
];
423 if (show_file(arg
) && as_is
< 2)
424 verify_filename(prefix
, arg
);
427 if (!strcmp(arg
,"-n")) {
429 die("-n requires an argument");
430 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
)) {
436 if (!prefixcmp(arg
, "-n")) {
437 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
))
443 if (!strcmp(arg
, "--")) {
445 /* Pass on the "--" if we show anything but files.. */
446 if (filter
& (DO_FLAGS
| DO_REVS
))
450 if (!strcmp(arg
, "--default")) {
455 if (!strcmp(arg
, "--revs-only")) {
459 if (!strcmp(arg
, "--no-revs")) {
463 if (!strcmp(arg
, "--flags")) {
464 filter
&= ~DO_NONFLAGS
;
467 if (!strcmp(arg
, "--no-flags")) {
471 if (!strcmp(arg
, "--verify")) {
472 filter
&= ~(DO_FLAGS
|DO_NOREV
);
476 if (!strcmp(arg
, "--quiet") || !strcmp(arg
, "-q")) {
480 if (!strcmp(arg
, "--short") ||
481 !prefixcmp(arg
, "--short=")) {
482 filter
&= ~(DO_FLAGS
|DO_NOREV
);
484 abbrev
= DEFAULT_ABBREV
;
486 abbrev
= strtoul(arg
+ 8, NULL
, 10);
487 if (abbrev
< MINIMUM_ABBREV
)
488 abbrev
= MINIMUM_ABBREV
;
489 else if (40 <= abbrev
)
493 if (!strcmp(arg
, "--sq")) {
497 if (!strcmp(arg
, "--not")) {
498 show_type
^= REVERSED
;
501 if (!strcmp(arg
, "--symbolic")) {
502 symbolic
= SHOW_SYMBOLIC_ASIS
;
505 if (!strcmp(arg
, "--symbolic-full-name")) {
506 symbolic
= SHOW_SYMBOLIC_FULL
;
509 if (!strcmp(arg
, "--all")) {
510 for_each_ref(show_reference
, NULL
);
513 if (!strcmp(arg
, "--branches")) {
514 for_each_branch_ref(show_reference
, NULL
);
517 if (!strcmp(arg
, "--tags")) {
518 for_each_tag_ref(show_reference
, NULL
);
521 if (!strcmp(arg
, "--remotes")) {
522 for_each_remote_ref(show_reference
, NULL
);
525 if (!strcmp(arg
, "--show-prefix")) {
530 if (!strcmp(arg
, "--show-cdup")) {
531 const char *pfx
= prefix
;
532 if (!is_inside_work_tree()) {
533 const char *work_tree
=
536 printf("%s\n", work_tree
);
540 pfx
= strchr(pfx
, '/');
549 if (!strcmp(arg
, "--git-dir")) {
550 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
551 static char cwd
[PATH_MAX
];
560 if (!getcwd(cwd
, PATH_MAX
))
561 die("unable to get current working directory");
562 printf("%s/.git\n", cwd
);
565 if (!strcmp(arg
, "--is-inside-git-dir")) {
566 printf("%s\n", is_inside_git_dir() ? "true"
570 if (!strcmp(arg
, "--is-inside-work-tree")) {
571 printf("%s\n", is_inside_work_tree() ? "true"
575 if (!strcmp(arg
, "--is-bare-repository")) {
576 printf("%s\n", is_bare_repository() ? "true"
580 if (!prefixcmp(arg
, "--since=")) {
581 show_datestring("--max-age=", arg
+8);
584 if (!prefixcmp(arg
, "--after=")) {
585 show_datestring("--max-age=", arg
+8);
588 if (!prefixcmp(arg
, "--before=")) {
589 show_datestring("--min-age=", arg
+9);
592 if (!prefixcmp(arg
, "--until=")) {
593 show_datestring("--min-age=", arg
+8);
596 if (show_flag(arg
) && verify
)
597 die_no_single_rev(quiet
);
601 /* Not a flag argument */
602 if (try_difference(arg
))
604 if (try_parent_shorthands(arg
))
612 if (!get_sha1(name
, sha1
)) {
616 show_rev(type
, sha1
, name
);
620 die_no_single_rev(quiet
);
624 verify_filename(prefix
, arg
);
627 if (revs_count
== 1) {
628 show_rev(type
, sha1
, name
);
630 } else if (revs_count
== 0 && show_default())
632 die_no_single_rev(quiet
);