blob: aa71f4a4fac819531e47f87293bc7b01b22bab94 [file] [log] [blame]
Linus Torvalds178cb242005-06-13 10:06:50 -07001/*
2 * rev-parse.c
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
Linus Torvaldsa8be83f2005-06-20 20:28:09 -07007#include "commit.h"
Linus Torvalds960bba02005-07-03 13:07:52 -07008#include "refs.h"
Linus Torvaldsc1babb12005-09-20 14:13:24 -07009#include "quote.h"
Christian Couder895f10c2006-06-03 18:45:43 +020010#include "builtin.h"
Pierre Habouzit21d47832007-11-04 11:30:53 +010011#include "parse-options.h"
Linus Torvaldsa8be83f2005-06-20 20:28:09 -070012
Junio C Hamano4866ccf2005-08-24 14:30:04 -070013#define DO_REVS 1
14#define DO_NOREV 2
15#define DO_FLAGS 4
16#define DO_NONFLAGS 8
17static int filter = ~0;
18
David Rientjes96f1e582006-08-15 10:23:48 -070019static const char *def;
Linus Torvalds023d66e2005-06-24 10:12:55 -070020
Linus Torvalds042a4ed2005-06-26 11:34:30 -070021#define NORMAL 0
22#define REVERSED 1
23static int show_type = NORMAL;
Junio C Hamanoa6d97d42008-01-05 12:09:55 -080024
25#define SHOW_SYMBOLIC_ASIS 1
26#define SHOW_SYMBOLIC_FULL 2
David Rientjes96f1e582006-08-15 10:23:48 -070027static int symbolic;
28static int abbrev;
29static int output_sq;
Junio C Hamano4866ccf2005-08-24 14:30:04 -070030
Linus Torvalds921d8652005-06-13 11:14:20 -070031/*
32 * Some arguments are relevant "revision" arguments,
33 * others are about output format or other details.
34 * This sorts it all out.
35 */
36static int is_rev_argument(const char *arg)
37{
38 static const char *rev_args[] = {
Junio C Hamanoe091eb92005-10-05 14:49:54 -070039 "--all",
Junio C Hamano5ccfb752005-08-08 19:31:37 -070040 "--bisect",
Junio C Hamano5a83f3b2005-10-30 01:08:35 -080041 "--dense",
Seana62be772006-05-13 21:43:00 -040042 "--branches",
Junio C Hamano4866ccf2005-08-24 14:30:04 -070043 "--header",
44 "--max-age=",
45 "--max-count=",
Junio C Hamano4866ccf2005-08-24 14:30:04 -070046 "--min-age=",
Junio C Hamano5ccfb752005-08-08 19:31:37 -070047 "--no-merges",
Junio C Hamano4866ccf2005-08-24 14:30:04 -070048 "--objects",
Junio C Hamanoc6496572006-02-19 03:32:31 -080049 "--objects-edge",
Junio C Hamano4866ccf2005-08-24 14:30:04 -070050 "--parents",
51 "--pretty",
Seana62be772006-05-13 21:43:00 -040052 "--remotes",
Junio C Hamano5a83f3b2005-10-30 01:08:35 -080053 "--sparse",
Seana62be772006-05-13 21:43:00 -040054 "--tags",
Junio C Hamano4866ccf2005-08-24 14:30:04 -070055 "--topo-order",
Junio C Hamano4c8725f2006-02-15 22:05:33 -080056 "--date-order",
Junio C Hamano4866ccf2005-08-24 14:30:04 -070057 "--unpacked",
Linus Torvalds921d8652005-06-13 11:14:20 -070058 NULL
59 };
60 const char **p = rev_args;
61
Eric Wong82333402006-01-29 16:28:02 -080062 /* accept -<digit>, like traditional "head" */
63 if ((*arg == '-') && isdigit(arg[1]))
64 return 1;
65
Linus Torvalds921d8652005-06-13 11:14:20 -070066 for (;;) {
67 const char *str = *p++;
68 int len;
69 if (!str)
70 return 0;
71 len = strlen(str);
Junio C Hamano4866ccf2005-08-24 14:30:04 -070072 if (!strcmp(arg, str) ||
73 (str[len-1] == '=' && !strncmp(arg, str, len)))
Linus Torvalds921d8652005-06-13 11:14:20 -070074 return 1;
75 }
76}
77
Junio C Hamano4866ccf2005-08-24 14:30:04 -070078/* Output argument as a string, either SQ or normal */
Junio C Hamano5bb2c652005-07-22 19:08:32 -070079static void show(const char *arg)
80{
81 if (output_sq) {
82 int sq = '\'', ch;
83
84 putchar(sq);
85 while ((ch = *arg++)) {
86 if (ch == sq)
87 fputs("'\\'", stdout);
88 putchar(ch);
89 }
90 putchar(sq);
91 putchar(' ');
92 }
93 else
94 puts(arg);
95}
96
Johannes Sixte00f3792008-05-23 16:13:05 +020097/* Like show(), but with a negation prefix according to type */
98static void show_with_type(int type, const char *arg)
99{
100 if (type != show_type)
101 putchar('^');
102 show(arg);
103}
104
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700105/* Output a revision, only if filter allows it */
Junio C Hamano30b96fc2005-08-16 12:36:46 -0700106static void show_rev(int type, const unsigned char *sha1, const char *name)
Linus Torvalds023d66e2005-06-24 10:12:55 -0700107{
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700108 if (!(filter & DO_REVS))
Linus Torvalds023d66e2005-06-24 10:12:55 -0700109 return;
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700110 def = NULL;
Junio C Hamano5bb2c652005-07-22 19:08:32 -0700111
Junio C Hamanoa6d97d42008-01-05 12:09:55 -0800112 if (symbolic && name) {
113 if (symbolic == SHOW_SYMBOLIC_FULL) {
114 unsigned char discard[20];
115 char *full;
116
117 switch (dwim_ref(name, strlen(name), discard, &full)) {
118 case 0:
119 /*
120 * Not found -- not a ref. We could
121 * emit "name" here, but symbolic-full
122 * users are interested in finding the
123 * refs spelled in full, and they would
124 * need to filter non-refs if we did so.
125 */
126 break;
127 case 1: /* happy */
Johannes Sixte00f3792008-05-23 16:13:05 +0200128 show_with_type(type, full);
Junio C Hamanoa6d97d42008-01-05 12:09:55 -0800129 break;
130 default: /* ambiguous */
131 error("refname '%s' is ambiguous", name);
132 break;
133 }
134 } else {
Johannes Sixte00f3792008-05-23 16:13:05 +0200135 show_with_type(type, name);
Junio C Hamanoa6d97d42008-01-05 12:09:55 -0800136 }
137 }
Junio C Hamanod5012502006-01-25 01:35:38 -0800138 else if (abbrev)
Johannes Sixte00f3792008-05-23 16:13:05 +0200139 show_with_type(type, find_unique_abbrev(sha1, abbrev));
Junio C Hamano30b96fc2005-08-16 12:36:46 -0700140 else
Johannes Sixte00f3792008-05-23 16:13:05 +0200141 show_with_type(type, sha1_to_hex(sha1));
Linus Torvalds023d66e2005-06-24 10:12:55 -0700142}
143
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700144/* Output a flag, only if filter allows it. */
Junio C Hamano16cee382006-06-05 22:36:21 -0700145static int show_flag(const char *arg)
Linus Torvalds023d66e2005-06-24 10:12:55 -0700146{
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700147 if (!(filter & DO_FLAGS))
Linus Torvalds9523a4c2006-02-05 11:58:34 -0800148 return 0;
149 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
Linus Torvalds0360e992005-08-23 10:47:54 -0700150 show(arg);
Linus Torvalds9523a4c2006-02-05 11:58:34 -0800151 return 1;
152 }
153 return 0;
Linus Torvalds023d66e2005-06-24 10:12:55 -0700154}
155
Christian Couderdfd1b742008-05-11 18:28:25 +0200156static int show_default(void)
Linus Torvalds023d66e2005-06-24 10:12:55 -0700157{
Junio C Hamano16cee382006-06-05 22:36:21 -0700158 const char *s = def;
Linus Torvalds023d66e2005-06-24 10:12:55 -0700159
160 if (s) {
161 unsigned char sha1[20];
162
163 def = NULL;
Junio C Hamano9938af62005-08-03 22:15:49 -0700164 if (!get_sha1(s, sha1)) {
Junio C Hamano30b96fc2005-08-16 12:36:46 -0700165 show_rev(NORMAL, sha1, s);
Christian Couderdfd1b742008-05-11 18:28:25 +0200166 return 1;
Linus Torvalds023d66e2005-06-24 10:12:55 -0700167 }
Linus Torvalds023d66e2005-06-24 10:12:55 -0700168 }
Christian Couderdfd1b742008-05-11 18:28:25 +0200169 return 0;
Linus Torvalds023d66e2005-06-24 10:12:55 -0700170}
171
Junio C Hamano8da19772006-09-20 22:02:01 -0700172static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
Linus Torvalds960bba02005-07-03 13:07:52 -0700173{
Junio C Hamano30b96fc2005-08-16 12:36:46 -0700174 show_rev(NORMAL, sha1, refname);
Linus Torvalds960bba02005-07-03 13:07:52 -0700175 return 0;
176}
177
Linus Torvaldsc1babb12005-09-20 14:13:24 -0700178static void show_datestring(const char *flag, const char *datestr)
179{
Linus Torvaldsc1babb12005-09-20 14:13:24 -0700180 static char buffer[100];
Linus Torvaldsc1babb12005-09-20 14:13:24 -0700181
182 /* date handling requires both flags and revs */
183 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
184 return;
Linus Torvalds3c07b1d2005-11-14 19:29:06 -0800185 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
Linus Torvaldsc1babb12005-09-20 14:13:24 -0700186 show(buffer);
187}
188
Linus Torvalds9ad0a932006-02-05 21:41:47 -0800189static int show_file(const char *arg)
Linus Torvalds7a3dd472005-10-18 00:16:45 -0700190{
Linus Torvalds7b34c2f2005-10-25 15:24:55 -0700191 show_default();
Linus Torvalds9ad0a932006-02-05 21:41:47 -0800192 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
Linus Torvalds7a3dd472005-10-18 00:16:45 -0700193 show(arg);
Linus Torvalds9ad0a932006-02-05 21:41:47 -0800194 return 1;
195 }
196 return 0;
Linus Torvalds7a3dd472005-10-18 00:16:45 -0700197}
198
Junio C Hamanob7d936b2006-07-06 00:16:35 -0700199static int try_difference(const char *arg)
Santi BĂ©jar3dd4e732006-07-04 11:02:22 +0200200{
201 char *dotdot;
202 unsigned char sha1[20];
203 unsigned char end[20];
204 const char *next;
205 const char *this;
206 int symmetric;
207
208 if (!(dotdot = strstr(arg, "..")))
209 return 0;
210 next = dotdot + 2;
211 this = arg;
212 symmetric = (*next == '.');
213
214 *dotdot = 0;
215 next += symmetric;
216
217 if (!*next)
218 next = "HEAD";
219 if (dotdot == arg)
220 this = "HEAD";
221 if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
222 show_rev(NORMAL, end, next);
223 show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
224 if (symmetric) {
225 struct commit_list *exclude;
226 struct commit *a, *b;
227 a = lookup_commit_reference(sha1);
228 b = lookup_commit_reference(end);
229 exclude = get_merge_bases(a, b, 1);
230 while (exclude) {
231 struct commit_list *n = exclude->next;
232 show_rev(REVERSED,
233 exclude->item->object.sha1,NULL);
234 free(exclude);
235 exclude = n;
236 }
237 }
238 return 1;
239 }
240 *dotdot = '.';
241 return 0;
242}
243
Pierre Habouzit21d47832007-11-04 11:30:53 +0100244static int parseopt_dump(const struct option *o, const char *arg, int unset)
245{
246 struct strbuf *parsed = o->value;
247 if (unset)
248 strbuf_addf(parsed, " --no-%s", o->long_name);
249 else if (o->short_name)
250 strbuf_addf(parsed, " -%c", o->short_name);
251 else
252 strbuf_addf(parsed, " --%s", o->long_name);
253 if (arg) {
254 strbuf_addch(parsed, ' ');
255 sq_quote_buf(parsed, arg);
256 }
257 return 0;
258}
259
260static const char *skipspaces(const char *s)
261{
262 while (isspace(*s))
263 s++;
264 return s;
265}
266
267static int cmd_parseopt(int argc, const char **argv, const char *prefix)
268{
269 static int keep_dashdash = 0;
270 static char const * const parseopt_usage[] = {
Stephan Beyer1b1dd232008-07-13 15:36:15 +0200271 "git rev-parse --parseopt [options] -- [<args>...]",
Pierre Habouzit21d47832007-11-04 11:30:53 +0100272 NULL
273 };
274 static struct option parseopt_opts[] = {
275 OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash,
276 "keep the `--` passed as an arg"),
277 OPT_END(),
278 };
279
280 struct strbuf sb, parsed;
281 const char **usage = NULL;
282 struct option *opts = NULL;
283 int onb = 0, osz = 0, unb = 0, usz = 0;
284
285 strbuf_init(&parsed, 0);
286 strbuf_addstr(&parsed, "set --");
287 argc = parse_options(argc, argv, parseopt_opts, parseopt_usage,
288 PARSE_OPT_KEEP_DASHDASH);
289 if (argc < 1 || strcmp(argv[0], "--"))
290 usage_with_options(parseopt_usage, parseopt_opts);
291
292 strbuf_init(&sb, 0);
293 /* get the usage up to the first line with a -- on it */
294 for (;;) {
295 if (strbuf_getline(&sb, stdin, '\n') == EOF)
296 die("premature end of input");
297 ALLOC_GROW(usage, unb + 1, usz);
298 if (!strcmp("--", sb.buf)) {
299 if (unb < 1)
300 die("no usage string given before the `--' separator");
301 usage[unb] = NULL;
302 break;
303 }
304 usage[unb++] = strbuf_detach(&sb, NULL);
305 }
306
307 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
308 while (strbuf_getline(&sb, stdin, '\n') != EOF) {
309 const char *s;
310 struct option *o;
311
312 if (!sb.len)
313 continue;
314
315 ALLOC_GROW(opts, onb + 1, osz);
316 memset(opts + onb, 0, sizeof(opts[onb]));
317
318 o = &opts[onb++];
319 s = strchr(sb.buf, ' ');
320 if (!s || *sb.buf == ' ') {
321 o->type = OPTION_GROUP;
Jay Soffiane1033432008-02-25 23:07:39 -0500322 o->help = xstrdup(skipspaces(sb.buf));
Pierre Habouzit21d47832007-11-04 11:30:53 +0100323 continue;
324 }
325
326 o->type = OPTION_CALLBACK;
327 o->help = xstrdup(skipspaces(s));
328 o->value = &parsed;
Pierre Habouzitff962a32008-03-02 09:21:38 +0100329 o->flags = PARSE_OPT_NOARG;
Pierre Habouzit21d47832007-11-04 11:30:53 +0100330 o->callback = &parseopt_dump;
Pierre Habouzitff962a32008-03-02 09:21:38 +0100331 while (s > sb.buf && strchr("*=?!", s[-1])) {
332 switch (*--s) {
333 case '=':
334 o->flags &= ~PARSE_OPT_NOARG;
335 break;
336 case '?':
337 o->flags &= ~PARSE_OPT_NOARG;
338 o->flags |= PARSE_OPT_OPTARG;
339 break;
340 case '!':
341 o->flags |= PARSE_OPT_NONEG;
342 break;
343 case '*':
344 o->flags |= PARSE_OPT_HIDDEN;
345 break;
346 }
Pierre Habouzit21d47832007-11-04 11:30:53 +0100347 }
348
349 if (s - sb.buf == 1) /* short option only */
350 o->short_name = *sb.buf;
351 else if (sb.buf[1] != ',') /* long option only */
352 o->long_name = xmemdupz(sb.buf, s - sb.buf);
353 else {
354 o->short_name = *sb.buf;
355 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
356 }
357 }
358 strbuf_release(&sb);
359
360 /* put an OPT_END() */
361 ALLOC_GROW(opts, onb + 1, osz);
362 memset(opts + onb, 0, sizeof(opts[onb]));
363 argc = parse_options(argc, argv, opts, usage,
364 keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0);
365
366 strbuf_addf(&parsed, " --");
Christian Couderb319ce42007-12-03 05:51:50 +0100367 sq_quote_argv(&parsed, argv, 0);
Pierre Habouzit21d47832007-11-04 11:30:53 +0100368 puts(parsed.buf);
369 return 0;
370}
371
Christian Couderb1b35962008-04-26 13:57:23 +0200372static void die_no_single_rev(int quiet)
373{
374 if (quiet)
375 exit(1);
376 else
377 die("Needed a single revision");
378}
379
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700380int cmd_rev_parse(int argc, const char **argv, const char *prefix)
Linus Torvalds178cb242005-06-13 10:06:50 -0700381{
Christian Couderdfd1b742008-05-11 18:28:25 +0200382 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
Linus Torvalds178cb242005-06-13 10:06:50 -0700383 unsigned char sha1[20];
Christian Couderdfd1b742008-05-11 18:28:25 +0200384 const char *name = NULL;
Seana62be772006-05-13 21:43:00 -0400385
Pierre Habouzit21d47832007-11-04 11:30:53 +0100386 if (argc > 1 && !strcmp("--parseopt", argv[1]))
387 return cmd_parseopt(argc - 1, argv + 1, prefix);
388
Junio C Hamano5410a022007-11-06 12:23:14 -0800389 prefix = setup_git_directory();
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100390 git_config(git_default_config, NULL);
Linus Torvalds178cb242005-06-13 10:06:50 -0700391 for (i = 1; i < argc; i++) {
Junio C Hamano16cee382006-06-05 22:36:21 -0700392 const char *arg = argv[i];
Linus Torvaldsfb18a2e2006-03-26 16:28:20 -0800393
Linus Torvalds178cb242005-06-13 10:06:50 -0700394 if (as_is) {
Linus Torvaldsfb18a2e2006-03-26 16:28:20 -0800395 if (show_file(arg) && as_is < 2)
Linus Torvaldse23d0b42006-04-26 10:15:54 -0700396 verify_filename(prefix, arg);
Linus Torvalds178cb242005-06-13 10:06:50 -0700397 continue;
398 }
Eric Wong3af06982006-01-29 16:26:40 -0800399 if (!strcmp(arg,"-n")) {
400 if (++i >= argc)
401 die("-n requires an argument");
402 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
403 show(arg);
404 show(argv[i]);
405 }
406 continue;
407 }
Junio C Hamano1968d772007-02-20 01:55:07 -0800408 if (!prefixcmp(arg, "-n")) {
Eric Wong3af06982006-01-29 16:26:40 -0800409 if ((filter & DO_FLAGS) && (filter & DO_REVS))
410 show(arg);
411 continue;
412 }
413
Linus Torvalds178cb242005-06-13 10:06:50 -0700414 if (*arg == '-') {
415 if (!strcmp(arg, "--")) {
Linus Torvaldsfb18a2e2006-03-26 16:28:20 -0800416 as_is = 2;
Linus Torvaldsa08b6502005-10-20 17:16:30 -0700417 /* Pass on the "--" if we show anything but files.. */
418 if (filter & (DO_FLAGS | DO_REVS))
419 show_file(arg);
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700420 continue;
Linus Torvalds178cb242005-06-13 10:06:50 -0700421 }
422 if (!strcmp(arg, "--default")) {
Linus Torvalds178cb242005-06-13 10:06:50 -0700423 def = argv[i+1];
424 i++;
425 continue;
426 }
Linus Torvalds8ebb0182005-06-13 10:21:11 -0700427 if (!strcmp(arg, "--revs-only")) {
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700428 filter &= ~DO_NOREV;
Linus Torvalds8ebb0182005-06-13 10:21:11 -0700429 continue;
430 }
431 if (!strcmp(arg, "--no-revs")) {
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700432 filter &= ~DO_REVS;
Linus Torvalds8ebb0182005-06-13 10:21:11 -0700433 continue;
434 }
Linus Torvaldsf79b65a2005-07-06 10:08:08 -0700435 if (!strcmp(arg, "--flags")) {
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700436 filter &= ~DO_NONFLAGS;
Linus Torvaldsf79b65a2005-07-06 10:08:08 -0700437 continue;
438 }
439 if (!strcmp(arg, "--no-flags")) {
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700440 filter &= ~DO_FLAGS;
Linus Torvaldsf79b65a2005-07-06 10:08:08 -0700441 continue;
442 }
Linus Torvalds023d66e2005-06-24 10:12:55 -0700443 if (!strcmp(arg, "--verify")) {
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700444 filter &= ~(DO_FLAGS|DO_NOREV);
445 verify = 1;
Linus Torvalds023d66e2005-06-24 10:12:55 -0700446 continue;
Linus Torvalds921d8652005-06-13 11:14:20 -0700447 }
Christian Couderb1b35962008-04-26 13:57:23 +0200448 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
449 quiet = 1;
450 continue;
451 }
Junio C Hamano62a604b2006-01-26 17:02:07 -0800452 if (!strcmp(arg, "--short") ||
Junio C Hamanocc44c762007-02-20 01:53:29 -0800453 !prefixcmp(arg, "--short=")) {
Junio C Hamanod5012502006-01-25 01:35:38 -0800454 filter &= ~(DO_FLAGS|DO_NOREV);
455 verify = 1;
456 abbrev = DEFAULT_ABBREV;
Jonas Fonseca44de0da2006-02-18 02:10:53 +0100457 if (arg[7] == '=')
458 abbrev = strtoul(arg + 8, NULL, 10);
Junio C Hamano1dc4fb82006-01-26 00:48:19 -0800459 if (abbrev < MINIMUM_ABBREV)
460 abbrev = MINIMUM_ABBREV;
461 else if (40 <= abbrev)
462 abbrev = 40;
Junio C Hamanod5012502006-01-25 01:35:38 -0800463 continue;
464 }
Junio C Hamano5bb2c652005-07-22 19:08:32 -0700465 if (!strcmp(arg, "--sq")) {
466 output_sq = 1;
467 continue;
468 }
Linus Torvalds042a4ed2005-06-26 11:34:30 -0700469 if (!strcmp(arg, "--not")) {
470 show_type ^= REVERSED;
471 continue;
472 }
Junio C Hamano30b96fc2005-08-16 12:36:46 -0700473 if (!strcmp(arg, "--symbolic")) {
Junio C Hamanoa6d97d42008-01-05 12:09:55 -0800474 symbolic = SHOW_SYMBOLIC_ASIS;
475 continue;
476 }
477 if (!strcmp(arg, "--symbolic-full-name")) {
478 symbolic = SHOW_SYMBOLIC_FULL;
Junio C Hamano30b96fc2005-08-16 12:36:46 -0700479 continue;
480 }
Linus Torvalds960bba02005-07-03 13:07:52 -0700481 if (!strcmp(arg, "--all")) {
Junio C Hamanocb5d7092006-09-20 21:47:42 -0700482 for_each_ref(show_reference, NULL);
Linus Torvalds960bba02005-07-03 13:07:52 -0700483 continue;
484 }
Seana62be772006-05-13 21:43:00 -0400485 if (!strcmp(arg, "--branches")) {
Junio C Hamanocb5d7092006-09-20 21:47:42 -0700486 for_each_branch_ref(show_reference, NULL);
Seana62be772006-05-13 21:43:00 -0400487 continue;
488 }
489 if (!strcmp(arg, "--tags")) {
Junio C Hamanocb5d7092006-09-20 21:47:42 -0700490 for_each_tag_ref(show_reference, NULL);
Seana62be772006-05-13 21:43:00 -0400491 continue;
492 }
493 if (!strcmp(arg, "--remotes")) {
Junio C Hamanocb5d7092006-09-20 21:47:42 -0700494 for_each_remote_ref(show_reference, NULL);
Seana62be772006-05-13 21:43:00 -0400495 continue;
496 }
Linus Torvaldsd288a702005-08-16 18:06:34 -0700497 if (!strcmp(arg, "--show-prefix")) {
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700498 if (prefix)
499 puts(prefix);
Linus Torvaldsd288a702005-08-16 18:06:34 -0700500 continue;
501 }
Junio C Hamano5f94c732005-12-22 22:35:38 -0800502 if (!strcmp(arg, "--show-cdup")) {
503 const char *pfx = prefix;
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100504 if (!is_inside_work_tree()) {
505 const char *work_tree =
506 get_git_work_tree();
507 if (work_tree)
508 printf("%s\n", work_tree);
509 continue;
510 }
Junio C Hamano5f94c732005-12-22 22:35:38 -0800511 while (pfx) {
512 pfx = strchr(pfx, '/');
513 if (pfx) {
514 pfx++;
515 printf("../");
516 }
517 }
518 putchar('\n');
519 continue;
520 }
Linus Torvaldsa8783ee2005-09-18 11:18:30 -0700521 if (!strcmp(arg, "--git-dir")) {
522 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
523 static char cwd[PATH_MAX];
524 if (gitdir) {
525 puts(gitdir);
526 continue;
527 }
528 if (!prefix) {
529 puts(".git");
530 continue;
531 }
532 if (!getcwd(cwd, PATH_MAX))
533 die("unable to get current working directory");
534 printf("%s/.git\n", cwd);
535 continue;
536 }
Johannes Schindelin6d9ba672007-01-23 13:30:20 +0100537 if (!strcmp(arg, "--is-inside-git-dir")) {
538 printf("%s\n", is_inside_git_dir() ? "true"
539 : "false");
540 continue;
541 }
Matthias Lederhofer892c41b2007-06-06 09:10:42 +0200542 if (!strcmp(arg, "--is-inside-work-tree")) {
543 printf("%s\n", is_inside_work_tree() ? "true"
544 : "false");
545 continue;
546 }
Matthias Lederhofer493c7742007-06-03 16:46:36 +0200547 if (!strcmp(arg, "--is-bare-repository")) {
548 printf("%s\n", is_bare_repository() ? "true"
549 : "false");
550 continue;
551 }
Junio C Hamanocc44c762007-02-20 01:53:29 -0800552 if (!prefixcmp(arg, "--since=")) {
Linus Torvaldsc1babb12005-09-20 14:13:24 -0700553 show_datestring("--max-age=", arg+8);
554 continue;
555 }
Junio C Hamanocc44c762007-02-20 01:53:29 -0800556 if (!prefixcmp(arg, "--after=")) {
Linus Torvaldsc1babb12005-09-20 14:13:24 -0700557 show_datestring("--max-age=", arg+8);
558 continue;
559 }
Junio C Hamanocc44c762007-02-20 01:53:29 -0800560 if (!prefixcmp(arg, "--before=")) {
Linus Torvaldsc1babb12005-09-20 14:13:24 -0700561 show_datestring("--min-age=", arg+9);
562 continue;
563 }
Junio C Hamanocc44c762007-02-20 01:53:29 -0800564 if (!prefixcmp(arg, "--until=")) {
Linus Torvaldsc1babb12005-09-20 14:13:24 -0700565 show_datestring("--min-age=", arg+8);
566 continue;
567 }
Linus Torvalds9523a4c2006-02-05 11:58:34 -0800568 if (show_flag(arg) && verify)
Christian Couderb1b35962008-04-26 13:57:23 +0200569 die_no_single_rev(quiet);
Linus Torvalds178cb242005-06-13 10:06:50 -0700570 continue;
571 }
Junio C Hamano4866ccf2005-08-24 14:30:04 -0700572
573 /* Not a flag argument */
Santi BĂ©jar3dd4e732006-07-04 11:02:22 +0200574 if (try_difference(arg))
575 continue;
Christian Couderdfd1b742008-05-11 18:28:25 +0200576 name = arg;
577 type = NORMAL;
578 if (*arg == '^') {
579 name++;
580 type = REVERSED;
Linus Torvalds800644c2005-06-20 08:29:13 -0700581 }
Christian Couderdfd1b742008-05-11 18:28:25 +0200582 if (!get_sha1(name, sha1)) {
583 if (verify)
584 revs_count++;
585 else
586 show_rev(type, sha1, name);
Linus Torvalds800644c2005-06-20 08:29:13 -0700587 continue;
588 }
Christian Couder75ecfce2008-04-26 15:19:29 +0200589 if (verify)
590 die_no_single_rev(quiet);
Linus Torvalds9ad0a932006-02-05 21:41:47 -0800591 as_is = 1;
592 if (!show_file(arg))
593 continue;
Linus Torvaldse23d0b42006-04-26 10:15:54 -0700594 verify_filename(prefix, arg);
Linus Torvalds178cb242005-06-13 10:06:50 -0700595 }
Christian Couderdfd1b742008-05-11 18:28:25 +0200596 if (verify) {
597 if (revs_count == 1) {
598 show_rev(type, sha1, name);
599 return 0;
600 } else if (revs_count == 0 && show_default())
601 return 0;
Christian Couderb1b35962008-04-26 13:57:23 +0200602 die_no_single_rev(quiet);
Christian Couderdfd1b742008-05-11 18:28:25 +0200603 } else
604 show_default();
Linus Torvalds178cb242005-06-13 10:06:50 -0700605 return 0;
606}