]>
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" |
9dc01bf0 JH |
12 | #include "diff.h" |
13 | #include "revision.h" | |
a76295da | 14 | #include "split-index.h" |
bf0231c6 | 15 | #include "submodule.h" |
a8be83fe | 16 | |
4866ccf0 JH |
17 | #define DO_REVS 1 |
18 | #define DO_NOREV 2 | |
19 | #define DO_FLAGS 4 | |
20 | #define DO_NONFLAGS 8 | |
21 | static int filter = ~0; | |
22 | ||
96f1e58f | 23 | static const char *def; |
023d66ed | 24 | |
042a4ed7 LT |
25 | #define NORMAL 0 |
26 | #define REVERSED 1 | |
27 | static int show_type = NORMAL; | |
a6d97d49 JH |
28 | |
29 | #define SHOW_SYMBOLIC_ASIS 1 | |
30 | #define SHOW_SYMBOLIC_FULL 2 | |
96f1e58f DR |
31 | static int symbolic; |
32 | static int abbrev; | |
a45d3469 BW |
33 | static int abbrev_ref; |
34 | static int abbrev_ref_strict; | |
96f1e58f | 35 | static int output_sq; |
4866ccf0 | 36 | |
f8c87212 | 37 | static int stuck_long; |
9dc01bf0 | 38 | static struct string_list *ref_excludes; |
f8c87212 | 39 | |
921d865e LT |
40 | /* |
41 | * Some arguments are relevant "revision" arguments, | |
42 | * others are about output format or other details. | |
43 | * This sorts it all out. | |
44 | */ | |
45 | static int is_rev_argument(const char *arg) | |
46 | { | |
47 | static const char *rev_args[] = { | |
e091eb93 | 48 | "--all", |
4866ccf0 | 49 | "--bisect", |
5a83f3be | 50 | "--dense", |
b09fe971 | 51 | "--branches=", |
a62be77f | 52 | "--branches", |
4866ccf0 | 53 | "--header", |
cc243c3c | 54 | "--ignore-missing", |
921d865e | 55 | "--max-age=", |
4866ccf0 | 56 | "--max-count=", |
4866ccf0 | 57 | "--min-age=", |
5ccfb758 | 58 | "--no-merges", |
ad5aeede MG |
59 | "--min-parents=", |
60 | "--no-min-parents", | |
61 | "--max-parents=", | |
62 | "--no-max-parents", | |
4866ccf0 | 63 | "--objects", |
c6496575 | 64 | "--objects-edge", |
4866ccf0 JH |
65 | "--parents", |
66 | "--pretty", | |
b09fe971 | 67 | "--remotes=", |
a62be77f | 68 | "--remotes", |
d08bae7e | 69 | "--glob=", |
5a83f3be | 70 | "--sparse", |
b09fe971 | 71 | "--tags=", |
a62be77f | 72 | "--tags", |
4866ccf0 | 73 | "--topo-order", |
4c8725f1 | 74 | "--date-order", |
4866ccf0 | 75 | "--unpacked", |
921d865e LT |
76 | NULL |
77 | }; | |
78 | const char **p = rev_args; | |
79 | ||
8233340c EW |
80 | /* accept -<digit>, like traditional "head" */ |
81 | if ((*arg == '-') && isdigit(arg[1])) | |
82 | return 1; | |
83 | ||
921d865e LT |
84 | for (;;) { |
85 | const char *str = *p++; | |
86 | int len; | |
87 | if (!str) | |
88 | return 0; | |
89 | len = strlen(str); | |
4866ccf0 JH |
90 | if (!strcmp(arg, str) || |
91 | (str[len-1] == '=' && !strncmp(arg, str, len))) | |
921d865e LT |
92 | return 1; |
93 | } | |
94 | } | |
95 | ||
4866ccf0 | 96 | /* Output argument as a string, either SQ or normal */ |
5bb2c65a JH |
97 | static void show(const char *arg) |
98 | { | |
99 | if (output_sq) { | |
100 | int sq = '\'', ch; | |
101 | ||
102 | putchar(sq); | |
103 | while ((ch = *arg++)) { | |
104 | if (ch == sq) | |
105 | fputs("'\\'", stdout); | |
106 | putchar(ch); | |
107 | } | |
108 | putchar(sq); | |
109 | putchar(' '); | |
110 | } | |
111 | else | |
112 | puts(arg); | |
113 | } | |
114 | ||
e00f3790 JS |
115 | /* Like show(), but with a negation prefix according to type */ |
116 | static void show_with_type(int type, const char *arg) | |
117 | { | |
118 | if (type != show_type) | |
119 | putchar('^'); | |
120 | show(arg); | |
121 | } | |
122 | ||
4866ccf0 | 123 | /* Output a revision, only if filter allows it */ |
30b96fce | 124 | static void show_rev(int type, const unsigned char *sha1, const char *name) |
023d66ed | 125 | { |
4866ccf0 | 126 | if (!(filter & DO_REVS)) |
023d66ed | 127 | return; |
4866ccf0 | 128 | def = NULL; |
5bb2c65a | 129 | |
a45d3469 BW |
130 | if ((symbolic || abbrev_ref) && name) { |
131 | if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) { | |
a6d97d49 JH |
132 | unsigned char discard[20]; |
133 | char *full; | |
134 | ||
135 | switch (dwim_ref(name, strlen(name), discard, &full)) { | |
136 | case 0: | |
137 | /* | |
138 | * Not found -- not a ref. We could | |
139 | * emit "name" here, but symbolic-full | |
140 | * users are interested in finding the | |
141 | * refs spelled in full, and they would | |
142 | * need to filter non-refs if we did so. | |
143 | */ | |
144 | break; | |
145 | case 1: /* happy */ | |
a45d3469 BW |
146 | if (abbrev_ref) |
147 | full = shorten_unambiguous_ref(full, | |
148 | abbrev_ref_strict); | |
e00f3790 | 149 | show_with_type(type, full); |
a6d97d49 JH |
150 | break; |
151 | default: /* ambiguous */ | |
152 | error("refname '%s' is ambiguous", name); | |
153 | break; | |
154 | } | |
28b35632 | 155 | free(full); |
a6d97d49 | 156 | } else { |
e00f3790 | 157 | show_with_type(type, name); |
a6d97d49 JH |
158 | } |
159 | } | |
d5012508 | 160 | else if (abbrev) |
e00f3790 | 161 | show_with_type(type, find_unique_abbrev(sha1, abbrev)); |
30b96fce | 162 | else |
e00f3790 | 163 | show_with_type(type, sha1_to_hex(sha1)); |
023d66ed LT |
164 | } |
165 | ||
4866ccf0 | 166 | /* Output a flag, only if filter allows it. */ |
16cee38a | 167 | static int show_flag(const char *arg) |
023d66ed | 168 | { |
4866ccf0 | 169 | if (!(filter & DO_FLAGS)) |
9523a4c2 LT |
170 | return 0; |
171 | if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) { | |
0360e99d | 172 | show(arg); |
9523a4c2 LT |
173 | return 1; |
174 | } | |
175 | return 0; | |
023d66ed LT |
176 | } |
177 | ||
dfd1b749 | 178 | static int show_default(void) |
023d66ed | 179 | { |
16cee38a | 180 | const char *s = def; |
023d66ed LT |
181 | |
182 | if (s) { | |
183 | unsigned char sha1[20]; | |
184 | ||
185 | def = NULL; | |
9938af6a | 186 | if (!get_sha1(s, sha1)) { |
30b96fce | 187 | show_rev(NORMAL, sha1, s); |
dfd1b749 | 188 | return 1; |
023d66ed | 189 | } |
023d66ed | 190 | } |
dfd1b749 | 191 | return 0; |
023d66ed LT |
192 | } |
193 | ||
e23b0368 | 194 | static int show_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data) |
960bba0d | 195 | { |
9dc01bf0 JH |
196 | if (ref_excluded(ref_excludes, refname)) |
197 | return 0; | |
e23b0368 | 198 | show_rev(NORMAL, oid->hash, refname); |
960bba0d LT |
199 | return 0; |
200 | } | |
201 | ||
e23b0368 | 202 | static int anti_reference(const char *refname, const struct object_id *oid, int flag, void *cb_data) |
ad3f9a71 | 203 | { |
e23b0368 | 204 | show_rev(REVERSED, oid->hash, refname); |
ad3f9a71 LT |
205 | return 0; |
206 | } | |
207 | ||
1b7ba794 | 208 | static int show_abbrev(const struct object_id *oid, void *cb_data) |
957d7406 | 209 | { |
1b7ba794 | 210 | show_rev(NORMAL, oid->hash, NULL); |
957d7406 JH |
211 | return 0; |
212 | } | |
213 | ||
c1babb1d LT |
214 | static void show_datestring(const char *flag, const char *datestr) |
215 | { | |
c1babb1d | 216 | static char buffer[100]; |
c1babb1d LT |
217 | |
218 | /* date handling requires both flags and revs */ | |
219 | if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS)) | |
220 | return; | |
3c07b1d1 | 221 | snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr)); |
c1babb1d LT |
222 | show(buffer); |
223 | } | |
224 | ||
12b9d327 | 225 | static int show_file(const char *arg, int output_prefix) |
7a3dd472 | 226 | { |
7b34c2fa | 227 | show_default(); |
9ad0a933 | 228 | if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) { |
12b9d327 JK |
229 | if (output_prefix) { |
230 | const char *prefix = startup_info->prefix; | |
231 | show(prefix_filename(prefix, | |
232 | prefix ? strlen(prefix) : 0, | |
233 | arg)); | |
234 | } else | |
235 | show(arg); | |
9ad0a933 LT |
236 | return 1; |
237 | } | |
238 | return 0; | |
7a3dd472 LT |
239 | } |
240 | ||
b7d936b2 | 241 | static int try_difference(const char *arg) |
3dd4e732 SB |
242 | { |
243 | char *dotdot; | |
244 | unsigned char sha1[20]; | |
245 | unsigned char end[20]; | |
246 | const char *next; | |
247 | const char *this; | |
248 | int symmetric; | |
003c84f6 | 249 | static const char head_by_default[] = "HEAD"; |
3dd4e732 SB |
250 | |
251 | if (!(dotdot = strstr(arg, ".."))) | |
252 | return 0; | |
253 | next = dotdot + 2; | |
254 | this = arg; | |
255 | symmetric = (*next == '.'); | |
256 | ||
257 | *dotdot = 0; | |
258 | next += symmetric; | |
259 | ||
260 | if (!*next) | |
003c84f6 | 261 | next = head_by_default; |
3dd4e732 | 262 | if (dotdot == arg) |
003c84f6 JH |
263 | this = head_by_default; |
264 | ||
265 | if (this == head_by_default && next == head_by_default && | |
266 | !symmetric) { | |
267 | /* | |
268 | * Just ".."? That is not a range but the | |
269 | * pathspec for the parent directory. | |
270 | */ | |
271 | *dotdot = '.'; | |
272 | return 0; | |
273 | } | |
274 | ||
c036c4c5 | 275 | if (!get_sha1_committish(this, sha1) && !get_sha1_committish(next, end)) { |
3dd4e732 SB |
276 | show_rev(NORMAL, end, next); |
277 | show_rev(symmetric ? NORMAL : REVERSED, sha1, this); | |
278 | if (symmetric) { | |
279 | struct commit_list *exclude; | |
280 | struct commit *a, *b; | |
281 | a = lookup_commit_reference(sha1); | |
282 | b = lookup_commit_reference(end); | |
2ce406cc | 283 | exclude = get_merge_bases(a, b); |
3dd4e732 | 284 | while (exclude) { |
e510ab89 | 285 | struct commit *commit = pop_commit(&exclude); |
ed1c9977 | 286 | show_rev(REVERSED, commit->object.oid.hash, NULL); |
3dd4e732 SB |
287 | } |
288 | } | |
62f162f8 | 289 | *dotdot = '.'; |
3dd4e732 SB |
290 | return 1; |
291 | } | |
292 | *dotdot = '.'; | |
293 | return 0; | |
294 | } | |
295 | ||
2122f8b9 BS |
296 | static int try_parent_shorthands(const char *arg) |
297 | { | |
298 | char *dotdot; | |
299 | unsigned char sha1[20]; | |
300 | struct commit *commit; | |
301 | struct commit_list *parents; | |
8779351d VN |
302 | int parent_number; |
303 | int include_rev = 0; | |
304 | int include_parents = 0; | |
305 | int exclude_parent = 0; | |
306 | ||
307 | if ((dotdot = strstr(arg, "^!"))) { | |
308 | include_rev = 1; | |
309 | if (dotdot[2]) | |
310 | return 0; | |
311 | } else if ((dotdot = strstr(arg, "^@"))) { | |
312 | include_parents = 1; | |
313 | if (dotdot[2]) | |
314 | return 0; | |
315 | } else if ((dotdot = strstr(arg, "^-"))) { | |
316 | include_rev = 1; | |
317 | exclude_parent = 1; | |
318 | ||
319 | if (dotdot[2]) { | |
320 | char *end; | |
321 | exclude_parent = strtoul(dotdot + 2, &end, 10); | |
322 | if (*end != '\0' || !exclude_parent) | |
323 | return 0; | |
324 | } | |
325 | } else | |
2122f8b9 BS |
326 | return 0; |
327 | ||
328 | *dotdot = 0; | |
62f162f8 JK |
329 | if (get_sha1_committish(arg, sha1)) { |
330 | *dotdot = '^'; | |
2122f8b9 | 331 | return 0; |
62f162f8 | 332 | } |
2122f8b9 | 333 | |
2122f8b9 | 334 | commit = lookup_commit_reference(sha1); |
8779351d VN |
335 | if (exclude_parent && |
336 | exclude_parent > commit_list_count(commit->parents)) { | |
337 | *dotdot = '^'; | |
338 | return 0; | |
339 | } | |
340 | ||
341 | if (include_rev) | |
342 | show_rev(NORMAL, sha1, arg); | |
343 | for (parents = commit->parents, parent_number = 1; | |
344 | parents; | |
345 | parents = parents->next, parent_number++) { | |
a2e7b04c JK |
346 | char *name = NULL; |
347 | ||
8779351d VN |
348 | if (exclude_parent && parent_number != exclude_parent) |
349 | continue; | |
350 | ||
a2e7b04c JK |
351 | if (symbolic) |
352 | name = xstrfmt("%s^%d", arg, parent_number); | |
8779351d | 353 | show_rev(include_parents ? NORMAL : REVERSED, |
a2e7b04c JK |
354 | parents->item->object.oid.hash, name); |
355 | free(name); | |
8779351d | 356 | } |
2122f8b9 | 357 | |
62f162f8 | 358 | *dotdot = '^'; |
2122f8b9 BS |
359 | return 1; |
360 | } | |
361 | ||
21d47835 PH |
362 | static int parseopt_dump(const struct option *o, const char *arg, int unset) |
363 | { | |
364 | struct strbuf *parsed = o->value; | |
365 | if (unset) | |
366 | strbuf_addf(parsed, " --no-%s", o->long_name); | |
f8c87212 | 367 | else if (o->short_name && (o->long_name == NULL || !stuck_long)) |
21d47835 PH |
368 | strbuf_addf(parsed, " -%c", o->short_name); |
369 | else | |
370 | strbuf_addf(parsed, " --%s", o->long_name); | |
371 | if (arg) { | |
f8c87212 NV |
372 | if (!stuck_long) |
373 | strbuf_addch(parsed, ' '); | |
374 | else if (o->long_name) | |
375 | strbuf_addch(parsed, '='); | |
21d47835 PH |
376 | sq_quote_buf(parsed, arg); |
377 | } | |
378 | return 0; | |
379 | } | |
380 | ||
381 | static const char *skipspaces(const char *s) | |
382 | { | |
383 | while (isspace(*s)) | |
384 | s++; | |
385 | return s; | |
386 | } | |
387 | ||
388 | static int cmd_parseopt(int argc, const char **argv, const char *prefix) | |
389 | { | |
6e0800ef | 390 | static int keep_dashdash = 0, stop_at_non_option = 0; |
21d47835 | 391 | static char const * const parseopt_usage[] = { |
9c9b4f2f | 392 | N_("git rev-parse --parseopt [<options>] -- [<args>...]"), |
21d47835 PH |
393 | NULL |
394 | }; | |
395 | static struct option parseopt_opts[] = { | |
d5d09d47 | 396 | OPT_BOOL(0, "keep-dashdash", &keep_dashdash, |
2c7c184c | 397 | N_("keep the `--` passed as an arg")), |
d5d09d47 | 398 | OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option, |
2c7c184c NTND |
399 | N_("stop parsing after the " |
400 | "first non-option argument")), | |
f8c87212 NV |
401 | OPT_BOOL(0, "stuck-long", &stuck_long, |
402 | N_("output in stuck long form")), | |
21d47835 PH |
403 | OPT_END(), |
404 | }; | |
2d893dff | 405 | static const char * const flag_chars = "*=?!"; |
21d47835 | 406 | |
f285a2d7 | 407 | struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT; |
21d47835 PH |
408 | const char **usage = NULL; |
409 | struct option *opts = NULL; | |
410 | int onb = 0, osz = 0, unb = 0, usz = 0; | |
411 | ||
21d47835 | 412 | strbuf_addstr(&parsed, "set --"); |
37782920 | 413 | argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage, |
21d47835 PH |
414 | PARSE_OPT_KEEP_DASHDASH); |
415 | if (argc < 1 || strcmp(argv[0], "--")) | |
416 | usage_with_options(parseopt_usage, parseopt_opts); | |
417 | ||
21d47835 PH |
418 | /* get the usage up to the first line with a -- on it */ |
419 | for (;;) { | |
72e37b6a | 420 | if (strbuf_getline(&sb, stdin) == EOF) |
21d47835 PH |
421 | die("premature end of input"); |
422 | ALLOC_GROW(usage, unb + 1, usz); | |
423 | if (!strcmp("--", sb.buf)) { | |
424 | if (unb < 1) | |
425 | die("no usage string given before the `--' separator"); | |
426 | usage[unb] = NULL; | |
427 | break; | |
428 | } | |
429 | usage[unb++] = strbuf_detach(&sb, NULL); | |
430 | } | |
431 | ||
9bab5b60 | 432 | /* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */ |
72e37b6a | 433 | while (strbuf_getline(&sb, stdin) != EOF) { |
21d47835 | 434 | const char *s; |
2d893dff | 435 | const char *help; |
21d47835 PH |
436 | struct option *o; |
437 | ||
438 | if (!sb.len) | |
439 | continue; | |
440 | ||
441 | ALLOC_GROW(opts, onb + 1, osz); | |
442 | memset(opts + onb, 0, sizeof(opts[onb])); | |
443 | ||
444 | o = &opts[onb++]; | |
2d893dff IB |
445 | help = strchr(sb.buf, ' '); |
446 | if (!help || *sb.buf == ' ') { | |
21d47835 | 447 | o->type = OPTION_GROUP; |
e1033436 | 448 | o->help = xstrdup(skipspaces(sb.buf)); |
21d47835 PH |
449 | continue; |
450 | } | |
451 | ||
452 | o->type = OPTION_CALLBACK; | |
2d893dff | 453 | o->help = xstrdup(skipspaces(help)); |
21d47835 | 454 | o->value = &parsed; |
ff962a3f | 455 | o->flags = PARSE_OPT_NOARG; |
21d47835 | 456 | o->callback = &parseopt_dump; |
9bab5b60 | 457 | |
2d893dff IB |
458 | /* name(s) */ |
459 | s = strpbrk(sb.buf, flag_chars); | |
460 | if (s == NULL) | |
461 | s = help; | |
462 | ||
463 | if (s - sb.buf == 1) /* short option only */ | |
464 | o->short_name = *sb.buf; | |
465 | else if (sb.buf[1] != ',') /* long option only */ | |
466 | o->long_name = xmemdupz(sb.buf, s - sb.buf); | |
467 | else { | |
468 | o->short_name = *sb.buf; | |
469 | o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2); | |
470 | } | |
471 | ||
472 | /* flags */ | |
473 | while (s < help) { | |
474 | switch (*s++) { | |
ff962a3f PH |
475 | case '=': |
476 | o->flags &= ~PARSE_OPT_NOARG; | |
2d893dff | 477 | continue; |
ff962a3f PH |
478 | case '?': |
479 | o->flags &= ~PARSE_OPT_NOARG; | |
480 | o->flags |= PARSE_OPT_OPTARG; | |
2d893dff | 481 | continue; |
ff962a3f PH |
482 | case '!': |
483 | o->flags |= PARSE_OPT_NONEG; | |
2d893dff | 484 | continue; |
ff962a3f PH |
485 | case '*': |
486 | o->flags |= PARSE_OPT_HIDDEN; | |
2d893dff | 487 | continue; |
ff962a3f | 488 | } |
2d893dff IB |
489 | s--; |
490 | break; | |
21d47835 PH |
491 | } |
492 | ||
2d893dff IB |
493 | if (s < help) |
494 | o->argh = xmemdupz(s, help - s); | |
21d47835 PH |
495 | } |
496 | strbuf_release(&sb); | |
497 | ||
498 | /* put an OPT_END() */ | |
499 | ALLOC_GROW(opts, onb + 1, osz); | |
500 | memset(opts + onb, 0, sizeof(opts[onb])); | |
37782920 | 501 | argc = parse_options(argc, argv, prefix, opts, usage, |
29981380 | 502 | (keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) | |
fcd91f8d | 503 | (stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) | |
47e9cd28 | 504 | PARSE_OPT_SHELL_EVAL); |
21d47835 | 505 | |
02962d36 | 506 | strbuf_addstr(&parsed, " --"); |
b319ce4c | 507 | sq_quote_argv(&parsed, argv, 0); |
21d47835 PH |
508 | puts(parsed.buf); |
509 | return 0; | |
510 | } | |
511 | ||
50325377 CC |
512 | static int cmd_sq_quote(int argc, const char **argv) |
513 | { | |
514 | struct strbuf buf = STRBUF_INIT; | |
515 | ||
516 | if (argc) | |
517 | sq_quote_argv(&buf, argv, 0); | |
518 | printf("%s\n", buf.buf); | |
519 | strbuf_release(&buf); | |
520 | ||
521 | return 0; | |
522 | } | |
523 | ||
b1b35969 CC |
524 | static void die_no_single_rev(int quiet) |
525 | { | |
526 | if (quiet) | |
527 | exit(1); | |
528 | else | |
529 | die("Needed a single revision"); | |
530 | } | |
531 | ||
7006b5be | 532 | static const char builtin_rev_parse_usage[] = |
9c9b4f2f | 533 | N_("git rev-parse --parseopt [<options>] -- [<args>...]\n" |
2c7c184c | 534 | " or: git rev-parse --sq-quote [<arg>...]\n" |
9c9b4f2f | 535 | " or: git rev-parse [<options>] [<arg>...]\n" |
2c7c184c NTND |
536 | "\n" |
537 | "Run \"git rev-parse --parseopt -h\" for more information on the first usage."); | |
7006b5be | 538 | |
9d16ca65 JK |
539 | /* |
540 | * Parse "opt" or "opt=<value>", setting value respectively to either | |
541 | * NULL or the string after "=". | |
542 | */ | |
543 | static int opt_with_value(const char *arg, const char *opt, const char **value) | |
544 | { | |
545 | if (skip_prefix(arg, opt, &arg)) { | |
546 | if (!*arg) { | |
547 | *value = NULL; | |
548 | return 1; | |
549 | } | |
550 | if (*arg++ == '=') { | |
551 | *value = arg; | |
552 | return 1; | |
553 | } | |
554 | } | |
555 | return 0; | |
556 | } | |
557 | ||
ffddfc63 JK |
558 | static void handle_ref_opt(const char *pattern, const char *prefix) |
559 | { | |
560 | if (pattern) | |
561 | for_each_glob_ref_in(show_reference, pattern, prefix, NULL); | |
562 | else | |
563 | for_each_ref_in(prefix, show_reference, NULL); | |
564 | clear_ref_exclusion(&ref_excludes); | |
565 | } | |
566 | ||
a633fca0 | 567 | int cmd_rev_parse(int argc, const char **argv, const char *prefix) |
178cb243 | 568 | { |
dfd1b749 | 569 | int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0; |
fc7d47f0 | 570 | int did_repo_setup = 0; |
14185673 | 571 | int has_dashdash = 0; |
12b9d327 | 572 | int output_prefix = 0; |
178cb243 | 573 | unsigned char sha1[20]; |
c41a87dd | 574 | unsigned int flags = 0; |
dfd1b749 | 575 | const char *name = NULL; |
c41a87dd | 576 | struct object_context unused; |
098aa867 | 577 | struct strbuf buf = STRBUF_INIT; |
a62be77f | 578 | |
21d47835 PH |
579 | if (argc > 1 && !strcmp("--parseopt", argv[1])) |
580 | return cmd_parseopt(argc - 1, argv + 1, prefix); | |
581 | ||
50325377 CC |
582 | if (argc > 1 && !strcmp("--sq-quote", argv[1])) |
583 | return cmd_sq_quote(argc - 2, argv + 2); | |
584 | ||
7006b5be JN |
585 | if (argc > 1 && !strcmp("-h", argv[1])) |
586 | usage(builtin_rev_parse_usage); | |
587 | ||
14185673 JK |
588 | for (i = 1; i < argc; i++) { |
589 | if (!strcmp(argv[i], "--")) { | |
590 | has_dashdash = 1; | |
591 | break; | |
592 | } | |
593 | } | |
594 | ||
fc7d47f0 JK |
595 | /* No options; just report on whether we're in a git repo or not. */ |
596 | if (argc == 1) { | |
597 | setup_git_directory(); | |
598 | git_config(git_default_config, NULL); | |
599 | return 0; | |
600 | } | |
601 | ||
178cb243 | 602 | for (i = 1; i < argc; i++) { |
16cee38a | 603 | const char *arg = argv[i]; |
fb18a2ed | 604 | |
fc7d47f0 JK |
605 | if (!strcmp(arg, "--local-env-vars")) { |
606 | int i; | |
607 | for (i = 0; local_repo_env[i]; i++) | |
608 | printf("%s\n", local_repo_env[i]); | |
609 | continue; | |
610 | } | |
611 | if (!strcmp(arg, "--resolve-git-dir")) { | |
612 | const char *gitdir = argv[++i]; | |
613 | if (!gitdir) | |
614 | die("--resolve-git-dir requires an argument"); | |
615 | gitdir = resolve_gitdir(gitdir); | |
616 | if (!gitdir) | |
617 | die("not a gitdir '%s'", argv[i]); | |
618 | puts(gitdir); | |
619 | continue; | |
620 | } | |
621 | ||
622 | /* The rest of the options require a git repository. */ | |
623 | if (!did_repo_setup) { | |
624 | prefix = setup_git_directory(); | |
625 | git_config(git_default_config, NULL); | |
626 | did_repo_setup = 1; | |
627 | } | |
628 | ||
557bd833 NTND |
629 | if (!strcmp(arg, "--git-path")) { |
630 | if (!argv[i + 1]) | |
631 | die("--git-path requires an argument"); | |
098aa867 JS |
632 | strbuf_reset(&buf); |
633 | puts(relative_path(git_path("%s", argv[i + 1]), | |
634 | prefix, &buf)); | |
557bd833 NTND |
635 | i++; |
636 | continue; | |
637 | } | |
178cb243 | 638 | if (as_is) { |
12b9d327 | 639 | if (show_file(arg, output_prefix) && as_is < 2) |
023e37c3 | 640 | verify_filename(prefix, arg, 0); |
178cb243 LT |
641 | continue; |
642 | } | |
3af06987 EW |
643 | if (!strcmp(arg,"-n")) { |
644 | if (++i >= argc) | |
645 | die("-n requires an argument"); | |
646 | if ((filter & DO_FLAGS) && (filter & DO_REVS)) { | |
647 | show(arg); | |
648 | show(argv[i]); | |
649 | } | |
650 | continue; | |
651 | } | |
59556548 | 652 | if (starts_with(arg, "-n")) { |
3af06987 EW |
653 | if ((filter & DO_FLAGS) && (filter & DO_REVS)) |
654 | show(arg); | |
655 | continue; | |
656 | } | |
657 | ||
178cb243 LT |
658 | if (*arg == '-') { |
659 | if (!strcmp(arg, "--")) { | |
fb18a2ed | 660 | as_is = 2; |
a08b6505 LT |
661 | /* Pass on the "--" if we show anything but files.. */ |
662 | if (filter & (DO_FLAGS | DO_REVS)) | |
12b9d327 | 663 | show_file(arg, 0); |
4866ccf0 | 664 | continue; |
178cb243 LT |
665 | } |
666 | if (!strcmp(arg, "--default")) { | |
a43219f2 DS |
667 | def = argv[++i]; |
668 | if (!def) | |
669 | die("--default requires an argument"); | |
178cb243 LT |
670 | continue; |
671 | } | |
12b9d327 | 672 | if (!strcmp(arg, "--prefix")) { |
a43219f2 DS |
673 | prefix = argv[++i]; |
674 | if (!prefix) | |
675 | die("--prefix requires an argument"); | |
12b9d327 JK |
676 | startup_info->prefix = prefix; |
677 | output_prefix = 1; | |
12b9d327 JK |
678 | continue; |
679 | } | |
8ebb0184 | 680 | if (!strcmp(arg, "--revs-only")) { |
4866ccf0 | 681 | filter &= ~DO_NOREV; |
8ebb0184 LT |
682 | continue; |
683 | } | |
684 | if (!strcmp(arg, "--no-revs")) { | |
4866ccf0 | 685 | filter &= ~DO_REVS; |
8ebb0184 LT |
686 | continue; |
687 | } | |
f79b65aa | 688 | if (!strcmp(arg, "--flags")) { |
4866ccf0 | 689 | filter &= ~DO_NONFLAGS; |
f79b65aa LT |
690 | continue; |
691 | } | |
692 | if (!strcmp(arg, "--no-flags")) { | |
4866ccf0 | 693 | filter &= ~DO_FLAGS; |
f79b65aa LT |
694 | continue; |
695 | } | |
023d66ed | 696 | if (!strcmp(arg, "--verify")) { |
4866ccf0 JH |
697 | filter &= ~(DO_FLAGS|DO_NOREV); |
698 | verify = 1; | |
023d66ed | 699 | continue; |
921d865e | 700 | } |
b1b35969 CC |
701 | if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) { |
702 | quiet = 1; | |
c41a87dd | 703 | flags |= GET_SHA1_QUIETLY; |
b1b35969 CC |
704 | continue; |
705 | } | |
9d16ca65 | 706 | if (opt_with_value(arg, "--short", &arg)) { |
d5012508 JH |
707 | filter &= ~(DO_FLAGS|DO_NOREV); |
708 | verify = 1; | |
709 | abbrev = DEFAULT_ABBREV; | |
9d16ca65 | 710 | if (!arg) |
7b5b7721 | 711 | continue; |
9d16ca65 | 712 | abbrev = strtoul(arg, NULL, 10); |
1dc4fb84 JH |
713 | if (abbrev < MINIMUM_ABBREV) |
714 | abbrev = MINIMUM_ABBREV; | |
715 | else if (40 <= abbrev) | |
716 | abbrev = 40; | |
d5012508 JH |
717 | continue; |
718 | } | |
5bb2c65a JH |
719 | if (!strcmp(arg, "--sq")) { |
720 | output_sq = 1; | |
721 | continue; | |
722 | } | |
042a4ed7 LT |
723 | if (!strcmp(arg, "--not")) { |
724 | show_type ^= REVERSED; | |
725 | continue; | |
726 | } | |
30b96fce | 727 | if (!strcmp(arg, "--symbolic")) { |
a6d97d49 JH |
728 | symbolic = SHOW_SYMBOLIC_ASIS; |
729 | continue; | |
730 | } | |
731 | if (!strcmp(arg, "--symbolic-full-name")) { | |
732 | symbolic = SHOW_SYMBOLIC_FULL; | |
30b96fce JH |
733 | continue; |
734 | } | |
9d16ca65 | 735 | if (opt_with_value(arg, "--abbrev-ref", &arg)) { |
a45d3469 BW |
736 | abbrev_ref = 1; |
737 | abbrev_ref_strict = warn_ambiguous_refs; | |
9d16ca65 JK |
738 | if (arg) { |
739 | if (!strcmp(arg, "strict")) | |
a45d3469 | 740 | abbrev_ref_strict = 1; |
9d16ca65 | 741 | else if (!strcmp(arg, "loose")) |
a45d3469 BW |
742 | abbrev_ref_strict = 0; |
743 | else | |
9d16ca65 JK |
744 | die("unknown mode for --abbrev-ref: %s", |
745 | arg); | |
a45d3469 BW |
746 | } |
747 | continue; | |
748 | } | |
960bba0d | 749 | if (!strcmp(arg, "--all")) { |
e23b0368 | 750 | for_each_ref(show_reference, NULL); |
960bba0d LT |
751 | continue; |
752 | } | |
ef87cc79 JK |
753 | if (skip_prefix(arg, "--disambiguate=", &arg)) { |
754 | for_each_abbrev(arg, show_abbrev, NULL); | |
957d7406 JH |
755 | continue; |
756 | } | |
ad3f9a71 | 757 | if (!strcmp(arg, "--bisect")) { |
e23b0368 MH |
758 | for_each_ref_in("refs/bisect/bad", show_reference, NULL); |
759 | for_each_ref_in("refs/bisect/good", anti_reference, NULL); | |
ad3f9a71 LT |
760 | continue; |
761 | } | |
ffddfc63 JK |
762 | if (opt_with_value(arg, "--branches", &arg)) { |
763 | handle_ref_opt(arg, "refs/heads/"); | |
a62be77f SE |
764 | continue; |
765 | } | |
ffddfc63 JK |
766 | if (opt_with_value(arg, "--tags", &arg)) { |
767 | handle_ref_opt(arg, "refs/tags/"); | |
d08bae7e IL |
768 | continue; |
769 | } | |
ef87cc79 | 770 | if (skip_prefix(arg, "--glob=", &arg)) { |
ffddfc63 | 771 | handle_ref_opt(arg, NULL); |
b09fe971 IL |
772 | continue; |
773 | } | |
ffddfc63 JK |
774 | if (opt_with_value(arg, "--remotes", &arg)) { |
775 | handle_ref_opt(arg, "refs/remotes/"); | |
9dc01bf0 JH |
776 | continue; |
777 | } | |
ef87cc79 JK |
778 | if (skip_prefix(arg, "--exclude=", &arg)) { |
779 | add_ref_exclusion(&ref_excludes, arg); | |
a62be77f SE |
780 | continue; |
781 | } | |
7cceca5c SD |
782 | if (!strcmp(arg, "--show-toplevel")) { |
783 | const char *work_tree = get_git_work_tree(); | |
784 | if (work_tree) | |
785 | puts(work_tree); | |
786 | continue; | |
787 | } | |
bf0231c6 SB |
788 | if (!strcmp(arg, "--show-superproject-working-tree")) { |
789 | const char *superproject = get_superproject_working_tree(); | |
790 | if (superproject) | |
791 | puts(superproject); | |
792 | continue; | |
793 | } | |
d288a700 | 794 | if (!strcmp(arg, "--show-prefix")) { |
4866ccf0 JH |
795 | if (prefix) |
796 | puts(prefix); | |
658219f1 RL |
797 | else |
798 | putchar('\n'); | |
d288a700 LT |
799 | continue; |
800 | } | |
5f94c730 JH |
801 | if (!strcmp(arg, "--show-cdup")) { |
802 | const char *pfx = prefix; | |
e90fdc39 JS |
803 | if (!is_inside_work_tree()) { |
804 | const char *work_tree = | |
805 | get_git_work_tree(); | |
806 | if (work_tree) | |
807 | printf("%s\n", work_tree); | |
808 | continue; | |
809 | } | |
5f94c730 JH |
810 | while (pfx) { |
811 | pfx = strchr(pfx, '/'); | |
812 | if (pfx) { | |
813 | pfx++; | |
814 | printf("../"); | |
815 | } | |
816 | } | |
817 | putchar('\n'); | |
818 | continue; | |
819 | } | |
a2f5a876 SG |
820 | if (!strcmp(arg, "--git-dir") || |
821 | !strcmp(arg, "--absolute-git-dir")) { | |
a8783eeb | 822 | const char *gitdir = getenv(GIT_DIR_ENVIRONMENT); |
56b9f6e7 | 823 | char *cwd; |
d06f15d9 | 824 | int len; |
a2f5a876 SG |
825 | if (arg[2] == 'g') { /* --git-dir */ |
826 | if (gitdir) { | |
827 | puts(gitdir); | |
828 | continue; | |
829 | } | |
830 | if (!prefix) { | |
831 | puts(".git"); | |
832 | continue; | |
833 | } | |
834 | } else { /* --absolute-git-dir */ | |
835 | if (!gitdir && !prefix) | |
836 | gitdir = ".git"; | |
837 | if (gitdir) { | |
838 | puts(real_path(gitdir)); | |
839 | continue; | |
840 | } | |
a8783eeb | 841 | } |
56b9f6e7 | 842 | cwd = xgetcwd(); |
d06f15d9 NTND |
843 | len = strlen(cwd); |
844 | printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : ""); | |
56b9f6e7 | 845 | free(cwd); |
a8783eeb LT |
846 | continue; |
847 | } | |
31e26ebc | 848 | if (!strcmp(arg, "--git-common-dir")) { |
098aa867 JS |
849 | strbuf_reset(&buf); |
850 | puts(relative_path(get_git_common_dir(), | |
851 | prefix, &buf)); | |
31e26ebc NTND |
852 | continue; |
853 | } | |
6d9ba67b JS |
854 | if (!strcmp(arg, "--is-inside-git-dir")) { |
855 | printf("%s\n", is_inside_git_dir() ? "true" | |
856 | : "false"); | |
857 | continue; | |
858 | } | |
892c41b9 ML |
859 | if (!strcmp(arg, "--is-inside-work-tree")) { |
860 | printf("%s\n", is_inside_work_tree() ? "true" | |
861 | : "false"); | |
862 | continue; | |
863 | } | |
493c774e ML |
864 | if (!strcmp(arg, "--is-bare-repository")) { |
865 | printf("%s\n", is_bare_repository() ? "true" | |
866 | : "false"); | |
867 | continue; | |
868 | } | |
a76295da NTND |
869 | if (!strcmp(arg, "--shared-index-path")) { |
870 | if (read_cache() < 0) | |
871 | die(_("Could not read the index")); | |
872 | if (the_index.split_index) { | |
873 | const unsigned char *sha1 = the_index.split_index->base_sha1; | |
098aa867 JS |
874 | const char *path = git_path("sharedindex.%s", sha1_to_hex(sha1)); |
875 | strbuf_reset(&buf); | |
876 | puts(relative_path(path, prefix, &buf)); | |
a76295da NTND |
877 | } |
878 | continue; | |
879 | } | |
ef87cc79 JK |
880 | if (skip_prefix(arg, "--since=", &arg)) { |
881 | show_datestring("--max-age=", arg); | |
c1babb1d LT |
882 | continue; |
883 | } | |
ef87cc79 JK |
884 | if (skip_prefix(arg, "--after=", &arg)) { |
885 | show_datestring("--max-age=", arg); | |
c1babb1d LT |
886 | continue; |
887 | } | |
ef87cc79 JK |
888 | if (skip_prefix(arg, "--before=", &arg)) { |
889 | show_datestring("--min-age=", arg); | |
c1babb1d LT |
890 | continue; |
891 | } | |
ef87cc79 JK |
892 | if (skip_prefix(arg, "--until=", &arg)) { |
893 | show_datestring("--min-age=", arg); | |
c1babb1d LT |
894 | continue; |
895 | } | |
9523a4c2 | 896 | if (show_flag(arg) && verify) |
b1b35969 | 897 | die_no_single_rev(quiet); |
178cb243 LT |
898 | continue; |
899 | } | |
4866ccf0 JH |
900 | |
901 | /* Not a flag argument */ | |
3dd4e732 SB |
902 | if (try_difference(arg)) |
903 | continue; | |
2122f8b9 BS |
904 | if (try_parent_shorthands(arg)) |
905 | continue; | |
dfd1b749 CC |
906 | name = arg; |
907 | type = NORMAL; | |
908 | if (*arg == '^') { | |
909 | name++; | |
910 | type = REVERSED; | |
800644c5 | 911 | } |
c41a87dd | 912 | if (!get_sha1_with_context(name, flags, sha1, &unused)) { |
dfd1b749 CC |
913 | if (verify) |
914 | revs_count++; | |
915 | else | |
916 | show_rev(type, sha1, name); | |
800644c5 LT |
917 | continue; |
918 | } | |
75ecfce3 CC |
919 | if (verify) |
920 | die_no_single_rev(quiet); | |
14185673 JK |
921 | if (has_dashdash) |
922 | die("bad revision '%s'", arg); | |
9ad0a933 | 923 | as_is = 1; |
12b9d327 | 924 | if (!show_file(arg, output_prefix)) |
9ad0a933 | 925 | continue; |
023e37c3 | 926 | verify_filename(prefix, arg, 1); |
023d66ed | 927 | } |
098aa867 | 928 | strbuf_release(&buf); |
dfd1b749 CC |
929 | if (verify) { |
930 | if (revs_count == 1) { | |
931 | show_rev(type, sha1, name); | |
932 | return 0; | |
933 | } else if (revs_count == 0 && show_default()) | |
934 | return 0; | |
b1b35969 | 935 | die_no_single_rev(quiet); |
dfd1b749 CC |
936 | } else |
937 | show_default(); | |
178cb243 LT |
938 | return 0; |
939 | } |