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