]>
Commit | Line | Data |
---|---|---|
178cb243 LT |
1 | /* |
2 | * rev-parse.c | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
6 | #include "cache.h" | |
a8be83fe | 7 | #include "commit.h" |
960bba0d | 8 | #include "refs.h" |
c1babb1d | 9 | #include "quote.h" |
895f10c3 | 10 | #include "builtin.h" |
21d47835 | 11 | #include "parse-options.h" |
a8be83fe | 12 | |
4866ccf0 JH |
13 | #define DO_REVS 1 |
14 | #define DO_NOREV 2 | |
15 | #define DO_FLAGS 4 | |
16 | #define DO_NONFLAGS 8 | |
17 | static int filter = ~0; | |
18 | ||
96f1e58f | 19 | static const char *def; |
023d66ed | 20 | |
042a4ed7 LT |
21 | #define NORMAL 0 |
22 | #define REVERSED 1 | |
23 | static int show_type = NORMAL; | |
a6d97d49 JH |
24 | |
25 | #define SHOW_SYMBOLIC_ASIS 1 | |
26 | #define SHOW_SYMBOLIC_FULL 2 | |
96f1e58f DR |
27 | static int symbolic; |
28 | static int abbrev; | |
a45d3469 BW |
29 | static int abbrev_ref; |
30 | static int abbrev_ref_strict; | |
96f1e58f | 31 | static int output_sq; |
4866ccf0 | 32 | |
921d865e LT |
33 | /* |
34 | * Some arguments are relevant "revision" arguments, | |
35 | * others are about output format or other details. | |
36 | * This sorts it all out. | |
37 | */ | |
38 | static int is_rev_argument(const char *arg) | |
39 | { | |
40 | static const char *rev_args[] = { | |
e091eb93 | 41 | "--all", |
4866ccf0 | 42 | "--bisect", |
5a83f3be | 43 | "--dense", |
a62be77f | 44 | "--branches", |
4866ccf0 | 45 | "--header", |
921d865e | 46 | "--max-age=", |
4866ccf0 | 47 | "--max-count=", |
4866ccf0 | 48 | "--min-age=", |
5ccfb758 | 49 | "--no-merges", |
4866ccf0 | 50 | "--objects", |
c6496575 | 51 | "--objects-edge", |
4866ccf0 JH |
52 | "--parents", |
53 | "--pretty", | |
a62be77f | 54 | "--remotes", |
5a83f3be | 55 | "--sparse", |
a62be77f | 56 | "--tags", |
4866ccf0 | 57 | "--topo-order", |
4c8725f1 | 58 | "--date-order", |
4866ccf0 | 59 | "--unpacked", |
921d865e LT |
60 | NULL |
61 | }; | |
62 | const char **p = rev_args; | |
63 | ||
8233340c EW |
64 | /* accept -<digit>, like traditional "head" */ |
65 | if ((*arg == '-') && isdigit(arg[1])) | |
66 | return 1; | |
67 | ||
921d865e LT |
68 | for (;;) { |
69 | const char *str = *p++; | |
70 | int len; | |
71 | if (!str) | |
72 | return 0; | |
73 | len = strlen(str); | |
4866ccf0 JH |
74 | if (!strcmp(arg, str) || |
75 | (str[len-1] == '=' && !strncmp(arg, str, len))) | |
921d865e LT |
76 | return 1; |
77 | } | |
78 | } | |
79 | ||
4866ccf0 | 80 | /* Output argument as a string, either SQ or normal */ |
5bb2c65a JH |
81 | static void show(const char *arg) |
82 | { | |
83 | if (output_sq) { | |
84 | int sq = '\'', ch; | |
85 | ||
86 | putchar(sq); | |
87 | while ((ch = *arg++)) { | |
88 | if (ch == sq) | |
89 | fputs("'\\'", stdout); | |
90 | putchar(ch); | |
91 | } | |
92 | putchar(sq); | |
93 | putchar(' '); | |
94 | } | |
95 | else | |
96 | puts(arg); | |
97 | } | |
98 | ||
e00f3790 JS |
99 | /* Like show(), but with a negation prefix according to type */ |
100 | static void show_with_type(int type, const char *arg) | |
101 | { | |
102 | if (type != show_type) | |
103 | putchar('^'); | |
104 | show(arg); | |
105 | } | |
106 | ||
4866ccf0 | 107 | /* Output a revision, only if filter allows it */ |
30b96fce | 108 | static void show_rev(int type, const unsigned char *sha1, const char *name) |
023d66ed | 109 | { |
4866ccf0 | 110 | if (!(filter & DO_REVS)) |
023d66ed | 111 | return; |
4866ccf0 | 112 | def = NULL; |
5bb2c65a | 113 | |
a45d3469 BW |
114 | if ((symbolic || abbrev_ref) && name) { |
115 | if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) { | |
a6d97d49 JH |
116 | unsigned char discard[20]; |
117 | char *full; | |
118 | ||
119 | switch (dwim_ref(name, strlen(name), discard, &full)) { | |
120 | case 0: | |
121 | /* | |
122 | * Not found -- not a ref. We could | |
123 | * emit "name" here, but symbolic-full | |
124 | * users are interested in finding the | |
125 | * refs spelled in full, and they would | |
126 | * need to filter non-refs if we did so. | |
127 | */ | |
128 | break; | |
129 | case 1: /* happy */ | |
a45d3469 BW |
130 | if (abbrev_ref) |
131 | full = shorten_unambiguous_ref(full, | |
132 | abbrev_ref_strict); | |
e00f3790 | 133 | show_with_type(type, full); |
a6d97d49 JH |
134 | break; |
135 | default: /* ambiguous */ | |
136 | error("refname '%s' is ambiguous", name); | |
137 | break; | |
138 | } | |
139 | } else { | |
e00f3790 | 140 | show_with_type(type, name); |
a6d97d49 JH |
141 | } |
142 | } | |
d5012508 | 143 | else if (abbrev) |
e00f3790 | 144 | show_with_type(type, find_unique_abbrev(sha1, abbrev)); |
30b96fce | 145 | else |
e00f3790 | 146 | show_with_type(type, sha1_to_hex(sha1)); |
023d66ed LT |
147 | } |
148 | ||
4866ccf0 | 149 | /* Output a flag, only if filter allows it. */ |
16cee38a | 150 | static int show_flag(const char *arg) |
023d66ed | 151 | { |
4866ccf0 | 152 | if (!(filter & DO_FLAGS)) |
9523a4c2 LT |
153 | return 0; |
154 | if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) { | |
0360e99d | 155 | show(arg); |
9523a4c2 LT |
156 | return 1; |
157 | } | |
158 | return 0; | |
023d66ed LT |
159 | } |
160 | ||
dfd1b749 | 161 | static int show_default(void) |
023d66ed | 162 | { |
16cee38a | 163 | const char *s = def; |
023d66ed LT |
164 | |
165 | if (s) { | |
166 | unsigned char sha1[20]; | |
167 | ||
168 | def = NULL; | |
9938af6a | 169 | if (!get_sha1(s, sha1)) { |
30b96fce | 170 | show_rev(NORMAL, sha1, s); |
dfd1b749 | 171 | return 1; |
023d66ed | 172 | } |
023d66ed | 173 | } |
dfd1b749 | 174 | return 0; |
023d66ed LT |
175 | } |
176 | ||
8da19775 | 177 | static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
960bba0d | 178 | { |
30b96fce | 179 | show_rev(NORMAL, sha1, refname); |
960bba0d LT |
180 | return 0; |
181 | } | |
182 | ||
ad3f9a71 LT |
183 | static int anti_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data) |
184 | { | |
185 | show_rev(REVERSED, sha1, refname); | |
186 | return 0; | |
187 | } | |
188 | ||
c1babb1d LT |
189 | static void show_datestring(const char *flag, const char *datestr) |
190 | { | |
c1babb1d | 191 | static char buffer[100]; |
c1babb1d LT |
192 | |
193 | /* date handling requires both flags and revs */ | |
194 | if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS)) | |
195 | return; | |
3c07b1d1 | 196 | snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr)); |
c1babb1d LT |
197 | show(buffer); |
198 | } | |
199 | ||
9ad0a933 | 200 | static int show_file(const char *arg) |
7a3dd472 | 201 | { |
7b34c2fa | 202 | show_default(); |
9ad0a933 | 203 | if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) { |
7a3dd472 | 204 | show(arg); |
9ad0a933 LT |
205 | return 1; |
206 | } | |
207 | return 0; | |
7a3dd472 LT |
208 | } |
209 | ||
b7d936b2 | 210 | static int try_difference(const char *arg) |
3dd4e732 SB |
211 | { |
212 | char *dotdot; | |
213 | unsigned char sha1[20]; | |
214 | unsigned char end[20]; | |
215 | const char *next; | |
216 | const char *this; | |
217 | int symmetric; | |
218 | ||
219 | if (!(dotdot = strstr(arg, ".."))) | |
220 | return 0; | |
221 | next = dotdot + 2; | |
222 | this = arg; | |
223 | symmetric = (*next == '.'); | |
224 | ||
225 | *dotdot = 0; | |
226 | next += symmetric; | |
227 | ||
228 | if (!*next) | |
229 | next = "HEAD"; | |
230 | if (dotdot == arg) | |
231 | this = "HEAD"; | |
232 | if (!get_sha1(this, sha1) && !get_sha1(next, end)) { | |
233 | show_rev(NORMAL, end, next); | |
234 | show_rev(symmetric ? NORMAL : REVERSED, sha1, this); | |
235 | if (symmetric) { | |
236 | struct commit_list *exclude; | |
237 | struct commit *a, *b; | |
238 | a = lookup_commit_reference(sha1); | |
239 | b = lookup_commit_reference(end); | |
240 | exclude = get_merge_bases(a, b, 1); | |
241 | while (exclude) { | |
242 | struct commit_list *n = exclude->next; | |
243 | show_rev(REVERSED, | |
244 | exclude->item->object.sha1,NULL); | |
245 | free(exclude); | |
246 | exclude = n; | |
247 | } | |
248 | } | |
249 | return 1; | |
250 | } | |
251 | *dotdot = '.'; | |
252 | return 0; | |
253 | } | |
254 | ||
2122f8b9 BS |
255 | static int try_parent_shorthands(const char *arg) |
256 | { | |
257 | char *dotdot; | |
258 | unsigned char sha1[20]; | |
259 | struct commit *commit; | |
260 | struct commit_list *parents; | |
261 | int parents_only; | |
262 | ||
263 | if ((dotdot = strstr(arg, "^!"))) | |
264 | parents_only = 0; | |
265 | else if ((dotdot = strstr(arg, "^@"))) | |
266 | parents_only = 1; | |
267 | ||
268 | if (!dotdot || dotdot[2]) | |
269 | return 0; | |
270 | ||
271 | *dotdot = 0; | |
272 | if (get_sha1(arg, sha1)) | |
273 | return 0; | |
274 | ||
275 | if (!parents_only) | |
276 | show_rev(NORMAL, sha1, arg); | |
277 | commit = lookup_commit_reference(sha1); | |
278 | for (parents = commit->parents; parents; parents = parents->next) | |
279 | show_rev(parents_only ? NORMAL : REVERSED, | |
280 | parents->item->object.sha1, arg); | |
281 | ||
282 | return 1; | |
283 | } | |
284 | ||
21d47835 PH |
285 | static int parseopt_dump(const struct option *o, const char *arg, int unset) |
286 | { | |
287 | struct strbuf *parsed = o->value; | |
288 | if (unset) | |
289 | strbuf_addf(parsed, " --no-%s", o->long_name); | |
290 | else if (o->short_name) | |
291 | strbuf_addf(parsed, " -%c", o->short_name); | |
292 | else | |
293 | strbuf_addf(parsed, " --%s", o->long_name); | |
294 | if (arg) { | |
295 | strbuf_addch(parsed, ' '); | |
296 | sq_quote_buf(parsed, arg); | |
297 | } | |
298 | return 0; | |
299 | } | |
300 | ||
301 | static const char *skipspaces(const char *s) | |
302 | { | |
303 | while (isspace(*s)) | |
304 | s++; | |
305 | return s; | |
306 | } | |
307 | ||
308 | static int cmd_parseopt(int argc, const char **argv, const char *prefix) | |
309 | { | |
6e0800ef | 310 | static int keep_dashdash = 0, stop_at_non_option = 0; |
21d47835 | 311 | static char const * const parseopt_usage[] = { |
1b1dd23f | 312 | "git rev-parse --parseopt [options] -- [<args>...]", |
21d47835 PH |
313 | NULL |
314 | }; | |
315 | static struct option parseopt_opts[] = { | |
316 | OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash, | |
317 | "keep the `--` passed as an arg"), | |
6e0800ef UKK |
318 | OPT_BOOLEAN(0, "stop-at-non-option", &stop_at_non_option, |
319 | "stop parsing after the " | |
320 | "first non-option argument"), | |
21d47835 PH |
321 | OPT_END(), |
322 | }; | |
323 | ||
f285a2d7 | 324 | struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT; |
21d47835 PH |
325 | const char **usage = NULL; |
326 | struct option *opts = NULL; | |
327 | int onb = 0, osz = 0, unb = 0, usz = 0; | |
328 | ||
21d47835 | 329 | strbuf_addstr(&parsed, "set --"); |
37782920 | 330 | argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage, |
21d47835 PH |
331 | PARSE_OPT_KEEP_DASHDASH); |
332 | if (argc < 1 || strcmp(argv[0], "--")) | |
333 | usage_with_options(parseopt_usage, parseopt_opts); | |
334 | ||
21d47835 PH |
335 | /* get the usage up to the first line with a -- on it */ |
336 | for (;;) { | |
337 | if (strbuf_getline(&sb, stdin, '\n') == EOF) | |
338 | die("premature end of input"); | |
339 | ALLOC_GROW(usage, unb + 1, usz); | |
340 | if (!strcmp("--", sb.buf)) { | |
341 | if (unb < 1) | |
342 | die("no usage string given before the `--' separator"); | |
343 | usage[unb] = NULL; | |
344 | break; | |
345 | } | |
346 | usage[unb++] = strbuf_detach(&sb, NULL); | |
347 | } | |
348 | ||
349 | /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */ | |
350 | while (strbuf_getline(&sb, stdin, '\n') != EOF) { | |
351 | const char *s; | |
352 | struct option *o; | |
353 | ||
354 | if (!sb.len) | |
355 | continue; | |
356 | ||
357 | ALLOC_GROW(opts, onb + 1, osz); | |
358 | memset(opts + onb, 0, sizeof(opts[onb])); | |
359 | ||
360 | o = &opts[onb++]; | |
361 | s = strchr(sb.buf, ' '); | |
362 | if (!s || *sb.buf == ' ') { | |
363 | o->type = OPTION_GROUP; | |
e1033436 | 364 | o->help = xstrdup(skipspaces(sb.buf)); |
21d47835 PH |
365 | continue; |
366 | } | |
367 | ||
368 | o->type = OPTION_CALLBACK; | |
369 | o->help = xstrdup(skipspaces(s)); | |
370 | o->value = &parsed; | |
ff962a3f | 371 | o->flags = PARSE_OPT_NOARG; |
21d47835 | 372 | o->callback = &parseopt_dump; |
ff962a3f PH |
373 | while (s > sb.buf && strchr("*=?!", s[-1])) { |
374 | switch (*--s) { | |
375 | case '=': | |
376 | o->flags &= ~PARSE_OPT_NOARG; | |
377 | break; | |
378 | case '?': | |
379 | o->flags &= ~PARSE_OPT_NOARG; | |
380 | o->flags |= PARSE_OPT_OPTARG; | |
381 | break; | |
382 | case '!': | |
383 | o->flags |= PARSE_OPT_NONEG; | |
384 | break; | |
385 | case '*': | |
386 | o->flags |= PARSE_OPT_HIDDEN; | |
387 | break; | |
388 | } | |
21d47835 PH |
389 | } |
390 | ||
391 | if (s - sb.buf == 1) /* short option only */ | |
392 | o->short_name = *sb.buf; | |
393 | else if (sb.buf[1] != ',') /* long option only */ | |
394 | o->long_name = xmemdupz(sb.buf, s - sb.buf); | |
395 | else { | |
396 | o->short_name = *sb.buf; | |
397 | o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2); | |
398 | } | |
399 | } | |
400 | strbuf_release(&sb); | |
401 | ||
402 | /* put an OPT_END() */ | |
403 | ALLOC_GROW(opts, onb + 1, osz); | |
404 | memset(opts + onb, 0, sizeof(opts[onb])); | |
37782920 | 405 | argc = parse_options(argc, argv, prefix, opts, usage, |
6e0800ef UKK |
406 | keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0 | |
407 | stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0); | |
21d47835 PH |
408 | |
409 | strbuf_addf(&parsed, " --"); | |
b319ce4c | 410 | sq_quote_argv(&parsed, argv, 0); |
21d47835 PH |
411 | puts(parsed.buf); |
412 | return 0; | |
413 | } | |
414 | ||
50325377 CC |
415 | static int cmd_sq_quote(int argc, const char **argv) |
416 | { | |
417 | struct strbuf buf = STRBUF_INIT; | |
418 | ||
419 | if (argc) | |
420 | sq_quote_argv(&buf, argv, 0); | |
421 | printf("%s\n", buf.buf); | |
422 | strbuf_release(&buf); | |
423 | ||
424 | return 0; | |
425 | } | |
426 | ||
b1b35969 CC |
427 | static void die_no_single_rev(int quiet) |
428 | { | |
429 | if (quiet) | |
430 | exit(1); | |
431 | else | |
432 | die("Needed a single revision"); | |
433 | } | |
434 | ||
7006b5be JN |
435 | static const char builtin_rev_parse_usage[] = |
436 | "git rev-parse --parseopt [options] -- [<args>...]\n" | |
437 | " or: git rev-parse --sq-quote [<arg>...]\n" | |
438 | " or: git rev-parse [options] [<arg>...]\n" | |
439 | "\n" | |
440 | "Run \"git rev-parse --parseopt -h\" for more information on the first usage."; | |
441 | ||
a633fca0 | 442 | int cmd_rev_parse(int argc, const char **argv, const char *prefix) |
178cb243 | 443 | { |
dfd1b749 | 444 | int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0; |
178cb243 | 445 | unsigned char sha1[20]; |
dfd1b749 | 446 | const char *name = NULL; |
a62be77f | 447 | |
21d47835 PH |
448 | if (argc > 1 && !strcmp("--parseopt", argv[1])) |
449 | return cmd_parseopt(argc - 1, argv + 1, prefix); | |
450 | ||
50325377 CC |
451 | if (argc > 1 && !strcmp("--sq-quote", argv[1])) |
452 | return cmd_sq_quote(argc - 2, argv + 2); | |
453 | ||
7006b5be JN |
454 | if (argc > 1 && !strcmp("-h", argv[1])) |
455 | usage(builtin_rev_parse_usage); | |
456 | ||
5410a02a | 457 | prefix = setup_git_directory(); |
ef90d6d4 | 458 | git_config(git_default_config, NULL); |
178cb243 | 459 | for (i = 1; i < argc; i++) { |
16cee38a | 460 | const char *arg = argv[i]; |
fb18a2ed | 461 | |
178cb243 | 462 | if (as_is) { |
fb18a2ed | 463 | if (show_file(arg) && as_is < 2) |
e23d0b4a | 464 | verify_filename(prefix, arg); |
178cb243 LT |
465 | continue; |
466 | } | |
3af06987 EW |
467 | if (!strcmp(arg,"-n")) { |
468 | if (++i >= argc) | |
469 | die("-n requires an argument"); | |
470 | if ((filter & DO_FLAGS) && (filter & DO_REVS)) { | |
471 | show(arg); | |
472 | show(argv[i]); | |
473 | } | |
474 | continue; | |
475 | } | |
1968d77d | 476 | if (!prefixcmp(arg, "-n")) { |
3af06987 EW |
477 | if ((filter & DO_FLAGS) && (filter & DO_REVS)) |
478 | show(arg); | |
479 | continue; | |
480 | } | |
481 | ||
178cb243 LT |
482 | if (*arg == '-') { |
483 | if (!strcmp(arg, "--")) { | |
fb18a2ed | 484 | as_is = 2; |
a08b6505 LT |
485 | /* Pass on the "--" if we show anything but files.. */ |
486 | if (filter & (DO_FLAGS | DO_REVS)) | |
487 | show_file(arg); | |
4866ccf0 | 488 | continue; |
178cb243 LT |
489 | } |
490 | if (!strcmp(arg, "--default")) { | |
178cb243 LT |
491 | def = argv[i+1]; |
492 | i++; | |
493 | continue; | |
494 | } | |
8ebb0184 | 495 | if (!strcmp(arg, "--revs-only")) { |
4866ccf0 | 496 | filter &= ~DO_NOREV; |
8ebb0184 LT |
497 | continue; |
498 | } | |
499 | if (!strcmp(arg, "--no-revs")) { | |
4866ccf0 | 500 | filter &= ~DO_REVS; |
8ebb0184 LT |
501 | continue; |
502 | } | |
f79b65aa | 503 | if (!strcmp(arg, "--flags")) { |
4866ccf0 | 504 | filter &= ~DO_NONFLAGS; |
f79b65aa LT |
505 | continue; |
506 | } | |
507 | if (!strcmp(arg, "--no-flags")) { | |
4866ccf0 | 508 | filter &= ~DO_FLAGS; |
f79b65aa LT |
509 | continue; |
510 | } | |
023d66ed | 511 | if (!strcmp(arg, "--verify")) { |
4866ccf0 JH |
512 | filter &= ~(DO_FLAGS|DO_NOREV); |
513 | verify = 1; | |
023d66ed | 514 | continue; |
921d865e | 515 | } |
b1b35969 CC |
516 | if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) { |
517 | quiet = 1; | |
518 | continue; | |
519 | } | |
62a604ba | 520 | if (!strcmp(arg, "--short") || |
cc44c765 | 521 | !prefixcmp(arg, "--short=")) { |
d5012508 JH |
522 | filter &= ~(DO_FLAGS|DO_NOREV); |
523 | verify = 1; | |
524 | abbrev = DEFAULT_ABBREV; | |
44de0da4 JF |
525 | if (arg[7] == '=') |
526 | abbrev = strtoul(arg + 8, NULL, 10); | |
1dc4fb84 JH |
527 | if (abbrev < MINIMUM_ABBREV) |
528 | abbrev = MINIMUM_ABBREV; | |
529 | else if (40 <= abbrev) | |
530 | abbrev = 40; | |
d5012508 JH |
531 | continue; |
532 | } | |
5bb2c65a JH |
533 | if (!strcmp(arg, "--sq")) { |
534 | output_sq = 1; | |
535 | continue; | |
536 | } | |
042a4ed7 LT |
537 | if (!strcmp(arg, "--not")) { |
538 | show_type ^= REVERSED; | |
539 | continue; | |
540 | } | |
30b96fce | 541 | if (!strcmp(arg, "--symbolic")) { |
a6d97d49 JH |
542 | symbolic = SHOW_SYMBOLIC_ASIS; |
543 | continue; | |
544 | } | |
545 | if (!strcmp(arg, "--symbolic-full-name")) { | |
546 | symbolic = SHOW_SYMBOLIC_FULL; | |
30b96fce JH |
547 | continue; |
548 | } | |
a45d3469 BW |
549 | if (!prefixcmp(arg, "--abbrev-ref") && |
550 | (!arg[12] || arg[12] == '=')) { | |
551 | abbrev_ref = 1; | |
552 | abbrev_ref_strict = warn_ambiguous_refs; | |
553 | if (arg[12] == '=') { | |
554 | if (!strcmp(arg + 13, "strict")) | |
555 | abbrev_ref_strict = 1; | |
556 | else if (!strcmp(arg + 13, "loose")) | |
557 | abbrev_ref_strict = 0; | |
558 | else | |
559 | die("unknown mode for %s", arg); | |
560 | } | |
561 | continue; | |
562 | } | |
960bba0d | 563 | if (!strcmp(arg, "--all")) { |
cb5d709f | 564 | for_each_ref(show_reference, NULL); |
960bba0d LT |
565 | continue; |
566 | } | |
ad3f9a71 LT |
567 | if (!strcmp(arg, "--bisect")) { |
568 | for_each_ref_in("refs/bisect/bad", show_reference, NULL); | |
569 | for_each_ref_in("refs/bisect/good", anti_reference, NULL); | |
570 | continue; | |
571 | } | |
a62be77f | 572 | if (!strcmp(arg, "--branches")) { |
cb5d709f | 573 | for_each_branch_ref(show_reference, NULL); |
a62be77f SE |
574 | continue; |
575 | } | |
576 | if (!strcmp(arg, "--tags")) { | |
cb5d709f | 577 | for_each_tag_ref(show_reference, NULL); |
a62be77f SE |
578 | continue; |
579 | } | |
580 | if (!strcmp(arg, "--remotes")) { | |
cb5d709f | 581 | for_each_remote_ref(show_reference, NULL); |
a62be77f SE |
582 | continue; |
583 | } | |
d288a700 | 584 | if (!strcmp(arg, "--show-prefix")) { |
4866ccf0 JH |
585 | if (prefix) |
586 | puts(prefix); | |
d288a700 LT |
587 | continue; |
588 | } | |
5f94c730 JH |
589 | if (!strcmp(arg, "--show-cdup")) { |
590 | const char *pfx = prefix; | |
e90fdc39 JS |
591 | if (!is_inside_work_tree()) { |
592 | const char *work_tree = | |
593 | get_git_work_tree(); | |
594 | if (work_tree) | |
595 | printf("%s\n", work_tree); | |
596 | continue; | |
597 | } | |
5f94c730 JH |
598 | while (pfx) { |
599 | pfx = strchr(pfx, '/'); | |
600 | if (pfx) { | |
601 | pfx++; | |
602 | printf("../"); | |
603 | } | |
604 | } | |
605 | putchar('\n'); | |
606 | continue; | |
607 | } | |
a8783eeb LT |
608 | if (!strcmp(arg, "--git-dir")) { |
609 | const char *gitdir = getenv(GIT_DIR_ENVIRONMENT); | |
610 | static char cwd[PATH_MAX]; | |
611 | if (gitdir) { | |
612 | puts(gitdir); | |
613 | continue; | |
614 | } | |
615 | if (!prefix) { | |
616 | puts(".git"); | |
617 | continue; | |
618 | } | |
619 | if (!getcwd(cwd, PATH_MAX)) | |
0721c314 | 620 | die_errno("unable to get current working directory"); |
a8783eeb LT |
621 | printf("%s/.git\n", cwd); |
622 | continue; | |
623 | } | |
6d9ba67b JS |
624 | if (!strcmp(arg, "--is-inside-git-dir")) { |
625 | printf("%s\n", is_inside_git_dir() ? "true" | |
626 | : "false"); | |
627 | continue; | |
628 | } | |
892c41b9 ML |
629 | if (!strcmp(arg, "--is-inside-work-tree")) { |
630 | printf("%s\n", is_inside_work_tree() ? "true" | |
631 | : "false"); | |
632 | continue; | |
633 | } | |
493c774e ML |
634 | if (!strcmp(arg, "--is-bare-repository")) { |
635 | printf("%s\n", is_bare_repository() ? "true" | |
636 | : "false"); | |
637 | continue; | |
638 | } | |
cc44c765 | 639 | if (!prefixcmp(arg, "--since=")) { |
c1babb1d LT |
640 | show_datestring("--max-age=", arg+8); |
641 | continue; | |
642 | } | |
cc44c765 | 643 | if (!prefixcmp(arg, "--after=")) { |
c1babb1d LT |
644 | show_datestring("--max-age=", arg+8); |
645 | continue; | |
646 | } | |
cc44c765 | 647 | if (!prefixcmp(arg, "--before=")) { |
c1babb1d LT |
648 | show_datestring("--min-age=", arg+9); |
649 | continue; | |
650 | } | |
cc44c765 | 651 | if (!prefixcmp(arg, "--until=")) { |
c1babb1d LT |
652 | show_datestring("--min-age=", arg+8); |
653 | continue; | |
654 | } | |
9523a4c2 | 655 | if (show_flag(arg) && verify) |
b1b35969 | 656 | die_no_single_rev(quiet); |
178cb243 LT |
657 | continue; |
658 | } | |
4866ccf0 JH |
659 | |
660 | /* Not a flag argument */ | |
3dd4e732 SB |
661 | if (try_difference(arg)) |
662 | continue; | |
2122f8b9 BS |
663 | if (try_parent_shorthands(arg)) |
664 | continue; | |
dfd1b749 CC |
665 | name = arg; |
666 | type = NORMAL; | |
667 | if (*arg == '^') { | |
668 | name++; | |
669 | type = REVERSED; | |
800644c5 | 670 | } |
dfd1b749 CC |
671 | if (!get_sha1(name, sha1)) { |
672 | if (verify) | |
673 | revs_count++; | |
674 | else | |
675 | show_rev(type, sha1, name); | |
800644c5 LT |
676 | continue; |
677 | } | |
75ecfce3 CC |
678 | if (verify) |
679 | die_no_single_rev(quiet); | |
9ad0a933 LT |
680 | as_is = 1; |
681 | if (!show_file(arg)) | |
682 | continue; | |
e23d0b4a | 683 | verify_filename(prefix, arg); |
023d66ed | 684 | } |
dfd1b749 CC |
685 | if (verify) { |
686 | if (revs_count == 1) { | |
687 | show_rev(type, sha1, name); | |
688 | return 0; | |
689 | } else if (revs_count == 0 && show_default()) | |
690 | return 0; | |
b1b35969 | 691 | die_no_single_rev(quiet); |
dfd1b749 CC |
692 | } else |
693 | show_default(); | |
178cb243 LT |
694 | return 0; |
695 | } |