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