revparse.c 20.2 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
#include "regexp.h"
16

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

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

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

26
	return git_object_lookup_prefix(out, repo, &oid, speclen, GIT_OBJECT_ANY);
27
}
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
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);
}

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

	if (*pattern == '\0') {
51
		git_error_set(GIT_ERROR_REGEX, "empty pattern");
52
		return GIT_EINVALIDSPEC;
53 54
	}

55
	error = git_regexp_compile(regex, pattern, 0);
56 57
	if (!error)
		return 0;
58

59
	git_regexp_dispose(regex);
60

61
	return error;
62 63
}

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

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

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

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

78 79
	error = git_regexp_match(&regex, spec);
	git_regexp_dispose(&regex);
80 81

	if (error)
82 83
		return GIT_ENOTFOUND;

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

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

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

99
	error = git_reference_dwim(&ref, repo, spec);
100
	if (!error) {
101 102

		error = git_object_lookup(
103
			object_out, repo, git_reference_target(ref), GIT_OBJECT_ANY);
104 105 106 107

		if (!error)
			*reference_out = ref;

108
		return error;
109 110
	}

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

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

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

121
	git_error_set(GIT_ERROR_REFERENCE, "revspec '%s' not found", spec);
122
	return GIT_ENOTFOUND;
123 124
}

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

130 131
	if (git__strntol32(&content, curly_braces_content, strlen(curly_braces_content),
			   &end_ptr, 10) < 0)
132
		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
	git_regexp preg;
146 147
	int error = -1;
	size_t i, numentries, cur;
148
	const git_reflog_entry *entry;
149
	const char *msg;
150 151
	git_buf buf = GIT_BUF_INIT;

152
	cur = position;
153

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

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

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

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

	numentries  = git_reflog_entrycount(reflog);

168
	for (i = 0; i < numentries; i++) {
169 170
		git_regmatch regexmatches[2];

171
		entry = git_reflog_entry_byindex(reflog, i);
172
		msg = git_reflog_entry_message(entry);
173 174
		if (!msg)
			continue;
175

176
		if (git_regexp_search(&preg, msg, 2, regexmatches) < 0)
177 178 179 180 181 182
			continue;

		cur--;

		if (cur > 0)
			continue;
183

184 185
		if ((git_buf_put(&buf, msg+regexmatches[1].start, regexmatches[1].end - regexmatches[1].start)) < 0)
			goto cleanup;
186

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

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

193
		error = maybe_abbrev(out, repo, git_buf_cstr(&buf));
194 195 196

		goto cleanup;
	}
197

198 199 200 201
	error = GIT_ENOTFOUND;

cleanup:
	git_reference_free(ref);
202
	git_buf_dispose(&buf);
203
	git_regexp_dispose(&preg);
204 205 206 207
	git_reflog_free(reflog);
	return error;
}

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

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

218
	numentries = git_reflog_entrycount(reflog);
219 220

	if (search_by_pos) {
221 222
		if (numentries < identifier + 1)
			goto notfound;
223 224

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

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

			if (commit_time.time > (git_time_t)identifier)
235
				continue;
236

237
			git_oid_cpy(oid, git_reflog_entry_id_new(entry));
238
			break;
239 240
		}

241 242
		if (i == numentries)
			goto notfound;
243
	}
244

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

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

	git_reflog_free(reflog);
	return GIT_ENOTFOUND;
256
}
257

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

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

272
	if (position == 0) {
273
		error = git_object_lookup(out, repo, git_reference_target(ref), GIT_OBJECT_ANY);
274 275
		goto cleanup;
	}
276

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

280
	error = git_object_lookup(out, repo, &oid, GIT_OBJECT_ANY);
281 282 283 284 285 286 287 288 289 290 291 292

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) {
293
		if ((error = git_reference_dwim(&ref, repo, identifier)) < 0)
294 295 296 297
			return error;
	} else {
		ref = *base_ref;
		*base_ref = NULL;
298 299
	}

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

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

308 309
	*base_ref = tracking;

310
cleanup:
311 312 313 314
	git_reference_free(ref);
	return error;
}

315
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)
316 317
{
	bool is_numeric;
318
	int parsed = 0, error = -1;
319 320 321 322 323 324 325 326 327 328 329
	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)) {
330
		error = GIT_EINVALIDSPEC;
331 332 333 334 335
		goto cleanup;
	}

	if (is_numeric) {
		if (parsed < 0)
336
			error = retrieve_previously_checked_out_branch_or_revision(out, ref, repo, git_buf_cstr(&identifier), -parsed);
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
		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;

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

cleanup:
355
	git_buf_dispose(&identifier);
356
	return error;
357 358
}

359
static git_object_t parse_obj_type(const char *str)
360
{
361
	if (!strcmp(str, "commit"))
362
		return GIT_OBJECT_COMMIT;
363 364

	if (!strcmp(str, "tree"))
365
		return GIT_OBJECT_TREE;
366

367
	if (!strcmp(str, "blob"))
368
		return GIT_OBJECT_BLOB;
369 370

	if (!strcmp(str, "tag"))
371
		return GIT_OBJECT_TAG;
372

373
	return GIT_OBJECT_INVALID;
374 375
}

376
static int dereference_to_non_tag(git_object **out, git_object *obj)
377
{
378
	if (git_object_type(obj) == GIT_OBJECT_TAG)
379
		return git_tag_peel(out, (git_tag *)obj);
380

381
	return git_object_dup(out, obj);
382
}
383

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

389
	if ((error = git_object_peel(&temp_commit, obj, GIT_OBJECT_COMMIT)) < 0)
390 391
		return (error == GIT_EAMBIGUOUS || error == GIT_ENOTFOUND) ?
			GIT_EINVALIDSPEC : error;
392 393

	if (n == 0) {
394
		*out = temp_commit;
395 396 397
		return 0;
	}

398
	error = git_commit_parent((git_commit **)out, (git_commit*)temp_commit, n - 1);
399

400 401
	git_object_free(temp_commit);
	return error;
402
}
403

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

409
	if ((error = git_object_peel(&temp_commit, obj, GIT_OBJECT_COMMIT)) < 0)
410 411
		return (error == GIT_EAMBIGUOUS || error == GIT_ENOTFOUND) ?
			GIT_EINVALIDSPEC : error;
412

413
	error = git_commit_nth_gen_ancestor((git_commit **)out, (git_commit*)temp_commit, n);
414

415 416
	git_object_free(temp_commit);
	return error;
417 418
}

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

428
	if ((error = git_object_peel(&tree, obj, GIT_OBJECT_TREE)) < 0)
429
		return error == GIT_ENOTFOUND ? GIT_EINVALIDSPEC : error;
430

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

436 437 438 439 440 441 442
	/*
	 * 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;

443
	error = git_tree_entry_to_object(out, git_object_owner(tree), entry);
444

445 446
cleanup:
	git_tree_entry_free(entry);
447
	git_object_free(tree);
448 449

	return error;
450 451
}

452
static int walk_and_search(git_object **out, git_revwalk *walk, git_regexp *regex)
453
{
454 455 456
	int error;
	git_oid oid;
	git_object *obj;
457

458
	while (!(error = git_revwalk_next(&oid, walk))) {
459

460
		error = git_object_lookup(&obj, git_revwalk_repository(walk), &oid, GIT_OBJECT_COMMIT);
461
		if ((error < 0) && (error != GIT_ENOTFOUND))
462 463
			return -1;

464
		if (!git_regexp_match(regex, git_commit_message((git_commit*)obj))) {
465 466 467 468 469
			*out = obj;
			return 0;
		}

		git_object_free(obj);
470 471
	}

Russell Belfer committed
472
	if (error < 0 && error == GIT_ITEROVER)
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)
{
480
	git_regexp preg;
481
	git_revwalk *walk = NULL;
482
	int error;
483

484 485
	if ((error = build_regex(&preg, pattern)) < 0)
		return error;
486

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

	git_revwalk_sorting(walk, GIT_SORT_TIME);

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

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

500
cleanup:
501
	git_regexp_dispose(&preg);
502 503 504 505 506 507 508
	git_revwalk_free(walk);

	return error;
}

static int handle_caret_curly_syntax(git_object **out, git_object *obj, const char *curly_braces_content)
{
509
	git_object_t expected_type;
510 511 512 513 514 515 516 517 518

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

519
	if (expected_type == GIT_OBJECT_INVALID)
520
		return GIT_EINVALIDSPEC;
521

522
	return git_object_peel(out, obj, expected_type);
523 524
}

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

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

	(*pos)++;

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

	(*pos)++;

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

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

	(*pos)++;

	return 0;
}

550
static int extract_path(git_buf *buf, const char *spec, size_t *pos)
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
{
	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;
}

566
static int extract_how_many(int *n, const char *spec, size_t *pos)
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
{
	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])) {
583
			if (git__strntol32(&parsed, spec + *pos, strlen(spec + *pos), &end_ptr, 10) < 0)
584
				return GIT_EINVALIDSPEC;
585 586 587

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

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

605
	error = git_object_lookup(object, reference->db->repo, git_reference_target(resolved), GIT_OBJECT_ANY);
606 607 608 609 610
	git_reference_free(resolved);

	return error;
}

611
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)
612 613 614 615 616 617 618
{
	int error;
	git_buf identifier = GIT_BUF_INIT;

	if (*object != NULL)
		return 0;

619 620
	if (*reference != NULL)
		return object_from_reference(object, *reference);
621

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

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

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

	return error;
}

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

639
	return GIT_EINVALIDSPEC;
640 641
}

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

	if (reference != NULL)
		return true;

	if (identifier_len > 0)
		return true;

	return false;
}

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

661
	return GIT_EINVALIDSPEC;
662 663
}

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

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

678 679
	bool should_return_reference = true;

680
	assert(object_out && reference_out && repo && spec);
681

682 683
	*object_out = NULL;
	*reference_out = NULL;
684

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

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

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

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

699 700 701 702 703
				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;
704
			} else {
705 706 707 708 709 710 711 712 713 714
				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;
715 716 717
			}
			break;

718 719 720 721
		case '~':
		{
			git_object *temp_object = NULL;

722 723
			should_return_reference = false;

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

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

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

			git_object_free(base_rev);
			base_rev = temp_object;
735
			break;
736
		}
737

738 739 740 741
		case ':':
		{
			git_object *temp_object = NULL;

742 743
			should_return_reference = false;

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

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

				if ((error = handle_colon_syntax(&temp_object, base_rev, git_buf_cstr(&buf))) < 0)
					goto cleanup;
753
			} else {
754 755 756 757 758 759 760 761 762
				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)
					 */
763
					git_error_set(GIT_ERROR_INVALID, "unimplemented");
764 765 766
					error = GIT_ERROR;
					goto cleanup;
				}
767
			}
768 769 770

			git_object_free(base_rev);
			base_rev = temp_object;
771
			break;
772 773 774
		}

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

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

781
				if ((error = ensure_base_rev_is_not_known_yet(base_rev)) < 0)
782
					goto cleanup;
783

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

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

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

			pos++;
			identifier_len++;
799
		}
800
	}
801

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

805 806 807 808 809
	if (!should_return_reference) {
		git_reference_free(reference);
		reference = NULL;
	}

810 811 812
	*object_out = base_rev;
	*reference_out = reference;
	*identifier_len_out = identifier_len;
813
	error = 0;
814

815
cleanup:
816 817
	if (error) {
		if (error == GIT_EINVALIDSPEC)
818
			git_error_set(GIT_ERROR_INVALID,
819
				"failed to parse revision specifier - Invalid pattern '%s'", spec);
820

821
		git_object_free(base_rev);
822
		git_reference_free(reference);
823
	}
824

825
	git_buf_dispose(&buf);
826
	return error;
827
}
828

829 830 831 832 833 834
int git_revparse_ext(
	git_object **object_out,
	git_reference **reference_out,
	git_repository *repo,
	const char *spec)
{
Russell Belfer committed
835 836
	int error;
	size_t identifier_len;
837 838 839 840 841 842 843 844
	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
845
	GIT_UNUSED(identifier_len);
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 875 876

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

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

886
	assert(revspec && repo && spec);
Vicent Marti committed
887

888
	memset(revspec, 0x0, sizeof(*revspec));
889 890 891 892

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

895 896 897 898 899 900 901
		/*
		 * 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, "..")) {
902
			git_error_set(GIT_ERROR_INVALID, "Invalid pattern '..'");
903 904 905
			return GIT_EINVALIDSPEC;
		}

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

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

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

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

	return error;
}