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 void show_datestring(const char *flag
, const char *datestr
)
185 static char buffer
[100];
187 /* date handling requires both flags and revs */
188 if ((filter
& (DO_FLAGS
| DO_REVS
)) != (DO_FLAGS
| DO_REVS
))
190 snprintf(buffer
, sizeof(buffer
), "%s%lu", flag
, approxidate(datestr
));
194 static int show_file(const char *arg
)
197 if ((filter
& (DO_NONFLAGS
|DO_NOREV
)) == (DO_NONFLAGS
|DO_NOREV
)) {
204 static int try_difference(const char *arg
)
207 unsigned char sha1
[20];
208 unsigned char end
[20];
213 if (!(dotdot
= strstr(arg
, "..")))
217 symmetric
= (*next
== '.');
226 if (!get_sha1(this, sha1
) && !get_sha1(next
, end
)) {
227 show_rev(NORMAL
, end
, next
);
228 show_rev(symmetric
? NORMAL
: REVERSED
, sha1
, this);
230 struct commit_list
*exclude
;
231 struct commit
*a
, *b
;
232 a
= lookup_commit_reference(sha1
);
233 b
= lookup_commit_reference(end
);
234 exclude
= get_merge_bases(a
, b
, 1);
236 struct commit_list
*n
= exclude
->next
;
238 exclude
->item
->object
.sha1
,NULL
);
249 static int try_parent_shorthands(const char *arg
)
252 unsigned char sha1
[20];
253 struct commit
*commit
;
254 struct commit_list
*parents
;
257 if ((dotdot
= strstr(arg
, "^!")))
259 else if ((dotdot
= strstr(arg
, "^@")))
262 if (!dotdot
|| dotdot
[2])
266 if (get_sha1(arg
, sha1
))
270 show_rev(NORMAL
, sha1
, arg
);
271 commit
= lookup_commit_reference(sha1
);
272 for (parents
= commit
->parents
; parents
; parents
= parents
->next
)
273 show_rev(parents_only
? NORMAL
: REVERSED
,
274 parents
->item
->object
.sha1
, arg
);
279 static int parseopt_dump(const struct option
*o
, const char *arg
, int unset
)
281 struct strbuf
*parsed
= o
->value
;
283 strbuf_addf(parsed
, " --no-%s", o
->long_name
);
284 else if (o
->short_name
)
285 strbuf_addf(parsed
, " -%c", o
->short_name
);
287 strbuf_addf(parsed
, " --%s", o
->long_name
);
289 strbuf_addch(parsed
, ' ');
290 sq_quote_buf(parsed
, arg
);
295 static const char *skipspaces(const char *s
)
302 static int cmd_parseopt(int argc
, const char **argv
, const char *prefix
)
304 static int keep_dashdash
= 0;
305 static char const * const parseopt_usage
[] = {
306 "git rev-parse --parseopt [options] -- [<args>...]",
309 static struct option parseopt_opts
[] = {
310 OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash
,
311 "keep the `--` passed as an arg"),
315 struct strbuf sb
= STRBUF_INIT
, parsed
= STRBUF_INIT
;
316 const char **usage
= NULL
;
317 struct option
*opts
= NULL
;
318 int onb
= 0, osz
= 0, unb
= 0, usz
= 0;
320 strbuf_addstr(&parsed
, "set --");
321 argc
= parse_options(argc
, argv
, parseopt_opts
, parseopt_usage
,
322 PARSE_OPT_KEEP_DASHDASH
);
323 if (argc
< 1 || strcmp(argv
[0], "--"))
324 usage_with_options(parseopt_usage
, parseopt_opts
);
326 /* get the usage up to the first line with a -- on it */
328 if (strbuf_getline(&sb
, stdin
, '\n') == EOF
)
329 die("premature end of input");
330 ALLOC_GROW(usage
, unb
+ 1, usz
);
331 if (!strcmp("--", sb
.buf
)) {
333 die("no usage string given before the `--' separator");
337 usage
[unb
++] = strbuf_detach(&sb
, NULL
);
340 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
341 while (strbuf_getline(&sb
, stdin
, '\n') != EOF
) {
348 ALLOC_GROW(opts
, onb
+ 1, osz
);
349 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
352 s
= strchr(sb
.buf
, ' ');
353 if (!s
|| *sb
.buf
== ' ') {
354 o
->type
= OPTION_GROUP
;
355 o
->help
= xstrdup(skipspaces(sb
.buf
));
359 o
->type
= OPTION_CALLBACK
;
360 o
->help
= xstrdup(skipspaces(s
));
362 o
->flags
= PARSE_OPT_NOARG
;
363 o
->callback
= &parseopt_dump
;
364 while (s
> sb
.buf
&& strchr("*=?!", s
[-1])) {
367 o
->flags
&= ~PARSE_OPT_NOARG
;
370 o
->flags
&= ~PARSE_OPT_NOARG
;
371 o
->flags
|= PARSE_OPT_OPTARG
;
374 o
->flags
|= PARSE_OPT_NONEG
;
377 o
->flags
|= PARSE_OPT_HIDDEN
;
382 if (s
- sb
.buf
== 1) /* short option only */
383 o
->short_name
= *sb
.buf
;
384 else if (sb
.buf
[1] != ',') /* long option only */
385 o
->long_name
= xmemdupz(sb
.buf
, s
- sb
.buf
);
387 o
->short_name
= *sb
.buf
;
388 o
->long_name
= xmemdupz(sb
.buf
+ 2, s
- sb
.buf
- 2);
393 /* put an OPT_END() */
394 ALLOC_GROW(opts
, onb
+ 1, osz
);
395 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
396 argc
= parse_options(argc
, argv
, opts
, usage
,
397 keep_dashdash
? PARSE_OPT_KEEP_DASHDASH
: 0);
399 strbuf_addf(&parsed
, " --");
400 sq_quote_argv(&parsed
, argv
, 0);
405 static void die_no_single_rev(int quiet
)
410 die("Needed a single revision");
413 int cmd_rev_parse(int argc
, const char **argv
, const char *prefix
)
415 int i
, as_is
= 0, verify
= 0, quiet
= 0, revs_count
= 0, type
= 0;
416 unsigned char sha1
[20];
417 const char *name
= NULL
;
419 if (argc
> 1 && !strcmp("--parseopt", argv
[1]))
420 return cmd_parseopt(argc
- 1, argv
+ 1, prefix
);
422 prefix
= setup_git_directory();
423 git_config(git_default_config
, NULL
);
424 for (i
= 1; i
< argc
; i
++) {
425 const char *arg
= argv
[i
];
428 if (show_file(arg
) && as_is
< 2)
429 verify_filename(prefix
, arg
);
432 if (!strcmp(arg
,"-n")) {
434 die("-n requires an argument");
435 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
)) {
441 if (!prefixcmp(arg
, "-n")) {
442 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
))
448 if (!strcmp(arg
, "--")) {
450 /* Pass on the "--" if we show anything but files.. */
451 if (filter
& (DO_FLAGS
| DO_REVS
))
455 if (!strcmp(arg
, "--default")) {
460 if (!strcmp(arg
, "--revs-only")) {
464 if (!strcmp(arg
, "--no-revs")) {
468 if (!strcmp(arg
, "--flags")) {
469 filter
&= ~DO_NONFLAGS
;
472 if (!strcmp(arg
, "--no-flags")) {
476 if (!strcmp(arg
, "--verify")) {
477 filter
&= ~(DO_FLAGS
|DO_NOREV
);
481 if (!strcmp(arg
, "--quiet") || !strcmp(arg
, "-q")) {
485 if (!strcmp(arg
, "--short") ||
486 !prefixcmp(arg
, "--short=")) {
487 filter
&= ~(DO_FLAGS
|DO_NOREV
);
489 abbrev
= DEFAULT_ABBREV
;
491 abbrev
= strtoul(arg
+ 8, NULL
, 10);
492 if (abbrev
< MINIMUM_ABBREV
)
493 abbrev
= MINIMUM_ABBREV
;
494 else if (40 <= abbrev
)
498 if (!strcmp(arg
, "--sq")) {
502 if (!strcmp(arg
, "--not")) {
503 show_type
^= REVERSED
;
506 if (!strcmp(arg
, "--symbolic")) {
507 symbolic
= SHOW_SYMBOLIC_ASIS
;
510 if (!strcmp(arg
, "--symbolic-full-name")) {
511 symbolic
= SHOW_SYMBOLIC_FULL
;
514 if (!prefixcmp(arg
, "--abbrev-ref") &&
515 (!arg
[12] || arg
[12] == '=')) {
517 abbrev_ref_strict
= warn_ambiguous_refs
;
518 if (arg
[12] == '=') {
519 if (!strcmp(arg
+ 13, "strict"))
520 abbrev_ref_strict
= 1;
521 else if (!strcmp(arg
+ 13, "loose"))
522 abbrev_ref_strict
= 0;
524 die("unknown mode for %s", arg
);
528 if (!strcmp(arg
, "--all")) {
529 for_each_ref(show_reference
, NULL
);
532 if (!strcmp(arg
, "--branches")) {
533 for_each_branch_ref(show_reference
, NULL
);
536 if (!strcmp(arg
, "--tags")) {
537 for_each_tag_ref(show_reference
, NULL
);
540 if (!strcmp(arg
, "--remotes")) {
541 for_each_remote_ref(show_reference
, NULL
);
544 if (!strcmp(arg
, "--show-prefix")) {
549 if (!strcmp(arg
, "--show-cdup")) {
550 const char *pfx
= prefix
;
551 if (!is_inside_work_tree()) {
552 const char *work_tree
=
555 printf("%s\n", work_tree
);
559 pfx
= strchr(pfx
, '/');
568 if (!strcmp(arg
, "--git-dir")) {
569 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
570 static char cwd
[PATH_MAX
];
579 if (!getcwd(cwd
, PATH_MAX
))
580 die("unable to get current working directory");
581 printf("%s/.git\n", cwd
);
584 if (!strcmp(arg
, "--is-inside-git-dir")) {
585 printf("%s\n", is_inside_git_dir() ? "true"
589 if (!strcmp(arg
, "--is-inside-work-tree")) {
590 printf("%s\n", is_inside_work_tree() ? "true"
594 if (!strcmp(arg
, "--is-bare-repository")) {
595 printf("%s\n", is_bare_repository() ? "true"
599 if (!prefixcmp(arg
, "--since=")) {
600 show_datestring("--max-age=", arg
+8);
603 if (!prefixcmp(arg
, "--after=")) {
604 show_datestring("--max-age=", arg
+8);
607 if (!prefixcmp(arg
, "--before=")) {
608 show_datestring("--min-age=", arg
+9);
611 if (!prefixcmp(arg
, "--until=")) {
612 show_datestring("--min-age=", arg
+8);
615 if (show_flag(arg
) && verify
)
616 die_no_single_rev(quiet
);
620 /* Not a flag argument */
621 if (try_difference(arg
))
623 if (try_parent_shorthands(arg
))
631 if (!get_sha1(name
, sha1
)) {
635 show_rev(type
, sha1
, name
);
639 die_no_single_rev(quiet
);
643 verify_filename(prefix
, arg
);
646 if (revs_count
== 1) {
647 show_rev(type
, sha1
, name
);
649 } else if (revs_count
== 0 && show_default())
651 die_no_single_rev(quiet
);