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 /* Output a revision, only if filter allows it */
98 static void show_rev(int type
, const unsigned char *sha1
, const char *name
)
100 if (!(filter
& DO_REVS
))
104 if (type
!= show_type
)
106 if (symbolic
&& name
) {
107 if (symbolic
== SHOW_SYMBOLIC_FULL
) {
108 unsigned char discard
[20];
111 switch (dwim_ref(name
, strlen(name
), discard
, &full
)) {
114 * Not found -- not a ref. We could
115 * emit "name" here, but symbolic-full
116 * users are interested in finding the
117 * refs spelled in full, and they would
118 * need to filter non-refs if we did so.
124 default: /* ambiguous */
125 error("refname '%s' is ambiguous", name
);
133 show(find_unique_abbrev(sha1
, abbrev
));
135 show(sha1_to_hex(sha1
));
138 /* Output a flag, only if filter allows it. */
139 static int show_flag(const char *arg
)
141 if (!(filter
& DO_FLAGS
))
143 if (filter
& (is_rev_argument(arg
) ? DO_REVS
: DO_NOREV
)) {
150 static int show_default(void)
155 unsigned char sha1
[20];
158 if (!get_sha1(s
, sha1
)) {
159 show_rev(NORMAL
, sha1
, s
);
166 static int show_reference(const char *refname
, const unsigned char *sha1
, int flag
, void *cb_data
)
168 show_rev(NORMAL
, sha1
, refname
);
172 static void show_datestring(const char *flag
, const char *datestr
)
174 static char buffer
[100];
176 /* date handling requires both flags and revs */
177 if ((filter
& (DO_FLAGS
| DO_REVS
)) != (DO_FLAGS
| DO_REVS
))
179 snprintf(buffer
, sizeof(buffer
), "%s%lu", flag
, approxidate(datestr
));
183 static int show_file(const char *arg
)
186 if ((filter
& (DO_NONFLAGS
|DO_NOREV
)) == (DO_NONFLAGS
|DO_NOREV
)) {
193 static int try_difference(const char *arg
)
196 unsigned char sha1
[20];
197 unsigned char end
[20];
202 if (!(dotdot
= strstr(arg
, "..")))
206 symmetric
= (*next
== '.');
215 if (!get_sha1(this, sha1
) && !get_sha1(next
, end
)) {
216 show_rev(NORMAL
, end
, next
);
217 show_rev(symmetric
? NORMAL
: REVERSED
, sha1
, this);
219 struct commit_list
*exclude
;
220 struct commit
*a
, *b
;
221 a
= lookup_commit_reference(sha1
);
222 b
= lookup_commit_reference(end
);
223 exclude
= get_merge_bases(a
, b
, 1);
225 struct commit_list
*n
= exclude
->next
;
227 exclude
->item
->object
.sha1
,NULL
);
238 static int parseopt_dump(const struct option
*o
, const char *arg
, int unset
)
240 struct strbuf
*parsed
= o
->value
;
242 strbuf_addf(parsed
, " --no-%s", o
->long_name
);
243 else if (o
->short_name
)
244 strbuf_addf(parsed
, " -%c", o
->short_name
);
246 strbuf_addf(parsed
, " --%s", o
->long_name
);
248 strbuf_addch(parsed
, ' ');
249 sq_quote_buf(parsed
, arg
);
254 static const char *skipspaces(const char *s
)
261 static int cmd_parseopt(int argc
, const char **argv
, const char *prefix
)
263 static int keep_dashdash
= 0;
264 static char const * const parseopt_usage
[] = {
265 "git-rev-parse --parseopt [options] -- [<args>...]",
268 static struct option parseopt_opts
[] = {
269 OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash
,
270 "keep the `--` passed as an arg"),
274 struct strbuf sb
, parsed
;
275 const char **usage
= NULL
;
276 struct option
*opts
= NULL
;
277 int onb
= 0, osz
= 0, unb
= 0, usz
= 0;
279 strbuf_init(&parsed
, 0);
280 strbuf_addstr(&parsed
, "set --");
281 argc
= parse_options(argc
, argv
, parseopt_opts
, parseopt_usage
,
282 PARSE_OPT_KEEP_DASHDASH
);
283 if (argc
< 1 || strcmp(argv
[0], "--"))
284 usage_with_options(parseopt_usage
, parseopt_opts
);
287 /* get the usage up to the first line with a -- on it */
289 if (strbuf_getline(&sb
, stdin
, '\n') == EOF
)
290 die("premature end of input");
291 ALLOC_GROW(usage
, unb
+ 1, usz
);
292 if (!strcmp("--", sb
.buf
)) {
294 die("no usage string given before the `--' separator");
298 usage
[unb
++] = strbuf_detach(&sb
, NULL
);
301 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
302 while (strbuf_getline(&sb
, stdin
, '\n') != EOF
) {
309 ALLOC_GROW(opts
, onb
+ 1, osz
);
310 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
313 s
= strchr(sb
.buf
, ' ');
314 if (!s
|| *sb
.buf
== ' ') {
315 o
->type
= OPTION_GROUP
;
316 o
->help
= xstrdup(skipspaces(sb
.buf
));
320 o
->type
= OPTION_CALLBACK
;
321 o
->help
= xstrdup(skipspaces(s
));
323 o
->flags
= PARSE_OPT_NOARG
;
324 o
->callback
= &parseopt_dump
;
325 while (s
> sb
.buf
&& strchr("*=?!", s
[-1])) {
328 o
->flags
&= ~PARSE_OPT_NOARG
;
331 o
->flags
&= ~PARSE_OPT_NOARG
;
332 o
->flags
|= PARSE_OPT_OPTARG
;
335 o
->flags
|= PARSE_OPT_NONEG
;
338 o
->flags
|= PARSE_OPT_HIDDEN
;
343 if (s
- sb
.buf
== 1) /* short option only */
344 o
->short_name
= *sb
.buf
;
345 else if (sb
.buf
[1] != ',') /* long option only */
346 o
->long_name
= xmemdupz(sb
.buf
, s
- sb
.buf
);
348 o
->short_name
= *sb
.buf
;
349 o
->long_name
= xmemdupz(sb
.buf
+ 2, s
- sb
.buf
- 2);
354 /* put an OPT_END() */
355 ALLOC_GROW(opts
, onb
+ 1, osz
);
356 memset(opts
+ onb
, 0, sizeof(opts
[onb
]));
357 argc
= parse_options(argc
, argv
, opts
, usage
,
358 keep_dashdash
? PARSE_OPT_KEEP_DASHDASH
: 0);
360 strbuf_addf(&parsed
, " --");
361 sq_quote_argv(&parsed
, argv
, 0);
366 static void die_no_single_rev(int quiet
)
371 die("Needed a single revision");
374 int cmd_rev_parse(int argc
, const char **argv
, const char *prefix
)
376 int i
, as_is
= 0, verify
= 0, quiet
= 0, revs_count
= 0, type
= 0;
377 unsigned char sha1
[20];
378 const char *name
= NULL
;
380 if (argc
> 1 && !strcmp("--parseopt", argv
[1]))
381 return cmd_parseopt(argc
- 1, argv
+ 1, prefix
);
383 prefix
= setup_git_directory();
384 git_config(git_default_config
);
385 for (i
= 1; i
< argc
; i
++) {
386 const char *arg
= argv
[i
];
389 if (show_file(arg
) && as_is
< 2)
390 verify_filename(prefix
, arg
);
393 if (!strcmp(arg
,"-n")) {
395 die("-n requires an argument");
396 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
)) {
402 if (!prefixcmp(arg
, "-n")) {
403 if ((filter
& DO_FLAGS
) && (filter
& DO_REVS
))
409 if (!strcmp(arg
, "--")) {
411 /* Pass on the "--" if we show anything but files.. */
412 if (filter
& (DO_FLAGS
| DO_REVS
))
416 if (!strcmp(arg
, "--default")) {
421 if (!strcmp(arg
, "--revs-only")) {
425 if (!strcmp(arg
, "--no-revs")) {
429 if (!strcmp(arg
, "--flags")) {
430 filter
&= ~DO_NONFLAGS
;
433 if (!strcmp(arg
, "--no-flags")) {
437 if (!strcmp(arg
, "--verify")) {
438 filter
&= ~(DO_FLAGS
|DO_NOREV
);
442 if (!strcmp(arg
, "--quiet") || !strcmp(arg
, "-q")) {
446 if (!strcmp(arg
, "--short") ||
447 !prefixcmp(arg
, "--short=")) {
448 filter
&= ~(DO_FLAGS
|DO_NOREV
);
450 abbrev
= DEFAULT_ABBREV
;
452 abbrev
= strtoul(arg
+ 8, NULL
, 10);
453 if (abbrev
< MINIMUM_ABBREV
)
454 abbrev
= MINIMUM_ABBREV
;
455 else if (40 <= abbrev
)
459 if (!strcmp(arg
, "--sq")) {
463 if (!strcmp(arg
, "--not")) {
464 show_type
^= REVERSED
;
467 if (!strcmp(arg
, "--symbolic")) {
468 symbolic
= SHOW_SYMBOLIC_ASIS
;
471 if (!strcmp(arg
, "--symbolic-full-name")) {
472 symbolic
= SHOW_SYMBOLIC_FULL
;
475 if (!strcmp(arg
, "--all")) {
476 for_each_ref(show_reference
, NULL
);
479 if (!strcmp(arg
, "--branches")) {
480 for_each_branch_ref(show_reference
, NULL
);
483 if (!strcmp(arg
, "--tags")) {
484 for_each_tag_ref(show_reference
, NULL
);
487 if (!strcmp(arg
, "--remotes")) {
488 for_each_remote_ref(show_reference
, NULL
);
491 if (!strcmp(arg
, "--show-prefix")) {
496 if (!strcmp(arg
, "--show-cdup")) {
497 const char *pfx
= prefix
;
498 if (!is_inside_work_tree()) {
499 const char *work_tree
=
502 printf("%s\n", work_tree
);
506 pfx
= strchr(pfx
, '/');
515 if (!strcmp(arg
, "--git-dir")) {
516 const char *gitdir
= getenv(GIT_DIR_ENVIRONMENT
);
517 static char cwd
[PATH_MAX
];
526 if (!getcwd(cwd
, PATH_MAX
))
527 die("unable to get current working directory");
528 printf("%s/.git\n", cwd
);
531 if (!strcmp(arg
, "--is-inside-git-dir")) {
532 printf("%s\n", is_inside_git_dir() ? "true"
536 if (!strcmp(arg
, "--is-inside-work-tree")) {
537 printf("%s\n", is_inside_work_tree() ? "true"
541 if (!strcmp(arg
, "--is-bare-repository")) {
542 printf("%s\n", is_bare_repository() ? "true"
546 if (!prefixcmp(arg
, "--since=")) {
547 show_datestring("--max-age=", arg
+8);
550 if (!prefixcmp(arg
, "--after=")) {
551 show_datestring("--max-age=", arg
+8);
554 if (!prefixcmp(arg
, "--before=")) {
555 show_datestring("--min-age=", arg
+9);
558 if (!prefixcmp(arg
, "--until=")) {
559 show_datestring("--min-age=", arg
+8);
562 if (show_flag(arg
) && verify
)
563 die_no_single_rev(quiet
);
567 /* Not a flag argument */
568 if (try_difference(arg
))
576 if (!get_sha1(name
, sha1
)) {
580 show_rev(type
, sha1
, name
);
584 die_no_single_rev(quiet
);
588 verify_filename(prefix
, arg
);
591 if (revs_count
== 1) {
592 show_rev(type
, sha1
, name
);
594 } else if (revs_count
== 0 && show_default())
596 die_no_single_rev(quiet
);