Commit 5c87b5a8f703352f7d80841b30ee7c3e09a1a12a

Patrick Steinhardt 2019-07-04T12:19:07

Merge pull request #5152 from csware/attr-system-attr-file Fix Regression: attr: Correctly load system attr file (on Windows)

diff --git a/src/attr.c b/src/attr.c
index f50d28c..877bc87 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -333,6 +333,7 @@ static int attr_setup(git_repository *repo, git_attr_session *attr_session)
 				       NULL, git_repository_attr_cache(repo)->cfg_attr_file)) < 0)
 		goto out;
 
+	git_buf_clear(&path); /* git_repository_item_path expects an empty buffer, because it uses git_buf_set */
 	if ((error = git_repository_item_path(&path, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 ||
 	    (error = preload_attr_file(repo, attr_session, GIT_ATTR_FILE__FROM_FILE,
 				       path.ptr, GIT_ATTR_FILE_INREPO)) < 0) {
diff --git a/src/attr_file.c b/src/attr_file.c
index edcfbda..5583837 100644
--- a/src/attr_file.c
+++ b/src/attr_file.c
@@ -919,6 +919,7 @@ int git_attr_session__init(git_attr_session *session, git_repository *repo)
 {
 	assert(repo);
 
+	memset(session, 0, sizeof(*session));
 	session->key = git_atomic_inc(&repo->attr_session_key);
 
 	return 0;
diff --git a/tests/attr/repo.c b/tests/attr/repo.c
index 5f50e90..32f7b66 100644
--- a/tests/attr/repo.c
+++ b/tests/attr/repo.c
@@ -2,6 +2,7 @@
 #include "fileops.h"
 #include "git2/attr.h"
 #include "attr.h"
+#include "sysdir.h"
 
 #include "attr_expect.h"
 #include "git2/sys/repository.h"
@@ -17,6 +18,7 @@ void test_attr_repo__cleanup(void)
 {
 	cl_git_sandbox_cleanup();
 	g_repo = NULL;
+	cl_sandbox_set_search_path_defaults();
 }
 
 static struct attr_expected get_one_test_cases[] = {
@@ -376,3 +378,46 @@ void test_attr_repo__bare_repo_with_index(void)
 	cl_assert(GIT_ATTR_IS_FALSE(values[2]));
 	cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[3]));
 }
+
+void test_attr_repo__sysdir(void)
+{
+	git_buf sysdir = GIT_BUF_INIT;
+	const char *value;
+
+	cl_git_pass(p_mkdir("system", 0777));
+	cl_git_rewritefile("system/gitattributes", "file merge=foo");
+	cl_git_pass(git_buf_joinpath(&sysdir, clar_sandbox_path(), "system"));
+	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"));
+	git_buf_dispose(&sysdir);
+}
+
+void test_attr_repo__sysdir_with_session(void)
+{
+	const char *values[2], *attrs[2] = { "foo", "bar" };
+	git_buf sysdir = GIT_BUF_INIT;
+	git_attr_session session;
+
+	cl_git_pass(p_mkdir("system", 0777));
+	cl_git_rewritefile("system/gitattributes", "file foo=1 bar=2");
+	cl_git_pass(git_buf_joinpath(&sysdir, clar_sandbox_path(), "system"));
+	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));
+	cl_git_pass(git_attr_get_many_with_session(values, g_repo, &session, 0, "file", ARRAY_SIZE(attrs), attrs));
+
+	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"));
+	git_buf_dispose(&sysdir);
+	git_attr_session__free(&session);
+}