Documentation: fix invalid reference to 'mybranch' in user manual
[git/git-p4.git] / builtin-rev-parse.c
bloba7860ed75ac4a7b1895bfe088e34a39b24448f57
1 /*
2 * rev-parse.c
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "commit.h"
8 #include "refs.h"
9 #include "quote.h"
10 #include "builtin.h"
11 #include "parse-options.h"
13 #define DO_REVS 1
14 #define DO_NOREV 2
15 #define DO_FLAGS 4
16 #define DO_NONFLAGS 8
17 static int filter = ~0;
19 static const char *def;
21 #define NORMAL 0
22 #define REVERSED 1
23 static int show_type = NORMAL;
25 #define SHOW_SYMBOLIC_ASIS 1
26 #define SHOW_SYMBOLIC_FULL 2
27 static int symbolic;
28 static int abbrev;
29 static int output_sq;
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[] = {
39 "--all",
40 "--bisect",
41 "--dense",
42 "--branches",
43 "--header",
44 "--max-age=",
45 "--max-count=",
46 "--min-age=",
47 "--no-merges",
48 "--objects",
49 "--objects-edge",
50 "--parents",
51 "--pretty",
52 "--remotes",
53 "--sparse",
54 "--tags",
55 "--topo-order",
56 "--date-order",
57 "--unpacked",
58 NULL
60 const char **p = rev_args;
62 /* accept -<digit>, like traditional "head" */
63 if ((*arg == '-') && isdigit(arg[1]))
64 return 1;
66 for (;;) {
67 const char *str = *p++;
68 int len;
69 if (!str)
70 return 0;
71 len = strlen(str);
72 if (!strcmp(arg, str) ||
73 (str[len-1] == '=' && !strncmp(arg, str, len)))
74 return 1;
78 /* Output argument as a string, either SQ or normal */
79 static void show(const char *arg)
81 if (output_sq) {
82 int sq = '\'', ch;
84 putchar(sq);
85 while ((ch = *arg++)) {
86 if (ch == sq)
87 fputs("'\\'", stdout);
88 putchar(ch);
90 putchar(sq);
91 putchar(' ');
93 else
94 puts(arg);
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)
101 putchar('^');
102 show(arg);
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))
109 return;
110 def = NULL;
112 if (symbolic && name) {
113 if (symbolic == SHOW_SYMBOLIC_FULL) {
114 unsigned char discard[20];
115 char *full;
117 switch (dwim_ref(name, strlen(name), discard, &full)) {
118 case 0:
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.
126 break;
127 case 1: /* happy */
128 show_with_type(type, full);
129 break;
130 default: /* ambiguous */
131 error("refname '%s' is ambiguous", name);
132 break;
134 } else {
135 show_with_type(type, name);
138 else if (abbrev)
139 show_with_type(type, find_unique_abbrev(sha1, abbrev));
140 else
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))
148 return 0;
149 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
150 show(arg);
151 return 1;
153 return 0;
156 static int show_default(void)
158 const char *s = def;
160 if (s) {
161 unsigned char sha1[20];
163 def = NULL;
164 if (!get_sha1(s, sha1)) {
165 show_rev(NORMAL, sha1, s);
166 return 1;
169 return 0;
172 static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
174 show_rev(NORMAL, sha1, refname);
175 return 0;
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))
184 return;
185 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
186 show(buffer);
189 static int show_file(const char *arg)
191 show_default();
192 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
193 show(arg);
194 return 1;
196 return 0;
199 static int try_difference(const char *arg)
201 char *dotdot;
202 unsigned char sha1[20];
203 unsigned char end[20];
204 const char *next;
205 const char *this;
206 int symmetric;
208 if (!(dotdot = strstr(arg, "..")))
209 return 0;
210 next = dotdot + 2;
211 this = arg;
212 symmetric = (*next == '.');
214 *dotdot = 0;
215 next += symmetric;
217 if (!*next)
218 next = "HEAD";
219 if (dotdot == arg)
220 this = "HEAD";
221 if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
222 show_rev(NORMAL, end, next);
223 show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
224 if (symmetric) {
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);
230 while (exclude) {
231 struct commit_list *n = exclude->next;
232 show_rev(REVERSED,
233 exclude->item->object.sha1,NULL);
234 free(exclude);
235 exclude = n;
238 return 1;
240 *dotdot = '.';
241 return 0;
244 static int parseopt_dump(const struct option *o, const char *arg, int unset)
246 struct strbuf *parsed = o->value;
247 if (unset)
248 strbuf_addf(parsed, " --no-%s", o->long_name);
249 else if (o->short_name)
250 strbuf_addf(parsed, " -%c", o->short_name);
251 else
252 strbuf_addf(parsed, " --%s", o->long_name);
253 if (arg) {
254 strbuf_addch(parsed, ' ');
255 sq_quote_buf(parsed, arg);
257 return 0;
260 static const char *skipspaces(const char *s)
262 while (isspace(*s))
263 s++;
264 return s;
267 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
269 static int keep_dashdash = 0;
270 static char const * const parseopt_usage[] = {
271 "git-rev-parse --parseopt [options] -- [<args>...]",
272 NULL
274 static struct option parseopt_opts[] = {
275 OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash,
276 "keep the `--` passed as an arg"),
277 OPT_END(),
280 struct strbuf sb, parsed;
281 const char **usage = NULL;
282 struct option *opts = NULL;
283 int onb = 0, osz = 0, unb = 0, usz = 0;
285 strbuf_init(&parsed, 0);
286 strbuf_addstr(&parsed, "set --");
287 argc = parse_options(argc, argv, parseopt_opts, parseopt_usage,
288 PARSE_OPT_KEEP_DASHDASH);
289 if (argc < 1 || strcmp(argv[0], "--"))
290 usage_with_options(parseopt_usage, parseopt_opts);
292 strbuf_init(&sb, 0);
293 /* get the usage up to the first line with a -- on it */
294 for (;;) {
295 if (strbuf_getline(&sb, stdin, '\n') == EOF)
296 die("premature end of input");
297 ALLOC_GROW(usage, unb + 1, usz);
298 if (!strcmp("--", sb.buf)) {
299 if (unb < 1)
300 die("no usage string given before the `--' separator");
301 usage[unb] = NULL;
302 break;
304 usage[unb++] = strbuf_detach(&sb, NULL);
307 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
308 while (strbuf_getline(&sb, stdin, '\n') != EOF) {
309 const char *s;
310 struct option *o;
312 if (!sb.len)
313 continue;
315 ALLOC_GROW(opts, onb + 1, osz);
316 memset(opts + onb, 0, sizeof(opts[onb]));
318 o = &opts[onb++];
319 s = strchr(sb.buf, ' ');
320 if (!s || *sb.buf == ' ') {
321 o->type = OPTION_GROUP;
322 o->help = xstrdup(skipspaces(sb.buf));
323 continue;
326 o->type = OPTION_CALLBACK;
327 o->help = xstrdup(skipspaces(s));
328 o->value = &parsed;
329 o->flags = PARSE_OPT_NOARG;
330 o->callback = &parseopt_dump;
331 while (s > sb.buf && strchr("*=?!", s[-1])) {
332 switch (*--s) {
333 case '=':
334 o->flags &= ~PARSE_OPT_NOARG;
335 break;
336 case '?':
337 o->flags &= ~PARSE_OPT_NOARG;
338 o->flags |= PARSE_OPT_OPTARG;
339 break;
340 case '!':
341 o->flags |= PARSE_OPT_NONEG;
342 break;
343 case '*':
344 o->flags |= PARSE_OPT_HIDDEN;
345 break;
349 if (s - sb.buf == 1) /* short option only */
350 o->short_name = *sb.buf;
351 else if (sb.buf[1] != ',') /* long option only */
352 o->long_name = xmemdupz(sb.buf, s - sb.buf);
353 else {
354 o->short_name = *sb.buf;
355 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
358 strbuf_release(&sb);
360 /* put an OPT_END() */
361 ALLOC_GROW(opts, onb + 1, osz);
362 memset(opts + onb, 0, sizeof(opts[onb]));
363 argc = parse_options(argc, argv, opts, usage,
364 keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0);
366 strbuf_addf(&parsed, " --");
367 sq_quote_argv(&parsed, argv, 0);
368 puts(parsed.buf);
369 return 0;
372 static void die_no_single_rev(int quiet)
374 if (quiet)
375 exit(1);
376 else
377 die("Needed a single revision");
380 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
382 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
383 unsigned char sha1[20];
384 const char *name = NULL;
386 if (argc > 1 && !strcmp("--parseopt", argv[1]))
387 return cmd_parseopt(argc - 1, argv + 1, prefix);
389 prefix = setup_git_directory();
390 git_config(git_default_config, NULL);
391 for (i = 1; i < argc; i++) {
392 const char *arg = argv[i];
394 if (as_is) {
395 if (show_file(arg) && as_is < 2)
396 verify_filename(prefix, arg);
397 continue;
399 if (!strcmp(arg,"-n")) {
400 if (++i >= argc)
401 die("-n requires an argument");
402 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
403 show(arg);
404 show(argv[i]);
406 continue;
408 if (!prefixcmp(arg, "-n")) {
409 if ((filter & DO_FLAGS) && (filter & DO_REVS))
410 show(arg);
411 continue;
414 if (*arg == '-') {
415 if (!strcmp(arg, "--")) {
416 as_is = 2;
417 /* Pass on the "--" if we show anything but files.. */
418 if (filter & (DO_FLAGS | DO_REVS))
419 show_file(arg);
420 continue;
422 if (!strcmp(arg, "--default")) {
423 def = argv[i+1];
424 i++;
425 continue;
427 if (!strcmp(arg, "--revs-only")) {
428 filter &= ~DO_NOREV;
429 continue;
431 if (!strcmp(arg, "--no-revs")) {
432 filter &= ~DO_REVS;
433 continue;
435 if (!strcmp(arg, "--flags")) {
436 filter &= ~DO_NONFLAGS;
437 continue;
439 if (!strcmp(arg, "--no-flags")) {
440 filter &= ~DO_FLAGS;
441 continue;
443 if (!strcmp(arg, "--verify")) {
444 filter &= ~(DO_FLAGS|DO_NOREV);
445 verify = 1;
446 continue;
448 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
449 quiet = 1;
450 continue;
452 if (!strcmp(arg, "--short") ||
453 !prefixcmp(arg, "--short=")) {
454 filter &= ~(DO_FLAGS|DO_NOREV);
455 verify = 1;
456 abbrev = DEFAULT_ABBREV;
457 if (arg[7] == '=')
458 abbrev = strtoul(arg + 8, NULL, 10);
459 if (abbrev < MINIMUM_ABBREV)
460 abbrev = MINIMUM_ABBREV;
461 else if (40 <= abbrev)
462 abbrev = 40;
463 continue;
465 if (!strcmp(arg, "--sq")) {
466 output_sq = 1;
467 continue;
469 if (!strcmp(arg, "--not")) {
470 show_type ^= REVERSED;
471 continue;
473 if (!strcmp(arg, "--symbolic")) {
474 symbolic = SHOW_SYMBOLIC_ASIS;
475 continue;
477 if (!strcmp(arg, "--symbolic-full-name")) {
478 symbolic = SHOW_SYMBOLIC_FULL;
479 continue;
481 if (!strcmp(arg, "--all")) {
482 for_each_ref(show_reference, NULL);
483 continue;
485 if (!strcmp(arg, "--branches")) {
486 for_each_branch_ref(show_reference, NULL);
487 continue;
489 if (!strcmp(arg, "--tags")) {
490 for_each_tag_ref(show_reference, NULL);
491 continue;
493 if (!strcmp(arg, "--remotes")) {
494 for_each_remote_ref(show_reference, NULL);
495 continue;
497 if (!strcmp(arg, "--show-prefix")) {
498 if (prefix)
499 puts(prefix);
500 continue;
502 if (!strcmp(arg, "--show-cdup")) {
503 const char *pfx = prefix;
504 if (!is_inside_work_tree()) {
505 const char *work_tree =
506 get_git_work_tree();
507 if (work_tree)
508 printf("%s\n", work_tree);
509 continue;
511 while (pfx) {
512 pfx = strchr(pfx, '/');
513 if (pfx) {
514 pfx++;
515 printf("../");
518 putchar('\n');
519 continue;
521 if (!strcmp(arg, "--git-dir")) {
522 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
523 static char cwd[PATH_MAX];
524 if (gitdir) {
525 puts(gitdir);
526 continue;
528 if (!prefix) {
529 puts(".git");
530 continue;
532 if (!getcwd(cwd, PATH_MAX))
533 die("unable to get current working directory");
534 printf("%s/.git\n", cwd);
535 continue;
537 if (!strcmp(arg, "--is-inside-git-dir")) {
538 printf("%s\n", is_inside_git_dir() ? "true"
539 : "false");
540 continue;
542 if (!strcmp(arg, "--is-inside-work-tree")) {
543 printf("%s\n", is_inside_work_tree() ? "true"
544 : "false");
545 continue;
547 if (!strcmp(arg, "--is-bare-repository")) {
548 printf("%s\n", is_bare_repository() ? "true"
549 : "false");
550 continue;
552 if (!prefixcmp(arg, "--since=")) {
553 show_datestring("--max-age=", arg+8);
554 continue;
556 if (!prefixcmp(arg, "--after=")) {
557 show_datestring("--max-age=", arg+8);
558 continue;
560 if (!prefixcmp(arg, "--before=")) {
561 show_datestring("--min-age=", arg+9);
562 continue;
564 if (!prefixcmp(arg, "--until=")) {
565 show_datestring("--min-age=", arg+8);
566 continue;
568 if (show_flag(arg) && verify)
569 die_no_single_rev(quiet);
570 continue;
573 /* Not a flag argument */
574 if (try_difference(arg))
575 continue;
576 name = arg;
577 type = NORMAL;
578 if (*arg == '^') {
579 name++;
580 type = REVERSED;
582 if (!get_sha1(name, sha1)) {
583 if (verify)
584 revs_count++;
585 else
586 show_rev(type, sha1, name);
587 continue;
589 if (verify)
590 die_no_single_rev(quiet);
591 as_is = 1;
592 if (!show_file(arg))
593 continue;
594 verify_filename(prefix, arg);
596 if (verify) {
597 if (revs_count == 1) {
598 show_rev(type, sha1, name);
599 return 0;
600 } else if (revs_count == 0 && show_default())
601 return 0;
602 die_no_single_rev(quiet);
603 } else
604 show_default();
605 return 0;