revparse.c 27.8 KB
Newer Older
1 2 3
#include "clar_libgit2.h"

#include "git2/revparse.h"
4 5
#include "refs.h"
#include "path.h"
6 7

static git_repository *g_repo;
8
static git_object *g_obj;
9

10
/* Helpers */
11 12 13 14 15 16
static void test_object_and_ref_inrepo(
	const char *spec,
	const char *expected_oid,
	const char *expected_refname,
	git_repository *repo,
	bool assert_reference_retrieval)
17
{
18
	char objstr[64] = {0};
19
	git_object *obj = NULL;
20
	git_reference *ref = NULL;
21
	int error;
22

23
	error = git_revparse_ext(&obj, &ref, repo, spec);
24

25
	if (expected_oid != NULL) {
26
		cl_git_pass(error);
27
		git_oid_fmt(objstr, git_object_id(obj));
28 29
		cl_assert_equal_s(objstr, expected_oid);
	} else
30
		cl_git_fail(error);
31

32 33 34 35 36 37 38
	if (assert_reference_retrieval) {
		if (expected_refname == NULL)
			cl_assert(NULL == ref);
		else
			cl_assert_equal_s(expected_refname, git_reference_name(ref));
	}

39
	git_object_free(obj);
40 41 42 43 44 45
	git_reference_free(ref);
}

static void test_object_inrepo(const char *spec, const char *expected_oid, git_repository *repo)
{
	test_object_and_ref_inrepo(spec, expected_oid, NULL, repo, false);
46
}
47

48 49 50 51
static void test_id_inrepo(
	const char *spec,
	const char *expected_left,
	const char *expected_right,
52
	git_revspec_t expected_flags,
53 54
	git_repository *repo)
{
55 56
	git_revspec revspec;
	int error = git_revparse(&revspec, repo, spec);
57 58 59 60

	if (expected_left) {
		char str[64] = {0};
		cl_assert_equal_i(0, error);
61
		git_oid_fmt(str, git_object_id(revspec.from));
62
		cl_assert_equal_s(str, expected_left);
63
		git_object_free(revspec.from);
64 65 66 67 68 69
	} else {
		cl_assert_equal_i(GIT_ENOTFOUND, error);
	}

	if (expected_right) {
		char str[64] = {0};
70
		git_oid_fmt(str, git_object_id(revspec.to));
71
		cl_assert_equal_s(str, expected_right);
72
		git_object_free(revspec.to);
73 74 75
	}

	if (expected_flags)
76
		cl_assert_equal_i(expected_flags, revspec.flags);
77 78
}

79 80 81 82 83
static void test_object(const char *spec, const char *expected_oid)
{
	test_object_inrepo(spec, expected_oid, g_repo);
}

84 85 86 87 88
static void test_object_and_ref(const char *spec, const char *expected_oid, const char *expected_refname)
{
	test_object_and_ref_inrepo(spec, expected_oid, expected_refname, g_repo, true);
}

89 90 91
static void test_rangelike(const char *rangelike,
						   const char *expected_left,
						   const char *expected_right,
92
						   git_revspec_t expected_revparseflags)
93 94
{
	char objstr[64] = {0};
95
	git_revspec revspec;
96 97
	int error;

98
	error = git_revparse(&revspec, g_repo, rangelike);
99 100 101

	if (expected_left != NULL) {
		cl_assert_equal_i(0, error);
102 103
		cl_assert_equal_i(revspec.flags, expected_revparseflags);
		git_oid_fmt(objstr, git_object_id(revspec.from));
104
		cl_assert_equal_s(objstr, expected_left);
105
		git_oid_fmt(objstr, git_object_id(revspec.to));
106 107 108
		cl_assert_equal_s(objstr, expected_right);
	} else
		cl_assert(error != 0);
109

110 111
	git_object_free(revspec.from);
	git_object_free(revspec.to);
112 113 114
}


115 116 117 118
static void test_id(
	const char *spec,
	const char *expected_left,
	const char *expected_right,
119
	git_revspec_t expected_flags)
120 121 122 123
{
	test_id_inrepo(spec, expected_left, expected_right, expected_flags, g_repo);
}

124 125 126 127 128 129 130 131
static void test_invalid_revspec(const char* invalid_spec)
{
	git_revspec revspec;

	cl_assert_equal_i(
		GIT_EINVALIDSPEC, git_revparse(&revspec, g_repo, invalid_spec));
}

132 133
void test_refs_revparse__initialize(void)
{
134
	cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
135 136 137 138
}

void test_refs_revparse__cleanup(void)
{
139
	git_repository_free(g_repo);
140 141
}

142 143
void test_refs_revparse__nonexistant_object(void)
{
144
	test_object("this-does-not-exist", NULL);
145 146
	test_object("this-does-not-exist^1", NULL);
	test_object("this-does-not-exist~2", NULL);
147
}
148

149
static void assert_invalid_single_spec(const char *invalid_spec)
150
{
151
	cl_assert_equal_i(
152
		GIT_EINVALIDSPEC, git_revparse_single(&g_obj, g_repo, invalid_spec));
153
}
154

155 156
void test_refs_revparse__invalid_reference_name(void)
{
157 158 159
	assert_invalid_single_spec("this doesn't make sense");
	assert_invalid_single_spec("Inv@{id");
	assert_invalid_single_spec("");
160 161
}

162 163
void test_refs_revparse__shas(void)
{
164 165
	test_object("c47800c7266a2be04c571c04d5a6614691ea99bd", "c47800c7266a2be04c571c04d5a6614691ea99bd");
	test_object("c47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd");
166 167 168 169
}

void test_refs_revparse__head(void)
{
170
	test_object("HEAD", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
171 172 173
	test_object("HEAD^0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
	test_object("HEAD~0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
	test_object("master", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
174 175 176 177
}

void test_refs_revparse__full_refs(void)
{
178 179 180
	test_object("refs/heads/master", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
	test_object("refs/heads/test", "e90810b8df3e80c413d903f631643c716887138d");
	test_object("refs/tags/test", "b25fa35b38051e4ae45d4222e795f9df2e43f1d1");
181 182 183 184
}

void test_refs_revparse__partial_refs(void)
{
185 186 187
	test_object("point_to_blob", "1385f264afb75a56a5bec74243be9b367ba4ca08");
	test_object("packed-test", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045");
	test_object("br2", "a4a7dce85cf63874e984719f4fdd239f5145052f");
188 189 190 191
}

void test_refs_revparse__describe_output(void)
{
192 193
	test_object("blah-7-gc47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd");
	test_object("not-good", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
194 195
}

196 197
void test_refs_revparse__nth_parent(void)
{
198 199 200 201 202
	assert_invalid_single_spec("be3563a^-1");
	assert_invalid_single_spec("^");
	assert_invalid_single_spec("be3563a^{tree}^");
	assert_invalid_single_spec("point_to_blob^{blob}^");
	assert_invalid_single_spec("this doesn't make sense^1");
203

204 205 206 207
	test_object("be3563a^1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
	test_object("be3563a^", "9fd738e8f7967c078dceed8190330fc8648ee56a");
	test_object("be3563a^2", "c47800c7266a2be04c571c04d5a6614691ea99bd");
	test_object("be3563a^1^1", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045");
208
	test_object("be3563a^^", "4a202b346bb0fb0db7eff3cffeb3c70babbd2045");
209 210
	test_object("be3563a^2^1", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
	test_object("be3563a^0", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
211
	test_object("be3563a^{commit}^", "9fd738e8f7967c078dceed8190330fc8648ee56a");
212

213
	test_object("be3563a^42", NULL);
214 215 216 217
}

void test_refs_revparse__not_tag(void)
{
218 219
	test_object("point_to_blob^{}", "1385f264afb75a56a5bec74243be9b367ba4ca08");
	test_object("wrapped_tag^{}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
220 221 222 223 224
	test_object("master^{}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
	test_object("master^{tree}^{}", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
	test_object("e90810b^{}", "e90810b8df3e80c413d903f631643c716887138d");
	test_object("tags/e90810b^{}", "e90810b8df3e80c413d903f631643c716887138d");
	test_object("e908^{}", "e90810b8df3e80c413d903f631643c716887138d");
225 226 227 228
}

void test_refs_revparse__to_type(void)
{
229
	assert_invalid_single_spec("wrapped_tag^{trip}");
230 231
	test_object("point_to_blob^{commit}", NULL);
	cl_assert_equal_i(
232
		GIT_EPEEL, git_revparse_single(&g_obj, g_repo, "wrapped_tag^{blob}"));
233

234 235 236
	test_object("wrapped_tag^{commit}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
	test_object("wrapped_tag^{tree}", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
	test_object("point_to_blob^{blob}", "1385f264afb75a56a5bec74243be9b367ba4ca08");
237
	test_object("master^{commit}^{commit}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
238 239
}

240 241
void test_refs_revparse__linear_history(void)
{
242
	assert_invalid_single_spec("~");
243 244
	test_object("foo~bar", NULL);

245 246 247 248 249 250
	assert_invalid_single_spec("master~bar");
	assert_invalid_single_spec("master~-1");
	assert_invalid_single_spec("master~0bar");
	assert_invalid_single_spec("this doesn't make sense~2");
	assert_invalid_single_spec("be3563a^{tree}~");
	assert_invalid_single_spec("point_to_blob^{blob}~");
251

252 253 254 255
	test_object("master~0", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
	test_object("master~1", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
	test_object("master~2", "9fd738e8f7967c078dceed8190330fc8648ee56a");
	test_object("master~1~1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
256
	test_object("master~~", "9fd738e8f7967c078dceed8190330fc8648ee56a");
257 258 259 260
}

void test_refs_revparse__chaining(void)
{
261 262 263 264
	assert_invalid_single_spec("master@{0}@{0}");
	assert_invalid_single_spec("@{u}@{-1}");
	assert_invalid_single_spec("@{-1}@{-1}");
	assert_invalid_single_spec("@{-3}@{0}");
265 266 267 268 269

	test_object("master@{0}~1^1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
	test_object("@{u}@{0}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
	test_object("@{-1}@{0}", "a4a7dce85cf63874e984719f4fdd239f5145052f");
	test_object("@{-4}@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
270 271 272
	test_object("master~1^1", "9fd738e8f7967c078dceed8190330fc8648ee56a");
	test_object("master~1^2", "c47800c7266a2be04c571c04d5a6614691ea99bd");
	test_object("master^1^2~1", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
273
	test_object("master^^2^", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
274
	test_object("master^1^1^1^1^1", "8496071c1b46c854b31185ea97743be6a8774479");
275
	test_object("master^^1^2^1", NULL);
276 277
}

278 279
void test_refs_revparse__upstream(void)
{
280 281
	assert_invalid_single_spec("e90810b@{u}");
	assert_invalid_single_spec("refs/tags/e90810b@{u}");
282
	test_object("refs/heads/e90810b@{u}", NULL);
283 284 285 286 287 288 289 290

	test_object("master@{upstream}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
	test_object("@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
	test_object("master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
	test_object("heads/master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
	test_object("refs/heads/master@{u}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
}

291
void test_refs_revparse__ordinal(void)
292
{
293
	assert_invalid_single_spec("master@{-2}");
294

295
	/* TODO: make the test below actually fail
296
	 * cl_git_fail(git_revparse_single(&g_obj, g_repo, "master@{1a}"));
297
	 */
298

299 300
	test_object("nope@{0}", NULL);
	test_object("master@{31415}", NULL);
301
	test_object("@{1000}", NULL);
302
	test_object("@{2}", NULL);
303

304 305
	test_object("@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
	test_object("@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
306 307 308 309 310 311 312 313 314

	test_object("master@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
	test_object("master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
	test_object("heads/master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
	test_object("refs/heads/master@{1}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
}

void test_refs_revparse__previous_head(void)
{
315 316 317
	assert_invalid_single_spec("@{-xyz}");
	assert_invalid_single_spec("@{-0}");
	assert_invalid_single_spec("@{-1b}");
318 319 320 321 322

	test_object("@{-42}", NULL);

	test_object("@{-2}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
	test_object("@{-1}", "a4a7dce85cf63874e984719f4fdd239f5145052f");
323 324
}

325 326
static void create_fake_stash_reference_and_reflog(git_repository *repo)
{
327
	git_reference *master, *new_master;
328
	git_str log_path = GIT_STR_INIT;
329

330
	git_str_joinpath(&log_path, git_repository_path(repo), "logs/refs/fakestash");
331

332
	cl_assert_equal_i(false, git_fs_path_isfile(git_str_cstr(&log_path)));
333 334

	cl_git_pass(git_reference_lookup(&master, repo, "refs/heads/master"));
335
	cl_git_pass(git_reference_rename(&new_master, master, "refs/fakestash", 0, NULL));
336
	git_reference_free(master);
337

338
	cl_assert_equal_i(true, git_fs_path_isfile(git_str_cstr(&log_path)));
339

340
	git_str_dispose(&log_path);
341
	git_reference_free(new_master);
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
}

void test_refs_revparse__reflog_of_a_ref_under_refs(void)
{
	git_repository *repo = cl_git_sandbox_init("testrepo.git");

	test_object_inrepo("refs/fakestash", NULL, repo);

	create_fake_stash_reference_and_reflog(repo);

	/*
	 * $ git reflog -1 refs/fakestash
	 * a65fedf refs/fakestash@{0}: commit: checking in
	 *
	 * $ git reflog -1 refs/fakestash@{0}
	 * a65fedf refs/fakestash@{0}: commit: checking in
	 *
	 * $ git reflog -1 fakestash
	 * a65fedf fakestash@{0}: commit: checking in
	 *
	 * $ git reflog -1 fakestash@{0}
	 * a65fedf fakestash@{0}: commit: checking in
	 */
	test_object_inrepo("refs/fakestash", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
	test_object_inrepo("refs/fakestash@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
	test_object_inrepo("fakestash", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
	test_object_inrepo("fakestash@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);

	cl_git_sandbox_cleanup();
}

373 374
void test_refs_revparse__revwalk(void)
{
375 376
	test_object("master^{/not found in any commit}", NULL);
	test_object("master^{/merge}", NULL);
377
	assert_invalid_single_spec("master^{/((}");
378

379 380 381 382
	test_object("master^{/anoth}", "5b5b025afb0b4c913b4c338a42934a3863bf3644");
	test_object("master^{/Merge}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
	test_object("br2^{/Merge}", "a4a7dce85cf63874e984719f4fdd239f5145052f");
	test_object("master^{/fo.rth}", "9fd738e8f7967c078dceed8190330fc8648ee56a");
383 384 385 386
}

void test_refs_revparse__date(void)
{
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
	/*
	 * $ git reflog HEAD --date=iso
	 * a65fedf HEAD@{2012-04-30 08:23:41 -0900}: checkout: moving from br2 to master
	 * a4a7dce HEAD@{2012-04-30 08:23:37 -0900}: commit: checking in
	 * c47800c HEAD@{2012-04-30 08:23:28 -0900}: checkout: moving from master to br2
	 * a65fedf HEAD@{2012-04-30 08:23:23 -0900}: commit:
	 * be3563a HEAD@{2012-04-30 10:22:43 -0700}: clone: from /Users/ben/src/libgit2/tes
	 *
	 * $ git reflog HEAD --date=raw
	 * a65fedf HEAD@{1335806621 -0900}: checkout: moving from br2 to master
	 * a4a7dce HEAD@{1335806617 -0900}: commit: checking in
	 * c47800c HEAD@{1335806608 -0900}: checkout: moving from master to br2
	 * a65fedf HEAD@{1335806603 -0900}: commit:
	 * be3563a HEAD@{1335806563 -0700}: clone: from /Users/ben/src/libgit2/tests/resour
	 */
	test_object("HEAD@{1 second}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
	test_object("HEAD@{1 second ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
	test_object("HEAD@{2 days ago}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");

	/*
	 * $ git reflog master --date=iso
	 * a65fedf master@{2012-04-30 09:23:23 -0800}: commit: checking in
	 * be3563a master@{2012-04-30 09:22:43 -0800}: clone: from /Users/ben/src...
	 *
	 * $ git reflog master --date=raw
	 * a65fedf master@{1335806603 -0800}: commit: checking in
	 * be3563a master@{1335806563 -0800}: clone: from /Users/ben/src/libgit2/tests/reso
	 */


	/*
418 419 420
	 * $ git rev-parse "master@{2012-04-30 17:22:42 +0000}"
	 * warning: log for 'master' only goes back to Mon, 30 Apr 2012 09:22:43 -0800
	 * be3563ae3f795b2b4353bcce3a527ad0a4f7f644
421
	 */
422 423
	test_object("master@{2012-04-30 17:22:42 +0000}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
	test_object("master@{2012-04-30 09:22:42 -0800}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452

	/*
	 * $ git reflog -1 "master@{2012-04-30 17:22:43 +0000}"
	 * be3563a master@{Mon Apr 30 09:22:43 2012 -0800}: clone: from /Users/ben/src/libg
	 */
	test_object("master@{2012-04-30 17:22:43 +0000}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
	test_object("master@{2012-04-30 09:22:43 -0800}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");

	/*
	 * $ git reflog -1 "master@{2012-4-30 09:23:27 -0800}"
	 * a65fedf master@{Mon Apr 30 09:23:23 2012 -0800}: commit: checking in
	 */
	test_object("master@{2012-4-30 09:23:27 -0800}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");

	/*
	 * $ git reflog -1 master@{2012-05-03}
	 * a65fedf master@{Mon Apr 30 09:23:23 2012 -0800}: commit: checking in
	 */
	test_object("master@{2012-05-03}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");

	/*
	 * $ git reflog -1 "master@{1335806603}"
	 * a65fedf
	 *
	 * $ git reflog -1 "master@{1335806602}"
	 * be3563a
	 */
	test_object("master@{1335806603}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
	test_object("master@{1335806602}", "be3563ae3f795b2b4353bcce3a527ad0a4f7f644");
453 454 455 456 457 458

	/*
	 * $ git rev-parse "with-empty-log@{2 days ago}" --
	 * fatal: log for refs/heads/with-empty-log is empty
	 */
	test_object("with-empty-log@{2 days ago}", NULL);
459
}
460

461 462 463 464 465 466 467 468 469 470 471 472 473
void test_refs_revparse__invalid_date(void)
{
	/*
	 * $ git rev-parse HEAD@{} --
	 * fatal: bad revision 'HEAD@{}'
	 *
	 * $ git rev-parse HEAD@{NEITHER_INTEGER_NOR_DATETIME} --
	 * fatal: bad revision 'HEAD@{NEITHER_INTEGER_NOR_DATETIME}'
	 */
	test_object("HEAD@{}", NULL);
	test_object("HEAD@{NEITHER_INTEGER_NOR_DATETIME}", NULL);
}

474 475
void test_refs_revparse__colon(void)
{
476 477 478
	assert_invalid_single_spec(":/");
	assert_invalid_single_spec("point_to_blob:readme.txt");
	cl_git_fail(git_revparse_single(&g_obj, g_repo, ":2:README")); /* Not implemented  */
479

480 481 482 483 484
	test_object(":/not found in any commit", NULL);
	test_object("subtrees:ab/42.txt", NULL);
	test_object("subtrees:ab/4.txt/nope", NULL);
	test_object("subtrees:nope", NULL);
	test_object("test/master^1:branch_file.txt", NULL);
485

486 487 488 489 490 491 492 493 494 495 496 497 498 499
	/* From tags */
	test_object("test:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf");
	test_object("tags/test:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf");
	test_object("e90810b:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf");
	test_object("tags/e90810b:readme.txt", "0266163a49e280c4f5ed1e08facd36a2bd716bcf");

	/* From commits */
	test_object("a65f:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc");

	/* From trees */
	test_object("a65f^{tree}:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc");
	test_object("944c:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc");

	/* Retrieving trees */
500 501 502
	test_object("master:", "944c0f6e4dfa41595e6eb3ceecdb14f50fe18162");
	test_object("subtrees:", "ae90f12eea699729ed24555e40b9fd669da12a12");
	test_object("subtrees:ab", "f1425cef211cc08caa31e7b545ffb232acb098c3");
503
	test_object("subtrees:ab/", "f1425cef211cc08caa31e7b545ffb232acb098c3");
504

505
	/* Retrieving blobs */
506 507 508 509 510 511 512
	test_object("subtrees:ab/4.txt", "d6c93164c249c8000205dd4ec5cbca1b516d487f");
	test_object("subtrees:ab/de/fgh/1.txt", "1f67fc4386b2d171e0d21be1c447e12660561f9b");
	test_object("master:README", "a8233120f6ad708f843d861ce2b7228ec4e3dec6");
	test_object("master:new.txt", "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd");
	test_object(":/Merge", "a4a7dce85cf63874e984719f4fdd239f5145052f");
	test_object(":/one", "c47800c7266a2be04c571c04d5a6614691ea99bd");
	test_object(":/packed commit t", "41bc8c69075bbdb46c5c6f0566cc8cc5b46e8bd9");
513
	test_object("test/master^2:branch_file.txt", "45b983be36b73c0788dc9cbcb76cbb80fc7bb057");
514
	test_object("test/master@{1}:branch_file.txt", "3697d64be941a53d4ae8f6a271e4e3fa56b022cc");
515
}
516 517 518 519 520 521 522 523 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 552 553 554 555 556 557 558 559 560 561 562 563 564

void test_refs_revparse__disambiguation(void)
{
	/*
	 * $ git show e90810b
	 * tag e90810b
	 * Tagger: Vicent Marti <tanoku@gmail.com>
	 * Date:   Thu Aug 12 03:59:17 2010 +0200
	 *
	 * This is a very simple tag.
	 *
	 * commit e90810b8df3e80c413d903f631643c716887138d
	 * Author: Vicent Marti <tanoku@gmail.com>
	 * Date:   Thu Aug 5 18:42:20 2010 +0200
	 *
	 *     Test commit 2
	 *
	 * diff --git a/readme.txt b/readme.txt
	 * index 6336846..0266163 100644
	 * --- a/readme.txt
	 * +++ b/readme.txt
	 * @@ -1 +1,2 @@
	 *  Testing a readme.txt
	 * +Now we add a single line here
	 *
	 * $ git show-ref e90810b
	 * 7b4384978d2493e851f9cca7858815fac9b10980 refs/tags/e90810b
	 *
	 */
	test_object("e90810b", "7b4384978d2493e851f9cca7858815fac9b10980");

	/*
	 * $ git show e90810
	 * commit e90810b8df3e80c413d903f631643c716887138d
	 * Author: Vicent Marti <tanoku@gmail.com>
	 * Date:   Thu Aug 5 18:42:20 2010 +0200
	 *
	 *     Test commit 2
	 *
	 * diff --git a/readme.txt b/readme.txt
	 * index 6336846..0266163 100644
	 * --- a/readme.txt
	 * +++ b/readme.txt
	 * @@ -1 +1,2 @@
	 *  Testing a readme.txt
	 * +Now we add a single line here
	 */
	test_object("e90810", "e90810b8df3e80c413d903f631643c716887138d");
}
Ben Straub committed
565 566 567

void test_refs_revparse__a_too_short_objectid_returns_EAMBIGUOUS(void)
{
568
	cl_assert_equal_i(
569
		GIT_EAMBIGUOUS, git_revparse_single(&g_obj, g_repo, "e90"));
Ben Straub committed
570
}
571

572 573 574
/*
 * $ echo "aabqhq" | git hash-object -t blob --stdin
 * dea509d0b3cb8ee0650f6ca210bc83f4678851ba
575
 *
576 577 578 579 580 581 582 583 584 585 586 587 588
 * $ echo "aaazvc" | git hash-object -t blob --stdin
 * dea509d097ce692e167dfc6a48a7a280cc5e877e
 */
void test_refs_revparse__a_not_precise_enough_objectid_returns_EAMBIGUOUS(void)
{
	git_repository *repo;
	git_index *index;
	git_object *obj;

	repo = cl_git_sandbox_init("testrepo");

	cl_git_mkfile("testrepo/one.txt", "aabqhq\n");
	cl_git_mkfile("testrepo/two.txt", "aaazvc\n");
589

590 591 592
	cl_git_pass(git_repository_index(&index, repo));
	cl_git_pass(git_index_add_bypath(index, "one.txt"));
	cl_git_pass(git_index_add_bypath(index, "two.txt"));
593

594 595 596 597 598 599 600 601 602
	cl_git_fail_with(git_revparse_single(&obj, repo, "dea509d0"), GIT_EAMBIGUOUS);

	cl_git_pass(git_revparse_single(&obj, repo, "dea509d09"));

	git_object_free(obj);
	git_index_free(index);
	cl_git_sandbox_cleanup();
}

603 604 605 606 607
void test_refs_revparse__issue_994(void)
{
	git_repository *repo;
	git_reference *head, *with_at;
	git_object *target;
608

609 610 611
	repo = cl_git_sandbox_init("testrepo.git");

	cl_assert_equal_i(GIT_ENOTFOUND,
612
		git_revparse_single(&target, repo, "origin/bim_with_3d@11296"));
613 614

	cl_assert_equal_i(GIT_ENOTFOUND,
615
		git_revparse_single(&target, repo, "refs/remotes/origin/bim_with_3d@11296"));
616 617 618


	cl_git_pass(git_repository_head(&head, repo));
619
	cl_git_pass(git_reference_create(
620 621 622
		&with_at,
		repo,
		"refs/remotes/origin/bim_with_3d@11296",
623
		git_reference_target(head),
624
		0,
625
		NULL));
626

627
	cl_git_pass(git_revparse_single(&target, repo, "origin/bim_with_3d@11296"));
628 629
	git_object_free(target);

630
	cl_git_pass(git_revparse_single(&target, repo, "refs/remotes/origin/bim_with_3d@11296"));
631 632 633 634 635 636
	git_object_free(target);

	git_reference_free(with_at);
	git_reference_free(head);
	cl_git_sandbox_cleanup();
}
637 638 639 640

/**
 * $ git rev-parse blah-7-gc47800c
 * c47800c7266a2be04c571c04d5a6614691ea99bd
nulltoken committed
641
 *
642 643
 * $ git rev-parse HEAD~3
 * 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
nulltoken committed
644
 *
645
 * $ git branch blah-7-gc47800c HEAD~3
nulltoken committed
646
 *
647 648 649 650 651 652 653 654
 * $ git rev-parse blah-7-gc47800c
 * 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
 */
void test_refs_revparse__try_to_retrieve_branch_before_described_tag(void)
{
	git_repository *repo;
	git_reference *branch;
	git_object *target;
655
	char sha[GIT_OID_SHA1_HEXSIZE + 1];
656 657 658 659 660

	repo = cl_git_sandbox_init("testrepo.git");

	test_object_inrepo("blah-7-gc47800c", "c47800c7266a2be04c571c04d5a6614691ea99bd", repo);

661
	cl_git_pass(git_revparse_single(&target, repo, "HEAD~3"));
662
	cl_git_pass(git_branch_create(&branch, repo, "blah-7-gc47800c", (git_commit *)target, 0));
663

664
	git_oid_tostr(sha, GIT_OID_SHA1_HEXSIZE + 1, git_object_id(target));
665 666 667 668 669 670 671

	test_object_inrepo("blah-7-gc47800c", sha, repo);

	git_reference_free(branch);
	git_object_free(target);
	cl_git_sandbox_cleanup();
}
672 673 674 675

/**
 * $ git rev-parse a65fedf39aefe402d3bb6e24df4d4f5fe4547750
 * a65fedf39aefe402d3bb6e24df4d4f5fe4547750
nulltoken committed
676
 *
677 678
 * $ git rev-parse HEAD~3
 * 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
nulltoken committed
679
 *
680
 * $ git branch a65fedf39aefe402d3bb6e24df4d4f5fe4547750 HEAD~3
nulltoken committed
681
 *
682 683
 * $ git rev-parse a65fedf39aefe402d3bb6e24df4d4f5fe4547750
 * a65fedf39aefe402d3bb6e24df4d4f5fe4547750
nulltoken committed
684
 *
685 686 687 688 689 690 691 692
 * $ git rev-parse heads/a65fedf39aefe402d3bb6e24df4d4f5fe4547750
 * 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
 */
void test_refs_revparse__try_to_retrieve_sha_before_branch(void)
{
	git_repository *repo;
	git_reference *branch;
	git_object *target;
693
	char sha[GIT_OID_SHA1_HEXSIZE + 1];
694 695 696 697 698

	repo = cl_git_sandbox_init("testrepo.git");

	test_object_inrepo("a65fedf39aefe402d3bb6e24df4d4f5fe4547750", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);

699
	cl_git_pass(git_revparse_single(&target, repo, "HEAD~3"));
700
	cl_git_pass(git_branch_create(&branch, repo, "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", (git_commit *)target, 0));
701

702
	git_oid_tostr(sha, GIT_OID_SHA1_HEXSIZE + 1, git_object_id(target));
703 704 705 706 707 708 709 710 711 712 713 714

	test_object_inrepo("a65fedf39aefe402d3bb6e24df4d4f5fe4547750", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", repo);
	test_object_inrepo("heads/a65fedf39aefe402d3bb6e24df4d4f5fe4547750", sha, repo);

	git_reference_free(branch);
	git_object_free(target);
	cl_git_sandbox_cleanup();
}

/**
 * $ git rev-parse c47800
 * c47800c7266a2be04c571c04d5a6614691ea99bd
nulltoken committed
715
 *
716 717
 * $ git rev-parse HEAD~3
 * 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
nulltoken committed
718
 *
719
 * $ git branch c47800 HEAD~3
nulltoken committed
720
 *
721 722 723 724 725 726 727 728
 * $ git rev-parse c47800
 * 4a202b346bb0fb0db7eff3cffeb3c70babbd2045
 */
void test_refs_revparse__try_to_retrieve_branch_before_abbrev_sha(void)
{
	git_repository *repo;
	git_reference *branch;
	git_object *target;
729
	char sha[GIT_OID_SHA1_HEXSIZE + 1];
730 731 732 733 734

	repo = cl_git_sandbox_init("testrepo.git");

	test_object_inrepo("c47800", "c47800c7266a2be04c571c04d5a6614691ea99bd", repo);

735
	cl_git_pass(git_revparse_single(&target, repo, "HEAD~3"));
736
	cl_git_pass(git_branch_create(&branch, repo, "c47800", (git_commit *)target, 0));
737

738
	git_oid_tostr(sha, GIT_OID_SHA1_HEXSIZE + 1, git_object_id(target));
739 740 741 742 743 744 745

	test_object_inrepo("c47800", sha, repo);

	git_reference_free(branch);
	git_object_free(target);
	cl_git_sandbox_cleanup();
}
746 747 748 749


void test_refs_revparse__range(void)
{
750 751
	assert_invalid_single_spec("be3563a^1..be3563a");

752 753 754
	test_rangelike("be3563a^1..be3563a",
	               "9fd738e8f7967c078dceed8190330fc8648ee56a",
	               "be3563ae3f795b2b4353bcce3a527ad0a4f7f644",
755
	               GIT_REVSPEC_RANGE);
756 757 758 759

	test_rangelike("be3563a^1...be3563a",
	               "9fd738e8f7967c078dceed8190330fc8648ee56a",
	               "be3563ae3f795b2b4353bcce3a527ad0a4f7f644",
760
	               GIT_REVSPEC_RANGE | GIT_REVSPEC_MERGE_BASE);
761 762 763

	test_rangelike("be3563a^1.be3563a", NULL, NULL, 0);
}
764 765 766

void test_refs_revparse__parses_range_operator(void)
{
767
	test_id("HEAD", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", NULL, GIT_REVSPEC_SINGLE);
Vicent Marti committed
768 769 770
	test_id("HEAD~3..HEAD",
		"4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
		"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
771
		GIT_REVSPEC_RANGE);
Vicent Marti committed
772 773 774 775

	test_id("HEAD~3...HEAD",
		"4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
		"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
776
		GIT_REVSPEC_RANGE | GIT_REVSPEC_MERGE_BASE);
777 778 779 780

	test_id("HEAD~3..",
		"4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
		"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
781
		GIT_REVSPEC_RANGE);
782 783 784 785

	test_id("HEAD~3...",
		"4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
		"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
786
		GIT_REVSPEC_RANGE | GIT_REVSPEC_MERGE_BASE);
787 788 789 790

	test_id("..HEAD~3",
		"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
		"4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
791
		GIT_REVSPEC_RANGE);
792 793 794 795

	test_id("...HEAD~3",
		"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
		"4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
796
		GIT_REVSPEC_RANGE | GIT_REVSPEC_MERGE_BASE);
797 798 799 800

	test_id("...",
		"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
		"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
801
		GIT_REVSPEC_RANGE | GIT_REVSPEC_MERGE_BASE);
802 803

	test_invalid_revspec("..");
804
}
805 806 807 808 809 810 811 812 813 814 815 816 817 818

void test_refs_revparse__ext_retrieves_both_the_reference_and_its_target(void)
{
	test_object_and_ref(
		"master@{upstream}",
		"be3563ae3f795b2b4353bcce3a527ad0a4f7f644",
		"refs/remotes/test/master");

	test_object_and_ref(
		"@{-1}",
		"a4a7dce85cf63874e984719f4fdd239f5145052f",
		"refs/heads/br2");
}

819
void test_refs_revparse__ext_can_expand_short_reference_names(void)
820 821 822 823
{
	test_object_and_ref(
		"master",
		"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
824
		"refs/heads/master");
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865

	test_object_and_ref(
		"HEAD",
		"a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
		"refs/heads/master");

    test_object_and_ref(
		"tags/test",
		"b25fa35b38051e4ae45d4222e795f9df2e43f1d1",
        "refs/tags/test");
}

void test_refs_revparse__ext_returns_NULL_reference_when_expression_points_at_a_revision(void)
{
    test_object_and_ref(
        "HEAD~3",
        "4a202b346bb0fb0db7eff3cffeb3c70babbd2045",
        NULL);

    test_object_and_ref(
        "HEAD~0",
        "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
        NULL);

    test_object_and_ref(
        "HEAD^0",
        "a65fedf39aefe402d3bb6e24df4d4f5fe4547750",
        NULL);

    test_object_and_ref(
		"@{-1}@{0}",
		"a4a7dce85cf63874e984719f4fdd239f5145052f",
		NULL);
}

void test_refs_revparse__ext_returns_NULL_reference_when_expression_points_at_a_tree_content(void)
{
    test_object_and_ref(
		"tags/test:readme.txt",
		"0266163a49e280c4f5ed1e08facd36a2bd716bcf",
        NULL);
866
}
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881

void test_refs_revparse__uneven_sizes(void)
{
	test_object("a65fedf39aefe402d3bb6e24df4d4f5fe454775",
				"a65fedf39aefe402d3bb6e24df4d4f5fe4547750");

	test_object("a65fedf39aefe402d3bb6e24df4d4f5fe45477",
				"a65fedf39aefe402d3bb6e24df4d4f5fe4547750");

	test_object("a65fedf39aefe402d3bb6e24df4d4f5fe4547",
				"a65fedf39aefe402d3bb6e24df4d4f5fe4547750");

	test_object("a65fedf39aefe402d3bb6e24df4d",
				"a65fedf39aefe402d3bb6e24df4d4f5fe4547750");
}
882 883 884 885 886 887 888

void test_refs_revparse__parses_at_head(void)
{
	test_id("HEAD", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", NULL, GIT_REVSPEC_SINGLE);
	test_id("@{0}", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", NULL, GIT_REVSPEC_SINGLE);
	test_id("@", "a65fedf39aefe402d3bb6e24df4d4f5fe4547750", NULL, GIT_REVSPEC_SINGLE);
}