rev-parse.c 16.6 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 14 15 16 17 18
#define DO_REVS		1
#define DO_NOREV	2
#define DO_FLAGS	4
#define DO_NONFLAGS	8
static int filter = ~0;

19
static const char *def;
20

21 22 23
#define NORMAL 0
#define REVERSED 1
static int show_type = NORMAL;
24 25 26

#define SHOW_SYMBOLIC_ASIS 1
#define SHOW_SYMBOLIC_FULL 2
27 28
static int symbolic;
static int abbrev;
29 30
static int abbrev_ref;
static int abbrev_ref_strict;
31
static int output_sq;
32

33 34 35 36 37 38 39 40
/*
 * 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[] = {
41
		"--all",
42
		"--bisect",
43
		"--dense",
44
		"--branches=",
45
		"--branches",
46
		"--header",
J
Junio C Hamano 已提交
47
		"--ignore-missing",
48
		"--max-age=",
49 50
		"--max-count=",
		"--min-age=",
J
Junio C Hamano 已提交
51
		"--no-merges",
52 53 54 55
		"--min-parents=",
		"--no-min-parents",
		"--max-parents=",
		"--no-max-parents",
56
		"--objects",
J
Junio C Hamano 已提交
57
		"--objects-edge",
58 59
		"--parents",
		"--pretty",
60
		"--remotes=",
61
		"--remotes",
I
Ilari Liusvaara 已提交
62
		"--glob=",
63
		"--sparse",
64
		"--tags=",
65
		"--tags",
66
		"--topo-order",
67
		"--date-order",
68
		"--unpacked",
69 70 71 72
		NULL
	};
	const char **p = rev_args;

73 74 75 76
	/* accept -<digit>, like traditional "head" */
	if ((*arg == '-') && isdigit(arg[1]))
		return 1;

77 78 79 80 81 82
	for (;;) {
		const char *str = *p++;
		int len;
		if (!str)
			return 0;
		len = strlen(str);
83 84
		if (!strcmp(arg, str) ||
		    (str[len-1] == '=' && !strncmp(arg, str, len)))
85 86 87 88
			return 1;
	}
}

89
/* Output argument as a string, either SQ or normal */
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
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);
}

108 109 110 111 112 113 114 115
/* 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);
}

116
/* Output a revision, only if filter allows it */
117
static void show_rev(int type, const unsigned char *sha1, const char *name)
118
{
119
	if (!(filter & DO_REVS))
120
		return;
121
	def = NULL;
122

123 124
	if ((symbolic || abbrev_ref) && name) {
		if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
125 126 127 128 129 130 131 132 133 134 135 136 137 138
			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 */
139 140 141
				if (abbrev_ref)
					full = shorten_unambiguous_ref(full,
						abbrev_ref_strict);
142
				show_with_type(type, full);
143 144 145 146 147 148
				break;
			default: /* ambiguous */
				error("refname '%s' is ambiguous", name);
				break;
			}
		} else {
149
			show_with_type(type, name);
150 151
		}
	}
J
Junio C Hamano 已提交
152
	else if (abbrev)
153
		show_with_type(type, find_unique_abbrev(sha1, abbrev));
154
	else
155
		show_with_type(type, sha1_to_hex(sha1));
156 157
}

158
/* Output a flag, only if filter allows it. */
159
static int show_flag(const char *arg)
160
{
161
	if (!(filter & DO_FLAGS))
162 163
		return 0;
	if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
164
		show(arg);
165 166 167
		return 1;
	}
	return 0;
168 169
}

170
static int show_default(void)
171
{
172
	const char *s = def;
173 174 175 176 177

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

		def = NULL;
178
		if (!get_sha1(s, sha1)) {
179
			show_rev(NORMAL, sha1, s);
180
			return 1;
181 182
		}
	}
183
	return 0;
184 185
}

186
static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
187
{
188
	show_rev(NORMAL, sha1, refname);
189 190 191
	return 0;
}

192 193 194 195 196 197
static int anti_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
{
	show_rev(REVERSED, sha1, refname);
	return 0;
}

198 199 200 201 202 203 204
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;
205
	snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
206 207 208
	show(buffer);
}

209
static int show_file(const char *arg)
210
{
211
	show_default();
212
	if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
213
		show(arg);
214 215 216
		return 1;
	}
	return 0;
217 218
}

219
static int try_difference(const char *arg)
S
Santi Béjar 已提交
220 221 222 223 224 225 226
{
	char *dotdot;
	unsigned char sha1[20];
	unsigned char end[20];
	const char *next;
	const char *this;
	int symmetric;
227
	static const char head_by_default[] = "HEAD";
S
Santi Béjar 已提交
228 229 230 231 232 233 234 235 236 237 238

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

	*dotdot = 0;
	next += symmetric;

	if (!*next)
239
		next = head_by_default;
S
Santi Béjar 已提交
240
	if (dotdot == arg)
241 242 243 244 245 246 247 248 249 250 251 252
		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;
	}

S
Santi Béjar 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
		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;
			}
		}
		return 1;
	}
	*dotdot = '.';
	return 0;
}

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
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;
	if (get_sha1(arg, sha1))
		return 0;

	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);

	return 1;
}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
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);
	else if (o->short_name)
		strbuf_addf(parsed, " -%c", o->short_name);
	else
		strbuf_addf(parsed, " --%s", o->long_name);
	if (arg) {
		strbuf_addch(parsed, ' ');
		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)
{
331
	static int keep_dashdash = 0, stop_at_non_option = 0;
332
	static char const * const parseopt_usage[] = {
S
Stephan Beyer 已提交
333
		"git rev-parse --parseopt [options] -- [<args>...]",
334 335 336 337 338
		NULL
	};
	static struct option parseopt_opts[] = {
		OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash,
					"keep the `--` passed as an arg"),
339 340 341
		OPT_BOOLEAN(0, "stop-at-non-option", &stop_at_non_option,
					"stop parsing after the "
					"first non-option argument"),
342 343 344
		OPT_END(),
	};

345
	struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
346 347 348 349 350
	const char **usage = NULL;
	struct option *opts = NULL;
	int onb = 0, osz = 0, unb = 0, usz = 0;

	strbuf_addstr(&parsed, "set --");
351
	argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	                     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);
	}

	/* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
	while (strbuf_getline(&sb, stdin, '\n') != EOF) {
		const char *s;
		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;
385
			o->help = xstrdup(skipspaces(sb.buf));
386 387 388 389 390 391
			continue;
		}

		o->type = OPTION_CALLBACK;
		o->help = xstrdup(skipspaces(s));
		o->value = &parsed;
392
		o->flags = PARSE_OPT_NOARG;
393
		o->callback = &parseopt_dump;
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
		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;
			}
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
		}

		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]));
426
	argc = parse_options(argc, argv, prefix, opts, usage,
427
			(keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0) |
J
Junio C Hamano 已提交
428
			(stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0) |
429
			PARSE_OPT_SHELL_EVAL);
430 431

	strbuf_addf(&parsed, " --");
432
	sq_quote_argv(&parsed, argv, 0);
433 434 435 436
	puts(parsed.buf);
	return 0;
}

437 438 439 440 441 442 443 444 445 446 447 448
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;
}

449 450 451 452 453 454 455 456
static void die_no_single_rev(int quiet)
{
	if (quiet)
		exit(1);
	else
		die("Needed a single revision");
}

457 458 459 460 461 462 463
static const char builtin_rev_parse_usage[] =
"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.";

464
int cmd_rev_parse(int argc, const char **argv, const char *prefix)
465
{
466
	int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
467
	unsigned char sha1[20];
468
	const char *name = NULL;
469

470 471 472
	if (argc > 1 && !strcmp("--parseopt", argv[1]))
		return cmd_parseopt(argc - 1, argv + 1, prefix);

473 474 475
	if (argc > 1 && !strcmp("--sq-quote", argv[1]))
		return cmd_sq_quote(argc - 2, argv + 2);

476 477 478 479 480 481 482
	if (argc == 2 && !strcmp("--local-env-vars", argv[1])) {
		int i;
		for (i = 0; local_repo_env[i]; i++)
			printf("%s\n", local_repo_env[i]);
		return 0;
	}

483 484 485 486 487 488 489 490
	if (argc > 2 && !strcmp(argv[1], "--resolve-git-dir")) {
		const char *gitdir = resolve_gitdir(argv[2]);
		if (!gitdir)
			die("not a gitdir '%s'", argv[2]);
		puts(gitdir);
		return 0;
	}

491 492 493
	if (argc > 1 && !strcmp("-h", argv[1]))
		usage(builtin_rev_parse_usage);

J
Junio C Hamano 已提交
494
	prefix = setup_git_directory();
495
	git_config(git_default_config, NULL);
496
	for (i = 1; i < argc; i++) {
497
		const char *arg = argv[i];
498

499
		if (as_is) {
500
			if (show_file(arg) && as_is < 2)
501
				verify_filename(prefix, arg, 0);
502 503
			continue;
		}
504 505 506 507 508 509 510 511 512
		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;
		}
513
		if (!prefixcmp(arg, "-n")) {
514 515 516 517 518
			if ((filter & DO_FLAGS) && (filter & DO_REVS))
				show(arg);
			continue;
		}

519 520
		if (*arg == '-') {
			if (!strcmp(arg, "--")) {
521
				as_is = 2;
522 523 524
				/* Pass on the "--" if we show anything but files.. */
				if (filter & (DO_FLAGS | DO_REVS))
					show_file(arg);
525
				continue;
526 527 528 529 530 531
			}
			if (!strcmp(arg, "--default")) {
				def = argv[i+1];
				i++;
				continue;
			}
532
			if (!strcmp(arg, "--revs-only")) {
533
				filter &= ~DO_NOREV;
534 535 536
				continue;
			}
			if (!strcmp(arg, "--no-revs")) {
537
				filter &= ~DO_REVS;
538 539
				continue;
			}
540
			if (!strcmp(arg, "--flags")) {
541
				filter &= ~DO_NONFLAGS;
542 543 544
				continue;
			}
			if (!strcmp(arg, "--no-flags")) {
545
				filter &= ~DO_FLAGS;
546 547
				continue;
			}
548
			if (!strcmp(arg, "--verify")) {
549 550
				filter &= ~(DO_FLAGS|DO_NOREV);
				verify = 1;
551
				continue;
552
			}
553 554 555 556
			if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
				quiet = 1;
				continue;
			}
557
			if (!strcmp(arg, "--short") ||
558
			    !prefixcmp(arg, "--short=")) {
J
Junio C Hamano 已提交
559 560 561
				filter &= ~(DO_FLAGS|DO_NOREV);
				verify = 1;
				abbrev = DEFAULT_ABBREV;
562 563
				if (arg[7] == '=')
					abbrev = strtoul(arg + 8, NULL, 10);
564 565 566 567
				if (abbrev < MINIMUM_ABBREV)
					abbrev = MINIMUM_ABBREV;
				else if (40 <= abbrev)
					abbrev = 40;
J
Junio C Hamano 已提交
568 569
				continue;
			}
570 571 572 573
			if (!strcmp(arg, "--sq")) {
				output_sq = 1;
				continue;
			}
574 575 576 577
			if (!strcmp(arg, "--not")) {
				show_type ^= REVERSED;
				continue;
			}
578
			if (!strcmp(arg, "--symbolic")) {
579 580 581 582 583
				symbolic = SHOW_SYMBOLIC_ASIS;
				continue;
			}
			if (!strcmp(arg, "--symbolic-full-name")) {
				symbolic = SHOW_SYMBOLIC_FULL;
584 585
				continue;
			}
586 587 588 589 590 591 592 593 594 595 596 597 598 599
			if (!prefixcmp(arg, "--abbrev-ref") &&
			    (!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;
			}
600
			if (!strcmp(arg, "--all")) {
601
				for_each_ref(show_reference, NULL);
602 603
				continue;
			}
604 605 606 607 608
			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;
			}
609 610 611 612 613
			if (!prefixcmp(arg, "--branches=")) {
				for_each_glob_ref_in(show_reference, arg + 11,
					"refs/heads/", NULL);
				continue;
			}
614
			if (!strcmp(arg, "--branches")) {
615
				for_each_branch_ref(show_reference, NULL);
616 617
				continue;
			}
618 619 620 621 622
			if (!prefixcmp(arg, "--tags=")) {
				for_each_glob_ref_in(show_reference, arg + 7,
					"refs/tags/", NULL);
				continue;
			}
623
			if (!strcmp(arg, "--tags")) {
624
				for_each_tag_ref(show_reference, NULL);
625 626
				continue;
			}
I
Ilari Liusvaara 已提交
627 628 629 630
			if (!prefixcmp(arg, "--glob=")) {
				for_each_glob_ref(show_reference, arg + 7, NULL);
				continue;
			}
631 632 633 634 635
			if (!prefixcmp(arg, "--remotes=")) {
				for_each_glob_ref_in(show_reference, arg + 10,
					"refs/remotes/", NULL);
				continue;
			}
636
			if (!strcmp(arg, "--remotes")) {
637
				for_each_remote_ref(show_reference, NULL);
638 639
				continue;
			}
640 641 642 643 644 645
			if (!strcmp(arg, "--show-toplevel")) {
				const char *work_tree = get_git_work_tree();
				if (work_tree)
					puts(work_tree);
				continue;
			}
646
			if (!strcmp(arg, "--show-prefix")) {
647 648
				if (prefix)
					puts(prefix);
649 650
				else
					putchar('\n');
651 652
				continue;
			}
J
Junio C Hamano 已提交
653 654
			if (!strcmp(arg, "--show-cdup")) {
				const char *pfx = prefix;
655 656 657 658 659 660 661
				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 已提交
662 663 664 665 666 667 668 669 670 671
				while (pfx) {
					pfx = strchr(pfx, '/');
					if (pfx) {
						pfx++;
						printf("../");
					}
				}
				putchar('\n');
				continue;
			}
672 673 674
			if (!strcmp(arg, "--git-dir")) {
				const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
				static char cwd[PATH_MAX];
675
				int len;
676 677 678 679 680 681 682 683 684
				if (gitdir) {
					puts(gitdir);
					continue;
				}
				if (!prefix) {
					puts(".git");
					continue;
				}
				if (!getcwd(cwd, PATH_MAX))
685
					die_errno("unable to get current working directory");
686 687
				len = strlen(cwd);
				printf("%s%s.git\n", cwd, len && cwd[len-1] != '/' ? "/" : "");
688 689
				continue;
			}
690 691 692 693 694
			if (!strcmp(arg, "--is-inside-git-dir")) {
				printf("%s\n", is_inside_git_dir() ? "true"
						: "false");
				continue;
			}
695 696 697 698 699
			if (!strcmp(arg, "--is-inside-work-tree")) {
				printf("%s\n", is_inside_work_tree() ? "true"
						: "false");
				continue;
			}
700 701 702 703 704
			if (!strcmp(arg, "--is-bare-repository")) {
				printf("%s\n", is_bare_repository() ? "true"
						: "false");
				continue;
			}
705
			if (!prefixcmp(arg, "--since=")) {
706 707 708
				show_datestring("--max-age=", arg+8);
				continue;
			}
709
			if (!prefixcmp(arg, "--after=")) {
710 711 712
				show_datestring("--max-age=", arg+8);
				continue;
			}
713
			if (!prefixcmp(arg, "--before=")) {
714 715 716
				show_datestring("--min-age=", arg+9);
				continue;
			}
717
			if (!prefixcmp(arg, "--until=")) {
718 719 720
				show_datestring("--min-age=", arg+8);
				continue;
			}
721
			if (show_flag(arg) && verify)
722
				die_no_single_rev(quiet);
723 724
			continue;
		}
725 726

		/* Not a flag argument */
S
Santi Béjar 已提交
727 728
		if (try_difference(arg))
			continue;
729 730
		if (try_parent_shorthands(arg))
			continue;
731 732 733 734 735
		name = arg;
		type = NORMAL;
		if (*arg == '^') {
			name++;
			type = REVERSED;
736
		}
737 738 739 740 741
		if (!get_sha1(name, sha1)) {
			if (verify)
				revs_count++;
			else
				show_rev(type, sha1, name);
742 743
			continue;
		}
744 745
		if (verify)
			die_no_single_rev(quiet);
746 747 748
		as_is = 1;
		if (!show_file(arg))
			continue;
749
		verify_filename(prefix, arg, 1);
750
	}
751 752 753 754 755 756
	if (verify) {
		if (revs_count == 1) {
			show_rev(type, sha1, name);
			return 0;
		} else if (revs_count == 0 && show_default())
			return 0;
757
		die_no_single_rev(quiet);
758 759
	} else
		show_default();
760 761
	return 0;
}