crlf.c 10.5 KB
Newer Older
1 2
#include "clar_libgit2.h"
#include "checkout_helpers.h"
3
#include "../filter/crlf.h"
4 5 6

#include "git2/checkout.h"
#include "repository.h"
7
#include "posix.h"
8 9 10 11 12 13 14 15 16 17 18 19 20

static git_repository *g_repo;

void test_checkout_crlf__initialize(void)
{
	g_repo = cl_git_sandbox_init("crlf");
}

void test_checkout_crlf__cleanup(void)
{
	cl_git_sandbox_cleanup();
}

21 22 23
void test_checkout_crlf__detect_crlf_autocrlf_false(void)
{
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
24
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
25 26 27 28 29 30 31 32 33

	cl_repo_set_bool(g_repo, "core.autocrlf", false);

	git_checkout_head(g_repo, &opts);

	check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
	check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
}

34 35 36 37
void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void)
{
	git_index *index;
	const git_index_entry *entry;
38
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
39
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
40

41
	cl_repo_set_bool(g_repo, "core.autocrlf", false);
42 43 44 45 46 47 48 49

	git_checkout_head(g_repo, &opts);

	git_repository_index(&index, g_repo);

	cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
	cl_assert(entry->file_size == strlen(ALL_LF_TEXT_RAW));

50 51 52
	cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL);
	cl_assert(entry->file_size == strlen(ALL_CRLF_TEXT_RAW));

53 54 55 56 57
	git_index_free(index);
}

void test_checkout_crlf__detect_crlf_autocrlf_true(void)
{
58
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
59
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
60

61
	cl_repo_set_bool(g_repo, "core.autocrlf", true);
62 63 64

	git_checkout_head(g_repo, &opts);

65 66 67 68 69 70
	if (GIT_EOL_NATIVE == GIT_EOL_LF)
		check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
	else
		check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);

	check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
71 72
}

73 74
void test_checkout_crlf__more_lf_autocrlf_true(void)
{
75
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
76
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
77 78 79 80 81

	cl_repo_set_bool(g_repo, "core.autocrlf", true);

	git_checkout_head(g_repo, &opts);

82
	check_file_contents("./crlf/more-lf", MORE_LF_TEXT_RAW);
83 84 85 86
}

void test_checkout_crlf__more_crlf_autocrlf_true(void)
{
87
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
88
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
89 90 91 92 93

	cl_repo_set_bool(g_repo, "core.autocrlf", true);

	git_checkout_head(g_repo, &opts);

94
	check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_RAW);
95 96
}

97 98 99
void test_checkout_crlf__all_crlf_autocrlf_true(void)
{
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
100
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
101 102 103 104 105 106 107 108

	cl_repo_set_bool(g_repo, "core.autocrlf", true);

	git_checkout_head(g_repo, &opts);

	check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
}

109 110 111
void test_checkout_crlf__detect_crlf_autocrlf_true_utf8(void)
{
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
112
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
113 114 115

	cl_repo_set_bool(g_repo, "core.autocrlf", true);

116
	git_repository_set_head(g_repo, "refs/heads/utf8");
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	git_checkout_head(g_repo, &opts);

	if (GIT_EOL_NATIVE == GIT_EOL_LF)
	{
		check_file_contents("./crlf/few-utf8-chars-lf.txt", FEW_UTF8_LF_RAW);
		check_file_contents("./crlf/many-utf8-chars-lf.txt", MANY_UTF8_LF_RAW);
	}
	else
	{
		check_file_contents("./crlf/few-utf8-chars-lf.txt", FEW_UTF8_CRLF_RAW);
		check_file_contents("./crlf/many-utf8-chars-lf.txt", MANY_UTF8_CRLF_RAW);
	}

	check_file_contents("./crlf/few-utf8-chars-crlf.txt", FEW_UTF8_CRLF_RAW);
	check_file_contents("./crlf/many-utf8-chars-crlf.txt", MANY_UTF8_CRLF_RAW);
}

134 135 136 137
void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
{
	git_index *index;
	const git_index_entry *entry;
138
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
139
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
140

141
	cl_repo_set_bool(g_repo, "core.autocrlf", true);
142 143 144 145 146 147

	git_checkout_head(g_repo, &opts);

	git_repository_index(&index, g_repo);

	cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
148 149 150 151 152

	if (GIT_EOL_NATIVE == GIT_EOL_LF)
		cl_assert_equal_sz(strlen(ALL_LF_TEXT_RAW), entry->file_size);
	else
		cl_assert_equal_sz(strlen(ALL_LF_TEXT_AS_CRLF), entry->file_size);
153

154
	cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL);
155
	cl_assert_equal_sz(strlen(ALL_CRLF_TEXT_RAW), entry->file_size);
156

157 158
	git_index_free(index);
}
159 160 161 162 163

void test_checkout_crlf__with_ident(void)
{
	git_index *index;
	git_blob *blob;
164
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
165
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

	cl_git_mkfile("crlf/.gitattributes",
		"*.txt text\n*.bin binary\n"
		"*.crlf text eol=crlf\n"
		"*.lf text eol=lf\n"
		"*.ident text ident\n"
		"*.identcrlf ident text eol=crlf\n"
		"*.identlf ident text eol=lf\n");

	cl_repo_set_bool(g_repo, "core.autocrlf", true);

	/* add files with $Id$ */

	cl_git_mkfile("crlf/lf.ident", ALL_LF_TEXT_RAW "\n$Id: initial content$\n");
	cl_git_mkfile("crlf/crlf.ident", ALL_CRLF_TEXT_RAW "\r\n$Id$\r\n\r\n");
181 182
	cl_git_mkfile("crlf/more1.identlf", "$Id$\n" MORE_LF_TEXT_RAW);
	cl_git_mkfile("crlf/more2.identcrlf", "\r\n$Id: $\r\n" MORE_CRLF_TEXT_RAW);
183 184 185 186

	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_index_add_bypath(index, "lf.ident"));
	cl_git_pass(git_index_add_bypath(index, "crlf.ident"));
187 188
	cl_git_pass(git_index_add_bypath(index, "more1.identlf"));
	cl_git_pass(git_index_add_bypath(index, "more2.identcrlf"));
189 190 191 192
	cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "Some ident files\n");

	git_checkout_head(g_repo, &opts);

193
	/* check that blobs have $Id$ */
194 195

	cl_git_pass(git_blob_lookup(&blob, g_repo,
196
		& git_index_get_bypath(index, "lf.ident", 0)->id));
197 198
	cl_assert_equal_s(
		ALL_LF_TEXT_RAW "\n$Id$\n", git_blob_rawcontent(blob));
199
	git_blob_free(blob);
200

201
	cl_git_pass(git_blob_lookup(&blob, g_repo,
202
		& git_index_get_bypath(index, "more2.identcrlf", 0)->id));
203 204
	cl_assert_equal_s(
		"\n$Id$\n" MORE_CRLF_TEXT_AS_LF, git_blob_rawcontent(blob));
205 206 207 208 209 210 211 212 213 214
	git_blob_free(blob);

	/* check that filesystem is initially untouched - matching core Git */

	cl_assert_equal_file(
		ALL_LF_TEXT_RAW "\n$Id: initial content$\n", 0, "crlf/lf.ident");

	/* check that forced checkout rewrites correctly */

	p_unlink("crlf/lf.ident");
215 216 217
	p_unlink("crlf/crlf.ident");
	p_unlink("crlf/more1.identlf");
	p_unlink("crlf/more2.identcrlf");
218 219 220

	git_checkout_head(g_repo, &opts);

221
	if (GIT_EOL_NATIVE == GIT_EOL_LF) {
222 223 224 225
		cl_assert_equal_file(
			ALL_LF_TEXT_RAW
			"\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467$\n",
			0, "crlf/lf.ident");
226 227 228 229 230
		cl_assert_equal_file(
			ALL_CRLF_TEXT_AS_LF
			"\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857$\n\n",
			0, "crlf/crlf.ident");
	} else {
231 232 233 234
		cl_assert_equal_file(
			ALL_LF_TEXT_AS_CRLF
			"\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467$\r\n",
			0, "crlf/lf.ident");
235 236 237 238 239 240 241 242 243 244 245 246 247
		cl_assert_equal_file(
			ALL_CRLF_TEXT_RAW
			"\r\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857$\r\n\r\n",
			0, "crlf/crlf.ident");
	}

	cl_assert_equal_file(
		"$Id: f7830382dac1f1583422be5530fdfbd26289431b$\n"
		MORE_LF_TEXT_AS_LF, 0, "crlf/more1.identlf");

	cl_assert_equal_file(
		"\r\n$Id: 74677a68413012ce8d7e7cfc3f12603df3a3eac4$\r\n"
		MORE_CRLF_TEXT_AS_CRLF, 0, "crlf/more2.identcrlf");
248 249 250

	git_index_free(index);
}
251 252 253

void test_checkout_crlf__autocrlf_false_no_attrs(void)
{
254
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
255
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
256 257 258 259 260 261 262 263 264 265 266

	cl_repo_set_bool(g_repo, "core.autocrlf", false);

	git_checkout_head(g_repo, &opts);

	check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
	check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
}

void test_checkout_crlf__autocrlf_true_no_attrs(void)
{
267
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
268
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

	cl_repo_set_bool(g_repo, "core.autocrlf", true);

	git_checkout_head(g_repo, &opts);

	if (GIT_EOL_NATIVE == GIT_EOL_CRLF) {
		check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
		check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
	} else {
		check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
		check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
	}
}

void test_checkout_crlf__autocrlf_input_no_attrs(void)
{
285
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
286
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
287 288 289 290 291 292 293 294 295 296 297

	cl_repo_set_string(g_repo, "core.autocrlf", "input");

	git_checkout_head(g_repo, &opts);

	check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
	check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
}

void test_checkout_crlf__autocrlf_false_text_auto_attr(void)
{
298
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
299
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
300 301 302 303 304 305 306

	cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");

	cl_repo_set_bool(g_repo, "core.autocrlf", false);

	git_checkout_head(g_repo, &opts);

307 308 309 310 311 312 313
	if (GIT_EOL_NATIVE == GIT_EOL_CRLF) {
		check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
		check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
	} else {
		check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
		check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
	}
314 315 316 317
}

void test_checkout_crlf__autocrlf_true_text_auto_attr(void)
{
318
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
319
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

	cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");

	cl_repo_set_bool(g_repo, "core.autocrlf", true);

	git_checkout_head(g_repo, &opts);

	if (GIT_EOL_NATIVE == GIT_EOL_CRLF) {
		check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
		check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
	} else {
		check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
		check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
	}
}

void test_checkout_crlf__autocrlf_input_text_auto_attr(void)
{
338
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
339
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
340 341 342 343 344 345 346 347 348 349

	cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");

	cl_repo_set_string(g_repo, "core.autocrlf", "input");

	git_checkout_head(g_repo, &opts);

	check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
	check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
}
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369

void test_checkout_crlf__can_write_empty_file(void)
{
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;

	cl_repo_set_bool(g_repo, "core.autocrlf", true);

	git_repository_set_head(g_repo, "refs/heads/empty-files");
	git_checkout_head(g_repo, &opts);

	check_file_contents("./crlf/test1.txt", "");

	if (GIT_EOL_NATIVE == GIT_EOL_LF)
		check_file_contents("./crlf/test2.txt", "test2.txt's content\n");
	else
		check_file_contents("./crlf/test2.txt", "test2.txt's content\r\n");

	check_file_contents("./crlf/test3.txt", "");
}