revparse.c 18.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * Copyright (C) 2009-2012 the libgit2 contributors
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include <assert.h>

#include "common.h"
#include "buffer.h"
12
#include "tree.h"
13

Ben Straub committed
14
#include "git2.h"
15

16
static int revspec_error(const char *revspec)
17
{
18 19
	giterr_set(GITERR_INVALID, "Failed to parse revision specifier - Invalid pattern '%s'", revspec);
	return -1;
20 21
}

22
static int disambiguate_refname(git_reference **out, git_repository *repo, const char *refname)
23
{
24 25 26 27 28
	int error, i;
	bool fallbackmode = true;
	git_reference *ref;
	git_buf refnamebuf = GIT_BUF_INIT, name = GIT_BUF_INIT;

29
	static const char* formatters[] = {
30
		"%s",
31 32 33 34 35
		GIT_REFS_DIR "%s",
		GIT_REFS_TAGS_DIR "%s",
		GIT_REFS_HEADS_DIR "%s",
		GIT_REFS_REMOTES_DIR "%s",
		GIT_REFS_REMOTES_DIR "%s/" GIT_HEAD_FILE,
36 37
		NULL
	};
38 39 40 41

	if (*refname)
		git_buf_puts(&name, refname);
	else {
42
		git_buf_puts(&name, GIT_HEAD_FILE);
43 44 45 46 47 48 49 50 51 52
		fallbackmode = false;
	}

	for (i = 0; formatters[i] && (fallbackmode || i == 0); i++) {

		git_buf_clear(&refnamebuf);

		if ((error = git_buf_printf(&refnamebuf, formatters[i], git_buf_cstr(&name))) < 0)
			goto cleanup;

53 54 55 56 57
		if (!git_reference_is_valid_name(git_buf_cstr(&refnamebuf))) {
			error = GIT_ENOTFOUND;
			continue;
		}

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
		error = git_reference_lookup_resolved(&ref, repo, git_buf_cstr(&refnamebuf), -1);

		if (!error) {
			*out = ref;
			error = 0;
			goto cleanup;
		}

		if (error != GIT_ENOTFOUND)
			goto cleanup;
	}

cleanup:
	git_buf_free(&name);
	git_buf_free(&refnamebuf);
	return error;
}

76 77 78 79 80 81 82 83 84 85
static int maybe_sha_or_abbrev(git_object**out, git_repository *repo, const char *spec)
{
	git_oid oid;
	size_t speclen = strlen(spec);

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

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

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
static int build_regex(regex_t *regex, const char *pattern)
{
	int error;

	if (*pattern == '\0') {
		giterr_set(GITERR_REGEX, "Empty pattern");
		return -1;
	}

	error = regcomp(regex, pattern, REG_EXTENDED);
	if (!error)
		return 0;
	
	giterr_set_regex(regex, error);
	regfree(regex);

	return -1;
}

106
static int maybe_describe(git_object**out, git_repository *repo, const char *spec)
107
{
108
	const char *substr;
109 110
	int error;
	regex_t regex;
111 112 113

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

114 115 116
	if (substr == NULL)
		return GIT_ENOTFOUND;
	
117 118
	if (build_regex(&regex, ".+-[0-9]+-g[0-9a-fA-F]+") < 0)
		return -1;
119

120 121 122 123
	error = regexec(&regex, spec, 0, NULL, 0);
	regfree(&regex);

	if (error)
124 125
		return GIT_ENOTFOUND;

126
	return maybe_sha_or_abbrev(out, repo, substr+2);
127 128
}

129
static int revparse_lookup_object(git_object **out, git_repository *repo, const char *spec)
130 131 132 133 134 135 136 137 138 139 140
{
	int error;
	git_reference *ref;

	error = maybe_describe(out, repo, spec);
	if (!error)
		return 0;

	if (error < 0 && error != GIT_ENOTFOUND)
		return error;

141 142 143 144
	error = disambiguate_refname(&ref, repo, spec);
	if (!error) {
		error = git_object_lookup(out, repo, git_reference_oid(ref), GIT_OBJ_ANY);
		git_reference_free(ref);
145
		return error;
146 147
	}

148
	if (error < 0 && error != GIT_ENOTFOUND)
149
		return error;
150

151 152 153 154 155 156 157
	error = maybe_sha_or_abbrev(out, repo, spec);
	if (!error)
		return 0;

	if (error < 0 && error != GIT_ENOTFOUND)
		return error;

158
	giterr_set(GITERR_REFERENCE, "Refspec '%s' not found.", spec);
159
	return GIT_ENOTFOUND;
160 161
}

162
static int try_parse_numeric(int *n, const char *curly_braces_content)
Ben Straub committed
163
{
164 165 166 167 168
	int content;
	const char *end_ptr;

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

170 171
	if (*end_ptr != '\0')
		return -1;
172

173 174
	*n = content;
	return 0;
Ben Straub committed
175 176
}

177
static int retrieve_previously_checked_out_branch_or_revision(git_object **out, git_reference **base_ref, git_repository *repo, const char *spec, const char *identifier, unsigned int position)
178
{
179
	git_reference *ref = NULL;
180
	git_reflog *reflog = NULL;
181 182
	regex_t preg;
	int numentries, i, cur, error = -1;
183
	const git_reflog_entry *entry;
184 185
	const char *msg;
	regmatch_t regexmatches[2];
186 187
	git_buf buf = GIT_BUF_INIT;

188
	cur = position;
189

190 191
	if (*identifier != '\0' || *base_ref != NULL)
		return revspec_error(spec);
192

193 194
	if (build_regex(&preg, "checkout: moving from (.*) to .*") < 0)
		return -1;
195

196 197
	if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0)
		goto cleanup;
198

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
	if (git_reflog_read(&reflog, ref) < 0)
		goto cleanup;

	numentries  = git_reflog_entrycount(reflog);

	for (i = numentries - 1; i >= 0; i--) {
		entry = git_reflog_entry_byindex(reflog, i);
		msg = git_reflog_entry_msg(entry);
		
		if (regexec(&preg, msg, 2, regexmatches, 0))
			continue;

		cur--;

		if (cur > 0)
			continue;
		
		git_buf_put(&buf, msg+regexmatches[1].rm_so, regexmatches[1].rm_eo - regexmatches[1].rm_so);

		if ((error = disambiguate_refname(base_ref, repo, git_buf_cstr(&buf))) == 0)
			goto cleanup;

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

		error = maybe_sha_or_abbrev(out, repo, git_buf_cstr(&buf));

		goto cleanup;
	}
	
	error = GIT_ENOTFOUND;

cleanup:
	git_reference_free(ref);
	git_buf_free(&buf);
	regfree(&preg);
	git_reflog_free(reflog);
	return error;
}

static int retrieve_oid_from_reflog(git_oid *oid, git_reference *ref, unsigned int identifier)
{
	git_reflog *reflog;
	int error = -1;
	unsigned int numentries;
	const git_reflog_entry *entry;
	bool search_by_pos = (identifier <= 100000000);

	if (git_reflog_read(&reflog, ref) < 0)
		return -1;

	numentries  = git_reflog_entrycount(reflog);

	if (search_by_pos) {
		if (numentries < identifier + 1) {
			giterr_set(
				GITERR_REFERENCE,
				"Reflog for '%s' has only %d entries, asked for %d",
				git_reference_name(ref),
				numentries,
				identifier);

			error = GIT_ENOTFOUND;
			goto cleanup;
263
		}
264 265 266 267 268 269

		entry = git_reflog_entry_byindex(reflog, identifier);
		git_oid_cpy(oid, git_reflog_entry_oidold(entry));
		error = 0;
		goto cleanup;

270
	} else {
271 272
		int i;
		git_time commit_time;
273

274 275 276 277 278 279
		for (i = numentries - 1; i >= 0; i--) {
			entry = git_reflog_entry_byindex(reflog, i);
			commit_time = git_reflog_entry_committer(entry)->when;
					
			if (commit_time.time - identifier > 0)
				continue;
280

281 282
			git_oid_cpy(oid, git_reflog_entry_oidnew(entry));
			error = 0;
283 284 285
			goto cleanup;
		}

286 287
		error = GIT_ENOTFOUND;
	}
288

289 290 291 292
cleanup:
	git_reflog_free(reflog);
	return error;
}
293

294 295 296 297 298
static int retrieve_revobject_from_reflog(git_object **out, git_reference **base_ref, git_repository *repo, const char *identifier, unsigned int position)
{
	git_reference *ref;
	git_oid oid;
	int error = -1;
299

300 301 302 303 304 305 306
	if (*base_ref == NULL) {
		if ((error = disambiguate_refname(&ref, repo, identifier)) < 0)
			return error;
	} else {
		ref = *base_ref;
		*base_ref = NULL;
	}
307

308 309 310 311
	if (position == 0) {
		error = git_object_lookup(out, repo, git_reference_oid(ref), GIT_OBJ_ANY);
		goto cleanup;
	}
312

313 314
	if ((error = retrieve_oid_from_reflog(&oid, ref, position)) < 0)
		goto cleanup;
315

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
	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) {
		if ((error = disambiguate_refname(&ref, repo, identifier)) < 0)
			return error;
	} else {
		ref = *base_ref;
		*base_ref = NULL;
334 335
	}

336
	if ((error = git_branch_tracking(&tracking, ref)) < 0)
337 338 339 340
		goto cleanup;
	
	*base_ref = tracking;

341
cleanup:
342 343 344 345
	git_reference_free(ref);
	return error;
}

346
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)
347 348
{
	bool is_numeric;
349
	int parsed = 0, error = -1;
350 351 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 385 386 387
	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)) {
		error = revspec_error(spec);
		goto cleanup;
	}

	if (is_numeric) {
		if (parsed < 0)
			error = retrieve_previously_checked_out_branch_or_revision(out, ref, repo, spec, git_buf_cstr(&identifier), -parsed);
		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;

	error = retrieve_revobject_from_reflog(out, ref, repo, git_buf_cstr(&identifier), (unsigned int)timestamp);

cleanup:
	git_buf_free(&identifier);
	return error;
388 389
}

390 391
static git_otype parse_obj_type(const char *str)
{
392 393 394 395 396 397 398 399 400 401 402 403
	if (!strcmp(str, "commit"))
		return GIT_OBJ_COMMIT;

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

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

404
	return GIT_OBJ_BAD;
405 406
}

407
static int dereference_to_non_tag(git_object **out, git_object *obj)
408
{
409 410
	if (git_object_type(obj) == GIT_OBJ_TAG)
		return git_tag_peel(out, (git_tag *)obj);
411

412 413
	return git_object__dup(out, obj);
}
414

415 416 417 418
static int handle_caret_parent_syntax(git_object **out, git_object *obj, int n)
{
	git_object *temp_commit = NULL;
	int error;
419

420
	if (git_object_peel(&temp_commit, obj, GIT_OBJ_COMMIT) < 0)
421
		return -1;
422 423

	if (n == 0) {
424
		*out = temp_commit;
425 426 427
		return 0;
	}

428
	error = git_commit_parent((git_commit **)out, (git_commit*)temp_commit, n - 1);
429

430 431
	git_object_free(temp_commit);
	return error;
432
}
433

434
static int handle_linear_syntax(git_object **out, git_object *obj, int n)
435
{
436 437
	git_object *temp_commit = NULL;
	int error;
438

439
	if (git_object_peel(&temp_commit, obj, GIT_OBJ_COMMIT) < 0)
440
		return -1;
441

442
	error = git_commit_nth_gen_ancestor((git_commit **)out, (git_commit*)temp_commit, n);
443

444 445
	git_object_free(temp_commit);
	return error;
446 447
}

448 449
static int handle_colon_syntax(
	git_object **out,
450 451
	git_object *obj,
	const char *path)
452
{
453
	git_object *tree;
454 455
	int error = -1;
	git_tree_entry *entry = NULL;
456

457
	if (git_object_peel(&tree, obj, GIT_OBJ_TREE) < 0)
458
		return -1;
459

460 461 462 463
	if (*path == '\0') {
		*out = tree;
		return 0;
	}
464

465 466 467 468 469 470 471
	/*
	 * 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;

472
	error = git_tree_entry_to_object(out, git_object_owner(tree), entry);
473

474 475
cleanup:
	git_tree_entry_free(entry);
476
	git_object_free(tree);
477 478

	return error;
479 480
}

481
static int walk_and_search(git_object **out, git_revwalk *walk, regex_t *regex)
482
{
483 484 485 486 487
	int error;
	git_oid oid;
	git_object *obj;
	
	while (!(error = git_revwalk_next(&oid, walk))) {
488

489 490 491 492 493 494 495 496 497 498
		if ((error = git_object_lookup(&obj, git_revwalk_repository(walk), &oid, GIT_OBJ_COMMIT) < 0) &&
			(error != GIT_ENOTFOUND))
			return -1;

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

		git_object_free(obj);
499 500
	}

Russell Belfer committed
501
	if (error < 0 && error == GIT_ITEROVER)
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
		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;
	int error = -1;

	if (build_regex(&preg, pattern) < 0)
		return -1;
		
	if (git_revwalk_new(&walk, repo) < 0)
		goto cleanup;

	git_revwalk_sorting(walk, GIT_SORT_TIME);

	if (spec_oid == NULL) {
		// TODO: @carlosmn: The glob should be refs/* but this makes git_revwalk_next() fails
523
		if (git_revwalk_push_glob(walk, GIT_REFS_HEADS_DIR "*") < 0)
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
			goto cleanup;
	} else if (git_revwalk_push(walk, spec_oid) < 0)
			goto cleanup;

	error = walk_and_search(out, walk, &preg);
		
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)
		return -1;

552
	return git_object_peel(out, obj, expected_type);
553 554
}

555
static int extract_curly_braces_content(git_buf *buf, const char *spec, size_t *pos)
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
{
	git_buf_clear(buf);

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

	(*pos)++;

	if (spec[*pos] == '\0' || spec[*pos] != '{')
		return revspec_error(spec);

	(*pos)++;

	while (spec[*pos] != '}') {
		if (spec[*pos] == '\0')
			return revspec_error(spec);

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

	(*pos)++;

	return 0;
}

580
static int extract_path(git_buf *buf, const char *spec, size_t *pos)
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
{
	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;
}

596
static int extract_how_many(int *n, const char *spec, size_t *pos)
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
{
	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])) {
			if ((git__strtol32(&parsed, spec + *pos, &end_ptr, 10) < 0) < 0)
				return revspec_error(spec);

			accumulated += (parsed - 1);
			*pos = end_ptr - spec;
618
		}
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640

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

	error = git_object_lookup(object, reference->owner, git_reference_oid(resolved), GIT_OBJ_ANY);
	git_reference_free(resolved);

	return error;
}

641
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)
642 643 644 645 646 647 648 649 650 651 652 653 654 655
{
	int error;
	git_buf identifier = GIT_BUF_INIT;

	if (*object != NULL)
		return 0;

	if (*reference != NULL) {
		if ((error = object_from_reference(object, *reference)) < 0)
			return error;

		git_reference_free(*reference);
		*reference = NULL;
		return 0;
656 657
	}

658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
	if (!allow_empty_identifier && identifier_len == 0)
		return revspec_error(spec);

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

	error = revparse_lookup_object(object, repo, git_buf_cstr(&identifier));
	git_buf_free(&identifier);

	return error;
}

static int ensure_base_rev_is_not_known_yet(git_object *object, const char *spec)
{
	if (object == NULL)
		return 0;

	return revspec_error(spec);
}

678
static bool any_left_hand_identifier(git_object *object, git_reference *reference, size_t identifier_len)
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
{
	if (object != NULL)
		return true;

	if (reference != NULL)
		return true;

	if (identifier_len > 0)
		return true;

	return false;
}

static int ensure_left_hand_identifier_is_not_known_yet(git_object *object, git_reference *reference, const char *spec)
{
	if (!ensure_base_rev_is_not_known_yet(object, spec) && reference == NULL)
		return 0;

	return revspec_error(spec);
698 699
}

700 701
int git_revparse_single(git_object **out, git_repository *repo, const char *spec)
{
702
	size_t pos = 0, identifier_len = 0;
703 704 705 706 707
	int error = -1, n;
	git_buf buf = GIT_BUF_INIT;

	git_reference *reference = NULL;
	git_object *base_rev = NULL;
708 709 710

	assert(out && repo && spec);

711
	*out = NULL;
712

713
	while (spec[pos]) {
714 715 716 717
		switch (spec[pos]) {
		case '^':
			if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0)
				goto cleanup;
718

719 720
			if (spec[pos+1] == '{') {
				git_object *temp_object = NULL;
721

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

725 726 727 728 729
				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;
730
			} else {
731 732 733 734 735 736 737 738 739 740
				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;
741 742 743
			}
			break;

744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
		case '~':
		{
			git_object *temp_object = NULL;

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

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

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

			git_object_free(base_rev);
			base_rev = temp_object;
759
			break;
760
		}
761

762 763 764 765 766 767 768 769 770 771 772 773 774
		case ':':
		{
			git_object *temp_object = NULL;

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

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

				if ((error = handle_colon_syntax(&temp_object, base_rev, git_buf_cstr(&buf))) < 0)
					goto cleanup;
775
			} else {
776 777 778 779 780 781 782 783 784 785 786 787 788
				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)
					 */
					giterr_set(GITERR_INVALID, "Unimplemented");
					error = GIT_ERROR;
					goto cleanup;
				}
789
			}
790 791 792

			git_object_free(base_rev);
			base_rev = temp_object;
793
			break;
794 795 796 797
		}

		case '@':
		{
798 799
			if (spec[pos+1] == '{') {
				git_object *temp_object = NULL;
800

801 802
				if ((error = extract_curly_braces_content(&buf, spec, &pos)) < 0)
					goto cleanup;
803

804 805
				if ((error = ensure_base_rev_is_not_known_yet(base_rev, spec)) < 0)
					goto cleanup;
806

807 808
				if ((error = handle_at_syntax(&temp_object, &reference, spec, identifier_len, repo, git_buf_cstr(&buf))) < 0)
					goto cleanup;
809

810 811 812 813 814 815
				if (temp_object != NULL)
					base_rev = temp_object;
				break;
			} else {
				/* Fall through */
			}
816 817
		}

818 819 820 821 822 823
		default:
			if ((error = ensure_left_hand_identifier_is_not_known_yet(base_rev, reference, spec)) < 0)
				goto cleanup;

			pos++;
			identifier_len++;
824
		}
825
	}
826

827
	if ((error = ensure_base_rev_loaded(&base_rev, &reference, spec, identifier_len, repo, false)) < 0)
828 829 830 831
		goto cleanup;

	*out = base_rev;
	error = 0;
832

833 834 835 836 837 838
cleanup:
	if (error)
		git_object_free(base_rev);
	git_reference_free(reference);
	git_buf_free(&buf);
	return error;
839
}