revparse.c 20 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6 7
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

8 9
#include "common.h"

10 11 12
#include <assert.h>

#include "buffer.h"
13
#include "tree.h"
14
#include "refdb.h"
15

Ben Straub committed
16
#include "git2.h"
17

18
static int maybe_sha_or_abbrev(git_object** out, git_repository *repo, const char *spec, size_t speclen)
19 20 21 22 23 24 25 26
{
	git_oid oid;

	if (git_oid_fromstrn(&oid, spec, speclen) < 0)
		return GIT_ENOTFOUND;

	return git_object_lookup_prefix(out, repo, &oid, speclen, GIT_OBJ_ANY);
}
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
static int maybe_sha(git_object** out, git_repository *repo, const char *spec)
{
	size_t speclen = strlen(spec);

	if (speclen != GIT_OID_HEXSZ)
		return GIT_ENOTFOUND;

	return maybe_sha_or_abbrev(out, repo, spec, speclen);
}

static int maybe_abbrev(git_object** out, git_repository *repo, const char *spec)
{
	size_t speclen = strlen(spec);

	return maybe_sha_or_abbrev(out, repo, spec, speclen);
}

45 46 47 48 49
static int build_regex(regex_t *regex, const char *pattern)
{
	int error;

	if (*pattern == '\0') {
50
		giterr_set(GITERR_REGEX, "empty pattern");
51
		return GIT_EINVALIDSPEC;
52 53
	}

54
	error = p_regcomp(regex, pattern, REG_EXTENDED);
55 56
	if (!error)
		return 0;
57

58 59
	error = giterr_set_regex(regex, error);

60 61
	regfree(regex);

62
	return error;
63 64
}

65
static int maybe_describe(git_object**out, git_repository *repo, const char *spec)
66
{
67
	const char *substr;
68 69
	int error;
	regex_t regex;
70 71 72

	substr = strstr(spec, "-g");

73 74
	if (substr == NULL)
		return GIT_ENOTFOUND;
75

76 77
	if (build_regex(&regex, ".+-[0-9]+-g[0-9a-fA-F]+") < 0)
		return -1;
78

79 80 81 82
	error = regexec(&regex, spec, 0, NULL, 0);
	regfree(&regex);

	if (error)
83 84
		return GIT_ENOTFOUND;

85
	return maybe_abbrev(out, repo, substr+2);
86 87
}

88 89 90 91 92
static int revparse_lookup_object(
	git_object **object_out,
	git_reference **reference_out,
	git_repository *repo,
	const char *spec)
93 94 95 96
{
	int error;
	git_reference *ref;

97
	if ((error = maybe_sha(object_out, repo, spec)) != GIT_ENOTFOUND)
98 99
		return error;

100
	error = git_reference_dwim(&ref, repo, spec);
101
	if (!error) {
102 103 104 105 106 107 108

		error = git_object_lookup(
			object_out, repo, git_reference_target(ref), GIT_OBJ_ANY);

		if (!error)
			*reference_out = ref;

109
		return error;
110 111
	}

112
	if (error != GIT_ENOTFOUND)
113
		return error;
114

115 116 117
	if ((strlen(spec) < GIT_OID_HEXSZ) &&
		((error = maybe_abbrev(object_out, repo, spec)) != GIT_ENOTFOUND))
			return error;
118

119
	if ((error = maybe_describe(object_out, repo, spec)) != GIT_ENOTFOUND)
120 121
		return error;

122
	giterr_set(GITERR_REFERENCE, "revspec '%s' not found", spec);
123
	return GIT_ENOTFOUND;
124 125
}

126
static int try_parse_numeric(int *n, const char *curly_braces_content)
Ben Straub committed
127
{
128
	int32_t content;
129 130 131 132
	const char *end_ptr;

	if (git__strtol32(&content, curly_braces_content, &end_ptr, 10) < 0)
		return -1;
133

134 135
	if (*end_ptr != '\0')
		return -1;
136

137
	*n = (int)content;
138
	return 0;
Ben Straub committed
139 140
}

141
static int retrieve_previously_checked_out_branch_or_revision(git_object **out, git_reference **base_ref, git_repository *repo, const char *identifier, size_t position)
142
{
143
	git_reference *ref = NULL;
144
	git_reflog *reflog = NULL;
145
	regex_t preg;
146 147
	int error = -1;
	size_t i, numentries, cur;
148
	const git_reflog_entry *entry;
149 150
	const char *msg;
	regmatch_t regexmatches[2];
151 152
	git_buf buf = GIT_BUF_INIT;

153
	cur = position;
154

155
	if (*identifier != '\0' || *base_ref != NULL)
156
		return GIT_EINVALIDSPEC;
157

158 159
	if (build_regex(&preg, "checkout: moving from (.*) to .*") < 0)
		return -1;
160

161 162
	if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0)
		goto cleanup;
163

164
	if (git_reflog_read(&reflog, repo, GIT_HEAD_FILE) < 0)
165 166 167 168
		goto cleanup;

	numentries  = git_reflog_entrycount(reflog);

169
	for (i = 0; i < numentries; i++) {
170
		entry = git_reflog_entry_byindex(reflog, i);
171
		msg = git_reflog_entry_message(entry);
172 173
		if (!msg)
			continue;
174

175 176 177 178 179 180 181
		if (regexec(&preg, msg, 2, regexmatches, 0))
			continue;

		cur--;

		if (cur > 0)
			continue;
182

183 184
		git_buf_put(&buf, msg+regexmatches[1].rm_so, regexmatches[1].rm_eo - regexmatches[1].rm_so);

185
		if ((error = git_reference_dwim(base_ref, repo, git_buf_cstr(&buf))) == 0)
186 187 188 189 190
			goto cleanup;

		if (error < 0 && error != GIT_ENOTFOUND)
			goto cleanup;

191
		error = maybe_abbrev(out, repo, git_buf_cstr(&buf));
192 193 194

		goto cleanup;
	}
195

196 197 198 199
	error = GIT_ENOTFOUND;

cleanup:
	git_reference_free(ref);
200
	git_buf_dispose(&buf);
201 202 203 204 205
	regfree(&preg);
	git_reflog_free(reflog);
	return error;
}

206
static int retrieve_oid_from_reflog(git_oid *oid, git_reference *ref, size_t identifier)
207 208
{
	git_reflog *reflog;
209
	size_t numentries;
210 211 212
	const git_reflog_entry *entry;
	bool search_by_pos = (identifier <= 100000000);

213
	if (git_reflog_read(&reflog, git_reference_owner(ref), git_reference_name(ref)) < 0)
214 215
		return -1;

216
	numentries = git_reflog_entrycount(reflog);
217 218

	if (search_by_pos) {
219 220
		if (numentries < identifier + 1)
			goto notfound;
221 222

		entry = git_reflog_entry_byindex(reflog, identifier);
223
		git_oid_cpy(oid, git_reflog_entry_id_new(entry));
224
	} else {
225
		size_t i;
226
		git_time commit_time;
227

228
		for (i = 0; i < numentries; i++) {
229 230
			entry = git_reflog_entry_byindex(reflog, i);
			commit_time = git_reflog_entry_committer(entry)->when;
231 232

			if (commit_time.time > (git_time_t)identifier)
233
				continue;
234

235
			git_oid_cpy(oid, git_reflog_entry_id_new(entry));
236
			break;
237 238
		}

239 240
		if (i == numentries)
			goto notfound;
241
	}
242

243
	git_reflog_free(reflog);
244 245 246 247 248
	return 0;

notfound:
	giterr_set(
		GITERR_REFERENCE,
249
		"reflog for '%s' has only %"PRIuZ" entries, asked for %"PRIuZ,
250 251 252 253
		git_reference_name(ref), numentries, identifier);

	git_reflog_free(reflog);
	return GIT_ENOTFOUND;
254
}
255

256
static int retrieve_revobject_from_reflog(git_object **out, git_reference **base_ref, git_repository *repo, const char *identifier, size_t position)
257 258 259 260
{
	git_reference *ref;
	git_oid oid;
	int error = -1;
261

262
	if (*base_ref == NULL) {
263
		if ((error = git_reference_dwim(&ref, repo, identifier)) < 0)
264 265 266 267 268
			return error;
	} else {
		ref = *base_ref;
		*base_ref = NULL;
	}
269

270
	if (position == 0) {
271
		error = git_object_lookup(out, repo, git_reference_target(ref), GIT_OBJ_ANY);
272 273
		goto cleanup;
	}
274

275 276
	if ((error = retrieve_oid_from_reflog(&oid, ref, position)) < 0)
		goto cleanup;
277

278 279 280 281 282 283 284 285 286 287 288 289 290
	error = git_object_lookup(out, repo, &oid, GIT_OBJ_ANY);

cleanup:
	git_reference_free(ref);
	return error;
}

static int retrieve_remote_tracking_reference(git_reference **base_ref, const char *identifier, git_repository *repo)
{
	git_reference *tracking, *ref;
	int error = -1;

	if (*base_ref == NULL) {
291
		if ((error = git_reference_dwim(&ref, repo, identifier)) < 0)
292 293 294 295
			return error;
	} else {
		ref = *base_ref;
		*base_ref = NULL;
296 297
	}

298 299 300 301 302
	if (!git_reference_is_branch(ref)) {
		error = GIT_EINVALIDSPEC;
		goto cleanup;
	}

303
	if ((error = git_branch_upstream(&tracking, ref)) < 0)
304
		goto cleanup;
305

306 307
	*base_ref = tracking;

308
cleanup:
309 310 311 312
	git_reference_free(ref);
	return error;
}

313
static int handle_at_syntax(git_object **out, git_reference **ref, const char *spec, size_t identifier_len, git_repository* repo, const char *curly_braces_content)
314 315
{
	bool is_numeric;
316
	int parsed = 0, error = -1;
317 318 319 320 321 322 323 324 325 326 327
	git_buf identifier = GIT_BUF_INIT;
	git_time_t timestamp;

	assert(*out == NULL);

	if (git_buf_put(&identifier, spec, identifier_len) < 0)
		return -1;

	is_numeric = !try_parse_numeric(&parsed, curly_braces_content);

	if (*curly_braces_content == '-' && (!is_numeric || parsed == 0)) {
328
		error = GIT_EINVALIDSPEC;
329 330 331 332 333
		goto cleanup;
	}

	if (is_numeric) {
		if (parsed < 0)
334
			error = retrieve_previously_checked_out_branch_or_revision(out, ref, repo, git_buf_cstr(&identifier), -parsed);
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
		else
			error = retrieve_revobject_from_reflog(out, ref, repo, git_buf_cstr(&identifier), parsed);

		goto cleanup;
	}

	if (!strcmp(curly_braces_content, "u") || !strcmp(curly_braces_content, "upstream")) {
		error = retrieve_remote_tracking_reference(ref, git_buf_cstr(&identifier), repo);

		goto cleanup;
	}

	if (git__date_parse(&timestamp, curly_braces_content) < 0)
		goto cleanup;

350
	error = retrieve_revobject_from_reflog(out, ref, repo, git_buf_cstr(&identifier), (size_t)timestamp);
351 352

cleanup:
353
	git_buf_dispose(&identifier);
354
	return error;
355 356
}

357 358
static git_otype parse_obj_type(const char *str)
{
359 360 361 362 363
	if (!strcmp(str, "commit"))
		return GIT_OBJ_COMMIT;

	if (!strcmp(str, "tree"))
		return GIT_OBJ_TREE;
364

365 366 367 368 369 370
	if (!strcmp(str, "blob"))
		return GIT_OBJ_BLOB;

	if (!strcmp(str, "tag"))
		return GIT_OBJ_TAG;

371
	return GIT_OBJ_BAD;
372 373
}

374
static int dereference_to_non_tag(git_object **out, git_object *obj)
375
{
376 377
	if (git_object_type(obj) == GIT_OBJ_TAG)
		return git_tag_peel(out, (git_tag *)obj);
378

379
	return git_object_dup(out, obj);
380
}
381

382 383 384 385
static int handle_caret_parent_syntax(git_object **out, git_object *obj, int n)
{
	git_object *temp_commit = NULL;
	int error;
386

387 388 389
	if ((error = git_object_peel(&temp_commit, obj, GIT_OBJ_COMMIT)) < 0)
		return (error == GIT_EAMBIGUOUS || error == GIT_ENOTFOUND) ?
			GIT_EINVALIDSPEC : error;
390 391

	if (n == 0) {
392
		*out = temp_commit;
393 394 395
		return 0;
	}

396
	error = git_commit_parent((git_commit **)out, (git_commit*)temp_commit, n - 1);
397

398 399
	git_object_free(temp_commit);
	return error;
400
}
401

402
static int handle_linear_syntax(git_object **out, git_object *obj, int n)
403
{
404 405
	git_object *temp_commit = NULL;
	int error;
406

407 408 409
	if ((error = git_object_peel(&temp_commit, obj, GIT_OBJ_COMMIT)) < 0)
		return (error == GIT_EAMBIGUOUS || error == GIT_ENOTFOUND) ?
			GIT_EINVALIDSPEC : error;
410

411
	error = git_commit_nth_gen_ancestor((git_commit **)out, (git_commit*)temp_commit, n);
412

413 414
	git_object_free(temp_commit);
	return error;
415 416
}

417 418
static int handle_colon_syntax(
	git_object **out,
419 420
	git_object *obj,
	const char *path)
421
{
422
	git_object *tree;
423 424
	int error = -1;
	git_tree_entry *entry = NULL;
425

426 427
	if ((error = git_object_peel(&tree, obj, GIT_OBJ_TREE)) < 0)
		return error == GIT_ENOTFOUND ? GIT_EINVALIDSPEC : error;
428

429 430 431 432
	if (*path == '\0') {
		*out = tree;
		return 0;
	}
433

434 435 436 437 438 439 440
	/*
	 * TODO: Handle the relative path syntax
	 * (:./relative/path and :../relative/path)
	 */
	if ((error = git_tree_entry_bypath(&entry, (git_tree *)tree, path)) < 0)
		goto cleanup;

441
	error = git_tree_entry_to_object(out, git_object_owner(tree), entry);
442

443 444
cleanup:
	git_tree_entry_free(entry);
445
	git_object_free(tree);
446 447

	return error;
448 449
}

450
static int walk_and_search(git_object **out, git_revwalk *walk, regex_t *regex)
451
{
452 453 454
	int error;
	git_oid oid;
	git_object *obj;
455

456
	while (!(error = git_revwalk_next(&oid, walk))) {
457

458 459
		error = git_object_lookup(&obj, git_revwalk_repository(walk), &oid, GIT_OBJ_COMMIT);
		if ((error < 0) && (error != GIT_ENOTFOUND))
460 461 462 463 464 465 466 467
			return -1;

		if (!regexec(regex, git_commit_message((git_commit*)obj), 0, NULL, 0)) {
			*out = obj;
			return 0;
		}

		git_object_free(obj);
468 469
	}

Russell Belfer committed
470
	if (error < 0 && error == GIT_ITEROVER)
471 472 473 474 475 476 477 478 479
		error = GIT_ENOTFOUND;

	return error;
}

static int handle_grep_syntax(git_object **out, git_repository *repo, const git_oid *spec_oid, const char *pattern)
{
	regex_t preg;
	git_revwalk *walk = NULL;
480
	int error;
481

482 483
	if ((error = build_regex(&preg, pattern)) < 0)
		return error;
484

485
	if ((error = git_revwalk_new(&walk, repo)) < 0)
486 487 488 489 490
		goto cleanup;

	git_revwalk_sorting(walk, GIT_SORT_TIME);

	if (spec_oid == NULL) {
491
		if ((error = git_revwalk_push_glob(walk, "refs/*")) < 0)
492
			goto cleanup;
493
	} else if ((error = git_revwalk_push(walk, spec_oid)) < 0)
494 495 496
			goto cleanup;

	error = walk_and_search(out, walk, &preg);
497

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
cleanup:
	regfree(&preg);
	git_revwalk_free(walk);

	return error;
}

static int handle_caret_curly_syntax(git_object **out, git_object *obj, const char *curly_braces_content)
{
	git_otype expected_type;

	if (*curly_braces_content == '\0')
		return dereference_to_non_tag(out, obj);

	if (*curly_braces_content == '/')
		return handle_grep_syntax(out, git_object_owner(obj), git_object_id(obj), curly_braces_content + 1);

	expected_type = parse_obj_type(curly_braces_content);

	if (expected_type == GIT_OBJ_BAD)
518
		return GIT_EINVALIDSPEC;
519

520
	return git_object_peel(out, obj, expected_type);
521 522
}

523
static int extract_curly_braces_content(git_buf *buf, const char *spec, size_t *pos)
524 525 526 527 528 529 530 531
{
	git_buf_clear(buf);

	assert(spec[*pos] == '^' || spec[*pos] == '@');

	(*pos)++;

	if (spec[*pos] == '\0' || spec[*pos] != '{')
532
		return GIT_EINVALIDSPEC;
533 534 535 536 537

	(*pos)++;

	while (spec[*pos] != '}') {
		if (spec[*pos] == '\0')
538
			return GIT_EINVALIDSPEC;
539 540 541 542 543 544 545 546 547

		git_buf_putc(buf, spec[(*pos)++]);
	}

	(*pos)++;

	return 0;
}

548
static int extract_path(git_buf *buf, const char *spec, size_t *pos)
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
{
	git_buf_clear(buf);

	assert(spec[*pos] == ':');

	(*pos)++;

	if (git_buf_puts(buf, spec + *pos) < 0)
		return -1;

	*pos += git_buf_len(buf);

	return 0;
}

564
static int extract_how_many(int *n, const char *spec, size_t *pos)
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
{
	const char *end_ptr;
	int parsed, accumulated;
	char kind = spec[*pos];

	assert(spec[*pos] == '^' || spec[*pos] == '~');

	accumulated = 0;

	do {
		do {
			(*pos)++;
			accumulated++;
		} while (spec[(*pos)] == kind && kind == '~');

		if (git__isdigit(spec[*pos])) {
581
			if (git__strtol32(&parsed, spec + *pos, &end_ptr, 10) < 0)
582
				return GIT_EINVALIDSPEC;
583 584 585

			accumulated += (parsed - 1);
			*pos = end_ptr - spec;
586
		}
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602

	} 	while (spec[(*pos)] == kind && kind == '~');

	*n = accumulated;

	return 0;
}

static int object_from_reference(git_object **object, git_reference *reference)
{
	git_reference *resolved = NULL;
	int error;

	if (git_reference_resolve(&resolved, reference) < 0)
		return -1;

603
	error = git_object_lookup(object, reference->db->repo, git_reference_target(resolved), GIT_OBJ_ANY);
604 605 606 607 608
	git_reference_free(resolved);

	return error;
}

609
static int ensure_base_rev_loaded(git_object **object, git_reference **reference, const char *spec, size_t identifier_len, git_repository *repo, bool allow_empty_identifier)
610 611 612 613 614 615 616
{
	int error;
	git_buf identifier = GIT_BUF_INIT;

	if (*object != NULL)
		return 0;

617 618
	if (*reference != NULL)
		return object_from_reference(object, *reference);
619

620
	if (!allow_empty_identifier && identifier_len == 0)
621
		return GIT_EINVALIDSPEC;
622 623 624 625

	if (git_buf_put(&identifier, spec, identifier_len) < 0)
		return -1;

626
	error = revparse_lookup_object(object, reference, repo, git_buf_cstr(&identifier));
627
	git_buf_dispose(&identifier);
628 629 630 631

	return error;
}

632
static int ensure_base_rev_is_not_known_yet(git_object *object)
633 634 635 636
{
	if (object == NULL)
		return 0;

637
	return GIT_EINVALIDSPEC;
638 639
}

640
static bool any_left_hand_identifier(git_object *object, git_reference *reference, size_t identifier_len)
641 642 643 644 645 646 647 648 649 650 651 652 653
{
	if (object != NULL)
		return true;

	if (reference != NULL)
		return true;

	if (identifier_len > 0)
		return true;

	return false;
}

654
static int ensure_left_hand_identifier_is_not_known_yet(git_object *object, git_reference *reference)
655
{
656
	if (!ensure_base_rev_is_not_known_yet(object) && reference == NULL)
657 658
		return 0;

659
	return GIT_EINVALIDSPEC;
660 661
}

662 663 664
int revparse__ext(
	git_object **object_out,
	git_reference **reference_out,
Russell Belfer committed
665
	size_t *identifier_len_out,
666 667
	git_repository *repo,
	const char *spec)
668
{
669
	size_t pos = 0, identifier_len = 0;
670 671 672 673 674
	int error = -1, n;
	git_buf buf = GIT_BUF_INIT;

	git_reference *reference = NULL;
	git_object *base_rev = NULL;
675

676 677
	bool should_return_reference = true;

678
	assert(object_out && reference_out && repo && spec);
679

680 681
	*object_out = NULL;
	*reference_out = NULL;
682

683
	while (spec[pos]) {
684 685
		switch (spec[pos]) {
		case '^':
686 687
			should_return_reference = false;

688
			if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0)
689
				goto cleanup;
690

691 692
			if (spec[pos+1] == '{') {
				git_object *temp_object = NULL;
693

694 695
				if ((error = extract_curly_braces_content(&buf, spec, &pos)) < 0)
					goto cleanup;
696

697 698 699 700 701
				if ((error = handle_caret_curly_syntax(&temp_object, base_rev, git_buf_cstr(&buf))) < 0)
					goto cleanup;

				git_object_free(base_rev);
				base_rev = temp_object;
702
			} else {
703 704 705 706 707 708 709 710 711 712
				git_object *temp_object = NULL;

				if ((error = extract_how_many(&n, spec, &pos)) < 0)
					goto cleanup;

				if ((error = handle_caret_parent_syntax(&temp_object, base_rev, n)) < 0)
					goto cleanup;

				git_object_free(base_rev);
				base_rev = temp_object;
713 714 715
			}
			break;

716 717 718 719
		case '~':
		{
			git_object *temp_object = NULL;

720 721
			should_return_reference = false;

722 723 724
			if ((error = extract_how_many(&n, spec, &pos)) < 0)
				goto cleanup;

725
			if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0)
726 727 728 729 730 731 732
				goto cleanup;

			if ((error = handle_linear_syntax(&temp_object, base_rev, n)) < 0)
				goto cleanup;

			git_object_free(base_rev);
			base_rev = temp_object;
733
			break;
734
		}
735

736 737 738 739
		case ':':
		{
			git_object *temp_object = NULL;

740 741
			should_return_reference = false;

742 743 744 745
			if ((error = extract_path(&buf, spec, &pos)) < 0)
				goto cleanup;

			if (any_left_hand_identifier(base_rev, reference, identifier_len)) {
746
				if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, true)) < 0)
747 748 749 750
					goto cleanup;

				if ((error = handle_colon_syntax(&temp_object, base_rev, git_buf_cstr(&buf))) < 0)
					goto cleanup;
751
			} else {
752 753 754 755 756 757 758 759 760
				if (*git_buf_cstr(&buf) == '/') {
					if ((error = handle_grep_syntax(&temp_object, repo, NULL, git_buf_cstr(&buf) + 1)) < 0)
						goto cleanup;
				} else {

					/*
					 * TODO: support merge-stage path lookup (":2:Makefile")
					 * and plain index blob lookup (:i-am/a/blob)
					 */
761
					giterr_set(GITERR_INVALID, "unimplemented");
762 763 764
					error = GIT_ERROR;
					goto cleanup;
				}
765
			}
766 767 768

			git_object_free(base_rev);
			base_rev = temp_object;
769
			break;
770 771 772
		}

		case '@':
773 774
			if (spec[pos+1] == '{') {
				git_object *temp_object = NULL;
775

776 777
				if ((error = extract_curly_braces_content(&buf, spec, &pos)) < 0)
					goto cleanup;
778

779
				if ((error = ensure_base_rev_is_not_known_yet(base_rev)) < 0)
780
					goto cleanup;
781

782 783
				if ((error = handle_at_syntax(&temp_object, &reference, spec, identifier_len, repo, git_buf_cstr(&buf))) < 0)
					goto cleanup;
784

785 786 787 788
				if (temp_object != NULL)
					base_rev = temp_object;
				break;
			}
789
			/* fall through */
790

791
		default:
792
			if ((error = ensure_left_hand_identifier_is_not_known_yet(base_rev, reference)) < 0)
793 794 795 796
				goto cleanup;

			pos++;
			identifier_len++;
797
		}
798
	}
799

800
	if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0)
801 802
		goto cleanup;

803 804 805 806 807
	if (!should_return_reference) {
		git_reference_free(reference);
		reference = NULL;
	}

808 809 810
	*object_out = base_rev;
	*reference_out = reference;
	*identifier_len_out = identifier_len;
811
	error = 0;
812

813
cleanup:
814 815 816
	if (error) {
		if (error == GIT_EINVALIDSPEC)
			giterr_set(GITERR_INVALID,
817
				"failed to parse revision specifier - Invalid pattern '%s'", spec);
818

819
		git_object_free(base_rev);
820
		git_reference_free(reference);
821
	}
822

823
	git_buf_dispose(&buf);
824
	return error;
825
}
826

827 828 829 830 831 832
int git_revparse_ext(
	git_object **object_out,
	git_reference **reference_out,
	git_repository *repo,
	const char *spec)
{
Russell Belfer committed
833 834
	int error;
	size_t identifier_len;
835 836 837 838 839 840 841 842
	git_object *obj = NULL;
	git_reference *ref = NULL;

	if ((error = revparse__ext(&obj, &ref, &identifier_len, repo, spec)) < 0)
		goto cleanup;

	*object_out = obj;
	*reference_out = ref;
Russell Belfer committed
843
	GIT_UNUSED(identifier_len);
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874

	return 0;

cleanup:
	git_object_free(obj);
	git_reference_free(ref);
	return error;
}

int git_revparse_single(git_object **out, git_repository *repo, const char *spec)
{
	int error;
	git_object *obj = NULL;
	git_reference *ref = NULL;

	*out = NULL;

	if ((error = git_revparse_ext(&obj, &ref, repo, spec)) < 0)
		goto cleanup;

	git_reference_free(ref);

	*out = obj;

	return 0;

cleanup:
	git_object_free(obj);
	git_reference_free(ref);
	return error;
}
875 876

int git_revparse(
877
	git_revspec *revspec,
Vicent Marti committed
878 879
	git_repository *repo,
	const char *spec)
880
{
881
	const char *dotdot;
882 883
	int error = 0;

884
	assert(revspec && repo && spec);
Vicent Marti committed
885

886
	memset(revspec, 0x0, sizeof(*revspec));
887 888 889 890

	if ((dotdot = strstr(spec, "..")) != NULL) {
		char *lstr;
		const char *rstr;
891
		revspec->flags = GIT_REVPARSE_RANGE;
892

893 894 895 896 897 898 899 900 901 902 903
		/*
		 * Following git.git, don't allow '..' because it makes command line
		 * arguments which can be either paths or revisions ambiguous when the
		 * path is almost certainly intended. The empty range '...' is still
		 * allowed.
		 */
		if (!git__strcmp(spec, "..")) {
			giterr_set(GITERR_INVALID, "Invalid pattern '..'");
			return GIT_EINVALIDSPEC;
		}

Vicent Marti committed
904
		lstr = git__substrdup(spec, dotdot - spec);
905 906
		rstr = dotdot + 2;
		if (dotdot[2] == '.') {
907
			revspec->flags |= GIT_REVPARSE_MERGE_BASE;
908 909 910
			rstr++;
		}

911 912 913 914 915 916 917 918 919 920 921
		error = git_revparse_single(
			&revspec->from,
			repo,
			*lstr == '\0' ? "HEAD" : lstr);

		if (!error) {
			error = git_revparse_single(
				&revspec->to,
				repo,
				*rstr == '\0' ? "HEAD" : rstr);
		}
922 923

		git__free((void*)lstr);
924
	} else {
925 926
		revspec->flags = GIT_REVPARSE_SINGLE;
		error = git_revparse_single(&revspec->from, repo, spec);
927 928 929 930
	}

	return error;
}