crlf.c 11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include "clar_libgit2.h"
#include "../filter/crlf.h"

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

#define FILE_CONTENTS_LF "one\ntwo\nthree\nfour\n"
#define FILE_CONTENTS_CRLF "one\r\ntwo\r\nthree\r\nfour\r\n"

#define FILE_OID_LF "f384549cbeb481e437091320de6d1f2e15e11b4a"
#define FILE_OID_CRLF "7fbf4d847b191141d80f30c8ab03d2ad4cd543a9"

static git_repository *g_repo;
static git_index *g_index;

17 18
static git_buf expected_fixture = GIT_BUF_INIT;

19 20
void test_index_crlf__initialize(void)
{
21
	g_repo = cl_git_sandbox_init_new("crlf");
22 23 24 25 26 27 28
	cl_git_pass(git_repository_index(&g_index, g_repo));
}

void test_index_crlf__cleanup(void)
{
	git_index_free(g_index);
	cl_git_sandbox_cleanup();
29 30 31

	if (expected_fixture.size) {
		cl_fixture_cleanup(expected_fixture.ptr);
lhchavez committed
32
		git_buf_dispose(&expected_fixture);
33 34 35 36 37
	}
}

struct compare_data
{
38
	const char *systype;
39 40 41 42 43 44 45 46
	const char *dirname;
	const char *safecrlf;
	const char *autocrlf;
	const char *attrs;
};

static int add_and_check_file(void *payload, git_buf *actual_path)
{
47
	git_buf expected_path = GIT_BUF_INIT;
48 49 50 51 52
	git_buf expected_path_fail = GIT_BUF_INIT;
	git_buf expected_contents = GIT_BUF_INIT;
	struct compare_data *cd = payload;
	char *basename;
	const git_index_entry *entry;
53
	git_blob *blob;
54
	bool failed = true;
55 56 57

	basename = git_path_basename(actual_path->ptr);

58 59 60 61
	if (!strcmp(basename, ".git") || !strcmp(basename, ".gitattributes")) {
		failed = false;
		goto done;
	}
62

63
	cl_git_pass(git_buf_joinpath(&expected_path, cd->dirname, basename));
64

65 66
	cl_git_pass(git_buf_puts(&expected_path_fail, expected_path.ptr));
	cl_git_pass(git_buf_puts(&expected_path_fail, ".fail"));
67

68 69
	if (git_path_isfile(expected_path.ptr)) {
		cl_git_pass(git_index_add_bypath(g_index, basename));
70

71 72
		cl_assert(entry = git_index_get_bypath(g_index, basename, 0));
		cl_git_pass(git_blob_lookup(&blob, g_repo, &entry->id));
73

74
		cl_git_pass(git_futils_readbuffer(&expected_contents, expected_path.ptr));
75 76 77

		if (strcmp(expected_contents.ptr, git_blob_rawcontent(blob)) != 0)
			goto done;
78

79 80 81
		git_blob_free(blob);
	} else if (git_path_isfile(expected_path_fail.ptr)) {
		cl_git_pass(git_futils_readbuffer(&expected_contents, expected_path_fail.ptr));
82 83
		git_buf_rtrim(&expected_contents);

84
		if (git_index_add_bypath(g_index, basename) == 0 ||
85 86
			git_error_last()->klass != GIT_ERROR_FILTER ||
			strcmp(expected_contents.ptr, git_error_last()->message) != 0)
87
			goto done;
88 89
	} else {
		cl_fail("unexpected index failure");
90 91 92 93 94 95 96
	}

	failed = false;

done:
	if (failed) {
		git_buf details = GIT_BUF_INIT;
97 98
		git_buf_printf(&details, "filename=%s, system=%s, autocrlf=%s, safecrlf=%s, attrs={%s}",
			basename, cd->systype, cd->autocrlf, cd->safecrlf, cd->attrs);
99
		clar__fail(__FILE__, __LINE__,
100 101
			"index contents did not match expected", details.ptr, 0);
		git_buf_dispose(&details);
102 103 104
	}

	git__free(basename);
105 106 107
	git_buf_dispose(&expected_contents);
	git_buf_dispose(&expected_path);
	git_buf_dispose(&expected_path_fail);
108 109 110
	return 0;
}

111 112 113 114 115 116 117 118
static const char *system_type(void)
{
	if (GIT_EOL_NATIVE == GIT_EOL_CRLF)
		return "windows";
	else
		return "posix";
}

119 120 121 122 123 124
static void test_add_index(const char *safecrlf, const char *autocrlf, const char *attrs)
{
	git_buf attrbuf = GIT_BUF_INIT;
	git_buf expected_dirname = GIT_BUF_INIT;
	git_buf sandboxname = GIT_BUF_INIT;
	git_buf reponame = GIT_BUF_INIT;
125
	struct compare_data compare_data = { system_type(), NULL, safecrlf, autocrlf, attrs };
126 127 128 129
	const char *c;

	git_buf_puts(&reponame, "crlf");

130
	git_buf_puts(&sandboxname, "autocrlf_");
131 132
	git_buf_puts(&sandboxname, autocrlf);

133 134 135
	git_buf_puts(&sandboxname, ",safecrlf_");
	git_buf_puts(&sandboxname, safecrlf);

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
	if (*attrs) {
		git_buf_puts(&sandboxname, ",");

		for (c = attrs; *c; c++) {
			if (*c == ' ')
				git_buf_putc(&sandboxname, ',');
			else if (*c == '=')
				git_buf_putc(&sandboxname, '_');
			else
				git_buf_putc(&sandboxname, *c);
		}

		git_buf_printf(&attrbuf, "* %s\n", attrs);
		cl_git_mkfile("crlf/.gitattributes", attrbuf.ptr);
	}

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

	cl_git_pass(git_index_clear(g_index));

157 158 159
	git_buf_joinpath(&expected_dirname, "crlf_data", system_type());
	git_buf_puts(&expected_dirname, "_to_odb");

160 161 162 163 164 165 166
	git_buf_joinpath(&expected_fixture, expected_dirname.ptr, sandboxname.ptr);
	cl_fixture_sandbox(expected_fixture.ptr);

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

	cl_fixture_cleanup(expected_fixture.ptr);
lhchavez committed
167
	git_buf_dispose(&expected_fixture);
168

lhchavez committed
169 170 171 172 173
	git_buf_dispose(&attrbuf);
	git_buf_dispose(&expected_fixture);
	git_buf_dispose(&expected_dirname);
	git_buf_dispose(&sandboxname);
	git_buf_dispose(&reponame);
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
}

static void set_up_workingdir(const char *name)
{
	git_vector contents = GIT_VECTOR_INIT;
	size_t i;
	const char *fn;

	git_path_dirload(&contents, name, 0, 0);
	git_vector_foreach(&contents, i, fn) {
		char *basename = git_path_basename(fn);
		bool skip = strncasecmp(basename, ".git", 4) == 0 && strlen(basename) == 4;

		git__free(basename);

		if (skip)
			continue;
		p_unlink(fn);
	}
	git_vector_free_deep(&contents);

	/* copy input files */
196
	git_path_dirload(&contents, cl_fixture("crlf"), 0, 0);
197 198 199
	git_vector_foreach(&contents, i, fn) {
		char *basename = git_path_basename(fn);
		git_buf dest_filename = GIT_BUF_INIT;
200 201 202 203 204 205 206

		if (strcmp(basename, ".gitted") &&
			strcmp(basename, ".gitattributes")) {
			git_buf_joinpath(&dest_filename, name, basename);
			cl_git_pass(git_futils_cp(fn, dest_filename.ptr, 0644));
		}

207
		git__free(basename);
lhchavez committed
208
		git_buf_dispose(&dest_filename);
209 210 211 212 213 214 215 216 217 218
	}
	git_vector_free_deep(&contents);
}

void test_index_crlf__matches_core_git(void)
{
	const char *safecrlf[] = { "true", "false", "warn", NULL };
	const char *autocrlf[] = { "true", "false", "input", NULL };
	const char *attrs[] = { "", "-crlf", "-text", "eol=crlf", "eol=lf",
		"text", "text eol=crlf", "text eol=lf",
219
		"text=auto", "text=auto eol=crlf", "text=auto eol=lf",
220 221 222 223 224 225 226 227 228 229 230
		NULL };
	const char **a, **b, **c;

	for (a = safecrlf; *a; a++) {
		for (b = autocrlf; *b; b++) {
			for (c = attrs; *c; c++) {
				set_up_workingdir("crlf");
				test_add_index(*a, *b, *c);
			}
		}
	}
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
}

void test_index_crlf__autocrlf_false_no_attrs(void)
{
	const git_index_entry *entry;
	git_oid oid;

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

	cl_git_mkfile("./crlf/newfile.txt",
		(GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF);

	cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
	entry = git_index_get_bypath(g_index, "newfile.txt", 0);

	cl_git_pass(git_oid_fromstr(&oid,
		(GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_OID_CRLF : FILE_OID_LF));
248
	cl_assert_equal_oid(&oid, &entry->id);
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
}

void test_index_crlf__autocrlf_true_no_attrs(void)
{
	const git_index_entry *entry;
	git_oid oid;

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

	cl_git_mkfile("./crlf/newfile.txt",
		(GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF);

	cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
	entry = git_index_get_bypath(g_index, "newfile.txt", 0);

	cl_git_pass(git_oid_fromstr(&oid, FILE_OID_LF));
265
	cl_assert_equal_oid(&oid, &entry->id);
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
}

void test_index_crlf__autocrlf_input_no_attrs(void)
{
	const git_index_entry *entry;
	git_oid oid;

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

	cl_git_mkfile("./crlf/newfile.txt",
		(GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF);

	cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
	entry = git_index_get_bypath(g_index, "newfile.txt", 0);

	cl_git_pass(git_oid_fromstr(&oid, FILE_OID_LF));
282
	cl_assert_equal_oid(&oid, &entry->id);
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
}

void test_index_crlf__autocrlf_false_text_auto_attr(void)
{
	const git_index_entry *entry;
	git_oid oid;

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

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

	cl_git_mkfile("./crlf/newfile.txt",
		(GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF);

	cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
	entry = git_index_get_bypath(g_index, "newfile.txt", 0);

	cl_git_pass(git_oid_fromstr(&oid, FILE_OID_LF));
301
	cl_assert_equal_oid(&oid, &entry->id);
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
}

void test_index_crlf__autocrlf_true_text_auto_attr(void)
{
	const git_index_entry *entry;
	git_oid oid;

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

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

	cl_git_mkfile("./crlf/newfile.txt",
		(GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF);

	cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
	entry = git_index_get_bypath(g_index, "newfile.txt", 0);

	cl_git_pass(git_oid_fromstr(&oid, FILE_OID_LF));
320
	cl_assert_equal_oid(&oid, &entry->id);
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
}

void test_index_crlf__autocrlf_input_text_auto_attr(void)
{
	const git_index_entry *entry;
	git_oid oid;

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

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

	cl_git_mkfile("./crlf/newfile.txt",
		(GIT_EOL_NATIVE == GIT_EOL_CRLF) ? FILE_CONTENTS_CRLF : FILE_CONTENTS_LF);

	cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
	entry = git_index_get_bypath(g_index, "newfile.txt", 0);

	cl_git_pass(git_oid_fromstr(&oid, FILE_OID_LF));
339
	cl_assert_equal_oid(&oid, &entry->id);
340
}
341

342 343 344 345 346 347 348 349 350 351 352 353 354 355
void test_index_crlf__safecrlf_true_autocrlf_input_text_auto_attr(void)
{
	const git_index_entry *entry;
	git_oid oid;

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

	cl_repo_set_string(g_repo, "core.autocrlf", "input");
	cl_repo_set_bool(g_repo, "core.safecrlf", true);

	cl_git_mkfile("./crlf/newfile.txt", FILE_CONTENTS_LF);

	cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
	entry = git_index_get_bypath(g_index, "newfile.txt", 0);
356
	cl_assert(entry);
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376

	cl_git_pass(git_oid_fromstr(&oid, FILE_OID_LF));
	cl_assert_equal_oid(&oid, &entry->id);

	cl_git_mkfile("./crlf/newfile2.txt", FILE_CONTENTS_CRLF);
	cl_git_fail(git_index_add_bypath(g_index, "newfile2.txt"));
}

void test_index_crlf__safecrlf_true_autocrlf_input_text__no_attr(void)
{
	const git_index_entry *entry;
	git_oid oid;

	cl_repo_set_string(g_repo, "core.autocrlf", "input");
	cl_repo_set_bool(g_repo, "core.safecrlf", true);

	cl_git_mkfile("./crlf/newfile.txt", FILE_CONTENTS_LF);

	cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));
	entry = git_index_get_bypath(g_index, "newfile.txt", 0);
377
	cl_assert(entry);
378 379 380 381 382 383 384 385

	cl_git_pass(git_oid_fromstr(&oid, FILE_OID_LF));
	cl_assert_equal_oid(&oid, &entry->id);

	cl_git_mkfile("./crlf/newfile2.txt", FILE_CONTENTS_CRLF);
	cl_git_fail(git_index_add_bypath(g_index, "newfile2.txt"));
}

386 387 388 389 390 391
void test_index_crlf__safecrlf_true_no_attrs(void)
{
	cl_repo_set_bool(g_repo, "core.autocrlf", true);
	cl_repo_set_bool(g_repo, "core.safecrlf", true);

	cl_git_mkfile("crlf/newfile.txt", ALL_LF_TEXT_RAW);
392
	cl_git_fail(git_index_add_bypath(g_index, "newfile.txt"));
393 394 395 396 397 398 399 400 401 402

	cl_git_mkfile("crlf/newfile.txt", ALL_CRLF_TEXT_RAW);
	cl_git_pass(git_index_add_bypath(g_index, "newfile.txt"));

	cl_git_mkfile("crlf/newfile.txt", MORE_CRLF_TEXT_RAW);
	cl_git_fail(git_index_add_bypath(g_index, "newfile.txt"));

	cl_git_mkfile("crlf/newfile.txt", MORE_LF_TEXT_RAW);
	cl_git_fail(git_index_add_bypath(g_index, "newfile.txt"));
}