crlf.c 6.46 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 21 22 23 24 25

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

void test_checkout_crlf__detect_crlf_autocrlf_false(void)
{
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;

26
	cl_repo_set_bool(g_repo, "core.autocrlf", false);
27 28 29

	git_checkout_head(g_repo, &opts);

30 31
	check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
	check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
32 33 34 35 36 37 38 39 40
}

void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void)
{
	git_index *index;
	const git_index_entry *entry;
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;

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 58 59 60
	git_index_free(index);
}

void test_checkout_crlf__detect_crlf_autocrlf_true(void)
{
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;

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 75 76 77 78 79 80 81
void test_checkout_crlf__more_lf_autocrlf_true(void)
{
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;

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

	git_checkout_head(g_repo, &opts);

82 83 84 85
	if (GIT_EOL_NATIVE == GIT_EOL_LF)
		check_file_contents("./crlf/more-lf", MORE_LF_TEXT_RAW);
	else
		check_file_contents("./crlf/more-lf", MORE_LF_TEXT_AS_CRLF);
86 87 88 89 90 91 92 93 94 95 96
}

void test_checkout_crlf__more_crlf_autocrlf_true(void)
{
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;

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

	git_checkout_head(g_repo, &opts);

97 98 99 100
	if (GIT_EOL_NATIVE == GIT_EOL_LF)
		check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_RAW);
	else
		check_file_contents("./crlf/more-crlf", MORE_CRLF_TEXT_AS_CRLF);
101 102 103 104 105 106 107 108 109 110 111
}

void test_checkout_crlf__all_crlf_autocrlf_true(void)
{
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;

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

	git_checkout_head(g_repo, &opts);

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

115 116 117 118 119 120 121
void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
{
	git_index *index;
	const git_index_entry *entry;
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;

122
	cl_repo_set_bool(g_repo, "core.autocrlf", true);
123 124 125 126 127 128

	git_checkout_head(g_repo, &opts);

	git_repository_index(&index, g_repo);

	cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
129 130 131 132 133

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

135
	cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL);
136
	cl_assert_equal_sz(strlen(ALL_CRLF_TEXT_RAW), entry->file_size);
137

138 139
	git_index_free(index);
}
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

void test_checkout_crlf__with_ident(void)
{
	git_index *index;
	git_blob *blob;
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
	opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;

	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");
162 163
	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);
164 165 166 167

	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"));
168 169
	cl_git_pass(git_index_add_bypath(index, "more1.identlf"));
	cl_git_pass(git_index_add_bypath(index, "more2.identcrlf"));
170 171 172 173
	cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "Some ident files\n");

	git_checkout_head(g_repo, &opts);

174
	/* check that blobs have $Id$ */
175 176 177 178 179

	cl_git_pass(git_blob_lookup(&blob, g_repo,
		& git_index_get_bypath(index, "lf.ident", 0)->oid));
	cl_assert_equal_s(
		ALL_LF_TEXT_RAW "\n$Id$\n", git_blob_rawcontent(blob));
180
	git_blob_free(blob);
181

182 183 184 185
	cl_git_pass(git_blob_lookup(&blob, g_repo,
		& git_index_get_bypath(index, "more2.identcrlf", 0)->oid));
	cl_assert_equal_s(
		"\n$Id$\n" MORE_CRLF_TEXT_AS_LF, git_blob_rawcontent(blob));
186 187 188 189 190 191 192 193 194 195
	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");
196 197 198
	p_unlink("crlf/crlf.ident");
	p_unlink("crlf/more1.identlf");
	p_unlink("crlf/more2.identcrlf");
199 200 201

	git_checkout_head(g_repo, &opts);

202
	if (GIT_EOL_NATIVE == GIT_EOL_LF) {
203 204 205 206
		cl_assert_equal_file(
			ALL_LF_TEXT_RAW
			"\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467$\n",
			0, "crlf/lf.ident");
207 208 209 210 211
		cl_assert_equal_file(
			ALL_CRLF_TEXT_AS_LF
			"\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857$\n\n",
			0, "crlf/crlf.ident");
	} else {
212 213 214 215
		cl_assert_equal_file(
			ALL_LF_TEXT_AS_CRLF
			"\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467$\r\n",
			0, "crlf/lf.ident");
216 217 218 219 220 221 222 223 224 225 226 227 228
		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");
229 230 231

	git_index_free(index);
}