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