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