Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4f98ba

Browse files
committed
Addition checkout target directory tests
This adds additonal tests of the checkout target directory option including using it to dump data from bare repos.
1 parent 6a15e8d commit d4f98ba

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

tests-clar/checkout/index.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ void test_checkout_index__target_directory(void)
515515

516516
opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
517517
opts.target_directory = "alternative";
518+
cl_assert(!git_path_isdir("alternative"));
518519

519520
opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
520521
opts.notify_cb = checkout_count_callback;
@@ -533,4 +534,74 @@ void test_checkout_index__target_directory(void)
533534
check_file_contents("./alternative/README", "hey there\n");
534535
check_file_contents("./alternative/branch_file.txt", "hi\nbye!\n");
535536
check_file_contents("./alternative/new.txt", "my new file\n");
537+
538+
cl_git_pass(git_futils_rmdir_r(
539+
"alternative", NULL, GIT_RMDIR_REMOVE_FILES));
540+
}
541+
542+
void test_checkout_index__target_directory_from_bare(void)
543+
{
544+
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
545+
git_index *index;
546+
git_object *head = NULL;
547+
checkout_counts cts;
548+
memset(&cts, 0, sizeof(cts));
549+
550+
test_checkout_index__cleanup();
551+
552+
g_repo = cl_git_sandbox_init("testrepo.git");
553+
cl_assert(git_repository_is_bare(g_repo));
554+
555+
cl_git_pass(git_repository_index(&index, g_repo));
556+
cl_git_pass(git_revparse_single(&head, g_repo, "HEAD^{tree}"));
557+
cl_git_pass(git_index_read_tree(index, (const git_tree *)head));
558+
cl_git_pass(git_index_write(index));
559+
git_index_free(index);
560+
561+
opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
562+
563+
opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
564+
opts.notify_cb = checkout_count_callback;
565+
opts.notify_payload = &cts;
566+
567+
/* fail to checkout a bare repo */
568+
cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
569+
570+
opts.target_directory = "alternative";
571+
cl_assert(!git_path_isdir("alternative"));
572+
573+
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
574+
575+
cl_assert_equal_i(0, cts.n_untracked);
576+
cl_assert_equal_i(0, cts.n_ignored);
577+
cl_assert_equal_i(3, cts.n_updates);
578+
579+
check_file_contents("./alternative/README", "hey there\n");
580+
check_file_contents("./alternative/branch_file.txt", "hi\nbye!\n");
581+
check_file_contents("./alternative/new.txt", "my new file\n");
582+
583+
cl_git_pass(git_futils_rmdir_r(
584+
"alternative", NULL, GIT_RMDIR_REMOVE_FILES));
585+
}
586+
587+
void test_checkout_index__can_get_repo_from_index(void)
588+
{
589+
git_index *index;
590+
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
591+
592+
cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
593+
cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
594+
cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));
595+
596+
opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
597+
598+
cl_git_pass(git_repository_index(&index, g_repo));
599+
600+
cl_git_pass(git_checkout_index(NULL, index, &opts));
601+
602+
check_file_contents("./testrepo/README", "hey there\n");
603+
check_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
604+
check_file_contents("./testrepo/new.txt", "my new file\n");
605+
606+
git_index_free(index);
536607
}

tests-clar/checkout/tree.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,8 @@ void test_checkout_tree__fails_when_dir_in_use(void)
596596
cl_git_pass(p_chdir("../.."));
597597

598598
cl_assert(git_path_is_empty_dir("testrepo/a"));
599+
600+
git_object_free(obj);
599601
#endif
600602
}
601603

@@ -628,5 +630,47 @@ void test_checkout_tree__can_continue_when_dir_in_use(void)
628630
cl_git_pass(p_chdir("../.."));
629631

630632
cl_assert(git_path_is_empty_dir("testrepo/a"));
633+
634+
git_object_free(obj);
631635
#endif
632636
}
637+
638+
void test_checkout_tree__target_directory_from_bare(void)
639+
{
640+
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
641+
git_oid oid;
642+
checkout_counts cts;
643+
memset(&cts, 0, sizeof(cts));
644+
645+
test_checkout_tree__cleanup(); /* cleanup default checkout */
646+
647+
g_repo = cl_git_sandbox_init("testrepo.git");
648+
cl_assert(git_repository_is_bare(g_repo));
649+
650+
opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
651+
652+
opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
653+
opts.notify_cb = checkout_count_callback;
654+
opts.notify_payload = &cts;
655+
656+
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD"));
657+
cl_git_pass(git_object_lookup(&g_object, g_repo, &oid, GIT_OBJ_ANY));
658+
659+
cl_git_fail(git_checkout_tree(g_repo, g_object, &opts));
660+
661+
opts.target_directory = "alternative";
662+
cl_assert(!git_path_isdir("alternative"));
663+
664+
cl_git_pass(git_checkout_tree(g_repo, g_object, &opts));
665+
666+
cl_assert_equal_i(0, cts.n_untracked);
667+
cl_assert_equal_i(0, cts.n_ignored);
668+
cl_assert_equal_i(3, cts.n_updates);
669+
670+
check_file_contents("./alternative/README", "hey there\n");
671+
check_file_contents("./alternative/branch_file.txt", "hi\nbye!\n");
672+
check_file_contents("./alternative/new.txt", "my new file\n");
673+
674+
cl_git_pass(git_futils_rmdir_r(
675+
"alternative", NULL, GIT_RMDIR_REMOVE_FILES));
676+
}

0 commit comments

Comments
 (0)