Blame tests/checkout/index.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "checkout_helpers.h"
Packit Service 20376f
Packit Service 20376f
#include "git2/checkout.h"
Packit Service 20376f
#include "fileops.h"
Packit Service 20376f
#include "repository.h"
Packit Service 20376f
#include "remote.h"
Packit Service 20376f
Packit Service 20376f
static git_repository *g_repo;
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__initialize(void)
Packit Service 20376f
{
Packit Service 20376f
	git_tree *tree;
Packit Service 20376f
Packit Service 20376f
	g_repo = cl_git_sandbox_init("testrepo");
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_head_tree(&tree, g_repo));
Packit Service 20376f
Packit Service 20376f
	reset_index_to_treeish((git_object *)tree);
Packit Service 20376f
	git_tree_free(tree);
Packit Service 20376f
Packit Service 20376f
	cl_git_rewritefile(
Packit Service 20376f
		"./testrepo/.gitattributes",
Packit Service 20376f
		"* text eol=lf\n");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__cleanup(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_sandbox_cleanup();
Packit Service 20376f
Packit Service 20376f
	/* try to remove alternative dir */
Packit Service 20376f
	if (git_path_isdir("alternative"))
Packit Service 20376f
		git_futils_rmdir_r("alternative", NULL, GIT_RMDIR_REMOVE_FILES);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__cannot_checkout_a_bare_repository(void)
Packit Service 20376f
{
Packit Service 20376f
	test_checkout_index__cleanup();
Packit Service 20376f
Packit Service 20376f
	g_repo = cl_git_sandbox_init("testrepo.git");
Packit Service 20376f
Packit Service 20376f
	cl_git_fail(git_checkout_index(g_repo, NULL, NULL));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__can_create_missing_files(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
Packit Service 20376f
	cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
Packit Service 20376f
	cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	check_file_contents("./testrepo/README", "hey there\n");
Packit Service 20376f
	check_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
Packit Service 20376f
	check_file_contents("./testrepo/new.txt", "my new file\n");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__can_remove_untracked_files(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
Packit Service 20376f
	git_futils_mkdir("./testrepo/dir/subdir/subsubdir", 0755, GIT_MKDIR_PATH);
Packit Service 20376f
	cl_git_mkfile("./testrepo/dir/one", "one\n");
Packit Service 20376f
	cl_git_mkfile("./testrepo/dir/subdir/two", "two\n");
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(true, git_path_isdir("./testrepo/dir/subdir/subsubdir"));
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy =
Packit Service 20376f
		GIT_CHECKOUT_SAFE |
Packit Service 20376f
		GIT_CHECKOUT_RECREATE_MISSING |
Packit Service 20376f
		GIT_CHECKOUT_REMOVE_UNTRACKED;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(false, git_path_isdir("./testrepo/dir"));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__honor_the_specified_pathspecs(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
	char *entries[] = { "*.txt" };
Packit Service 20376f
Packit Service 20376f
	opts.paths.strings = entries;
Packit Service 20376f
	opts.paths.count = 1;
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
Packit Service 20376f
	cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
Packit Service 20376f
	cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
Packit Service 20376f
	check_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
Packit Service 20376f
	check_file_contents("./testrepo/new.txt", "my new file\n");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__honor_the_gitattributes_directives(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
	const char *attributes =
Packit Service 20376f
		"branch_file.txt text eol=crlf\n"
Packit Service 20376f
		"new.txt text eol=lf\n";
Packit Service 20376f
Packit Service 20376f
	cl_git_mkfile("./testrepo/.gitattributes", attributes);
Packit Service 20376f
	cl_repo_set_bool(g_repo, "core.autocrlf", false);
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	check_file_contents("./testrepo/README", "hey there\n");
Packit Service 20376f
	check_file_contents("./testrepo/new.txt", "my new file\n");
Packit Service 20376f
	check_file_contents("./testrepo/branch_file.txt", "hi\r\nbye!\r\n");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__honor_coreautocrlf_setting_set_to_true(void)
Packit Service 20376f
{
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
	const char *expected_readme_text = "hey there\r\n";
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(p_unlink("./testrepo/.gitattributes"));
Packit Service 20376f
	cl_repo_set_bool(g_repo, "core.autocrlf", true);
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	check_file_contents("./testrepo/README", expected_readme_text);
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__honor_coresymlinks_default(void)
Packit Service 20376f
{
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
	git_remote *origin;
Packit Service 20376f
	git_object *target;
Packit Service 20376f
	char cwd[GIT_PATH_MAX];
Packit Service 20376f
Packit Service 20376f
	const char *url = git_repository_path(g_repo);
Packit Service 20376f
Packit Service 20376f
	cl_assert(getcwd(cwd, sizeof(cwd)) != NULL);
Packit Service 20376f
	cl_assert_equal_i(0, p_mkdir("readonly", 0555)); // Read-only directory
Packit Service 20376f
	cl_assert_equal_i(0, chdir("readonly"));
Packit Service 20376f
	cl_git_pass(git_repository_init(&repo, "../symlink.git", true));
Packit Service 20376f
	cl_assert_equal_i(0, chdir(cwd));
Packit Service 20376f
	cl_assert_equal_i(0, p_mkdir("symlink", 0777));
Packit Service 20376f
	cl_git_pass(git_repository_set_workdir(repo, "symlink", 1));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url));
Packit Service 20376f
	cl_git_pass(git_remote_fetch(origin, NULL, NULL, NULL));
Packit Service 20376f
	git_remote_free(origin);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_revparse_single(&target, repo, "remotes/origin/master"));
Packit Service 20376f
	cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
Packit Service 20376f
	git_object_free(target);
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	check_file_contents("./symlink/link_to_new.txt", "new.txt");
Packit Service 20376f
#else
Packit Service 20376f
	{
Packit Service 20376f
		char link_data[1024];
Packit Service 20376f
		size_t link_size = 1024;
Packit Service 20376f
Packit Service 20376f
		link_size = p_readlink("./symlink/link_to_new.txt", link_data, link_size);
Packit Service 20376f
		link_data[link_size] = '\0';
Packit Service 20376f
		cl_assert_equal_i(link_size, strlen("new.txt"));
Packit Service 20376f
		cl_assert_equal_s(link_data, "new.txt");
Packit Service 20376f
		check_file_contents("./symlink/link_to_new.txt", "my new file\n");
Packit Service 20376f
	}
Packit Service 20376f
#endif
Packit Service 20376f
Packit Service 20376f
	cl_fixture_cleanup("symlink");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__honor_coresymlinks_setting_set_to_true(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_repo_set_bool(g_repo, "core.symlinks", true);
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	check_file_contents("./testrepo/link_to_new.txt", "new.txt");
Packit Service 20376f
#else
Packit Service 20376f
	{
Packit Service 20376f
		char link_data[1024];
Packit Service 20376f
		size_t link_size = 1024;
Packit Service 20376f
Packit Service 20376f
		link_size = p_readlink("./testrepo/link_to_new.txt", link_data, link_size);
Packit Service 20376f
		link_data[link_size] = '\0';
Packit Service 20376f
		cl_assert_equal_i(link_size, strlen("new.txt"));
Packit Service 20376f
		cl_assert_equal_s(link_data, "new.txt");
Packit Service 20376f
		check_file_contents("./testrepo/link_to_new.txt", "my new file\n");
Packit Service 20376f
	}
Packit Service 20376f
#endif
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__honor_coresymlinks_setting_set_to_false(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_repo_set_bool(g_repo, "core.symlinks", false);
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	check_file_contents("./testrepo/link_to_new.txt", "new.txt");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__donot_overwrite_modified_file_by_default(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
Packit Service 20376f
Packit Service 20376f
	/* set this up to not return an error code on conflicts, but it
Packit Service 20376f
	 * still will not have permission to overwrite anything...
Packit Service 20376f
	 */
Packit Service 20376f
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_ALLOW_CONFLICTS;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	check_file_contents("./testrepo/new.txt", "This isn't what's stored!");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__can_overwrite_modified_file(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	check_file_contents("./testrepo/new.txt", "my new file\n");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__options_disable_filters(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_mkfile("./testrepo/.gitattributes", "*.txt text eol=crlf\n");
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
Packit Service 20376f
	opts.disable_filters = false;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	check_file_contents("./testrepo/new.txt", "my new file\r\n");
Packit Service 20376f
Packit Service 20376f
	p_unlink("./testrepo/new.txt");
Packit Service 20376f
Packit Service 20376f
	opts.disable_filters = true;
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	check_file_contents("./testrepo/new.txt", "my new file\n");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__options_dir_modes(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
	struct stat st;
Packit Service 20376f
	git_oid oid;
Packit Service 20376f
	git_commit *commit;
Packit Service 20376f
	mode_t um;
Packit Service 20376f
Packit Service 20376f
	if (!cl_is_chmod_supported())
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
Packit Service 20376f
	cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));
Packit Service 20376f
Packit Service 20376f
	reset_index_to_treeish((git_object *)commit);
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
Packit Service 20376f
	opts.dir_mode = 0701;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	/* umask will influence actual directory creation mode */
Packit Service 20376f
	(void)p_umask(um = p_umask(022));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(p_stat("./testrepo/a", &st);;
Packit Service 20376f
	/* Haiku & Hurd use other mode bits, so we must mask them out */
Packit Service 20376f
	cl_assert_equal_i_fmt(st.st_mode & (S_IFMT | 07777), (GIT_FILEMODE_TREE | 0701) & ~um, "%07o");
Packit Service 20376f
Packit Service 20376f
	/* File-mode test, since we're on the 'dir' branch */
Packit Service 20376f
	cl_git_pass(p_stat("./testrepo/a/b.txt", &st);;
Packit Service 20376f
	cl_assert_equal_i_fmt(st.st_mode & (S_IFMT | 07777), GIT_FILEMODE_BLOB_EXECUTABLE & ~um, "%07o");
Packit Service 20376f
Packit Service 20376f
	git_commit_free(commit);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__options_override_file_modes(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
	struct stat st;
Packit Service 20376f
Packit Service 20376f
	if (!cl_is_chmod_supported())
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
Packit Service 20376f
	opts.file_mode = 0700;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(p_stat("./testrepo/new.txt", &st);;
Packit Service 20376f
	cl_assert_equal_i_fmt(st.st_mode & GIT_MODE_PERMS_MASK, 0700, "%07o");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__options_open_flags(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_mkfile("./testrepo/new.txt", "hi\n");
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy =
Packit Service 20376f
		GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_REMOVE_EXISTING;
Packit Service 20376f
	opts.file_open_flags = O_CREAT | O_RDWR | O_APPEND;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	check_file_contents("./testrepo/new.txt", "hi\nmy new file\n");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
struct notify_data {
Packit Service 20376f
	const char *file;
Packit Service 20376f
	const char *sha;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
static int test_checkout_notify_cb(
Packit Service 20376f
	git_checkout_notify_t why,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	const git_diff_file *baseline,
Packit Service 20376f
	const git_diff_file *target,
Packit Service 20376f
	const git_diff_file *workdir,
Packit Service 20376f
	void *payload)
Packit Service 20376f
{
Packit Service 20376f
	struct notify_data *expectations = (struct notify_data *)payload;
Packit Service 20376f
Packit Service 20376f
	GIT_UNUSED(workdir);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(GIT_CHECKOUT_NOTIFY_CONFLICT, why);
Packit Service 20376f
	cl_assert_equal_s(expectations->file, path);
Packit Service 20376f
	cl_assert_equal_i(0, git_oid_streq(&baseline->id, expectations->sha));
Packit Service 20376f
	cl_assert_equal_i(0, git_oid_streq(&target->id, expectations->sha));
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__can_notify_of_skipped_files(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
	struct notify_data data;
Packit Service 20376f
Packit Service 20376f
	cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
Packit Service 20376f
Packit Service 20376f
	/*
Packit Service 20376f
	 * $ git ls-tree HEAD
Packit Service 20376f
	 * 100644 blob a8233120f6ad708f843d861ce2b7228ec4e3dec6    README
Packit Service 20376f
	 * 100644 blob 3697d64be941a53d4ae8f6a271e4e3fa56b022cc    branch_file.txt
Packit Service 20376f
	 * 100644 blob a71586c1dfe8a71c6cbf6c129f404c5642ff31bd    new.txt
Packit Service 20376f
	 */
Packit Service 20376f
	data.file = "new.txt";
Packit Service 20376f
	data.sha = "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd";
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy = GIT_CHECKOUT_SAFE |
Packit Service 20376f
		GIT_CHECKOUT_RECREATE_MISSING |
Packit Service 20376f
		GIT_CHECKOUT_ALLOW_CONFLICTS;
Packit Service 20376f
	opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
Packit Service 20376f
	opts.notify_cb = test_checkout_notify_cb;
Packit Service 20376f
	opts.notify_payload = &dat;;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static int dont_notify_cb(
Packit Service 20376f
	git_checkout_notify_t why,
Packit Service 20376f
	const char *path,
Packit Service 20376f
	const git_diff_file *baseline,
Packit Service 20376f
	const git_diff_file *target,
Packit Service 20376f
	const git_diff_file *workdir,
Packit Service 20376f
	void *payload)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(why);
Packit Service 20376f
	GIT_UNUSED(path);
Packit Service 20376f
	GIT_UNUSED(baseline);
Packit Service 20376f
	GIT_UNUSED(target);
Packit Service 20376f
	GIT_UNUSED(workdir);
Packit Service 20376f
	GIT_UNUSED(payload);
Packit Service 20376f
Packit Service 20376f
	cl_assert(false);
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__wont_notify_of_expected_line_ending_changes(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(p_unlink("./testrepo/.gitattributes"));
Packit Service 20376f
	cl_repo_set_bool(g_repo, "core.autocrlf", true);
Packit Service 20376f
Packit Service 20376f
	cl_git_mkfile("./testrepo/new.txt", "my new file\r\n");
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy =
Packit Service 20376f
		GIT_CHECKOUT_SAFE |
Packit Service 20376f
		GIT_CHECKOUT_RECREATE_MISSING |
Packit Service 20376f
		GIT_CHECKOUT_ALLOW_CONFLICTS;
Packit Service 20376f
	opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
Packit Service 20376f
	opts.notify_cb = dont_notify_cb;
Packit Service 20376f
	opts.notify_payload = NULL;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void checkout_progress_counter(
Packit Service 20376f
	const char *path, size_t cur, size_t tot, void *payload)
Packit Service 20376f
{
Packit Service 20376f
	GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot);
Packit Service 20376f
	(*(int *)payload)++;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__calls_progress_callback(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
	int calls = 0;
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
Packit Service 20376f
	opts.progress_cb = checkout_progress_counter;
Packit Service 20376f
	opts.progress_payload = &calls;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
	cl_assert(calls > 0);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__can_overcome_name_clashes(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
	git_index *index;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_index(&index, g_repo));
Packit Service 20376f
	git_index_clear(index);
Packit Service 20376f
Packit Service 20376f
	cl_git_mkfile("./testrepo/path0", "content\r\n");
Packit Service 20376f
	cl_git_pass(p_mkdir("./testrepo/path1", 0777));
Packit Service 20376f
	cl_git_mkfile("./testrepo/path1/file1", "content\r\n");
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_index_add_bypath(index, "path0"));
Packit Service 20376f
	cl_git_pass(git_index_add_bypath(index, "path1/file1"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(p_unlink("./testrepo/path0"));
Packit Service 20376f
	cl_git_pass(git_futils_rmdir_r(
Packit Service 20376f
		"./testrepo/path1", NULL, GIT_RMDIR_REMOVE_FILES));
Packit Service 20376f
Packit Service 20376f
	cl_git_mkfile("./testrepo/path1", "content\r\n");
Packit Service 20376f
	cl_git_pass(p_mkdir("./testrepo/path0", 0777));
Packit Service 20376f
	cl_git_mkfile("./testrepo/path0/file0", "content\r\n");
Packit Service 20376f
Packit Service 20376f
	cl_assert(git_path_isfile("./testrepo/path1"));
Packit Service 20376f
	cl_assert(git_path_isfile("./testrepo/path0/file0"));
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy =
Packit Service 20376f
		GIT_CHECKOUT_SAFE | 
Packit Service 20376f
		GIT_CHECKOUT_RECREATE_MISSING |
Packit Service 20376f
		GIT_CHECKOUT_ALLOW_CONFLICTS;
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, index, &opts));
Packit Service 20376f
Packit Service 20376f
	cl_assert(git_path_isfile("./testrepo/path1"));
Packit Service 20376f
	cl_assert(git_path_isfile("./testrepo/path0/file0"));
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, index, &opts));
Packit Service 20376f
Packit Service 20376f
	cl_assert(git_path_isfile("./testrepo/path0"));
Packit Service 20376f
	cl_assert(git_path_isfile("./testrepo/path1/file1"));
Packit Service 20376f
Packit Service 20376f
	git_index_free(index);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__validates_struct_version(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
	const git_error *err;
Packit Service 20376f
Packit Service 20376f
	opts.version = 1024;
Packit Service 20376f
	cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	err = giterr_last();
Packit Service 20376f
	cl_assert_equal_i(err->klass, GITERR_INVALID);
Packit Service 20376f
Packit Service 20376f
	opts.version = 0;
Packit Service 20376f
	giterr_clear();
Packit Service 20376f
	cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	err = giterr_last();
Packit Service 20376f
	cl_assert_equal_i(err->klass, GITERR_INVALID);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__can_update_prefixed_files(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
Packit Service 20376f
	cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
Packit Service 20376f
	cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));
Packit Service 20376f
Packit Service 20376f
	cl_git_mkfile("./testrepo/READ", "content\n");
Packit Service 20376f
	cl_git_mkfile("./testrepo/README.after", "content\n");
Packit Service 20376f
	cl_git_pass(p_mkdir("./testrepo/branch_file", 0777));
Packit Service 20376f
	cl_git_pass(p_mkdir("./testrepo/branch_file/contained_dir", 0777));
Packit Service 20376f
	cl_git_mkfile("./testrepo/branch_file/contained_file", "content\n");
Packit Service 20376f
	cl_git_pass(p_mkdir("./testrepo/branch_file.txt.after", 0777));
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy =
Packit Service 20376f
		GIT_CHECKOUT_SAFE |
Packit Service 20376f
		GIT_CHECKOUT_RECREATE_MISSING |
Packit Service 20376f
		GIT_CHECKOUT_REMOVE_UNTRACKED;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	/* remove untracked will remove the .gitattributes file before the blobs
Packit Service 20376f
	 * were created, so they will have had crlf filtering applied on Windows
Packit Service 20376f
	 */
Packit Service 20376f
	check_file_contents_nocr("./testrepo/README", "hey there\n");
Packit Service 20376f
	check_file_contents_nocr("./testrepo/branch_file.txt", "hi\nbye!\n");
Packit Service 20376f
	check_file_contents_nocr("./testrepo/new.txt", "my new file\n");
Packit Service 20376f
Packit Service 20376f
	cl_assert(!git_path_exists("testrepo/READ"));
Packit Service 20376f
	cl_assert(!git_path_exists("testrepo/README.after"));
Packit Service 20376f
	cl_assert(!git_path_exists("testrepo/branch_file"));
Packit Service 20376f
	cl_assert(!git_path_exists("testrepo/branch_file.txt.after"));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__can_checkout_a_newly_initialized_repository(void)
Packit Service 20376f
{
Packit Service 20376f
	test_checkout_index__cleanup();
Packit Service 20376f
Packit Service 20376f
	g_repo = cl_git_sandbox_init("empty_standard_repo");
Packit Service 20376f
	cl_git_remove_placeholders(git_repository_path(g_repo), "dummy-marker.txt");
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, NULL));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__issue_1397(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
Packit Service 20376f
	test_checkout_index__cleanup();
Packit Service 20376f
Packit Service 20376f
	g_repo = cl_git_sandbox_init("issue_1397");
Packit Service 20376f
Packit Service 20376f
	cl_repo_set_bool(g_repo, "core.autocrlf", true);
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	check_file_contents("./issue_1397/crlf_file.txt", "first line\r\nsecond line\r\nboth with crlf");
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__target_directory(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
	checkout_counts cts;
Packit Service 20376f
	memset(&cts, 0, sizeof(cts));
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy = GIT_CHECKOUT_SAFE |
Packit Service 20376f
		GIT_CHECKOUT_RECREATE_MISSING;
Packit Service 20376f
	opts.target_directory = "alternative";
Packit Service 20376f
	cl_assert(!git_path_isdir("alternative"));
Packit Service 20376f
Packit Service 20376f
	opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
Packit Service 20376f
	opts.notify_cb = checkout_count_callback;
Packit Service 20376f
	opts.notify_payload = &cts;
Packit Service 20376f
Packit Service 20376f
	/* create some files that *would* conflict if we were using the wd */
Packit Service 20376f
	cl_git_mkfile("testrepo/README", "I'm in the way!\n");
Packit Service 20376f
	cl_git_mkfile("testrepo/new.txt", "my new file\n");
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(0, cts.n_untracked);
Packit Service 20376f
	cl_assert_equal_i(0, cts.n_ignored);
Packit Service 20376f
	cl_assert_equal_i(4, cts.n_updates);
Packit Service 20376f
Packit Service 20376f
	check_file_contents("./alternative/README", "hey there\n");
Packit Service 20376f
	check_file_contents("./alternative/branch_file.txt", "hi\nbye!\n");
Packit Service 20376f
	check_file_contents("./alternative/new.txt", "my new file\n");
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_futils_rmdir_r(
Packit Service 20376f
		"alternative", NULL, GIT_RMDIR_REMOVE_FILES));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__target_directory_from_bare(void)
Packit Service 20376f
{
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
	git_index *index;
Packit Service 20376f
	git_object *head = NULL;
Packit Service 20376f
	checkout_counts cts;
Packit Service 20376f
	memset(&cts, 0, sizeof(cts));
Packit Service 20376f
Packit Service 20376f
	test_checkout_index__cleanup();
Packit Service 20376f
Packit Service 20376f
	g_repo = cl_git_sandbox_init("testrepo.git");
Packit Service 20376f
	cl_assert(git_repository_is_bare(g_repo));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_index(&index, g_repo));
Packit Service 20376f
	cl_git_pass(git_revparse_single(&head, g_repo, "HEAD^{tree}"));
Packit Service 20376f
	cl_git_pass(git_index_read_tree(index, (const git_tree *)head));
Packit Service 20376f
	cl_git_pass(git_index_write(index));
Packit Service 20376f
	git_index_free(index);
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy = GIT_CHECKOUT_SAFE |
Packit Service 20376f
		GIT_CHECKOUT_RECREATE_MISSING;
Packit Service 20376f
Packit Service 20376f
	opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
Packit Service 20376f
	opts.notify_cb = checkout_count_callback;
Packit Service 20376f
	opts.notify_payload = &cts;
Packit Service 20376f
Packit Service 20376f
	/* fail to checkout a bare repo */
Packit Service 20376f
	cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	opts.target_directory = "alternative";
Packit Service 20376f
	cl_assert(!git_path_isdir("alternative"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(0, cts.n_untracked);
Packit Service 20376f
	cl_assert_equal_i(0, cts.n_ignored);
Packit Service 20376f
	cl_assert_equal_i(3, cts.n_updates);
Packit Service 20376f
Packit Service 20376f
	/* files will have been filtered if needed, so strip CR */
Packit Service 20376f
	check_file_contents_nocr("./alternative/README", "hey there\n");
Packit Service 20376f
	check_file_contents_nocr("./alternative/branch_file.txt", "hi\nbye!\n");
Packit Service 20376f
	check_file_contents_nocr("./alternative/new.txt", "my new file\n");
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_futils_rmdir_r(
Packit Service 20376f
		"alternative", NULL, GIT_RMDIR_REMOVE_FILES));
Packit Service 20376f
Packit Service 20376f
	git_object_free(head);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__can_get_repo_from_index(void)
Packit Service 20376f
{
Packit Service 20376f
	git_index *index;
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
Packit Service 20376f
	cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
Packit Service 20376f
	cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));
Packit Service 20376f
Packit Service 20376f
	opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_index(&index, g_repo));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(NULL, index, &opts));
Packit Service 20376f
Packit Service 20376f
	check_file_contents("./testrepo/README", "hey there\n");
Packit Service 20376f
	check_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
Packit Service 20376f
	check_file_contents("./testrepo/new.txt", "my new file\n");
Packit Service 20376f
Packit Service 20376f
	git_index_free(index);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void add_conflict(git_index *index, const char *path)
Packit Service 20376f
{
Packit Service 20376f
	git_index_entry entry;
Packit Service 20376f
Packit Service 20376f
	memset(&entry, 0, sizeof(git_index_entry));
Packit Service 20376f
Packit Service 20376f
	entry.mode = 0100644;
Packit Service 20376f
	entry.path = path;
Packit Service 20376f
Packit Service 20376f
	git_oid_fromstr(&entry.id, "d427e0b2e138501a3d15cc376077a3631e15bd46");
Packit Service 20376f
	GIT_IDXENTRY_STAGE_SET(&entry, 1);
Packit Service 20376f
	cl_git_pass(git_index_add(index, &entry));
Packit Service 20376f
Packit Service 20376f
	git_oid_fromstr(&entry.id, "4e886e602529caa9ab11d71f86634bd1b6e0de10");
Packit Service 20376f
	GIT_IDXENTRY_STAGE_SET(&entry, 2);
Packit Service 20376f
	cl_git_pass(git_index_add(index, &entry));
Packit Service 20376f
Packit Service 20376f
	git_oid_fromstr(&entry.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863");
Packit Service 20376f
	GIT_IDXENTRY_STAGE_SET(&entry, 3);
Packit Service 20376f
	cl_git_pass(git_index_add(index, &entry));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__writes_conflict_file(void)
Packit Service 20376f
{
Packit Service 20376f
	git_index *index;
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
	git_buf conflicting_buf = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_index(&index, g_repo));
Packit Service 20376f
Packit Service 20376f
	add_conflict(index, "conflicting.txt");
Packit Service 20376f
	cl_git_pass(git_index_write(index));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_futils_readbuffer(&conflicting_buf, "testrepo/conflicting.txt"));
Packit Service 20376f
	cl_assert(strcmp(conflicting_buf.ptr,
Packit Service 20376f
		"<<<<<<< ours\n"
Packit Service 20376f
		"this file is changed in master and branch\n"
Packit Service 20376f
		"=======\n"
Packit Service 20376f
		"this file is changed in branch and master\n"
Packit Service 20376f
		">>>>>>> theirs\n") == 0);
Packit Service 20376f
	git_buf_free(&conflicting_buf);
Packit Service 20376f
Packit Service 20376f
	git_index_free(index);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__adding_conflict_removes_stage_0(void)
Packit Service 20376f
{
Packit Service 20376f
	git_index *new_index, *index;
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_index_new(&new_index));
Packit Service 20376f
Packit Service 20376f
	add_conflict(new_index, "new.txt");
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, new_index, &opts));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_index(&index, g_repo));
Packit Service 20376f
Packit Service 20376f
	cl_assert(git_index_get_bypath(index, "new.txt", 0) == NULL);
Packit Service 20376f
	cl_assert(git_index_get_bypath(index, "new.txt", 1) != NULL);
Packit Service 20376f
	cl_assert(git_index_get_bypath(index, "new.txt", 2) != NULL);
Packit Service 20376f
	cl_assert(git_index_get_bypath(index, "new.txt", 3) != NULL);
Packit Service 20376f
Packit Service 20376f
	git_index_free(index);
Packit Service 20376f
	git_index_free(new_index);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_checkout_index__conflicts_honor_coreautocrlf(void)
Packit Service 20376f
{
Packit Service 20376f
#ifdef GIT_WIN32
Packit Service 20376f
	git_index *index;
Packit Service 20376f
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
Packit Service 20376f
	git_buf conflicting_buf = GIT_BUF_INIT;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(p_unlink("./testrepo/.gitattributes"));
Packit Service 20376f
	cl_repo_set_bool(g_repo, "core.autocrlf", true);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_repository_index(&index, g_repo));
Packit Service 20376f
Packit Service 20376f
	add_conflict(index, "conflicting.txt");
Packit Service 20376f
	cl_git_pass(git_index_write(index));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_futils_readbuffer(&conflicting_buf, "testrepo/conflicting.txt"));
Packit Service 20376f
	cl_assert(strcmp(conflicting_buf.ptr,
Packit Service 20376f
		"<<<<<<< ours\r\n"
Packit Service 20376f
		"this file is changed in master and branch\r\n"
Packit Service 20376f
		"=======\r\n"
Packit Service 20376f
		"this file is changed in branch and master\r\n"
Packit Service 20376f
		">>>>>>> theirs\r\n") == 0);
Packit Service 20376f
	git_buf_free(&conflicting_buf);
Packit Service 20376f
Packit Service 20376f
	git_index_free(index);
Packit Service 20376f
#endif
Packit Service 20376f
}