]> git.proxmox.com Git - libgit2.git/blobdiff - tests/attr/repo.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / attr / repo.c
index f9ba585fb329b44e5f7fdc349215d0ab3b3ac005..abd2381541d87f47deb2eabffdf101f0f8ebdc56 100644 (file)
@@ -1,19 +1,16 @@
 #include "clar_libgit2.h"
-#include "fileops.h"
+#include "futils.h"
 #include "git2/attr.h"
 #include "attr.h"
+#include "sysdir.h"
 
 #include "attr_expect.h"
+#include "git2/sys/repository.h"
 
 static git_repository *g_repo = NULL;
 
 void test_attr_repo__initialize(void)
 {
-       /* Before each test, instantiate the attr repo from the fixtures and
-        * rename the .gitted to .git so it is a repo with a working dir.
-        * Also rename gitattributes to .gitattributes, because it contains
-        * macro definitions which are only allowed in the root.
-        */
        g_repo = cl_git_sandbox_init("attr");
 }
 
@@ -21,56 +18,85 @@ 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[] = {
+       { "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 },
+};
+
 void test_attr_repo__get_one(void)
 {
-       struct attr_expected 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 },
-               { NULL, NULL, 0, NULL }
-       }, *scan;
-
-       for (scan = test_cases; scan->path != NULL; scan++) {
+       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);
+               attr_check_expected(
+                       scan->expected, scan->expected_str, scan->attr, value);
        }
 
-       cl_assert(git_attr_cache__is_cached(g_repo, 0, ".git/info/attributes"));
-       cl_assert(git_attr_cache__is_cached(g_repo, 0, ".gitattributes"));
-       cl_assert(git_attr_cache__is_cached(g_repo, 0, "sub/.gitattributes"));
+       cl_assert(git_attr_cache__is_cached(
+               g_repo, GIT_ATTR_FILE_SOURCE_FILE, ".git/info/attributes"));
+       cl_assert(git_attr_cache__is_cached(
+               g_repo, GIT_ATTR_FILE_SOURCE_FILE, ".gitattributes"));
+       cl_assert(git_attr_cache__is_cached(
+               g_repo, GIT_ATTR_FILE_SOURCE_FILE, "sub/.gitattributes"));
+}
+
+void test_attr_repo__get_one_start_deep(void)
+{
+       int i;
+
+       for (i = (int)ARRAY_SIZE(get_one_test_cases) - 1; i >= 0; --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(
+               g_repo, GIT_ATTR_FILE_SOURCE_FILE, ".git/info/attributes"));
+       cl_assert(git_attr_cache__is_cached(
+               g_repo, GIT_ATTR_FILE_SOURCE_FILE, ".gitattributes"));
+       cl_assert(git_attr_cache__is_cached(
+               g_repo, GIT_ATTR_FILE_SOURCE_FILE, "sub/.gitattributes"));
 }
 
 void test_attr_repo__get_many(void)
@@ -80,23 +106,23 @@ void test_attr_repo__get_many(void)
 
        cl_git_pass(git_attr_get_many(values, g_repo, 0, "root_test1", 4, names));
 
-       cl_assert(GIT_ATTR_TRUE(values[0]));
-       cl_assert(GIT_ATTR_TRUE(values[1]));
-       cl_assert(GIT_ATTR_UNSPECIFIED(values[2]));
-       cl_assert(GIT_ATTR_UNSPECIFIED(values[3]));
+       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]));
 
        cl_git_pass(git_attr_get_many(values, g_repo, 0, "root_test2", 4, names));
 
-       cl_assert(GIT_ATTR_TRUE(values[0]));
-       cl_assert(GIT_ATTR_FALSE(values[1]));
-       cl_assert(GIT_ATTR_UNSPECIFIED(values[2]));
-       cl_assert(GIT_ATTR_UNSPECIFIED(values[3]));
+       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]));
 
        cl_git_pass(git_attr_get_many(values, g_repo, 0, "sub/subdir_test1", 4, names));
 
-       cl_assert(GIT_ATTR_TRUE(values[0]));
-       cl_assert(GIT_ATTR_TRUE(values[1]));
-       cl_assert(GIT_ATTR_UNSPECIFIED(values[2]));
+       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_equal_s("yes", values[3]);
 }
 
@@ -110,9 +136,9 @@ void test_attr_repo__get_many_in_place(void)
 
        cl_git_pass(git_attr_get_many(vals, g_repo, 0, "sub/subdir_test1", 4, vals));
 
-       cl_assert(GIT_ATTR_TRUE(vals[0]));
-       cl_assert(GIT_ATTR_TRUE(vals[1]));
-       cl_assert(GIT_ATTR_UNSPECIFIED(vals[2]));
+       cl_assert(GIT_ATTR_IS_TRUE(vals[0]));
+       cl_assert(GIT_ATTR_IS_TRUE(vals[1]));
+       cl_assert(GIT_ATTR_IS_UNSPECIFIED(vals[2]));
        cl_assert_equal_s("yes", vals[3]);
 }
 
@@ -178,89 +204,19 @@ void test_attr_repo__manpage_example(void)
        const char *value;
 
        cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "foo"));
-       cl_assert(GIT_ATTR_TRUE(value));
+       cl_assert(GIT_ATTR_IS_TRUE(value));
 
        cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "bar"));
-       cl_assert(GIT_ATTR_UNSPECIFIED(value));
+       cl_assert(GIT_ATTR_IS_UNSPECIFIED(value));
 
        cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "baz"));
-       cl_assert(GIT_ATTR_FALSE(value));
+       cl_assert(GIT_ATTR_IS_FALSE(value));
 
        cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "merge"));
        cl_assert_equal_s("filfre", value);
 
        cl_git_pass(git_attr_get(&value, g_repo, 0, "sub/abc", "frotz"));
-       cl_assert(GIT_ATTR_UNSPECIFIED(value));
-}
-
-void test_attr_repo__macros(void)
-{
-       const char *names[5] = { "rootattr", "binary", "diff", "crlf", "frotz" };
-       const char *names2[5] = { "mymacro", "positive", "negative", "rootattr", "another" };
-       const char *names3[3] = { "macro2", "multi2", "multi3" };
-       const char *values[5];
-
-       cl_git_pass(git_attr_get_many(values, g_repo, 0, "binfile", 5, names));
-
-       cl_assert(GIT_ATTR_TRUE(values[0]));
-       cl_assert(GIT_ATTR_TRUE(values[1]));
-       cl_assert(GIT_ATTR_FALSE(values[2]));
-       cl_assert(GIT_ATTR_FALSE(values[3]));
-       cl_assert(GIT_ATTR_UNSPECIFIED(values[4]));
-
-       cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_test", 5, names2));
-
-       cl_assert(GIT_ATTR_TRUE(values[0]));
-       cl_assert(GIT_ATTR_TRUE(values[1]));
-       cl_assert(GIT_ATTR_FALSE(values[2]));
-       cl_assert(GIT_ATTR_UNSPECIFIED(values[3]));
-       cl_assert_equal_s("77", values[4]);
-
-       cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_test", 3, names3));
-
-       cl_assert(GIT_ATTR_TRUE(values[0]));
-       cl_assert(GIT_ATTR_FALSE(values[1]));
-       cl_assert_equal_s("answer", values[2]);
-}
-
-void test_attr_repo__bad_macros(void)
-{
-       const char *names[6] = { "rootattr", "positive", "negative",
-               "firstmacro", "secondmacro", "thirdmacro" };
-       const char *values[6];
-
-       cl_git_pass(git_attr_get_many(values, g_repo, 0, "macro_bad", 6, names));
-
-       /* these three just confirm that the "mymacro" rule ran */
-       cl_assert(GIT_ATTR_UNSPECIFIED(values[0]));
-       cl_assert(GIT_ATTR_TRUE(values[1]));
-       cl_assert(GIT_ATTR_FALSE(values[2]));
-
-       /* file contains:
-        *     # let's try some malicious macro defs
-        *     [attr]firstmacro -thirdmacro -secondmacro
-        *     [attr]secondmacro firstmacro -firstmacro
-        *     [attr]thirdmacro secondmacro=hahaha -firstmacro
-        *     macro_bad firstmacro secondmacro thirdmacro
-        *
-        * firstmacro assignment list ends up with:
-        *     -thirdmacro -secondmacro
-        * secondmacro assignment list expands "firstmacro" and ends up with:
-        *     -thirdmacro -secondmacro -firstmacro
-        * thirdmacro assignment don't expand so list ends up with:
-        *     secondmacro="hahaha"
-        *
-        * macro_bad assignment list ends up with:
-        *     -thirdmacro -secondmacro firstmacro &&
-        *     -thirdmacro -secondmacro -firstmacro secondmacro &&
-        *     secondmacro="hahaha" thirdmacro
-        *
-        * so summary results should be:
-        *     -firstmacro secondmacro="hahaha" thirdmacro
-        */
-       cl_assert(GIT_ATTR_FALSE(values[3]));
-       cl_assert_equal_s("hahaha", values[4]);
-       cl_assert(GIT_ATTR_TRUE(values[5]));
+       cl_assert(GIT_ATTR_IS_UNSPECIFIED(value));
 }
 
 #define CONTENT "I'm going to be dynamically processed\r\n" \
@@ -274,12 +230,12 @@ void test_attr_repo__bad_macros(void)
 
 static void add_to_workdir(const char *filename, const char *content)
 {
-       git_buf buf = GIT_BUF_INIT;
+       git_str buf = GIT_STR_INIT;
 
-       cl_git_pass(git_buf_joinpath(&buf, "attr", filename));
-       cl_git_rewritefile(git_buf_cstr(&buf), content);
+       cl_git_pass(git_str_joinpath(&buf, "attr", filename));
+       cl_git_rewritefile(git_str_cstr(&buf), content);
 
-       git_buf_free(&buf);
+       git_str_dispose(&buf);
 }
 
 static void assert_proper_normalization(git_index *index, const char *filename, const char *expected_sha)
@@ -293,7 +249,7 @@ static void assert_proper_normalization(git_index *index, const char *filename,
        cl_assert(!git_index_find(&index_pos, index, filename));
 
        entry = git_index_get_byindex(index, index_pos);
-       cl_assert_equal_i(0, git_oid_streq(&entry->oid, expected_sha));
+       cl_assert_equal_i(0, git_oid_streq(&entry->id, expected_sha));
 }
 
 void test_attr_repo__staging_properly_normalizes_line_endings_according_to_gitattributes_directives(void)
@@ -310,3 +266,140 @@ void test_attr_repo__staging_properly_normalizes_line_endings_according_to_gitat
 
        git_index_free(index);
 }
+
+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"));
+       cl_assert(!git_fs_path_exists("attr/.gitattributes"));
+
+       cl_git_pass(git_repository_set_bare(g_repo));
+
+       cl_git_pass(git_attr_get_many(values, g_repo, 0, "file.txt", 4, names));
+
+       cl_assert(GIT_ATTR_IS_TRUE(values[0]));
+       cl_assert_equal_s("foobar", values[1]);
+       cl_assert(GIT_ATTR_IS_FALSE(values[2]));
+       cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[3]));
+
+       cl_git_pass(git_attr_get_many(values, g_repo, 0, "trial.txt", 4, names));
+
+       cl_assert(GIT_ATTR_IS_FALSE(values[0]));
+       cl_assert_equal_s("barfoo", values[1]);
+       cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[2]));
+       cl_assert(GIT_ATTR_IS_TRUE(values[3]));
+
+       cl_git_pass(git_attr_get_many(values, g_repo, 0, "sub/sub/subdir.txt", 4, names));
+
+       cl_assert(GIT_ATTR_IS_TRUE(values[0]));
+       cl_assert_equal_s("foobar", values[1]);
+       cl_assert(GIT_ATTR_IS_FALSE(values[2]));
+       cl_assert(GIT_ATTR_IS_UNSPECIFIED(values[3]));
+}
+
+void test_attr_repo__sysdir(void)
+{
+       git_str sysdir = GIT_STR_INIT;
+       const char *value;
+
+       cl_git_pass(p_mkdir("system", 0777));
+       cl_git_rewritefile("system/gitattributes", "file merge=foo");
+       cl_git_pass(git_str_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_str_dispose(&sysdir);
+}
+
+void test_attr_repo__sysdir_with_session(void)
+{
+       const char *values[2], *attrs[2] = { "foo", "bar" };
+       git_str sysdir = GIT_STR_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_str_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, NULL, "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_str_dispose(&sysdir);
+       git_attr_session__free(&session);
+}
+
+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)
+{
+       git_str sysdir = GIT_STR_INIT;
+       const char *value;
+
+       cl_git_pass(p_mkdir("system", 0777));
+       cl_git_pass(git_str_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_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");
+
+       git_str_dispose(&sysdir);
+}
+
+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);
+}