rev-parse.c 19.3 KB
Newer Older
1 2 3 4 5 6
/*
 * rev-parse.c
 *
 * Copyright (C) Linus Torvalds, 2005
 */
#include "cache.h"
7
#include "commit.h"
8
#include "refs.h"
9
#include "quote.h"
C
Christian Couder 已提交
10
#include "builtin.h"
11
#include "parse-options.h"
12 13
#include "diff.h"
#include "revision.h"
14
#include "split-index.h"
15

16 17 18 19 20 21
#define DO_REVS		1
#define DO_NOREV	2
#define DO_FLAGS	4
#define DO_NONFLAGS	8
static int filter = ~0;

22
static const char *def;
23

24 25 26
#define NORMAL 0
#define REVERSED 1
static int show_type = NORMAL;
27 28 29

#define SHOW_SYMBOLIC_ASIS 1
#define SHOW_SYMBOLIC_FULL 2
30 31
static int symbolic;
static int abbrev;
32 33
static int abbrev_ref;
static int abbrev_ref_strict;
34
static int output_sq;
35

36
static int stuck_long;
37
static struct string_list *ref_excludes;
38

39 40 41 42 43 44 45 46
/*
 * Some arguments are relevant "revision" arguments,
 * others are about output format or other details.
 * This sorts it all out.
 */
static int is_rev_argument(const char *arg)
{
	static const char *rev_args[] = {
47
		"--all",
48
		"--bisect",
49
		"--dense",
50
		"--branches=",
51
		"--branches",
52
		"--header",
J
Junio C Hamano 已提交
53
		"--ignore-missing",
54
		"--max-age=",
55 56
		"--max-count=",
		"--min-age=",
J
Junio C Hamano 已提交
57
		"--no-merges",
58 59 60 61
		"--min-parents=",
		"--no-min-parents",
		"--max-parents=",
		"--no-max-parents",
62
		"--objects",
J
Junio C Hamano 已提交
63
		"--objects-edge",
64 65
		"--parents",
		"--pretty",
66
		"--remotes=",
67
		"--remotes",
I
Ilari Liusvaara 已提交
68
		"--glob=",
69
		"--sparse",
70
		"--tags=",
71
		"--tags",
72
		"--topo-order",
73
		"--date-order",
74
		"--unpacked",
75 76 77 78
		NULL
	};
	const char **p = rev_args;

79 80 81 82
	/* accept -<digit>, like traditional "head" */
	if ((*arg == '-') && isdigit(arg[1]))
		return 1;

83 84 85 86 87 88
	for (;;) {
		const char *str = *p++;
		int len;
		if (!str)
			return 0;
		len = strlen(str);
89 90
		if (!strcmp(arg, str) ||
		    (str[len-1] == '=' && !strncmp(arg, str, len)))
91 92 93 94
			return 1;
	}
}

95
/* Output argument as a string, either SQ or normal */
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
static void show(const char *arg)
{
	if (output_sq) {
		int sq = '\'', ch;

		putchar(sq);
		while ((ch = *arg++)) {
			if (ch == sq)
				fputs("'\\'", stdout);
			putchar(ch);
		}
		putchar(sq);
		putchar(' ');
	}
	else
		puts(arg);
}

114 115 116 117 118 119 120 121
/* Like show(), but with a negation prefix according to type */
static void show_with_type(int type, const char *arg)
{
	if (type != show_type)
		putchar('^');
	show(arg);
}

122
/* Output a revision, only if filter allows it */
123
static void show_rev(int type, const unsigned char *sha1, const char *name)
124
{
125
	if (!(filter & DO_REVS))
126
		return;
127
	def = NULL;
128

129 130
	if ((symbolic || abbrev_ref) && name) {
		if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
131 132 133 134 135 136 137 138 139 140 141 142 143 144
			unsigned char discard[20];
			char *full;

			switch (dwim_ref(name, strlen(name), discard, &full)) {
			case 0:
				/*
				 * Not found -- not a ref.  We could
				 * emit "name" here, but symbolic-full
				 * users are interested in finding the
				 * refs spelled in full, and they would
				 * need to filter non-refs if we did so.
				 */
				break;
			case 1: /* happy */
145 146 147
				if (abbrev_ref)
					full = shorten_unambiguous_ref(full,
						abbrev_ref_strict);
148
				show_with_type(type, full);
149 150 151 152 153
				break;
			default: /* ambiguous */
				error("refname '%s' is ambiguous", name);
				break;
			}
J
Jeff King 已提交
154
			free(full);
155
		} else {
156
			show_with_type(type, name);
157 158
		}
	}
J
Junio C Hamano 已提交
159
	else if (abbrev)
160
		show_with_type(type, find_unique_abbrev(sha1, abbrev));
161
	else
162
		show_with_type(type, sha1_to_hex(sha1));
163 164
}

165
/* Output a flag, only if filter allows it. */
166
static int show_flag(const char *arg)
167
{
168
	if (!(filter & DO_FLAGS))
169 170
		return 0;
	if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
171
		show(arg);
172 173 174
		return 1;
	}
	return 0;
175 176
}

177
static int show_default(void)
178
{
179
	const char *s = def;
180 181 182 183 184

	if (s) {
		unsigned char sha1[20];

		def = NULL;
185
		if (!get_sha1(s, sha1)) {
186
			show_rev(NORMAL, sha1, s);
187
			return 1;
188 189
		}
	}
190
	return 0;
191 192
}

193
static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
194
{
195 196
	if (ref_excluded(ref_excludes, refname))
		return 0;
197
	show_rev(NORMAL, sha1, refname);
198 199 200
	return 0;
}

201 202 203 204 205 206
static int anti_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
{
	show_rev(REVERSED, sha1, refname);
	return 0;
}

207 208 209 210 211 212
static int show_abbrev(const unsigned char *sha1, void *cb_data)
{
	show_rev(NORMAL, sha1, NULL);
	return 0;
}

213 214 215 216 217 218 219
static void show_datestring(const char *flag, const char *datestr)
{
	static char buffer[100];

	/* date handling requires both flags and revs */
	if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
		return;
220
	snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
221 222 223
	show(buffer);
}

J
John Keeping 已提交
224
static int show_file(const char *arg, int output_prefix)
225
{
226
	show_default();
227
	if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
J
John Keeping 已提交
228 229 230 231 232 233 234
		if (output_prefix) {
			const char *prefix = startup_info->prefix;
			show(prefix_filename(prefix,
					     prefix ? strlen(prefix) : 0,
					     arg));
		} else
			show(arg);
235 236 237
		return 1;
	}
	return 0;
238 239
}

240
static int try_difference(const char *arg)
S
Santi Béjar 已提交
241 242 243 244 245 246 247
{
	char *dotdot;
	unsigned char sha1[20];
	unsigned char end[20];
	const char *next;
	const char *this;
	int symmetric;
248
	static const char head_by_default[] = "HEAD";
S
Santi Béjar 已提交
249 250 251 252 253 254 255 256 257 258 259

	if (!(dotdot = strstr(arg, "..")))
		return 0;
	next = dotdot + 2;
	this = arg;
	symmetric = (*next == '.');

	*dotdot = 0;
	next += symmetric;

	if (!*next)
260
		next = head_by_default;
S
Santi Béjar 已提交
261
	if (dotdot == arg)
262 263 264 265 266 267 268 269 270 271 272 273
		this = head_by_default;

	if (this == head_by_default && next == head_by_default &&
	    !symmetric) {
		/*
		 * Just ".."?  That is not a range but the
		 * pathspec for the parent directory.
		 */
		*dotdot = '.';
		return 0;
	}

274
	if (!get_sha1_committish(this, sha1) && !get_sha1_committish(next, end)) {
S
Santi Béjar 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
		show_rev(NORMAL, end, next);
		show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
		if (symmetric) {
			struct commit_list *exclude;
			struct commit *a, *b;
			a = lookup_commit_reference(sha1);
			b = lookup_commit_reference(end);
			exclude = get_merge_bases(a, b, 1);
			while (exclude) {
				struct commit_list *n = exclude->next;
				show_rev(REVERSED,
					 exclude->item->object.sha1,NULL);
				free(exclude);
				exclude = n;
			}
		}
291
		*dotdot = '.';
S
Santi Béjar 已提交
292 293 294 295 296 297
		return 1;
	}
	*dotdot = '.';
	return 0;
}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
static int try_parent_shorthands(const char *arg)
{
	char *dotdot;
	unsigned char sha1[20];
	struct commit *commit;
	struct commit_list *parents;
	int parents_only;

	if ((dotdot = strstr(arg, "^!")))
		parents_only = 0;
	else if ((dotdot = strstr(arg, "^@")))
		parents_only = 1;

	if (!dotdot || dotdot[2])
		return 0;

	*dotdot = 0;
315 316
	if (get_sha1_committish(arg, sha1)) {
		*dotdot = '^';
317
		return 0;
318
	}
319 320 321 322 323 324 325 326

	if (!parents_only)
		show_rev(NORMAL, sha1, arg);
	commit = lookup_commit_reference(sha1);
	for (parents = commit->parents; parents; parents = parents->next)
		show_rev(parents_only ? NORMAL : REVERSED,
				parents->item->object.sha1, arg);

327
	*dotdot = '^';
328 329 330
	return 1;
}

331 332 333 334 335
static int parseopt_dump(const struct option *o, const char *arg, int unset)
{
	struct strbuf *parsed = o->value;
	if (unset)
		strbuf_addf(parsed, " --no-%s", o->long_name);
336
	else if (o->short_name && (o->long_name == NULL || !stuck_long))
337 338 339 340
		strbuf_addf(parsed, " -%c", o->short_name);
	else
		strbuf_addf(parsed, " --%s", o->long_name);
	if (arg) {
341 342 343 344
		if (!stuck_long)
			strbuf_addch(parsed, ' ');
		else if (o->long_name)
			strbuf_addch(parsed, '=');
345 346 347 348 349 350 351 352 353 354 355 356 357 358
		sq_quote_buf(parsed, arg);
	}
	return 0;
}

static const char *skipspaces(const char *s)
{
	while (isspace(*s))
		s++;
	return s;
}

static int cmd_parseopt(int argc, const char **argv, const char *prefix)
{
359
	static int keep_dashdash = 0, stop_at_non_option = 0;
360
	static char const * const parseopt_usage[] = {
361
		N_("git rev-parse --parseopt [options] -- [<args>...]"),
362 363 364
		NULL
	};
	static struct option parseopt_opts[] = {
365
		OPT_BOOL(0, "keep-dashdash", &keep_dashdash,
366
					N_("keep the `--` passed as an arg")),
367
		OPT_BOOL(0, "stop-at-non-option", &stop_at_non_option,
368 369
					N_("stop parsing after the "
					   "first non-option argument")),
370 371
		OPT_BOOL(0, "stuck-long", &stuck_long,
					N_("output in stuck long form")),
372 373 374
		OPT_END(),
	};

375
	struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
376 377 378 379 380
	const char **usage = NULL;
	struct option *opts = NULL;
	int onb = 0, osz = 0, unb = 0, usz = 0;

	strbuf_addstr(&parsed, "set --");
381
	argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
	                     PARSE_OPT_KEEP_DASHDASH);
	if (argc < 1 || strcmp(argv[0], "--"))
		usage_with_options(parseopt_usage, parseopt_opts);

	/* get the usage up to the first line with a -- on it */
	for (;;) {
		if (strbuf_getline(&sb, stdin, '\n') == EOF)
			die("premature end of input");
		ALLOC_GROW(usage, unb + 1, usz);
		if (!strcmp("--", sb.buf)) {
			if (unb < 1)
				die("no usage string given before the `--' separator");
			usage[unb] = NULL;
			break;
		}
		usage[unb++] = strbuf_detach(&sb, NULL);
	}

400
	/* parse: (<short>|<short>,<long>|<long>)[*=?!]*<arghint>? SP+ <help> */
401 402
	while (strbuf_getline(&sb, stdin, '\n') != EOF) {
		const char *s;
403
		const char *end;
404 405 406 407 408 409 410 411 412 413 414 415
		struct option *o;

		if (!sb.len)
			continue;

		ALLOC_GROW(opts, onb + 1, osz);
		memset(opts + onb, 0, sizeof(opts[onb]));

		o = &opts[onb++];
		s = strchr(sb.buf, ' ');
		if (!s || *sb.buf == ' ') {
			o->type = OPTION_GROUP;
416
			o->help = xstrdup(skipspaces(sb.buf));
417 418 419 420 421 422
			continue;
		}

		o->type = OPTION_CALLBACK;
		o->help = xstrdup(skipspaces(s));
		o->value = &parsed;
423
		o->flags = PARSE_OPT_NOARG;
424
		o->callback = &parseopt_dump;
425 426 427 428 429 430 431 432 433 434

		/* Possible argument name hint */
		end = s;
		while (s > sb.buf && strchr("*=?!", s[-1]) == NULL)
			--s;
		if (s != sb.buf && s != end)
			o->argh = xmemdupz(s, end - s);
		if (s == sb.buf)
			s = end;

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
		while (s > sb.buf && strchr("*=?!", s[-1])) {
			switch (*--s) {
			case '=':
				o->flags &= ~PARSE_OPT_NOARG;
				break;
			case '?':
				o->flags &= ~PARSE_OPT_NOARG;
				o->flags |= PARSE_OPT_OPTARG;
				break;
			case '!':
				o->flags |= PARSE_OPT_NONEG;
				break;
			case '*':
				o->flags |= PARSE_OPT_HIDDEN;
				break;
			}
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
		}

		if (s - sb.buf == 1) /* short option only */
			o->short_name = *sb.buf;
		else if (sb.buf[1] != ',') /* long option only */
			o->long_name = xmemdupz(sb.buf, s - sb.buf);
		else {
			o->short_name = *sb.buf;
			o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
		}
	}
	strbuf_release(&sb);

	/* put an OPT_END() */
	ALLOC_GROW(opts, onb + 1, osz);
	memset(opts + onb, 0, sizeof(opts[onb]));
467
	argc = parse_options(argc, argv, prefix, opts, usage,
468
			(keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
J
Junio C Hamano 已提交
469
			(stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
470
			PARSE_OPT_SHELL_EVAL);
471 472

	strbuf_addf(&parsed, " --");
473
	sq_quote_argv(&parsed, argv, 0);
474 475 476 477
	puts(parsed.buf);
	return 0;
}

478 479 480 481 482 483 484 485 486 487 488 489
static int cmd_sq_quote(int argc, const char **argv)
{
	struct strbuf buf = STRBUF_INIT;

	if (argc)
		sq_quote_argv(&buf, argv, 0);
	printf("%s\n", buf.buf);
	strbuf_release(&buf);

	return 0;
}

490 491 492 493 494 495 496 497
static void die_no_single_rev(int quiet)
{
	if (quiet)
		exit(1);
	else
		die("Needed a single revision");
}

498
static const char builtin_rev_parse_usage[] =
499 500 501 502 503
N_("git rev-parse --parseopt [options] -- [<args>...]\n"
   "   or: git rev-parse --sq-quote [<arg>...]\n"
   "   or: git rev-parse [options] [<arg>...]\n"
   "\n"
   "Run \"git rev-parse --parseopt -h\" for more information on the first usage.");
504

505
int cmd_rev_parse(int argc, const char **argv, const char *prefix)
506
{
507
	int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
508
	int has_dashdash = 0;
J
John Keeping 已提交
509
	int output_prefix = 0;
510
	unsigned char sha1[20];
511
	unsigned int flags = 0;
512
	const char *name = NULL;
513
	struct object_context unused;
514

515 516 517
	if (argc > 1 && !strcmp("--parseopt", argv[1]))
		return cmd_parseopt(argc - 1, argv + 1, prefix);

518 519 520
	if (argc > 1 && !strcmp("--sq-quote", argv[1]))
		return cmd_sq_quote(argc - 2, argv + 2);

521 522 523
	if (argc > 1 && !strcmp("-h", argv[1]))
		usage(builtin_rev_parse_usage);

524 525 526 527 528 529 530
	for (i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "--")) {
			has_dashdash = 1;
			break;
		}
	}

J
Junio C Hamano 已提交
531
	prefix = setup_git_directory();
532
	git_config(git_default_config, NULL);
533
	for (i = 1; i < argc; i++) {
534
		const char *arg = argv[i];
535

536 537 538 539 540 541 542
		if (!strcmp(arg, "--git-path")) {
			if (!argv[i + 1])
				die("--git-path requires an argument");
			puts(git_path("%s", argv[i + 1]));
			i++;
			continue;
		}
543
		if (as_is) {
J
John Keeping 已提交
544
			if (show_file(arg, output_prefix) && as_is < 2)
545
				verify_filename(prefix, arg, 0);
546 547
			continue;
		}
548 549 550 551 552 553 554 555 556
		if (!strcmp(arg,"-n")) {
			if (++i >= argc)
				die("-n requires an argument");
			if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
				show(arg);
				show(argv[i]);
			}
			continue;
		}
557
		if (starts_with(arg, "-n")) {
558 559 560 561 562
			if ((filter & DO_FLAGS) && (filter & DO_REVS))
				show(arg);
			continue;
		}

563 564
		if (*arg == '-') {
			if (!strcmp(arg, "--")) {
565
				as_is = 2;
566 567
				/* Pass on the "--" if we show anything but files.. */
				if (filter & (DO_FLAGS | DO_REVS))
J
John Keeping 已提交
568
					show_file(arg, 0);
569
				continue;
570 571
			}
			if (!strcmp(arg, "--default")) {
572 573 574
				def = argv[++i];
				if (!def)
					die("--default requires an argument");
575 576
				continue;
			}
J
John Keeping 已提交
577
			if (!strcmp(arg, "--prefix")) {
578 579 580
				prefix = argv[++i];
				if (!prefix)
					die("--prefix requires an argument");
J
John Keeping 已提交
581 582 583 584
				startup_info->prefix = prefix;
				output_prefix = 1;
				continue;
			}
585
			if (!strcmp(arg, "--revs-only")) {
586
				filter &= ~DO_NOREV;
587 588 589
				continue;
			}
			if (!strcmp(arg, "--no-revs")) {
590
				filter &= ~DO_REVS;
591 592
				continue;
			}
593
			if (!strcmp(arg, "--flags")) {
594
				filter &= ~DO_NONFLAGS;
595 596 597
				continue;
			}
			if (!strcmp(arg, "--no-flags")) {
598
				filter &= ~DO_FLAGS;
599 600
				continue;
			}
601
			if (!strcmp(arg, "--verify")) {
602 603
				filter &= ~(DO_FLAGS|DO_NOREV);
				verify = 1;
604
				continue;
605
			}
606 607
			if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
				quiet = 1;
608
				flags |= GET_SHA1_QUIETLY;
609 610
				continue;
			}
611
			if (!strcmp(arg, "--short") ||
612
			    starts_with(arg, "--short=")) {
J
Junio C Hamano 已提交
613 614 615
				filter &= ~(DO_FLAGS|DO_NOREV);
				verify = 1;
				abbrev = DEFAULT_ABBREV;
616 617
				if (arg[7] == '=')
					abbrev = strtoul(arg + 8, NULL, 10);
618 619 620 621
				if (abbrev < MINIMUM_ABBREV)
					abbrev = MINIMUM_ABBREV;
				else if (40 <= abbrev)
					abbrev = 40;
J
Junio C Hamano 已提交
622 623
				continue;
			}
624 625 626 627
			if (!strcmp(arg, "--sq")) {
				output_sq = 1;
				continue;
			}
628 629 630 631
			if (!strcmp(arg, "--not")) {
				show_type ^= REVERSED;
				continue;
			}
632
			if (!strcmp(arg, "--symbolic")) {
633 634 635 636 637
				symbolic = SHOW_SYMBOLIC_ASIS;
				continue;
			}
			if (!strcmp(arg, "--symbolic-full-name")) {
				symbolic = SHOW_SYMBOLIC_FULL;
638 639
				continue;
			}
640
			if (starts_with(arg, "--abbrev-ref") &&
641 642 643 644 645 646 647 648 649 650 651 652 653
			    (!arg[12] || arg[12] == '=')) {
				abbrev_ref = 1;
				abbrev_ref_strict = warn_ambiguous_refs;
				if (arg[12] == '=') {
					if (!strcmp(arg + 13, "strict"))
						abbrev_ref_strict = 1;
					else if (!strcmp(arg + 13, "loose"))
						abbrev_ref_strict = 0;
					else
						die("unknown mode for %s", arg);
				}
				continue;
			}
654
			if (!strcmp(arg, "--all")) {
655
				for_each_ref(show_reference, NULL);
656 657
				continue;
			}
658
			if (starts_with(arg, "--disambiguate=")) {
659 660 661
				for_each_abbrev(arg + 15, show_abbrev, NULL);
				continue;
			}
662 663 664 665 666
			if (!strcmp(arg, "--bisect")) {
				for_each_ref_in("refs/bisect/bad", show_reference, NULL);
				for_each_ref_in("refs/bisect/good", anti_reference, NULL);
				continue;
			}
667
			if (starts_with(arg, "--branches=")) {
668 669
				for_each_glob_ref_in(show_reference, arg + 11,
					"refs/heads/", NULL);
670
				clear_ref_exclusion(&ref_excludes);
671 672
				continue;
			}
673
			if (!strcmp(arg, "--branches")) {
674
				for_each_branch_ref(show_reference, NULL);
675
				clear_ref_exclusion(&ref_excludes);
676 677
				continue;
			}
678
			if (starts_with(arg, "--tags=")) {
679 680
				for_each_glob_ref_in(show_reference, arg + 7,
					"refs/tags/", NULL);
681
				clear_ref_exclusion(&ref_excludes);
682 683
				continue;
			}
684
			if (!strcmp(arg, "--tags")) {
685
				for_each_tag_ref(show_reference, NULL);
686
				clear_ref_exclusion(&ref_excludes);
687 688
				continue;
			}
689
			if (starts_with(arg, "--glob=")) {
I
Ilari Liusvaara 已提交
690
				for_each_glob_ref(show_reference, arg + 7, NULL);
691
				clear_ref_exclusion(&ref_excludes);
I
Ilari Liusvaara 已提交
692 693
				continue;
			}
694
			if (starts_with(arg, "--remotes=")) {
695 696
				for_each_glob_ref_in(show_reference, arg + 10,
					"refs/remotes/", NULL);
697
				clear_ref_exclusion(&ref_excludes);
698 699
				continue;
			}
700
			if (!strcmp(arg, "--remotes")) {
701
				for_each_remote_ref(show_reference, NULL);
702 703 704
				clear_ref_exclusion(&ref_excludes);
				continue;
			}
705
			if (starts_with(arg, "--exclude=")) {
706
				add_ref_exclusion(&ref_excludes, arg + 10);
707 708
				continue;
			}
709 710 711 712 713 714
			if (!strcmp(arg, "--local-env-vars")) {
				int i;
				for (i = 0; local_repo_env[i]; i++)
					printf("%s\n", local_repo_env[i]);
				continue;
			}
715 716 717 718 719 720
			if (!strcmp(arg, "--show-toplevel")) {
				const char *work_tree = get_git_work_tree();
				if (work_tree)
					puts(work_tree);
				continue;
			}
721
			if (!strcmp(arg, "--show-prefix")) {
722 723
				if (prefix)
					puts(prefix);
724 725
				else
					putchar('\n');
726 727
				continue;
			}
J
Junio C Hamano 已提交
728 729
			if (!strcmp(arg, "--show-cdup")) {
				const char *pfx = prefix;
730 731 732 733 734 735 736
				if (!is_inside_work_tree()) {
					const char *work_tree =
						get_git_work_tree();
					if (work_tree)
						printf("%s\n", work_tree);
					continue;
				}
J
Junio C Hamano 已提交
737 738 739 740 741 742 743 744 745 746
				while (pfx) {
					pfx = strchr(pfx, '/');
					if (pfx) {
						pfx++;
						printf("../");
					}
				}
				putchar('\n');
				continue;
			}
747 748
			if (!strcmp(arg, "--git-dir")) {
				const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
749
				char *cwd;
750
				int len;
751 752 753 754 755 756 757 758
				if (gitdir) {
					puts(gitdir);
					continue;
				}
				if (!prefix) {
					puts(".git");
					continue;
				}
759
				cwd = xgetcwd();
760 761
				len = strlen(cwd);
				printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : "");
762
				free(cwd);
763 764
				continue;
			}
765 766 767 768
			if (!strcmp(arg, "--git-common-dir")) {
				puts(get_git_common_dir());
				continue;
			}
769
			if (!strcmp(arg, "--resolve-git-dir")) {
770
				const char *gitdir = argv[++i];
771
				if (!gitdir)
772 773 774 775
					die("--resolve-git-dir requires an argument");
				gitdir = resolve_gitdir(gitdir);
				if (!gitdir)
					die("not a gitdir '%s'", argv[i]);
776 777 778
				puts(gitdir);
				continue;
			}
779 780 781 782 783
			if (!strcmp(arg, "--is-inside-git-dir")) {
				printf("%s\n", is_inside_git_dir() ? "true"
						: "false");
				continue;
			}
784 785 786 787 788
			if (!strcmp(arg, "--is-inside-work-tree")) {
				printf("%s\n", is_inside_work_tree() ? "true"
						: "false");
				continue;
			}
789 790 791 792 793
			if (!strcmp(arg, "--is-bare-repository")) {
				printf("%s\n", is_bare_repository() ? "true"
						: "false");
				continue;
			}
794 795 796 797 798 799 800 801 802
			if (!strcmp(arg, "--shared-index-path")) {
				if (read_cache() < 0)
					die(_("Could not read the index"));
				if (the_index.split_index) {
					const unsigned char *sha1 = the_index.split_index->base_sha1;
					puts(git_path("sharedindex.%s", sha1_to_hex(sha1)));
				}
				continue;
			}
803
			if (starts_with(arg, "--since=")) {
804 805 806
				show_datestring("--max-age=", arg+8);
				continue;
			}
807
			if (starts_with(arg, "--after=")) {
808 809 810
				show_datestring("--max-age=", arg+8);
				continue;
			}
811
			if (starts_with(arg, "--before=")) {
812 813 814
				show_datestring("--min-age=", arg+9);
				continue;
			}
815
			if (starts_with(arg, "--until=")) {
816 817 818
				show_datestring("--min-age=", arg+8);
				continue;
			}
819
			if (show_flag(arg) && verify)
820
				die_no_single_rev(quiet);
821 822
			continue;
		}
823 824

		/* Not a flag argument */
S
Santi Béjar 已提交
825 826
		if (try_difference(arg))
			continue;
827 828
		if (try_parent_shorthands(arg))
			continue;
829 830 831 832 833
		name = arg;
		type = NORMAL;
		if (*arg == '^') {
			name++;
			type = REVERSED;
834
		}
835
		if (!get_sha1_with_context(name, flags, sha1, &unused)) {
836 837 838 839
			if (verify)
				revs_count++;
			else
				show_rev(type, sha1, name);
840 841
			continue;
		}
842 843
		if (verify)
			die_no_single_rev(quiet);
844 845
		if (has_dashdash)
			die("bad revision '%s'", arg);
846
		as_is = 1;
J
John Keeping 已提交
847
		if (!show_file(arg, output_prefix))
848
			continue;
849
		verify_filename(prefix, arg, 1);
850
	}
851 852 853 854 855 856
	if (verify) {
		if (revs_count == 1) {
			show_rev(type, sha1, name);
			return 0;
		} else if (revs_count == 0 && show_default())
			return 0;
857
		die_no_single_rev(quiet);
858 859
	} else
		show_default();
860 861
	return 0;
}