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