repo.c 12.2 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "futils.h"
3
#include "git2/attr.h"
4
#include "attr.h"
5
#include "sysdir.h"
6

7
#include "attr_expect.h"
8
#include "git2/sys/repository.h"
9

10 11 12 13
static git_repository *g_repo = NULL;

void test_attr_repo__initialize(void)
{
14
	g_repo = cl_git_sandbox_init("attr");
15 16 17 18
}

void test_attr_repo__cleanup(void)
{
19
	cl_git_sandbox_cleanup();
20
	g_repo = NULL;
21
	cl_sandbox_set_search_path_defaults();
22 23
}

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
static struct attr_expected get_one_test_cases[] = {
	{ "root_test1", "repoattr", EXPECT_TRUE, NULL },
	{ "root_test1", "rootattr", EXPECT_TRUE, NULL },
	{ "root_test1", "missingattr", EXPECT_UNDEFINED, NULL },
	{ "root_test1", "subattr", EXPECT_UNDEFINED, NULL },
	{ "root_test1", "negattr", EXPECT_UNDEFINED, NULL },
	{ "root_test2", "repoattr", EXPECT_TRUE, NULL },
	{ "root_test2", "rootattr", EXPECT_FALSE, NULL },
	{ "root_test2", "missingattr", EXPECT_UNDEFINED, NULL },
	{ "root_test2", "multiattr", EXPECT_FALSE, NULL },
	{ "root_test3", "repoattr", EXPECT_TRUE, NULL },
	{ "root_test3", "rootattr", EXPECT_UNDEFINED, NULL },
	{ "root_test3", "multiattr", EXPECT_STRING, "3" },
	{ "root_test3", "multi2", EXPECT_UNDEFINED, NULL },
	{ "sub/subdir_test1", "repoattr", EXPECT_TRUE, NULL },
	{ "sub/subdir_test1", "rootattr", EXPECT_TRUE, NULL },
	{ "sub/subdir_test1", "missingattr", EXPECT_UNDEFINED, NULL },
	{ "sub/subdir_test1", "subattr", EXPECT_STRING, "yes" },
	{ "sub/subdir_test1", "negattr", EXPECT_FALSE, NULL },
	{ "sub/subdir_test1", "another", EXPECT_UNDEFINED, NULL },
	{ "sub/subdir_test2.txt", "repoattr", EXPECT_TRUE, NULL },
	{ "sub/subdir_test2.txt", "rootattr", EXPECT_TRUE, NULL },
	{ "sub/subdir_test2.txt", "missingattr", EXPECT_UNDEFINED, NULL },
	{ "sub/subdir_test2.txt", "subattr", EXPECT_STRING, "yes" },
	{ "sub/subdir_test2.txt", "negattr", EXPECT_FALSE, NULL },
	{ "sub/subdir_test2.txt", "another", EXPECT_STRING, "zero" },
	{ "sub/subdir_test2.txt", "reposub", EXPECT_TRUE, NULL },
	{ "sub/sub/subdir.txt", "another", EXPECT_STRING, "one" },
	{ "sub/sub/subdir.txt", "reposubsub", EXPECT_TRUE, NULL },
	{ "sub/sub/subdir.txt", "reposub", EXPECT_UNDEFINED, NULL },
	{ "does-not-exist", "foo", EXPECT_STRING, "yes" },
	{ "sub/deep/file", "deepdeep", EXPECT_TRUE, NULL },
	{ "sub/sub/d/no", "test", EXPECT_STRING, "a/b/d/*" },
	{ "sub/sub/d/yes", "test", EXPECT_UNDEFINED, NULL },
};

60 61
void test_attr_repo__get_one(void)
{
62 63 64 65 66 67 68 69 70 71 72 73
	int i;

	for (i = 0; i < (int)ARRAY_SIZE(get_one_test_cases); ++i) {
		struct attr_expected *scan = &get_one_test_cases[i];
		const char *value;

		cl_git_pass(git_attr_get(&value, g_repo, 0, scan->path, scan->attr));
		attr_check_expected(
			scan->expected, scan->expected_str, scan->attr, value);
	}

	cl_assert(git_attr_cache__is_cached(
74
		g_repo, GIT_ATTR_FILE_SOURCE_FILE, ".git/info/attributes"));
75
	cl_assert(git_attr_cache__is_cached(
76
		g_repo, GIT_ATTR_FILE_SOURCE_FILE, ".gitattributes"));
77
	cl_assert(git_attr_cache__is_cached(
78
		g_repo, GIT_ATTR_FILE_SOURCE_FILE, "sub/.gitattributes"));
79 80 81 82 83
}

void test_attr_repo__get_one_start_deep(void)
{
	int i;
84

85 86
	for (i = (int)ARRAY_SIZE(get_one_test_cases) - 1; i >= 0; --i) {
		struct attr_expected *scan = &get_one_test_cases[i];
87
		const char *value;
88

89
		cl_git_pass(git_attr_get(&value, g_repo, 0, scan->path, scan->attr));
90 91
		attr_check_expected(
			scan->expected, scan->expected_str, scan->attr, value);
92
	}
93

94
	cl_assert(git_attr_cache__is_cached(
95
		g_repo, GIT_ATTR_FILE_SOURCE_FILE, ".git/info/attributes"));
96
	cl_assert(git_attr_cache__is_cached(
97
		g_repo, GIT_ATTR_FILE_SOURCE_FILE, ".gitattributes"));
98
	cl_assert(git_attr_cache__is_cached(
99
		g_repo, GIT_ATTR_FILE_SOURCE_FILE, "sub/.gitattributes"));
100 101 102 103 104 105 106
}

void test_attr_repo__get_many(void)
{
	const char *names[4] = { "repoattr", "rootattr", "missingattr", "subattr" };
	const char *values[4];

107
	cl_git_pass(git_attr_get_many(values, g_repo, 0, "root_test1", 4, names));
108

109 110 111 112
	cl_assert(GIT_ATTR_IS_TRUE(values[0]));
	cl_assert(GIT_ATTR_IS_TRUE(values[1]));
	cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[2]));
	cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[3]));
113

114
	cl_git_pass(git_attr_get_many(values, g_repo, 0, "root_test2", 4, names));
115

116 117 118 119
	cl_assert(GIT_ATTR_IS_TRUE(values[0]));
	cl_assert(GIT_ATTR_IS_FALSE(values[1]));
	cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[2]));
	cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[3]));
120

121
	cl_git_pass(git_attr_get_many(values, g_repo, 0, "sub/subdir_test1", 4, names));
122

123 124 125
	cl_assert(GIT_ATTR_IS_TRUE(values[0]));
	cl_assert(GIT_ATTR_IS_TRUE(values[1]));
	cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[2]));
126
	cl_assert_equal_s("yes", values[3]);
127 128
}

129 130 131 132 133 134 135 136 137 138
void test_attr_repo__get_many_in_place(void)
{
	const char *vals[4] = { "repoattr", "rootattr", "missingattr", "subattr" };

	/* it should be legal to look up values into the same array that has
	 * the attribute names, overwriting each name as the value is found.
	 */

	cl_git_pass(git_attr_get_many(vals, g_repo, 0, "sub/subdir_test1", 4, vals));

139 140 141
	cl_assert(GIT_ATTR_IS_TRUE(vals[0]));
	cl_assert(GIT_ATTR_IS_TRUE(vals[1]));
	cl_assert(GIT_ATTR_IS_UNSPECIFIED(vals[2]));
142 143 144
	cl_assert_equal_s("yes", vals[3]);
}

145
static int count_attrs(
146 147
	const char *name,
	const char *value,
148 149
	void *payload)
{
150 151
	GIT_UNUSED(name);
	GIT_UNUSED(value);
152 153 154

	*((int *)payload) += 1;

155
	return 0;
156 157
}

158 159
#define CANCEL_VALUE 12345

160 161 162 163 164 165 166 167 168 169 170
static int cancel_iteration(
	const char *name,
	const char *value,
	void *payload)
{
	GIT_UNUSED(name);
	GIT_UNUSED(value);

	*((int *)payload) -= 1;

	if (*((int *)payload) < 0)
171
		return CANCEL_VALUE;
172 173 174 175

	return 0;
}

176 177 178 179 180
void test_attr_repo__foreach(void)
{
	int count;

	count = 0;
181 182
	cl_git_pass(git_attr_foreach(
		g_repo, 0, "root_test1", &count_attrs, &count));
183 184 185
	cl_assert(count == 2);

	count = 0;
186
	cl_git_pass(git_attr_foreach(g_repo, 0, "sub/subdir_test1",
187 188 189 190
		&count_attrs, &count));
	cl_assert(count == 4); /* repoattr, rootattr, subattr, negattr */

	count = 0;
191
	cl_git_pass(git_attr_foreach(g_repo, 0, "sub/subdir_test2.txt",
192
		&count_attrs, &count));
193
	cl_assert(count == 6); /* repoattr, rootattr, subattr, reposub, negattr, another */
194 195 196

	count = 2;
	cl_assert_equal_i(
197
		CANCEL_VALUE, git_attr_foreach(
198 199
			g_repo, 0, "sub/subdir_test1", &cancel_iteration, &count)
	);
200
}
201 202 203 204 205

void test_attr_repo__manpage_example(void)
{
	const char *value;

206
	cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "foo"));
207
	cl_assert(GIT_ATTR_IS_TRUE(value));
208

209
	cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "bar"));
210
	cl_assert(GIT_ATTR_IS_UNSPECIFIED(value));
211

212
	cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "baz"));
213
	cl_assert(GIT_ATTR_IS_FALSE(value));
214

215
	cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "merge"));
216
	cl_assert_equal_s("filfre", value);
217

218
	cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "frotz"));
219
	cl_assert(GIT_ATTR_IS_UNSPECIFIED(value));
220 221
}

222 223 224 225 226 227 228 229 230 231 232
#define CONTENT "I'm going to be dynamically processed\r\n" \
	"And my line endings...\r\n" \
	"...are going to be\n" \
	"normalized!\r\n"

#define GITATTR "* text=auto\n" \
	"*.txt text\n" \
	"*.data binary\n"

static void add_to_workdir(const char *filename, const char *content)
{
233
	git_str buf = GIT_STR_INIT;
234

235 236
	cl_git_pass(git_str_joinpath(&buf, "attr", filename));
	cl_git_rewritefile(git_str_cstr(&buf), content);
237

238
	git_str_dispose(&buf);
239 240 241 242
}

static void assert_proper_normalization(git_index *index, const char *filename, const char *expected_sha)
{
243
	size_t index_pos;
Ben Straub committed
244
	const git_index_entry *entry;
245 246

	add_to_workdir(filename, CONTENT);
247
	cl_git_pass(git_index_add_bypath(index, filename));
248

249
	cl_assert(!git_index_find(&index_pos, index, filename));
250

Edward Thomson committed
251
	entry = git_index_get_byindex(index, index_pos);
252
	cl_assert_equal_i(0, git_oid_streq(&entry->id, expected_sha));
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
}

void test_attr_repo__staging_properly_normalizes_line_endings_according_to_gitattributes_directives(void)
{
	git_index* index;

	cl_git_pass(git_repository_index(&index, g_repo));

	add_to_workdir(".gitattributes", GITATTR);

	assert_proper_normalization(index, "text.txt", "22c74203bace3c2e950278c7ab08da0fca9f4e9b");
	assert_proper_normalization(index, "huh.dunno", "22c74203bace3c2e950278c7ab08da0fca9f4e9b");
	assert_proper_normalization(index, "binary.data", "66eeff1fcbacf589e6d70aa70edd3fce5be2b37c");

	git_index_free(index);
}
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

void test_attr_repo__bare_repo_with_index(void)
{
	const char *names[4] = { "test1", "test2", "test3", "test4" };
	const char *values[4];
	git_index *index;

	cl_git_pass(git_repository_index(&index, g_repo));

	cl_git_mkfile(
		"attr/.gitattributes",
		"*.txt test1 test2=foobar -test3\n"
		"trial.txt -test1 test2=barfoo !test3 test4\n");
	cl_git_pass(git_index_add_bypath(index, ".gitattributes"));
	git_index_free(index);

	cl_must_pass(p_unlink("attr/.gitattributes"));
286
	cl_assert(!git_fs_path_exists("attr/.gitattributes"));
287 288 289 290 291

	cl_git_pass(git_repository_set_bare(g_repo));

	cl_git_pass(git_attr_get_many(values, g_repo, 0, "file.txt", 4, names));

292
	cl_assert(GIT_ATTR_IS_TRUE(values[0]));
293
	cl_assert_equal_s("foobar", values[1]);
294 295
	cl_assert(GIT_ATTR_IS_FALSE(values[2]));
	cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[3]));
296 297 298

	cl_git_pass(git_attr_get_many(values, g_repo, 0, "trial.txt", 4, names));

299
	cl_assert(GIT_ATTR_IS_FALSE(values[0]));
300
	cl_assert_equal_s("barfoo", values[1]);
301 302
	cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[2]));
	cl_assert(GIT_ATTR_IS_TRUE(values[3]));
303 304 305

	cl_git_pass(git_attr_get_many(values, g_repo, 0, "sub/sub/subdir.txt", 4, names));

306
	cl_assert(GIT_ATTR_IS_TRUE(values[0]));
307
	cl_assert_equal_s("foobar", values[1]);
308 309
	cl_assert(GIT_ATTR_IS_FALSE(values[2]));
	cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[3]));
310
}
311 312 313

void test_attr_repo__sysdir(void)
{
314
	git_str sysdir = GIT_STR_INIT;
315 316 317 318
	const char *value;

	cl_git_pass(p_mkdir("system", 0777));
	cl_git_rewritefile("system/gitattributes", "file merge=foo");
319
	cl_git_pass(git_str_joinpath(&sysdir, clar_sandbox_path(), "system"));
320 321 322 323 324 325 326 327
	cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, sysdir.ptr));
	g_repo = cl_git_sandbox_reopen();

	cl_git_pass(git_attr_get(&value, g_repo, 0, "file", "merge"));
	cl_assert_equal_s(value, "foo");

	cl_git_pass(p_unlink("system/gitattributes"));
	cl_git_pass(p_rmdir("system"));
328
	git_str_dispose(&sysdir);
329 330 331 332 333
}

void test_attr_repo__sysdir_with_session(void)
{
	const char *values[2], *attrs[2] = { "foo", "bar" };
334
	git_str sysdir = GIT_STR_INIT;
335 336 337 338
	git_attr_session session;

	cl_git_pass(p_mkdir("system", 0777));
	cl_git_rewritefile("system/gitattributes", "file foo=1 bar=2");
339
	cl_git_pass(git_str_joinpath(&sysdir, clar_sandbox_path(), "system"));
340 341 342 343
	cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, sysdir.ptr));
	g_repo = cl_git_sandbox_reopen();

	cl_git_pass(git_attr_session__init(&session, g_repo));
344
	cl_git_pass(git_attr_get_many_with_session(values, g_repo, &session, NULL, "file", ARRAY_SIZE(attrs), attrs));
345 346 347 348 349 350

	cl_assert_equal_s(values[0], "1");
	cl_assert_equal_s(values[1], "2");

	cl_git_pass(p_unlink("system/gitattributes"));
	cl_git_pass(p_rmdir("system"));
351
	git_str_dispose(&sysdir);
352 353
	git_attr_session__free(&session);
}
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

void test_attr_repo__rewrite(void)
{
	const char *value;

	cl_git_rewritefile("attr/.gitattributes", "file.txt foo=first\n");
	cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "foo"));
	cl_assert_equal_s(value, "first");

	cl_git_rewritefile("attr/.gitattributes", "file.txt foo=second\n");
	cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "foo"));
	cl_assert_equal_s(value, "second");

	cl_git_rewritefile("attr/.gitattributes", "file.txt other=value\n");
	cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "foo"));
	cl_assert_equal_p(value, NULL);
}

void test_attr_repo__rewrite_sysdir(void)
{
374
	git_str sysdir = GIT_STR_INIT;
375 376 377
	const char *value;

	cl_git_pass(p_mkdir("system", 0777));
378
	cl_git_pass(git_str_joinpath(&sysdir, clar_sandbox_path(), "system"));
379 380 381 382 383 384 385 386 387 388 389
	cl_git_pass(git_sysdir_set(GIT_SYSDIR_SYSTEM, sysdir.ptr));
	g_repo = cl_git_sandbox_reopen();

	cl_git_rewritefile("system/gitattributes", "file foo=first");
	cl_git_pass(git_attr_get(&value, g_repo, 0, "file", "foo"));
	cl_assert_equal_s(value, "first");

	cl_git_rewritefile("system/gitattributes", "file foo=second");
	cl_git_pass(git_attr_get(&value, g_repo, 0, "file", "foo"));
	cl_assert_equal_s(value, "second");

390
	git_str_dispose(&sysdir);
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
}

void test_attr_repo__unlink(void)
{
	const char *value;

	cl_git_rewritefile("attr/.gitattributes", "file.txt foo=value1\n");
	cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "foo"));
	cl_assert_equal_s(value, "value1");

	cl_git_pass(p_unlink("attr/.gitattributes"));

	cl_git_pass(git_attr_get(&value, g_repo, 0, "file.txt", "foo"));
	cl_assert_equal_p(value, NULL);
}