]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-rev-parse.c
Update draft release notes for 1.6.0.2
[thirdparty/git.git] / builtin-rev-parse.c
CommitLineData
178cb243
LT
1/*
2 * rev-parse.c
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
a8be83fe 7#include "commit.h"
960bba0d 8#include "refs.h"
c1babb1d 9#include "quote.h"
895f10c3 10#include "builtin.h"
21d47835 11#include "parse-options.h"
a8be83fe 12
4866ccf0
JH
13#define DO_REVS 1
14#define DO_NOREV 2
15#define DO_FLAGS 4
16#define DO_NONFLAGS 8
17static int filter = ~0;
18
96f1e58f 19static const char *def;
023d66ed 20
042a4ed7
LT
21#define NORMAL 0
22#define REVERSED 1
23static int show_type = NORMAL;
a6d97d49
JH
24
25#define SHOW_SYMBOLIC_ASIS 1
26#define SHOW_SYMBOLIC_FULL 2
96f1e58f
DR
27static int symbolic;
28static int abbrev;
29static int output_sq;
4866ccf0 30
921d865e
LT
31/*
32 * Some arguments are relevant "revision" arguments,
33 * others are about output format or other details.
34 * This sorts it all out.
35 */
36static int is_rev_argument(const char *arg)
37{
38 static const char *rev_args[] = {
e091eb93 39 "--all",
4866ccf0 40 "--bisect",
5a83f3be 41 "--dense",
a62be77f 42 "--branches",
4866ccf0 43 "--header",
921d865e 44 "--max-age=",
4866ccf0 45 "--max-count=",
4866ccf0 46 "--min-age=",
5ccfb758 47 "--no-merges",
4866ccf0 48 "--objects",
c6496575 49 "--objects-edge",
4866ccf0
JH
50 "--parents",
51 "--pretty",
a62be77f 52 "--remotes",
5a83f3be 53 "--sparse",
a62be77f 54 "--tags",
4866ccf0 55 "--topo-order",
4c8725f1 56 "--date-order",
4866ccf0 57 "--unpacked",
921d865e
LT
58 NULL
59 };
60 const char **p = rev_args;
61
8233340c
EW
62 /* accept -<digit>, like traditional "head" */
63 if ((*arg == '-') && isdigit(arg[1]))
64 return 1;
65
921d865e
LT
66 for (;;) {
67 const char *str = *p++;
68 int len;
69 if (!str)
70 return 0;
71 len = strlen(str);
4866ccf0
JH
72 if (!strcmp(arg, str) ||
73 (str[len-1] == '=' && !strncmp(arg, str, len)))
921d865e
LT
74 return 1;
75 }
76}
77
4866ccf0 78/* Output argument as a string, either SQ or normal */
5bb2c65a
JH
79static void show(const char *arg)
80{
81 if (output_sq) {
82 int sq = '\'', ch;
83
84 putchar(sq);
85 while ((ch = *arg++)) {
86 if (ch == sq)
87 fputs("'\\'", stdout);
88 putchar(ch);
89 }
90 putchar(sq);
91 putchar(' ');
92 }
93 else
94 puts(arg);
95}
96
e00f3790
JS
97/* Like show(), but with a negation prefix according to type */
98static void show_with_type(int type, const char *arg)
99{
100 if (type != show_type)
101 putchar('^');
102 show(arg);
103}
104
4866ccf0 105/* Output a revision, only if filter allows it */
30b96fce 106static void show_rev(int type, const unsigned char *sha1, const char *name)
023d66ed 107{
4866ccf0 108 if (!(filter & DO_REVS))
023d66ed 109 return;
4866ccf0 110 def = NULL;
5bb2c65a 111
a6d97d49
JH
112 if (symbolic && name) {
113 if (symbolic == SHOW_SYMBOLIC_FULL) {
114 unsigned char discard[20];
115 char *full;
116
117 switch (dwim_ref(name, strlen(name), discard, &full)) {
118 case 0:
119 /*
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.
125 */
126 break;
127 case 1: /* happy */
e00f3790 128 show_with_type(type, full);
a6d97d49
JH
129 break;
130 default: /* ambiguous */
131 error("refname '%s' is ambiguous", name);
132 break;
133 }
134 } else {
e00f3790 135 show_with_type(type, name);
a6d97d49
JH
136 }
137 }
d5012508 138 else if (abbrev)
e00f3790 139 show_with_type(type, find_unique_abbrev(sha1, abbrev));
30b96fce 140 else
e00f3790 141 show_with_type(type, sha1_to_hex(sha1));
023d66ed
LT
142}
143
4866ccf0 144/* Output a flag, only if filter allows it. */
16cee38a 145static int show_flag(const char *arg)
023d66ed 146{
4866ccf0 147 if (!(filter & DO_FLAGS))
9523a4c2
LT
148 return 0;
149 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
0360e99d 150 show(arg);
9523a4c2
LT
151 return 1;
152 }
153 return 0;
023d66ed
LT
154}
155
dfd1b749 156static int show_default(void)
023d66ed 157{
16cee38a 158 const char *s = def;
023d66ed
LT
159
160 if (s) {
161 unsigned char sha1[20];
162
163 def = NULL;
9938af6a 164 if (!get_sha1(s, sha1)) {
30b96fce 165 show_rev(NORMAL, sha1, s);
dfd1b749 166 return 1;
023d66ed 167 }
023d66ed 168 }
dfd1b749 169 return 0;
023d66ed
LT
170}
171
8da19775 172static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
960bba0d 173{
30b96fce 174 show_rev(NORMAL, sha1, refname);
960bba0d
LT
175 return 0;
176}
177
c1babb1d
LT
178static void show_datestring(const char *flag, const char *datestr)
179{
c1babb1d 180 static char buffer[100];
c1babb1d
LT
181
182 /* date handling requires both flags and revs */
183 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
184 return;
3c07b1d1 185 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
c1babb1d
LT
186 show(buffer);
187}
188
9ad0a933 189static int show_file(const char *arg)
7a3dd472 190{
7b34c2fa 191 show_default();
9ad0a933 192 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
7a3dd472 193 show(arg);
9ad0a933
LT
194 return 1;
195 }
196 return 0;
7a3dd472
LT
197}
198
b7d936b2 199static int try_difference(const char *arg)
3dd4e732
SB
200{
201 char *dotdot;
202 unsigned char sha1[20];
203 unsigned char end[20];
204 const char *next;
205 const char *this;
206 int symmetric;
207
208 if (!(dotdot = strstr(arg, "..")))
209 return 0;
210 next = dotdot + 2;
211 this = arg;
212 symmetric = (*next == '.');
213
214 *dotdot = 0;
215 next += symmetric;
216
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;
236 }
237 }
238 return 1;
239 }
240 *dotdot = '.';
241 return 0;
242}
243
2122f8b9
BS
244static int try_parent_shorthands(const char *arg)
245{
246 char *dotdot;
247 unsigned char sha1[20];
248 struct commit *commit;
249 struct commit_list *parents;
250 int parents_only;
251
252 if ((dotdot = strstr(arg, "^!")))
253 parents_only = 0;
254 else if ((dotdot = strstr(arg, "^@")))
255 parents_only = 1;
256
257 if (!dotdot || dotdot[2])
258 return 0;
259
260 *dotdot = 0;
261 if (get_sha1(arg, sha1))
262 return 0;
263
264 if (!parents_only)
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);
270
271 return 1;
272}
273
21d47835
PH
274static int parseopt_dump(const struct option *o, const char *arg, int unset)
275{
276 struct strbuf *parsed = o->value;
277 if (unset)
278 strbuf_addf(parsed, " --no-%s", o->long_name);
279 else if (o->short_name)
280 strbuf_addf(parsed, " -%c", o->short_name);
281 else
282 strbuf_addf(parsed, " --%s", o->long_name);
283 if (arg) {
284 strbuf_addch(parsed, ' ');
285 sq_quote_buf(parsed, arg);
286 }
287 return 0;
288}
289
290static const char *skipspaces(const char *s)
291{
292 while (isspace(*s))
293 s++;
294 return s;
295}
296
297static int cmd_parseopt(int argc, const char **argv, const char *prefix)
298{
299 static int keep_dashdash = 0;
300 static char const * const parseopt_usage[] = {
1b1dd23f 301 "git rev-parse --parseopt [options] -- [<args>...]",
21d47835
PH
302 NULL
303 };
304 static struct option parseopt_opts[] = {
305 OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash,
306 "keep the `--` passed as an arg"),
307 OPT_END(),
308 };
309
310 struct strbuf sb, parsed;
311 const char **usage = NULL;
312 struct option *opts = NULL;
313 int onb = 0, osz = 0, unb = 0, usz = 0;
314
315 strbuf_init(&parsed, 0);
316 strbuf_addstr(&parsed, "set --");
317 argc = parse_options(argc, argv, parseopt_opts, parseopt_usage,
318 PARSE_OPT_KEEP_DASHDASH);
319 if (argc < 1 || strcmp(argv[0], "--"))
320 usage_with_options(parseopt_usage, parseopt_opts);
321
322 strbuf_init(&sb, 0);
323 /* get the usage up to the first line with a -- on it */
324 for (;;) {
325 if (strbuf_getline(&sb, stdin, '\n') == EOF)
326 die("premature end of input");
327 ALLOC_GROW(usage, unb + 1, usz);
328 if (!strcmp("--", sb.buf)) {
329 if (unb < 1)
330 die("no usage string given before the `--' separator");
331 usage[unb] = NULL;
332 break;
333 }
334 usage[unb++] = strbuf_detach(&sb, NULL);
335 }
336
337 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
338 while (strbuf_getline(&sb, stdin, '\n') != EOF) {
339 const char *s;
340 struct option *o;
341
342 if (!sb.len)
343 continue;
344
345 ALLOC_GROW(opts, onb + 1, osz);
346 memset(opts + onb, 0, sizeof(opts[onb]));
347
348 o = &opts[onb++];
349 s = strchr(sb.buf, ' ');
350 if (!s || *sb.buf == ' ') {
351 o->type = OPTION_GROUP;
e1033436 352 o->help = xstrdup(skipspaces(sb.buf));
21d47835
PH
353 continue;
354 }
355
356 o->type = OPTION_CALLBACK;
357 o->help = xstrdup(skipspaces(s));
358 o->value = &parsed;
ff962a3f 359 o->flags = PARSE_OPT_NOARG;
21d47835 360 o->callback = &parseopt_dump;
ff962a3f
PH
361 while (s > sb.buf && strchr("*=?!", s[-1])) {
362 switch (*--s) {
363 case '=':
364 o->flags &= ~PARSE_OPT_NOARG;
365 break;
366 case '?':
367 o->flags &= ~PARSE_OPT_NOARG;
368 o->flags |= PARSE_OPT_OPTARG;
369 break;
370 case '!':
371 o->flags |= PARSE_OPT_NONEG;
372 break;
373 case '*':
374 o->flags |= PARSE_OPT_HIDDEN;
375 break;
376 }
21d47835
PH
377 }
378
379 if (s - sb.buf == 1) /* short option only */
380 o->short_name = *sb.buf;
381 else if (sb.buf[1] != ',') /* long option only */
382 o->long_name = xmemdupz(sb.buf, s - sb.buf);
383 else {
384 o->short_name = *sb.buf;
385 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
386 }
387 }
388 strbuf_release(&sb);
389
390 /* put an OPT_END() */
391 ALLOC_GROW(opts, onb + 1, osz);
392 memset(opts + onb, 0, sizeof(opts[onb]));
393 argc = parse_options(argc, argv, opts, usage,
394 keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0);
395
396 strbuf_addf(&parsed, " --");
b319ce4c 397 sq_quote_argv(&parsed, argv, 0);
21d47835
PH
398 puts(parsed.buf);
399 return 0;
400}
401
b1b35969
CC
402static void die_no_single_rev(int quiet)
403{
404 if (quiet)
405 exit(1);
406 else
407 die("Needed a single revision");
408}
409
a633fca0 410int cmd_rev_parse(int argc, const char **argv, const char *prefix)
178cb243 411{
dfd1b749 412 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
178cb243 413 unsigned char sha1[20];
dfd1b749 414 const char *name = NULL;
a62be77f 415
21d47835
PH
416 if (argc > 1 && !strcmp("--parseopt", argv[1]))
417 return cmd_parseopt(argc - 1, argv + 1, prefix);
418
5410a02a 419 prefix = setup_git_directory();
ef90d6d4 420 git_config(git_default_config, NULL);
178cb243 421 for (i = 1; i < argc; i++) {
16cee38a 422 const char *arg = argv[i];
fb18a2ed 423
178cb243 424 if (as_is) {
fb18a2ed 425 if (show_file(arg) && as_is < 2)
e23d0b4a 426 verify_filename(prefix, arg);
178cb243
LT
427 continue;
428 }
3af06987
EW
429 if (!strcmp(arg,"-n")) {
430 if (++i >= argc)
431 die("-n requires an argument");
432 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
433 show(arg);
434 show(argv[i]);
435 }
436 continue;
437 }
1968d77d 438 if (!prefixcmp(arg, "-n")) {
3af06987
EW
439 if ((filter & DO_FLAGS) && (filter & DO_REVS))
440 show(arg);
441 continue;
442 }
443
178cb243
LT
444 if (*arg == '-') {
445 if (!strcmp(arg, "--")) {
fb18a2ed 446 as_is = 2;
a08b6505
LT
447 /* Pass on the "--" if we show anything but files.. */
448 if (filter & (DO_FLAGS | DO_REVS))
449 show_file(arg);
4866ccf0 450 continue;
178cb243
LT
451 }
452 if (!strcmp(arg, "--default")) {
178cb243
LT
453 def = argv[i+1];
454 i++;
455 continue;
456 }
8ebb0184 457 if (!strcmp(arg, "--revs-only")) {
4866ccf0 458 filter &= ~DO_NOREV;
8ebb0184
LT
459 continue;
460 }
461 if (!strcmp(arg, "--no-revs")) {
4866ccf0 462 filter &= ~DO_REVS;
8ebb0184
LT
463 continue;
464 }
f79b65aa 465 if (!strcmp(arg, "--flags")) {
4866ccf0 466 filter &= ~DO_NONFLAGS;
f79b65aa
LT
467 continue;
468 }
469 if (!strcmp(arg, "--no-flags")) {
4866ccf0 470 filter &= ~DO_FLAGS;
f79b65aa
LT
471 continue;
472 }
023d66ed 473 if (!strcmp(arg, "--verify")) {
4866ccf0
JH
474 filter &= ~(DO_FLAGS|DO_NOREV);
475 verify = 1;
023d66ed 476 continue;
921d865e 477 }
b1b35969
CC
478 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
479 quiet = 1;
480 continue;
481 }
62a604ba 482 if (!strcmp(arg, "--short") ||
cc44c765 483 !prefixcmp(arg, "--short=")) {
d5012508
JH
484 filter &= ~(DO_FLAGS|DO_NOREV);
485 verify = 1;
486 abbrev = DEFAULT_ABBREV;
44de0da4
JF
487 if (arg[7] == '=')
488 abbrev = strtoul(arg + 8, NULL, 10);
1dc4fb84
JH
489 if (abbrev < MINIMUM_ABBREV)
490 abbrev = MINIMUM_ABBREV;
491 else if (40 <= abbrev)
492 abbrev = 40;
d5012508
JH
493 continue;
494 }
5bb2c65a
JH
495 if (!strcmp(arg, "--sq")) {
496 output_sq = 1;
497 continue;
498 }
042a4ed7
LT
499 if (!strcmp(arg, "--not")) {
500 show_type ^= REVERSED;
501 continue;
502 }
30b96fce 503 if (!strcmp(arg, "--symbolic")) {
a6d97d49
JH
504 symbolic = SHOW_SYMBOLIC_ASIS;
505 continue;
506 }
507 if (!strcmp(arg, "--symbolic-full-name")) {
508 symbolic = SHOW_SYMBOLIC_FULL;
30b96fce
JH
509 continue;
510 }
960bba0d 511 if (!strcmp(arg, "--all")) {
cb5d709f 512 for_each_ref(show_reference, NULL);
960bba0d
LT
513 continue;
514 }
a62be77f 515 if (!strcmp(arg, "--branches")) {
cb5d709f 516 for_each_branch_ref(show_reference, NULL);
a62be77f
SE
517 continue;
518 }
519 if (!strcmp(arg, "--tags")) {
cb5d709f 520 for_each_tag_ref(show_reference, NULL);
a62be77f
SE
521 continue;
522 }
523 if (!strcmp(arg, "--remotes")) {
cb5d709f 524 for_each_remote_ref(show_reference, NULL);
a62be77f
SE
525 continue;
526 }
d288a700 527 if (!strcmp(arg, "--show-prefix")) {
4866ccf0
JH
528 if (prefix)
529 puts(prefix);
d288a700
LT
530 continue;
531 }
5f94c730
JH
532 if (!strcmp(arg, "--show-cdup")) {
533 const char *pfx = prefix;
e90fdc39
JS
534 if (!is_inside_work_tree()) {
535 const char *work_tree =
536 get_git_work_tree();
537 if (work_tree)
538 printf("%s\n", work_tree);
539 continue;
540 }
5f94c730
JH
541 while (pfx) {
542 pfx = strchr(pfx, '/');
543 if (pfx) {
544 pfx++;
545 printf("../");
546 }
547 }
548 putchar('\n');
549 continue;
550 }
a8783eeb
LT
551 if (!strcmp(arg, "--git-dir")) {
552 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
553 static char cwd[PATH_MAX];
554 if (gitdir) {
555 puts(gitdir);
556 continue;
557 }
558 if (!prefix) {
559 puts(".git");
560 continue;
561 }
562 if (!getcwd(cwd, PATH_MAX))
563 die("unable to get current working directory");
564 printf("%s/.git\n", cwd);
565 continue;
566 }
6d9ba67b
JS
567 if (!strcmp(arg, "--is-inside-git-dir")) {
568 printf("%s\n", is_inside_git_dir() ? "true"
569 : "false");
570 continue;
571 }
892c41b9
ML
572 if (!strcmp(arg, "--is-inside-work-tree")) {
573 printf("%s\n", is_inside_work_tree() ? "true"
574 : "false");
575 continue;
576 }
493c774e
ML
577 if (!strcmp(arg, "--is-bare-repository")) {
578 printf("%s\n", is_bare_repository() ? "true"
579 : "false");
580 continue;
581 }
cc44c765 582 if (!prefixcmp(arg, "--since=")) {
c1babb1d
LT
583 show_datestring("--max-age=", arg+8);
584 continue;
585 }
cc44c765 586 if (!prefixcmp(arg, "--after=")) {
c1babb1d
LT
587 show_datestring("--max-age=", arg+8);
588 continue;
589 }
cc44c765 590 if (!prefixcmp(arg, "--before=")) {
c1babb1d
LT
591 show_datestring("--min-age=", arg+9);
592 continue;
593 }
cc44c765 594 if (!prefixcmp(arg, "--until=")) {
c1babb1d
LT
595 show_datestring("--min-age=", arg+8);
596 continue;
597 }
9523a4c2 598 if (show_flag(arg) && verify)
b1b35969 599 die_no_single_rev(quiet);
178cb243
LT
600 continue;
601 }
4866ccf0
JH
602
603 /* Not a flag argument */
3dd4e732
SB
604 if (try_difference(arg))
605 continue;
2122f8b9
BS
606 if (try_parent_shorthands(arg))
607 continue;
dfd1b749
CC
608 name = arg;
609 type = NORMAL;
610 if (*arg == '^') {
611 name++;
612 type = REVERSED;
800644c5 613 }
dfd1b749
CC
614 if (!get_sha1(name, sha1)) {
615 if (verify)
616 revs_count++;
617 else
618 show_rev(type, sha1, name);
800644c5
LT
619 continue;
620 }
75ecfce3
CC
621 if (verify)
622 die_no_single_rev(quiet);
9ad0a933
LT
623 as_is = 1;
624 if (!show_file(arg))
625 continue;
e23d0b4a 626 verify_filename(prefix, arg);
023d66ed 627 }
dfd1b749
CC
628 if (verify) {
629 if (revs_count == 1) {
630 show_rev(type, sha1, name);
631 return 0;
632 } else if (revs_count == 0 && show_default())
633 return 0;
b1b35969 634 die_no_single_rev(quiet);
dfd1b749
CC
635 } else
636 show_default();
178cb243
LT
637 return 0;
638}