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