checkout.c 5.58 KB
Newer Older
Ben Straub committed
1 2 3 4 5 6 7 8 9 10
#include "clar_libgit2.h"

#include "git2/checkout.h"
#include "repository.h"


static git_repository *g_repo;

void test_checkout_checkout__initialize(void)
{
11
	const char *attributes = "* text eol=lf\n";
12

Ben Straub committed
13
	g_repo = cl_git_sandbox_init("testrepo");
14
	cl_git_mkfile("./testrepo/.gitattributes", attributes);
Ben Straub committed
15 16 17 18 19 20 21 22 23 24 25 26
}

void test_checkout_checkout__cleanup(void)
{
	cl_git_sandbox_cleanup();
}


static void test_file_contents(const char *path, const char *expectedcontents)
{
	int fd;
	char buffer[1024] = {0};
27
	size_t expectedlen, actuallen;
28

Ben Straub committed
29
	fd = p_open(path, O_RDONLY);
30
	cl_assert(fd >= 0);
31

32 33
	expectedlen = strlen(expectedcontents);
	actuallen = p_read(fd, buffer, 1024);
Ben Straub committed
34
	cl_git_pass(p_close(fd));
35

36
	cl_assert_equal_sz(actuallen, expectedlen);
37
	cl_assert_equal_s(buffer, expectedcontents);
Ben Straub committed
38 39 40 41 42 43 44
}


void test_checkout_checkout__bare(void)
{
	cl_git_sandbox_cleanup();
	g_repo = cl_git_sandbox_init("testrepo.git");
45
	cl_git_fail(git_checkout_head(g_repo, NULL, NULL));
Ben Straub committed
46 47 48 49
}

void test_checkout_checkout__default(void)
{
50
	cl_git_pass(git_checkout_head(g_repo, NULL, NULL));
Ben Straub committed
51 52 53 54 55 56 57 58 59 60 61
	test_file_contents("./testrepo/README", "hey there\n");
	test_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
	test_file_contents("./testrepo/new.txt", "my new file\n");
}


void test_checkout_checkout__crlf(void)
{
	const char *attributes =
		"branch_file.txt text eol=crlf\n"
		"new.txt text eol=lf\n";
Ben Straub committed
62 63 64 65
	git_config *cfg;

	cl_git_pass(git_repository_config__weakptr(&cfg, g_repo));
	cl_git_pass(git_config_set_bool(cfg, "core.autocrlf", false));
Ben Straub committed
66
	cl_git_mkfile("./testrepo/.gitattributes", attributes);
Ben Straub committed
67

68
	cl_git_pass(git_checkout_head(g_repo, NULL, NULL));
Ben Straub committed
69
	test_file_contents("./testrepo/README", "hey there\n"); 
70 71
	test_file_contents("./testrepo/new.txt", "my new file\n"); 
	test_file_contents("./testrepo/branch_file.txt", "hi\r\nbye!\r\n"); 
Ben Straub committed
72
}
Ben Straub committed
73

Ben Straub committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

void test_checkout_checkout__win32_autocrlf(void)
{
#ifdef GIT_WIN32
	git_config *cfg;
	const char *expected_readme_text = "hey there\r\n";

	cl_must_pass(p_unlink("./testrepo/.gitattributes"));
	cl_git_pass(git_repository_config__weakptr(&cfg, g_repo));
	cl_git_pass(git_config_set_bool(cfg, "core.autocrlf", true));

	cl_git_pass(git_checkout_head(g_repo, NULL, NULL));
	test_file_contents("./testrepo/README", expected_readme_text); 
#endif
}


91
static void enable_symlinks(bool enable)
Ben Straub committed
92
{
93 94
	git_config *cfg;
	cl_git_pass(git_repository_config(&cfg, g_repo));
95 96 97
	cl_git_pass(git_config_set_bool(cfg, "core.symlinks", enable));
	git_config_free(cfg);
}
Ben Straub committed
98

99 100
void test_checkout_checkout__symlinks(void)
{
101
	/* First try with symlinks forced on */
102
	enable_symlinks(true);
103
	cl_git_pass(git_checkout_head(g_repo, NULL, NULL));
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

#ifdef GIT_WIN32
	test_file_contents("./testrepo/link_to_new.txt", "new.txt");
#else
	{
		char link_data[1024];
		size_t link_size = 1024;

		link_size = p_readlink("./testrepo/link_to_new.txt", link_data, link_size);
		link_data[link_size] = '\0';
		cl_assert_equal_i(link_size, strlen("new.txt"));
		cl_assert_equal_s(link_data, "new.txt");
		test_file_contents("./testrepo/link_to_new.txt", "my new file\n");
	}
#endif

	/* Now with symlinks forced off */
121 122 123
	cl_git_sandbox_cleanup();
	g_repo = cl_git_sandbox_init("testrepo");
	enable_symlinks(false);
124
	cl_git_pass(git_checkout_head(g_repo, NULL, NULL));
125 126

	test_file_contents("./testrepo/link_to_new.txt", "new.txt");
Ben Straub committed
127
}
128

Ben Straub committed
129
void test_checkout_checkout__existing_file_skip(void)
130 131 132 133 134 135
{
	git_checkout_opts opts = {0};
	cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
	opts.existing_file_action = GIT_CHECKOUT_SKIP_EXISTING;
	cl_git_pass(git_checkout_head(g_repo, &opts, NULL));
	test_file_contents("./testrepo/new.txt", "This isn't what's stored!");
Ben Straub committed
136 137 138 139 140 141
}

void test_checkout_checkout__existing_file_overwrite(void)
{
	git_checkout_opts opts = {0};
	cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
	opts.existing_file_action = GIT_CHECKOUT_OVERWRITE_EXISTING;
	cl_git_pass(git_checkout_head(g_repo, &opts, NULL));
	test_file_contents("./testrepo/new.txt", "my new file\n");
}

void test_checkout_checkout__disable_filters(void)
{
	git_checkout_opts opts = {0};
	cl_git_mkfile("./testrepo/.gitattributes", "*.txt text eol=crlf\n");
	/* TODO cl_git_pass(git_checkout_head(g_repo, &opts, NULL));*/
	/* TODO test_file_contents("./testrepo/new.txt", "my new file\r\n");*/
	opts.disable_filters = true;
	cl_git_pass(git_checkout_head(g_repo, &opts, NULL));
	test_file_contents("./testrepo/new.txt", "my new file\n");
}

void test_checkout_checkout__dir_modes(void)
{
#ifndef GIT_WIN32
	git_checkout_opts opts = {0};
	struct stat st;
	git_reference *ref;

	cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/dir"));

167
	opts.dir_mode = 0701;
168 169
	cl_git_pass(git_checkout_reference(ref, &opts, NULL));
	cl_git_pass(p_stat("./testrepo/a", &st));
170 171 172 173 174
	cl_assert_equal_i(st.st_mode & 0777, 0701);

	/* File-mode test, since we're on the 'dir' branch */
	cl_git_pass(p_stat("./testrepo/a/b.txt", &st));
	cl_assert_equal_i(st.st_mode & 0777, 0755);
175 176

	git_reference_free(ref);
177 178 179
#endif
}

180
void test_checkout_checkout__override_file_modes(void)
181
{
182
#ifndef GIT_WIN32
183 184 185 186 187 188 189
	git_checkout_opts opts = {0};
	struct stat st;

	opts.file_mode = 0700;
	cl_git_pass(git_checkout_head(g_repo, &opts, NULL));
	cl_git_pass(p_stat("./testrepo/new.txt", &st));
	cl_assert_equal_i(st.st_mode & 0777, 0700);
190
#endif
191 192 193 194 195 196 197 198 199 200 201
}

void test_checkout_checkout__open_flags(void)
{
	git_checkout_opts opts = {0};

	cl_git_mkfile("./testrepo/new.txt", "hi\n");
	opts.file_open_flags = O_CREAT | O_RDWR | O_APPEND;
	cl_git_pass(git_checkout_head(g_repo, &opts, NULL));
	test_file_contents("./testrepo/new.txt", "hi\nmy new file\n");
}
202 203 204 205 206

void test_checkout_checkout__detached_head(void)
{
	/* TODO: write this when git_checkout_commit is implemented. */
}