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