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

#include "git2/checkout.h"
#include "repository.h"
8
#include "index.h"
9
#include "posix.h"
10 11 12

static git_repository *g_repo;

13 14 15
static const char *systype;
static git_buf expected_fixture = GIT_BUF_INIT;

16 17
static int unlink_file(void *payload, git_buf *path)
{
18 19 20
	char *fn;

	cl_assert(fn = git_path_basename(path->ptr));
21 22 23 24 25 26 27 28 29 30

	GIT_UNUSED(payload);

	if (strcmp(fn, ".git"))
		cl_must_pass(p_unlink(path->ptr));

	git__free(fn);
	return 0;
}

31 32
void test_checkout_crlf__initialize(void)
{
33 34
	git_buf reponame = GIT_BUF_INIT;

35
	g_repo = cl_git_sandbox_init("crlf");
36

37 38 39 40
	/*
	 * remove the contents of the working directory so that we can
	 * check out over it.
	 */
41
	cl_git_pass(git_buf_puts(&reponame, "crlf"));
42 43
	cl_git_pass(git_path_direach(&reponame, 0, unlink_file, NULL));

44 45 46 47
	if (GIT_EOL_NATIVE == GIT_EOL_CRLF)
		systype = "windows";
	else
		systype = "posix";
48 49

	git_buf_dispose(&reponame);
50 51 52 53 54
}

void test_checkout_crlf__cleanup(void)
{
	cl_git_sandbox_cleanup();
55 56 57

	if (expected_fixture.size) {
		cl_fixture_cleanup(expected_fixture.ptr);
58
		git_buf_dispose(&expected_fixture);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
	}
}

struct compare_data
{
	const char *dirname;
	const char *autocrlf;
	const char *attrs;
};

static int compare_file(void *payload, git_buf *actual_path)
{
	git_buf expected_path = GIT_BUF_INIT;
	git_buf actual_contents = GIT_BUF_INIT;
	git_buf expected_contents = GIT_BUF_INIT;
	struct compare_data *cd = payload;
	bool failed = true;
76 77
	int cmp_git, cmp_gitattributes;
	char *basename;
78

79 80 81 82 83
	basename = git_path_basename(actual_path->ptr);
	cmp_git = strcmp(basename, ".git");
	cmp_gitattributes = strcmp(basename, ".gitattributes");

	if (cmp_git == 0 || cmp_gitattributes == 0) {
84 85 86 87
		failed = false;
		goto done;
	}

88
	cl_git_pass(git_buf_joinpath(&expected_path, cd->dirname, basename));
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

	if (!git_path_isfile(expected_path.ptr) ||
		!git_path_isfile(actual_path->ptr))
		goto done;

	if (git_futils_readbuffer(&actual_contents, actual_path->ptr) < 0 ||
		git_futils_readbuffer(&expected_contents, expected_path.ptr) < 0)
		goto done;

	if (actual_contents.size != expected_contents.size)
		goto done;

	if (memcmp(actual_contents.ptr, expected_contents.ptr, expected_contents.size) != 0)
		goto done;

	failed = false;

done:
	if (failed) {
		git_buf details = GIT_BUF_INIT;
		git_buf_printf(&details, "filename=%s, system=%s, autocrlf=%s, attrs={%s}",
			git_path_basename(actual_path->ptr), systype, cd->autocrlf, cd->attrs);
111
		clar__fail(__FILE__, __func__, __LINE__,
112
			"checked out contents did not match expected", details.ptr, 0);
113
		git_buf_dispose(&details);
114 115
	}

116
	git__free(basename);
117 118 119
	git_buf_dispose(&expected_contents);
	git_buf_dispose(&actual_contents);
	git_buf_dispose(&expected_path);
120 121 122 123 124 125 126 127

	return 0;
}

static void test_checkout(const char *autocrlf, const char *attrs)
{
	git_buf attrbuf = GIT_BUF_INIT;
	git_buf expected_dirname = GIT_BUF_INIT;
128
	git_buf systype_and_direction = GIT_BUF_INIT;
129 130 131 132 133 134
	git_buf sandboxname = GIT_BUF_INIT;
	git_buf reponame = GIT_BUF_INIT;
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
	struct compare_data compare_data = { NULL, autocrlf, attrs };
	const char *c;

135
	cl_git_pass(git_buf_puts(&reponame, "crlf"));
136

137 138
	cl_git_pass(git_buf_puts(&systype_and_direction, systype));
	cl_git_pass(git_buf_puts(&systype_and_direction, "_to_workdir"));
139

140 141
	cl_git_pass(git_buf_puts(&sandboxname, "autocrlf_"));
	cl_git_pass(git_buf_puts(&sandboxname, autocrlf));
142 143

	if (*attrs) {
144
		cl_git_pass(git_buf_puts(&sandboxname, ","));
145 146 147

		for (c = attrs; *c; c++) {
			if (*c == ' ')
148
				cl_git_pass(git_buf_putc(&sandboxname, ','));
149
			else if (*c == '=')
150
				cl_git_pass(git_buf_putc(&sandboxname, '_'));
151
			else
152
				cl_git_pass(git_buf_putc(&sandboxname, *c));
153 154
		}

155
		cl_git_pass(git_buf_printf(&attrbuf, "* %s\n", attrs));
156 157 158 159 160
		cl_git_mkfile("crlf/.gitattributes", attrbuf.ptr);
	}

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

161 162
	cl_git_pass(git_buf_joinpath(&expected_dirname, systype_and_direction.ptr, sandboxname.ptr));
	cl_git_pass(git_buf_joinpath(&expected_fixture, "crlf_data", expected_dirname.ptr));
163 164 165
	cl_fixture_sandbox(expected_fixture.ptr);

	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
166
	cl_git_pass(git_checkout_head(g_repo, &opts));
167 168 169 170 171

	compare_data.dirname = sandboxname.ptr;
	cl_git_pass(git_path_direach(&reponame, 0, compare_file, &compare_data));

	cl_fixture_cleanup(expected_fixture.ptr);
172
	git_buf_dispose(&expected_fixture);
173

174 175 176 177
	git_buf_dispose(&attrbuf);
	git_buf_dispose(&expected_fixture);
	git_buf_dispose(&expected_dirname);
	git_buf_dispose(&sandboxname);
178
	git_buf_dispose(&systype_and_direction);
179
	git_buf_dispose(&reponame);
180 181 182 183 184
}

static void empty_workdir(const char *name)
{
	git_vector contents = GIT_VECTOR_INIT;
185 186
	char *basename;
	int cmp;
187 188 189
	size_t i;
	const char *fn;

190
	cl_git_pass(git_path_dirload(&contents, name, 0, 0));
191
	git_vector_foreach(&contents, i, fn) {
192 193
		cl_assert(basename = git_path_basename(fn));
		cmp = strncasecmp(basename, ".git", 4);
194 195 196

		git__free(basename);

197 198
		if (cmp)
			cl_git_pass(p_unlink(fn));
199 200 201 202 203 204 205 206 207
	}
	git_vector_free_deep(&contents);
}

void test_checkout_crlf__matches_core_git(void)
{
	const char *autocrlf[] = { "true", "false", "input", NULL };
	const char *attrs[] = { "", "-crlf", "-text", "eol=crlf", "eol=lf",
		"text", "text eol=crlf", "text eol=lf",
208
		"text=auto", "text=auto eol=crlf", "text=auto eol=lf",
209 210 211 212 213 214 215 216 217
		NULL };
	const char **a, **b;

	for (a = autocrlf; *a; a++) {
		for (b = attrs; *b; b++) {
			empty_workdir("crlf");
			test_checkout(*a, *b);
		}
	}
218 219
}

220 221 222
void test_checkout_crlf__detect_crlf_autocrlf_false(void)
{
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
223
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
224 225 226 227 228 229 230 231 232

	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);
}

233 234 235 236
void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void)
{
	git_index *index;
	const git_index_entry *entry;
237
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
238
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
239

240
	cl_repo_set_bool(g_repo, "core.autocrlf", false);
241 242

	git_repository_index(&index, g_repo);
243 244 245
	tick_index(index);

	git_checkout_head(g_repo, &opts);
246 247 248 249

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

250 251 252
	cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL);
	cl_assert(entry->file_size == strlen(ALL_CRLF_TEXT_RAW));

253 254 255 256 257
	git_index_free(index);
}

void test_checkout_crlf__detect_crlf_autocrlf_true(void)
{
258
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
259
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
260

261
	cl_repo_set_bool(g_repo, "core.autocrlf", true);
262 263 264

	git_checkout_head(g_repo, &opts);

265
	check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
266
	check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
267 268
}

269 270 271
void test_checkout_crlf__detect_crlf_autocrlf_true_utf8(void)
{
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
272
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
273 274 275

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

276
	git_repository_set_head(g_repo, "refs/heads/master");
277 278
	git_checkout_head(g_repo, &opts);

279 280
	check_file_contents("./crlf/few-utf8-chars-lf", FEW_UTF8_CRLF_RAW);
	check_file_contents("./crlf/many-utf8-chars-lf", MANY_UTF8_CRLF_RAW);
281

282 283
	check_file_contents("./crlf/few-utf8-chars-crlf", FEW_UTF8_CRLF_RAW);
	check_file_contents("./crlf/many-utf8-chars-crlf", MANY_UTF8_CRLF_RAW);
284 285
}

286 287 288 289
void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
{
	git_index *index;
	const git_index_entry *entry;
290
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
291
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
292

293
	cl_repo_set_bool(g_repo, "core.autocrlf", true);
294 295

	git_repository_index(&index, g_repo);
296 297 298
	tick_index(index);

	git_checkout_head(g_repo, &opts);
299 300

	cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
301

302
	cl_assert_equal_sz(strlen(ALL_LF_TEXT_AS_CRLF), entry->file_size);
303

304
	cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL);
305
	cl_assert_equal_sz(strlen(ALL_CRLF_TEXT_RAW), entry->file_size);
306

307 308
	git_index_free(index);
}
309 310 311 312

void test_checkout_crlf__with_ident(void)
{
	git_index *index;
Jacques Germishuys committed
313
	const git_index_entry *entry;
314
	git_blob *blob;
315
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
316
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

	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");
332 333
	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);
334 335 336 337

	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"));
338 339
	cl_git_pass(git_index_add_bypath(index, "more1.identlf"));
	cl_git_pass(git_index_add_bypath(index, "more2.identcrlf"));
340 341 342 343
	cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "Some ident files\n");

	git_checkout_head(g_repo, &opts);

344
	/* check that blobs have $Id$ */
345

346 347
	cl_assert((entry = git_index_get_bypath(index, "lf.ident", 0)));
	cl_git_pass(git_blob_lookup(&blob, g_repo, &entry->id));
348 349
	cl_assert_equal_s(
		ALL_LF_TEXT_RAW "\n$Id$\n", git_blob_rawcontent(blob));
350
	git_blob_free(blob);
351

352 353
	cl_assert((entry = git_index_get_bypath(index, "more2.identcrlf", 0)));
	cl_git_pass(git_blob_lookup(&blob, g_repo, &entry->id));
354 355
	cl_assert_equal_s(
		"\n$Id$\n" MORE_CRLF_TEXT_AS_LF, git_blob_rawcontent(blob));
356 357 358 359 360 361 362 363 364 365
	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");
366 367 368
	p_unlink("crlf/crlf.ident");
	p_unlink("crlf/more1.identlf");
	p_unlink("crlf/more2.identcrlf");
369

370
	cl_git_pass(git_checkout_head(g_repo, &opts));
371

372 373 374 375 376 377 378 379
	cl_assert_equal_file(
		ALL_LF_TEXT_AS_CRLF
		"\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467 $\r\n",
		0, "crlf/lf.ident");
	cl_assert_equal_file(
		ALL_CRLF_TEXT_RAW
		"\r\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857 $\r\n\r\n",
		0, "crlf/crlf.ident");
380 381

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

	cl_assert_equal_file(
386
		"\r\n$Id: 74677a68413012ce8d7e7cfc3f12603df3a3eac4 $\r\n"
387
		MORE_CRLF_TEXT_AS_CRLF, 0, "crlf/more2.identcrlf");
388 389 390

	git_index_free(index);
}
391 392 393

void test_checkout_crlf__autocrlf_false_no_attrs(void)
{
394
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
395
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
396 397 398

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

399
	cl_git_pass(git_checkout_head(g_repo, &opts));
400 401 402 403 404 405 406

	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)
{
407
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
408
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
409 410 411

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

412
	cl_git_pass(git_checkout_head(g_repo, &opts));
413

414 415
	check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
	check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
416 417 418 419
}

void test_checkout_crlf__autocrlf_input_no_attrs(void)
{
420
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
421
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
422 423 424

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

425
	cl_git_pass(git_checkout_head(g_repo, &opts));
426 427 428 429 430 431 432

	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)
{
433
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
434
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
435 436 437 438 439

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

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

440
	cl_git_pass(git_checkout_head(g_repo, &opts));
441

442 443 444 445 446 447 448
	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);
	}
449 450 451 452
}

void test_checkout_crlf__autocrlf_true_text_auto_attr(void)
{
453
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
454
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
455 456 457 458 459

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

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

460
	cl_git_pass(git_checkout_head(g_repo, &opts));
461

462 463
	check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
	check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
464 465 466 467
}

void test_checkout_crlf__autocrlf_input_text_auto_attr(void)
{
468
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
469
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
470 471 472 473 474

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

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

475
	cl_git_pass(git_checkout_head(g_repo, &opts));
476 477 478 479

	check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
	check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
}
480 481 482 483 484 485 486 487

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);

488 489
	cl_git_pass(git_repository_set_head(g_repo, "refs/heads/empty-files"));
	cl_git_pass(git_checkout_head(g_repo, &opts));
490 491 492

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

493
	check_file_contents("./crlf/test2.txt", "test2.txt's content\r\n");
494 495 496

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