tests.c 32.6 KB
Newer Older
Ben Straub committed
1 2 3
#include "clar_libgit2.h"
#include "index.h"

4 5
static const size_t index_entry_count = 109;
static const size_t index_entry_count_2 = 1437;
Ben Straub committed
6 7 8
#define TEST_INDEX_PATH cl_fixture("testrepo.git/index")
#define TEST_INDEX2_PATH cl_fixture("gitgit.index")
#define TEST_INDEXBIG_PATH cl_fixture("big.index")
9
#define TEST_INDEXBAD_PATH cl_fixture("bad.index")
Ben Straub committed
10 11


12
/* Suite data */
Ben Straub committed
13
struct test_entry {
14
   size_t index;
Ben Straub committed
15
   char path[128];
16
   off64_t file_size;
Ben Straub committed
17 18 19
   git_time_t mtime;
};

Ben Straub committed
20
static struct test_entry test_entries[] = {
Ben Straub committed
21 22
   {4, "Makefile", 5064, 0x4C3F7F33},
   {6, "git.git-authors", 2709, 0x4C3F7F33},
23 24 25
   {36, "src/index.c", 10014, 0x4C43368D},
   {48, "src/revobject.h", 1448, 0x4C3F7FE2},
   {62, "tests/Makefile", 2631, 0x4C3F7F33}
Ben Straub committed
26 27
};

28
/* Helpers */
29
static void copy_file(const char *src, const char *dst)
Ben Straub committed
30 31 32 33
{
	git_buf source_buf = GIT_BUF_INIT;
	git_file dst_fd;

34
	cl_git_pass(git_futils_readbuffer(&source_buf, src));
Ben Straub committed
35

36
	dst_fd = git_futils_creat_withpath(dst, 0777, 0666); /* -V536 */
Ben Straub committed
37 38 39
	if (dst_fd < 0)
		goto cleanup;

40
	cl_git_pass(p_write(dst_fd, source_buf.ptr, source_buf.size));
Ben Straub committed
41 42

cleanup:
43
	git_buf_dispose(&source_buf);
Ben Straub committed
44 45 46
	p_close(dst_fd);
}

47
static void files_are_equal(const char *a, const char *b)
Ben Straub committed
48 49 50
{
	git_buf buf_a = GIT_BUF_INIT;
	git_buf buf_b = GIT_BUF_INIT;
51
	int pass;
Ben Straub committed
52

53
	if (git_futils_readbuffer(&buf_a, a) < 0)
54
		cl_assert(0);
Ben Straub committed
55

56
	if (git_futils_readbuffer(&buf_b, b) < 0) {
57
		git_buf_dispose(&buf_a);
58
		cl_assert(0);
Ben Straub committed
59 60
	}

61
	pass = (buf_a.size == buf_b.size && !memcmp(buf_a.ptr, buf_b.ptr, buf_a.size));
Ben Straub committed
62

63 64
	git_buf_dispose(&buf_a);
	git_buf_dispose(&buf_b);
65 66

	cl_assert(pass);
Ben Straub committed
67 68 69
}


70
/* Fixture setup and teardown */
Ben Straub committed
71 72 73 74
void test_index_tests__initialize(void)
{
}

75 76 77 78 79
void test_index_tests__cleanup(void)
{
	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_UNSAVED_INDEX_SAFETY, 0));
}

Ben Straub committed
80 81 82 83 84 85 86 87
void test_index_tests__empty_index(void)
{
   git_index *index;

   cl_git_pass(git_index_open(&index, "in-memory-index"));
   cl_assert(index->on_disk == 0);

   cl_assert(git_index_entrycount(index) == 0);
88
   cl_assert(git_vector_is_sorted(&index->entries));
Ben Straub committed
89 90 91 92 93 94 95 96 97 98 99 100 101

   git_index_free(index);
}

void test_index_tests__default_test_index(void)
{
   git_index *index;
   unsigned int i;
   git_index_entry **entries;

   cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
   cl_assert(index->on_disk);

102
   cl_assert(git_index_entrycount(index) == index_entry_count);
103
   cl_assert(git_vector_is_sorted(&index->entries));
Ben Straub committed
104 105 106

   entries = (git_index_entry **)index->entries.contents;

Ben Straub committed
107
   for (i = 0; i < ARRAY_SIZE(test_entries); ++i) {
nulltoken committed
108
		git_index_entry *e = entries[test_entries[i].index];
Ben Straub committed
109

nulltoken committed
110
		cl_assert_equal_s(e->path, test_entries[i].path);
111 112
		cl_assert_equal_i(e->mtime.seconds, test_entries[i].mtime);
		cl_assert_equal_i(e->file_size, test_entries[i].file_size);
Ben Straub committed
113 114 115 116 117 118 119 120 121 122 123 124
   }

   git_index_free(index);
}

void test_index_tests__gitgit_index(void)
{
   git_index *index;

   cl_git_pass(git_index_open(&index, TEST_INDEX2_PATH));
   cl_assert(index->on_disk);

125
   cl_assert(git_index_entrycount(index) == index_entry_count_2);
126
   cl_assert(git_vector_is_sorted(&index->entries));
Ben Straub committed
127 128 129 130 131 132 133 134 135 136 137 138
   cl_assert(index->tree != NULL);

   git_index_free(index);
}

void test_index_tests__find_in_existing(void)
{
   git_index *index;
   unsigned int i;

   cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));

Ben Straub committed
139
   for (i = 0; i < ARRAY_SIZE(test_entries); ++i) {
nulltoken committed
140
		size_t idx;
141

nulltoken committed
142 143
		cl_assert(!git_index_find(&idx, index, test_entries[i].path));
		cl_assert(idx == test_entries[i].index);
Ben Straub committed
144 145 146 147 148 149 150 151 152 153 154 155
   }

   git_index_free(index);
}

void test_index_tests__find_in_empty(void)
{
   git_index *index;
   unsigned int i;

   cl_git_pass(git_index_open(&index, "fake-index"));

Ben Straub committed
156
   for (i = 0; i < ARRAY_SIZE(test_entries); ++i) {
nulltoken committed
157
		cl_assert(GIT_ENOTFOUND == git_index_find(NULL, index, test_entries[i].path));
Ben Straub committed
158 159 160 161 162
   }

   git_index_free(index);
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
void test_index_tests__find_prefix(void)
{
   git_index *index;
   const git_index_entry *entry;
   size_t pos;

   cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));

   cl_git_pass(git_index_find_prefix(&pos, index, "src"));
   entry = git_index_get_byindex(index, pos);
   cl_assert(git__strcmp(entry->path, "src/block-sha1/sha1.c") == 0);

   cl_git_pass(git_index_find_prefix(&pos, index, "src/co"));
   entry = git_index_get_byindex(index, pos);
   cl_assert(git__strcmp(entry->path, "src/commit.c") == 0);

   cl_assert(GIT_ENOTFOUND == git_index_find_prefix(NULL, index, "blah"));

   git_index_free(index);
}

Ben Straub committed
184 185 186 187
void test_index_tests__write(void)
{
   git_index *index;

188
   copy_file(TEST_INDEXBIG_PATH, "index_rewrite");
Ben Straub committed
189 190 191 192 193

   cl_git_pass(git_index_open(&index, "index_rewrite"));
   cl_assert(index->on_disk);

   cl_git_pass(git_index_write(index));
194
   files_are_equal(TEST_INDEXBIG_PATH, "index_rewrite");
Ben Straub committed
195 196 197 198 199 200 201 202

   git_index_free(index);

   p_unlink("index_rewrite");
}

void test_index_tests__sort0(void)
{
203 204
	/* sort the entires in an index */

Ben Straub committed
205 206 207 208 209 210 211 212 213 214 215 216 217
   /*
   * TODO: This no longer applies:
   * index sorting in Git uses some specific changes to the way
   * directories are sorted.
   *
   * We need to specificially check for this by creating a new
   * index, adding entries in random order and then
   * checking for consistency
   */
}

void test_index_tests__sort1(void)
{
218
   /* sort the entires in an empty index */
Ben Straub committed
219 220 221 222 223
   git_index *index;

   cl_git_pass(git_index_open(&index, "fake-index"));

   /* FIXME: this test is slightly dumb */
224
   cl_assert(git_vector_is_sorted(&index->entries));
Ben Straub committed
225 226 227 228

   git_index_free(index);
}

Vicent Marti committed
229 230 231 232 233 234
static void cleanup_myrepo(void *opaque)
{
	GIT_UNUSED(opaque);
	cl_fixture_cleanup("myrepo");
}

Ben Straub committed
235 236
void test_index_tests__add(void)
{
Vicent Marti committed
237 238 239 240 241
	git_index *index;
	git_filebuf file = GIT_FILEBUF_INIT;
	git_repository *repo;
	const git_index_entry *entry;
	git_oid id1;
Ben Straub committed
242

Vicent Marti committed
243
	cl_set_cleanup(&cleanup_myrepo, NULL);
Ben Straub committed
244

Vicent Marti committed
245 246
	/* Intialize a new repository */
	cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
Ben Straub committed
247

Vicent Marti committed
248 249 250
	/* Ensure we're the only guy in the room */
	cl_git_pass(git_repository_index(&index, repo));
	cl_assert(git_index_entrycount(index) == 0);
Ben Straub committed
251

Vicent Marti committed
252 253
	/* Create a new file in the working directory */
	cl_git_pass(git_futils_mkpath2file("myrepo/test.txt", 0777));
254
	cl_git_pass(git_filebuf_open(&file, "myrepo/test.txt", 0, 0666));
Vicent Marti committed
255
	cl_git_pass(git_filebuf_write(&file, "hey there\n", 10));
256
	cl_git_pass(git_filebuf_commit(&file));
Ben Straub committed
257

Vicent Marti committed
258 259 260 261 262
	/* Store the expected hash of the file/blob
	 * This has been generated by executing the following
	 * $ echo "hey there" | git hash-object --stdin
	 */
	cl_git_pass(git_oid_fromstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6"));
Ben Straub committed
263

Vicent Marti committed
264
	/* Add the new file to the index */
265
	cl_git_pass(git_index_add_bypath(index, "test.txt"));
Ben Straub committed
266

Vicent Marti committed
267 268 269
	/* Wow... it worked! */
	cl_assert(git_index_entrycount(index) == 1);
	entry = git_index_get_byindex(index, 0);
Ben Straub committed
270

Vicent Marti committed
271
	/* And the built-in hashing mechanism worked as expected */
272
	cl_assert_equal_oid(&id1, &entry->id);
Edward Thomson committed
273

Vicent Marti committed
274 275
	/* Test access by path instead of index */
	cl_assert((entry = git_index_get_bypath(index, "test.txt", 0)) != NULL);
276
	cl_assert_equal_oid(&id1, &entry->id);
Vicent Marti committed
277 278 279

	git_index_free(index);
	git_repository_free(repo);
Ben Straub committed
280 281
}

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
void test_index_tests__add_frombuffer(void)
{
	git_index *index;
	git_repository *repo;
        git_index_entry entry;
	const git_index_entry *returned_entry;

	git_oid id1;
	git_blob *blob;

	const char *content = "hey there\n";

	cl_set_cleanup(&cleanup_myrepo, NULL);

	/* Intialize a new repository */
	cl_git_pass(git_repository_init(&repo, "./myrepo", 0));

	/* Ensure we're the only guy in the room */
	cl_git_pass(git_repository_index(&index, repo));
	cl_assert(git_index_entrycount(index) == 0);

	/* Store the expected hash of the file/blob
	 * This has been generated by executing the following
	 * $ echo "hey there" | git hash-object --stdin
	 */
	cl_git_pass(git_oid_fromstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6"));

	/* Add the new file to the index */
	memset(&entry, 0x0, sizeof(git_index_entry));
	entry.mode = GIT_FILEMODE_BLOB;
	entry.path = "test.txt";
313
	cl_git_pass(git_index_add_from_buffer(index, &entry,
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
		content, strlen(content)));

	/* Wow... it worked! */
	cl_assert(git_index_entrycount(index) == 1);
	returned_entry = git_index_get_byindex(index, 0);

	/* And the built-in hashing mechanism worked as expected */
	cl_assert_equal_oid(&id1, &returned_entry->id);
	/* And mode is the one asked */
	cl_assert_equal_i(GIT_FILEMODE_BLOB, returned_entry->mode);

	/* Test access by path instead of index */
	cl_assert((returned_entry = git_index_get_bypath(index, "test.txt", 0)) != NULL);
	cl_assert_equal_oid(&id1, &returned_entry->id);

	/* Test the blob is in the repository */
	cl_git_pass(git_blob_lookup(&blob, repo, &id1));
	cl_assert_equal_s(
		content, git_blob_rawcontent(blob));
	git_blob_free(blob);

	git_index_free(index);
	git_repository_free(repo);
}

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
void test_index_tests__dirty_and_clean(void)
{
	git_repository *repo;
	git_index *index;
	git_index_entry entry = {{0}};

	/* Index is not dirty after opening */
	cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
	cl_git_pass(git_repository_index(&index, repo));

	cl_assert(git_index_entrycount(index) == 0);
	cl_assert(!git_index_is_dirty(index));

	/* Index is dirty after adding an entry */
	entry.mode = GIT_FILEMODE_BLOB;
	entry.path = "test.txt";
355
	cl_git_pass(git_index_add_from_buffer(index, &entry, "Hi.\n", 4));
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
	cl_assert(git_index_entrycount(index) == 1);
	cl_assert(git_index_is_dirty(index));

	/* Index is not dirty after write */
	cl_git_pass(git_index_write(index));
	cl_assert(!git_index_is_dirty(index));

	/* Index is dirty after removing an entry */
	cl_git_pass(git_index_remove_bypath(index, "test.txt"));
	cl_assert(git_index_entrycount(index) == 0);
	cl_assert(git_index_is_dirty(index));

	/* Index is not dirty after write */
	cl_git_pass(git_index_write(index));
	cl_assert(!git_index_is_dirty(index));

	/* Index remains not dirty after read */
	cl_git_pass(git_index_read(index, 0));
	cl_assert(!git_index_is_dirty(index));

	/* Index is dirty when we do an unforced read with dirty content */
377
	cl_git_pass(git_index_add_from_buffer(index, &entry, "Hi.\n", 4));
378 379 380 381 382 383 384 385 386 387 388 389 390 391
	cl_assert(git_index_entrycount(index) == 1);
	cl_assert(git_index_is_dirty(index));

	cl_git_pass(git_index_read(index, 0));
	cl_assert(git_index_is_dirty(index));

	/* Index is clean when we force a read with dirty content */
	cl_git_pass(git_index_read(index, 1));
	cl_assert(!git_index_is_dirty(index));

	git_index_free(index);
	git_repository_free(repo);
}

392
void test_index_tests__dirty_fails_optionally(void)
393 394 395 396 397 398 399 400 401 402 403 404
{
	git_repository *repo;
	git_index *index;
	git_index_entry entry = {{0}};

	/* Index is not dirty after opening */
	repo = cl_git_sandbox_init("testrepo");
	cl_git_pass(git_repository_index(&index, repo));

	/* Index is dirty after adding an entry */
	entry.mode = GIT_FILEMODE_BLOB;
	entry.path = "test.txt";
405
	cl_git_pass(git_index_add_from_buffer(index, &entry, "Hi.\n", 4));
406 407
	cl_assert(git_index_is_dirty(index));

408 409 410 411 412
	cl_git_pass(git_checkout_head(repo, NULL));

	/* Index is dirty (again) after adding an entry */
	entry.mode = GIT_FILEMODE_BLOB;
	entry.path = "test.txt";
413
	cl_git_pass(git_index_add_from_buffer(index, &entry, "Hi.\n", 4));
414 415 416
	cl_assert(git_index_is_dirty(index));

	cl_git_pass(git_libgit2_opts(GIT_OPT_ENABLE_UNSAVED_INDEX_SAFETY, 1));
417 418 419 420 421 422
	cl_git_fail_with(GIT_EINDEXDIRTY, git_checkout_head(repo, NULL));

	git_index_free(index);
	cl_git_sandbox_cleanup();
}

423 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 453 454 455 456 457
void test_index_tests__add_frombuffer_reset_entry(void)
{
	git_index *index;
	git_repository *repo;
        git_index_entry entry;
	const git_index_entry *returned_entry;
	git_filebuf file = GIT_FILEBUF_INIT;

	git_oid id1;
	git_blob *blob;
	const char *old_content = "here\n";
	const char *content = "hey there\n";

	cl_set_cleanup(&cleanup_myrepo, NULL);

	/* Intialize a new repository */
	cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
	cl_git_pass(git_repository_index(&index, repo));
	cl_git_pass(git_futils_mkpath2file("myrepo/test.txt", 0777));
	cl_git_pass(git_filebuf_open(&file, "myrepo/test.txt", 0, 0666));
	cl_git_pass(git_filebuf_write(&file, old_content, strlen(old_content)));
	cl_git_pass(git_filebuf_commit(&file));

	/* Store the expected hash of the file/blob
	 * This has been generated by executing the following
	 * $ echo "hey there" | git hash-object --stdin
	 */
	cl_git_pass(git_oid_fromstr(&id1, "a8233120f6ad708f843d861ce2b7228ec4e3dec6"));

	cl_git_pass(git_index_add_bypath(index, "test.txt"));

	/* Add the new file to the index */
	memset(&entry, 0x0, sizeof(git_index_entry));
	entry.mode = GIT_FILEMODE_BLOB;
	entry.path = "test.txt";
458
	cl_git_pass(git_index_add_from_buffer(index, &entry,
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
		content, strlen(content)));

	/* Wow... it worked! */
	cl_assert(git_index_entrycount(index) == 1);
	returned_entry = git_index_get_byindex(index, 0);

	/* And the built-in hashing mechanism worked as expected */
	cl_assert_equal_oid(&id1, &returned_entry->id);
	/* And mode is the one asked */
	cl_assert_equal_i(GIT_FILEMODE_BLOB, returned_entry->mode);

	/* Test access by path instead of index */
	cl_assert((returned_entry = git_index_get_bypath(index, "test.txt", 0)) != NULL);
	cl_assert_equal_oid(&id1, &returned_entry->id);
	cl_assert_equal_i(0, returned_entry->dev);
	cl_assert_equal_i(0, returned_entry->ino);
	cl_assert_equal_i(0, returned_entry->uid);
	cl_assert_equal_i(0, returned_entry->uid);
	cl_assert_equal_i(10, returned_entry->file_size);

            /* Test the blob is in the repository */
	cl_git_pass(git_blob_lookup(&blob, repo, &id1));
	cl_assert_equal_s(content, git_blob_rawcontent(blob));
	git_blob_free(blob);

	git_index_free(index);
	git_repository_free(repo);
}

Russell Belfer committed
488 489 490 491 492 493
static void cleanup_1397(void *opaque)
{
	GIT_UNUSED(opaque);
	cl_git_sandbox_cleanup();
}

494 495 496 497 498 499 500
void test_index_tests__add_issue_1397(void)
{
	git_index *index;
	git_repository *repo;
	const git_index_entry *entry;
	git_oid id1;

Russell Belfer committed
501
	cl_set_cleanup(&cleanup_1397, NULL);
502 503 504

	repo = cl_git_sandbox_init("issue_1397");

Russell Belfer committed
505
	cl_repo_set_bool(repo, "core.autocrlf", true);
506 507 508 509 510 511 512 513 514 515 516 517

	/* Ensure we're the only guy in the room */
	cl_git_pass(git_repository_index(&index, repo));

	/* Store the expected hash of the file/blob
	 * This has been generated by executing the following
	 * $ git hash-object crlf_file.txt
	 */
	cl_git_pass(git_oid_fromstr(&id1, "8312e0889a9cbab77c732b6bc39b51a683e3a318"));

	/* Make sure the initial SHA-1 is correct */
	cl_assert((entry = git_index_get_bypath(index, "crlf_file.txt", 0)) != NULL);
518
	cl_assert_equal_oid(&id1, &entry->id);
519 520 521 522 523 524

	/* Update the index */
	cl_git_pass(git_index_add_bypath(index, "crlf_file.txt"));

	/* Check the new SHA-1 */
	cl_assert((entry = git_index_get_bypath(index, "crlf_file.txt", 0)) != NULL);
525
	cl_assert_equal_oid(&id1, &entry->id);
526 527 528 529

	git_index_free(index);
}

530
void test_index_tests__add_bypath_to_a_bare_repository_returns_EBAREPO(void)
531 532 533 534 535 536 537
{
	git_repository *bare_repo;
	git_index *index;

	cl_git_pass(git_repository_open(&bare_repo, cl_fixture("testrepo.git")));
	cl_git_pass(git_repository_index(&index, bare_repo));

538
	cl_assert_equal_i(GIT_EBAREREPO, git_index_add_bypath(index, "test.txt"));
539 540 541 542

	git_index_free(index);
	git_repository_free(bare_repo);
}
543

544
static void assert_add_bypath_fails(git_repository *repo, const char *fn)
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
{
	git_index *index;
	git_buf path = GIT_BUF_INIT;

	cl_git_pass(git_repository_index(&index, repo));
	cl_assert(git_index_entrycount(index) == 0);

	git_buf_joinpath(&path, "./invalid", fn);

	cl_git_mkfile(path.ptr, NULL);
	cl_git_fail(git_index_add_bypath(index, fn));
	cl_must_pass(p_unlink(path.ptr));

	cl_assert(git_index_entrycount(index) == 0);

560
	git_buf_dispose(&path);
561 562 563
	git_index_free(index);
}

564
/* Test that writing an invalid filename fails */
565
void test_index_tests__cannot_add_invalid_filename(void)
566 567
{
	git_repository *repo;
568

569
	cl_must_pass(p_mkdir("invalid", 0700));
570 571 572
	cl_git_pass(git_repository_init(&repo, "./invalid", 0));
	cl_must_pass(p_mkdir("./invalid/subdir", 0777));

573
	/* cl_git_mkfile() needs the dir to exist */
574 575 576 577
	if (!git_path_exists("./invalid/.GIT"))
		cl_must_pass(p_mkdir("./invalid/.GIT", 0777));
	if (!git_path_exists("./invalid/.GiT"))
		cl_must_pass(p_mkdir("./invalid/.GiT", 0777));
578

579 580 581 582 583 584 585
	assert_add_bypath_fails(repo, ".git/hello");
	assert_add_bypath_fails(repo, ".GIT/hello");
	assert_add_bypath_fails(repo, ".GiT/hello");
	assert_add_bypath_fails(repo, "./.git/hello");
	assert_add_bypath_fails(repo, "./foo");
	assert_add_bypath_fails(repo, "./bar");
	assert_add_bypath_fails(repo, "subdir/../bar");
586 587 588 589 590 591

	git_repository_free(repo);

	cl_fixture_cleanup("invalid");
}

592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
static void assert_add_fails(git_repository *repo, const char *fn)
{
	git_index *index;
	git_buf path = GIT_BUF_INIT;
	git_index_entry entry = {{0}};

	cl_git_pass(git_repository_index(&index, repo));
	cl_assert(git_index_entrycount(index) == 0);

	entry.path = fn;
	entry.mode = GIT_FILEMODE_BLOB;
	cl_git_pass(git_oid_fromstr(&entry.id, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"));

	cl_git_fail(git_index_add(index, &entry));

	cl_assert(git_index_entrycount(index) == 0);

	git_buf_dispose(&path);
	git_index_free(index);
}

/*
 * Test that writing an invalid filename fails on filesystem
 * specific protected names
 */
void test_index_tests__cannot_add_protected_invalid_filename(void)
{
	git_repository *repo;
	git_index *index;

	cl_must_pass(p_mkdir("invalid", 0700));

	cl_git_pass(git_repository_init(&repo, "./invalid", 0));

	/* add a file to the repository so we can reference it later */
	cl_git_pass(git_repository_index(&index, repo));
	cl_git_mkfile("invalid/dummy.txt", "");
	cl_git_pass(git_index_add_bypath(index, "dummy.txt"));
	cl_must_pass(p_unlink("invalid/dummy.txt"));
	cl_git_pass(git_index_remove_bypath(index, "dummy.txt"));
	git_index_free(index);

	cl_repo_set_bool(repo, "core.protectHFS", true);
	cl_repo_set_bool(repo, "core.protectNTFS", true);

	assert_add_fails(repo, ".git./hello");
	assert_add_fails(repo, ".git\xe2\x80\xad/hello");
	assert_add_fails(repo, "git~1/hello");
	assert_add_fails(repo, ".git\xe2\x81\xaf/hello");
	assert_add_fails(repo, ".git::$INDEX_ALLOCATION/dummy-file");

	git_repository_free(repo);

	cl_fixture_cleanup("invalid");
}

648 649 650 651 652 653 654 655 656
static void replace_char(char *str, char in, char out)
{
	char *c = str;

	while (*c++)
		if (*c == in)
			*c = out;
}

657
static void assert_write_fails(git_repository *repo, const char *fn_orig)
658
{
659 660
	git_index *index;
	git_oid expected;
661 662 663
	const git_index_entry *entry;
	git_buf path = GIT_BUF_INIT;
	char *fn;
664 665 666 667

	cl_git_pass(git_repository_index(&index, repo));
	cl_assert(git_index_entrycount(index) == 0);

668 669 670 671 672 673
	/*
	 * Sneak a valid path into the index, we'll update it
	 * to an invalid path when we try to write the index.
	 */
	fn = git__strdup(fn_orig);
	replace_char(fn, '/', '_');
674
	replace_char(fn, ':', '!');
675 676 677 678 679 680 681 682

	git_buf_joinpath(&path, "./invalid", fn);

	cl_git_mkfile(path.ptr, NULL);

	cl_git_pass(git_index_add_bypath(index, fn));

	cl_assert(entry = git_index_get_bypath(index, fn, 0));
683

684 685
	/* kids, don't try this at home */
	replace_char((char *)entry->path, '_', '/');
686
	replace_char((char *)entry->path, '!', ':');
687 688 689 690

	/* write-tree */
	cl_git_fail(git_index_write_tree(&expected, index));

691 692 693
	p_unlink(path.ptr);

	cl_git_pass(git_index_remove_all(index, NULL, NULL, NULL));
694
	git_buf_dispose(&path);
695
	git_index_free(index);
696 697 698
	git__free(fn);
}

699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
void test_index_tests__write_tree_invalid_unowned_index(void)
{
	git_index *idx;
	git_repository *repo;
	git_index_entry entry = {{0}};
	git_oid tree_id;

	cl_git_pass(git_index_new(&idx));

	cl_git_pass(git_oid_fromstr(&entry.id, "8312e0a89a9cbab77c732b6bc39b51a783e3a318"));
	entry.path = "foo";
	entry.mode = GIT_FILEMODE_BLOB;
	cl_git_pass(git_index_add(idx, &entry));

	cl_git_pass(git_repository_init(&repo, "./invalid-id", 0));

	cl_git_fail(git_index_write_tree_to(&tree_id, idx, repo));

	git_index_free(idx);
	git_repository_free(repo);

	cl_fixture_cleanup("invalid-id");
}

723 724 725 726 727 728 729 730 731
/* Test that writing an invalid filename fails */
void test_index_tests__write_invalid_filename(void)
{
	git_repository *repo;

	p_mkdir("invalid", 0700);

	cl_git_pass(git_repository_init(&repo, "./invalid", 0));

732 733 734 735 736 737 738
	assert_write_fails(repo, ".git/hello");
	assert_write_fails(repo, ".GIT/hello");
	assert_write_fails(repo, ".GiT/hello");
	assert_write_fails(repo, "./.git/hello");
	assert_write_fails(repo, "./foo");
	assert_write_fails(repo, "./bar");
	assert_write_fails(repo, "foo/../bar");
739

740 741
	git_repository_free(repo);

742
	cl_fixture_cleanup("invalid");
743
}
744

745 746 747 748 749 750 751 752 753 754 755
void test_index_tests__honors_protect_filesystems(void)
{
	git_repository *repo;

	p_mkdir("invalid", 0700);

	cl_git_pass(git_repository_init(&repo, "./invalid", 0));

	cl_repo_set_bool(repo, "core.protectHFS", true);
	cl_repo_set_bool(repo, "core.protectNTFS", true);

756 757 758 759
	assert_write_fails(repo, ".git./hello");
	assert_write_fails(repo, ".git\xe2\x80\xad/hello");
	assert_write_fails(repo, "git~1/hello");
	assert_write_fails(repo, ".git\xe2\x81\xaf/hello");
760
	assert_write_fails(repo, ".git::$INDEX_ALLOCATION/dummy-file");
761 762 763 764 765 766

	git_repository_free(repo);

	cl_fixture_cleanup("invalid");
}

767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
void test_index_tests__protectntfs_on_by_default(void)
{
	git_repository *repo;

	p_mkdir("invalid", 0700);

	cl_git_pass(git_repository_init(&repo, "./invalid", 0));
	assert_write_fails(repo, ".git./hello");
	assert_write_fails(repo, "git~1/hello");

	git_repository_free(repo);

	cl_fixture_cleanup("invalid");
}

782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
void test_index_tests__can_disable_protectntfs(void)
{
	git_repository *repo;
	git_index *index;

	cl_must_pass(p_mkdir("valid", 0700));
	cl_git_rewritefile("valid/git~1", "steal the shortname");

	cl_git_pass(git_repository_init(&repo, "./valid", 0));
	cl_git_pass(git_repository_index(&index, repo));
	cl_repo_set_bool(repo, "core.protectNTFS", false);

	cl_git_pass(git_index_add_bypath(index, "git~1"));

	git_index_free(index);
	git_repository_free(repo);

	cl_fixture_cleanup("valid");
}

802 803 804 805 806 807 808 809 810 811 812 813
void test_index_tests__remove_entry(void)
{
	git_repository *repo;
	git_index *index;

	p_mkdir("index_test", 0770);

	cl_git_pass(git_repository_init(&repo, "index_test", 0));
	cl_git_pass(git_repository_index(&index, repo));
	cl_assert(git_index_entrycount(index) == 0);

	cl_git_mkfile("index_test/hello", NULL);
814
	cl_git_pass(git_index_add_bypath(index, "hello"));
815 816
	cl_git_pass(git_index_write(index));

817
	cl_git_pass(git_index_read(index, true)); /* reload */
818 819 820 821 822 823
	cl_assert(git_index_entrycount(index) == 1);
	cl_assert(git_index_get_bypath(index, "hello", 0) != NULL);

	cl_git_pass(git_index_remove(index, "hello", 0));
	cl_git_pass(git_index_write(index));

824
	cl_git_pass(git_index_read(index, true)); /* reload */
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
	cl_assert(git_index_entrycount(index) == 0);
	cl_assert(git_index_get_bypath(index, "hello", 0) == NULL);

	git_index_free(index);
	git_repository_free(repo);
	cl_fixture_cleanup("index_test");
}

void test_index_tests__remove_directory(void)
{
	git_repository *repo;
	git_index *index;

	p_mkdir("index_test", 0770);

	cl_git_pass(git_repository_init(&repo, "index_test", 0));
	cl_git_pass(git_repository_index(&index, repo));
	cl_assert_equal_i(0, (int)git_index_entrycount(index));

	p_mkdir("index_test/a", 0770);
	cl_git_mkfile("index_test/a/1.txt", NULL);
	cl_git_mkfile("index_test/a/2.txt", NULL);
	cl_git_mkfile("index_test/a/3.txt", NULL);
	cl_git_mkfile("index_test/b.txt", NULL);

850 851 852 853
	cl_git_pass(git_index_add_bypath(index, "a/1.txt"));
	cl_git_pass(git_index_add_bypath(index, "a/2.txt"));
	cl_git_pass(git_index_add_bypath(index, "a/3.txt"));
	cl_git_pass(git_index_add_bypath(index, "b.txt"));
854 855
	cl_git_pass(git_index_write(index));

856
	cl_git_pass(git_index_read(index, true)); /* reload */
857 858 859 860 861 862 863 864
	cl_assert_equal_i(4, (int)git_index_entrycount(index));
	cl_assert(git_index_get_bypath(index, "a/1.txt", 0) != NULL);
	cl_assert(git_index_get_bypath(index, "a/2.txt", 0) != NULL);
	cl_assert(git_index_get_bypath(index, "b.txt", 0) != NULL);

	cl_git_pass(git_index_remove(index, "a/1.txt", 0));
	cl_git_pass(git_index_write(index));

865
	cl_git_pass(git_index_read(index, true)); /* reload */
866 867 868 869 870 871 872 873
	cl_assert_equal_i(3, (int)git_index_entrycount(index));
	cl_assert(git_index_get_bypath(index, "a/1.txt", 0) == NULL);
	cl_assert(git_index_get_bypath(index, "a/2.txt", 0) != NULL);
	cl_assert(git_index_get_bypath(index, "b.txt", 0) != NULL);

	cl_git_pass(git_index_remove_directory(index, "a", 0));
	cl_git_pass(git_index_write(index));

874
	cl_git_pass(git_index_read(index, true)); /* reload */
875 876 877 878 879 880 881 882 883
	cl_assert_equal_i(1, (int)git_index_entrycount(index));
	cl_assert(git_index_get_bypath(index, "a/1.txt", 0) == NULL);
	cl_assert(git_index_get_bypath(index, "a/2.txt", 0) == NULL);
	cl_assert(git_index_get_bypath(index, "b.txt", 0) != NULL);

	git_index_free(index);
	git_repository_free(repo);
	cl_fixture_cleanup("index_test");
}
884 885 886 887 888 889

void test_index_tests__preserves_case(void)
{
	git_repository *repo;
	git_index *index;
	const git_index_entry *entry;
890
	int index_caps;
891 892 893 894 895 896

	cl_set_cleanup(&cleanup_myrepo, NULL);

	cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
	cl_git_pass(git_repository_index(&index, repo));

897 898
	index_caps = git_index_caps(index);

899 900 901 902 903 904 905
	cl_git_rewritefile("myrepo/test.txt", "hey there\n");
	cl_git_pass(git_index_add_bypath(index, "test.txt"));

	cl_git_pass(p_rename("myrepo/test.txt", "myrepo/TEST.txt"));
	cl_git_rewritefile("myrepo/TEST.txt", "hello again\n");
	cl_git_pass(git_index_add_bypath(index, "TEST.txt"));

906
	if (index_caps & GIT_INDEX_CAPABILITY_IGNORE_CASE)
907 908 909
		cl_assert_equal_i(1, (int)git_index_entrycount(index));
	else
		cl_assert_equal_i(2, (int)git_index_entrycount(index));
910 911 912 913 914 915

	/* Test access by path instead of index */
	cl_assert((entry = git_index_get_bypath(index, "test.txt", 0)) != NULL);
	/* The path should *not* have changed without an explicit remove */
	cl_assert(git__strcmp(entry->path, "test.txt") == 0);

916
	cl_assert((entry = git_index_get_bypath(index, "TEST.txt", 0)) != NULL);
917
	if (index_caps & GIT_INDEX_CAPABILITY_IGNORE_CASE)
918 919 920 921 922
		/* The path should *not* have changed without an explicit remove */
		cl_assert(git__strcmp(entry->path, "test.txt") == 0);
	else
		cl_assert(git__strcmp(entry->path, "TEST.txt") == 0);

923 924 925 926
	git_index_free(index);
	git_repository_free(repo);
}

927 928 929 930 931 932 933 934 935 936 937 938 939 940
void test_index_tests__elocked(void)
{
	git_repository *repo;
	git_index *index;
	git_filebuf file = GIT_FILEBUF_INIT;
	const git_error *err;
	int error;

	cl_set_cleanup(&cleanup_myrepo, NULL);

	cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
	cl_git_pass(git_repository_index(&index, repo));

	/* Lock the index file so we fail to lock it */
941
	cl_git_pass(git_filebuf_open(&file, index->index_file_path, 0, 0666));
942 943 944
	error = git_index_write(index);
	cl_assert_equal_i(GIT_ELOCKED, error);

945 946
	err = git_error_last();
	cl_assert_equal_i(err->klass, GIT_ERROR_INDEX);
947 948 949 950 951

	git_filebuf_cleanup(&file);
	git_index_free(index);
	git_repository_free(repo);
}
952 953 954 955 956 957 958 959 960

void test_index_tests__reload_from_disk(void)
{
	git_repository *repo;
	git_index *read_index;
	git_index *write_index;

	cl_set_cleanup(&cleanup_myrepo, NULL);

961
	cl_git_pass(git_futils_mkdir("./myrepo", 0777, GIT_MKDIR_PATH));
962 963 964 965 966 967 968 969 970 971
	cl_git_mkfile("./myrepo/a.txt", "a\n");
	cl_git_mkfile("./myrepo/b.txt", "b\n");

	cl_git_pass(git_repository_init(&repo, "./myrepo", 0));
	cl_git_pass(git_repository_index(&write_index, repo));
	cl_assert_equal_i(false, write_index->on_disk);

	cl_git_pass(git_index_open(&read_index, write_index->index_file_path));
	cl_assert_equal_i(false, read_index->on_disk);

Will Stamper committed
972
	/* Stage two new files against the write_index */
973 974 975 976 977 978 979 980 981 982 983 984
	cl_git_pass(git_index_add_bypath(write_index, "a.txt"));
	cl_git_pass(git_index_add_bypath(write_index, "b.txt"));

	cl_assert_equal_sz(2, git_index_entrycount(write_index));

	/* Persist the index changes to disk */
	cl_git_pass(git_index_write(write_index));
	cl_assert_equal_i(true, write_index->on_disk);

	/* Sync the changes back into the read_index */
	cl_assert_equal_sz(0, git_index_entrycount(read_index));

985
	cl_git_pass(git_index_read(read_index, true));
986 987 988 989 990 991 992 993
	cl_assert_equal_i(true, read_index->on_disk);

	cl_assert_equal_sz(2, git_index_entrycount(read_index));

	/* Remove the index file from the filesystem */
	cl_git_pass(p_unlink(write_index->index_file_path));

	/* Sync the changes back into the read_index */
994
	cl_git_pass(git_index_read(read_index, true));
995 996 997 998 999 1000 1001
	cl_assert_equal_i(false, read_index->on_disk);
	cl_assert_equal_sz(0, git_index_entrycount(read_index));

	git_index_free(read_index);
	git_index_free(write_index);
	git_repository_free(repo);
}
1002 1003 1004 1005 1006 1007 1008

void test_index_tests__corrupted_extension(void)
{
	git_index *index;

	cl_git_fail_with(git_index_open(&index, TEST_INDEXBAD_PATH), GIT_ERROR);
}
1009 1010 1011 1012 1013 1014 1015

void test_index_tests__reload_while_ignoring_case(void)
{
	git_index *index;
	unsigned int caps;

	cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
1016
	cl_git_pass(git_vector_verify_sorted(&index->entries));
1017 1018

	caps = git_index_caps(index);
1019
	cl_git_pass(git_index_set_caps(index, caps &= ~GIT_INDEX_CAPABILITY_IGNORE_CASE));
1020
	cl_git_pass(git_index_read(index, true));
1021
	cl_git_pass(git_vector_verify_sorted(&index->entries));
1022 1023
	cl_assert(git_index_get_bypath(index, ".HEADER", 0));
	cl_assert_equal_p(NULL, git_index_get_bypath(index, ".header", 0));
1024

1025
	cl_git_pass(git_index_set_caps(index, caps | GIT_INDEX_CAPABILITY_IGNORE_CASE));
1026
	cl_git_pass(git_index_read(index, true));
1027
	cl_git_pass(git_vector_verify_sorted(&index->entries));
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
	cl_assert(git_index_get_bypath(index, ".HEADER", 0));
	cl_assert(git_index_get_bypath(index, ".header", 0));

	git_index_free(index);
}

void test_index_tests__change_icase_on_instance(void)
{
	git_index *index;
	unsigned int caps;
	const git_index_entry *e;

	cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
	cl_git_pass(git_vector_verify_sorted(&index->entries));

	caps = git_index_caps(index);
1044
	cl_git_pass(git_index_set_caps(index, caps &= ~GIT_INDEX_CAPABILITY_IGNORE_CASE));
1045 1046 1047 1048 1049 1050 1051
	cl_assert_equal_i(false, index->ignore_case);
	cl_git_pass(git_vector_verify_sorted(&index->entries));
	cl_assert(e = git_index_get_bypath(index, "src/common.h", 0));
	cl_assert_equal_p(NULL, e = git_index_get_bypath(index, "SRC/Common.h", 0));
	cl_assert(e = git_index_get_bypath(index, "COPYING", 0));
	cl_assert_equal_p(NULL, e = git_index_get_bypath(index, "copying", 0));

1052
	cl_git_pass(git_index_set_caps(index, caps | GIT_INDEX_CAPABILITY_IGNORE_CASE));
1053 1054 1055 1056 1057 1058
	cl_assert_equal_i(true, index->ignore_case);
	cl_git_pass(git_vector_verify_sorted(&index->entries));
	cl_assert(e = git_index_get_bypath(index, "COPYING", 0));
	cl_assert_equal_s("COPYING", e->path);
	cl_assert(e = git_index_get_bypath(index, "copying", 0));
	cl_assert_equal_s("COPYING", e->path);
1059 1060 1061

	git_index_free(index);
}
1062 1063 1064

void test_index_tests__can_lock_index(void)
{
1065
	git_repository *repo;
1066 1067 1068 1069
	git_index *index;
	git_indexwriter one = GIT_INDEXWRITER_INIT,
		two = GIT_INDEXWRITER_INIT;

1070 1071 1072
	repo = cl_git_sandbox_init("testrepo.git");

	cl_git_pass(git_repository_index(&index, repo));
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
	cl_git_pass(git_indexwriter_init(&one, index));

	cl_git_fail_with(GIT_ELOCKED, git_indexwriter_init(&two, index));
	cl_git_fail_with(GIT_ELOCKED, git_index_write(index));

	cl_git_pass(git_indexwriter_commit(&one));

	cl_git_pass(git_index_write(index));

	git_indexwriter_cleanup(&one);
	git_indexwriter_cleanup(&two);
	git_index_free(index);
1085
	cl_git_sandbox_cleanup();
1086
}
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173

void test_index_tests__can_iterate(void)
{
	git_index *index;
	git_index_iterator *iterator;
	const git_index_entry *entry;
	size_t i, iterator_idx = 0, found = 0;
	int ret;

	cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
	cl_git_pass(git_index_iterator_new(&iterator, index));

	cl_assert(git_vector_is_sorted(&iterator->snap));

	for (i = 0; i < ARRAY_SIZE(test_entries); i++) {
		/* Advance iterator to next test entry index */
		do {
			ret = git_index_iterator_next(&entry, iterator);

			if (ret == GIT_ITEROVER)
				cl_fail("iterator did not contain all test entries");

			cl_git_pass(ret);
		} while (iterator_idx++ < test_entries[i].index);

		cl_assert_equal_s(entry->path, test_entries[i].path);
		cl_assert_equal_i(entry->mtime.seconds, test_entries[i].mtime);
		cl_assert_equal_i(entry->file_size, test_entries[i].file_size);
		found++;
	}

	while ((ret = git_index_iterator_next(&entry, iterator)) == 0)
		;

	if (ret != GIT_ITEROVER)
		cl_git_fail(ret);

	cl_assert_equal_i(found, ARRAY_SIZE(test_entries));

	git_index_iterator_free(iterator);
	git_index_free(index);
}

void test_index_tests__can_modify_while_iterating(void)
{
	git_index *index;
	git_index_iterator *iterator;
	const git_index_entry *entry;
	git_index_entry new_entry = {{0}};
	size_t expected = 0, seen = 0;
	int ret;

	cl_git_pass(git_index_open(&index, TEST_INDEX_PATH));
	cl_git_pass(git_index_iterator_new(&iterator, index));

	expected = git_index_entrycount(index);
	cl_assert(git_vector_is_sorted(&iterator->snap));

	/*
	 * After we've counted the entries, add a new one and change another;
	 * ensure that our iterator is backed by a snapshot and thus returns
	 * the number of entries from when the iterator was created.
	 */
	cl_git_pass(git_oid_fromstr(&new_entry.id, "8312e0a89a9cbab77c732b6bc39b51a783e3a318"));
	new_entry.path = "newfile";
	new_entry.mode = GIT_FILEMODE_BLOB;
	cl_git_pass(git_index_add(index, &new_entry));

	cl_git_pass(git_oid_fromstr(&new_entry.id, "4141414141414141414141414141414141414141"));
	new_entry.path = "Makefile";
	new_entry.mode = GIT_FILEMODE_BLOB;
	cl_git_pass(git_index_add(index, &new_entry));

	while (true) {
		ret = git_index_iterator_next(&entry, iterator);

		if (ret == GIT_ITEROVER)
			break;

		seen++;
	}

	cl_assert_equal_i(expected, seen);

	git_index_iterator_free(iterator);
	git_index_free(index);
}