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