]> git.proxmox.com Git - libgit2.git/blobdiff - tests/checkout/crlf.c
New upstream version 1.1.0+dfsg.1
[libgit2.git] / tests / checkout / crlf.c
index 2cf3af36416884da6222c39e34be75b7b53060fa..4950201615c1c4b7ccaa30af9511894c74800bdd 100644 (file)
@@ -1,7 +1,7 @@
 #include "clar_libgit2.h"
 #include "checkout_helpers.h"
 #include "../filter/crlf.h"
-#include "fileops.h"
+#include "futils.h"
 
 #include "git2/checkout.h"
 #include "repository.h"
@@ -13,14 +13,40 @@ static git_repository *g_repo;
 static const char *systype;
 static git_buf expected_fixture = GIT_BUF_INIT;
 
+static int unlink_file(void *payload, git_buf *path)
+{
+       char *fn;
+
+       cl_assert(fn = git_path_basename(path->ptr));
+
+       GIT_UNUSED(payload);
+
+       if (strcmp(fn, ".git"))
+               cl_must_pass(p_unlink(path->ptr));
+
+       git__free(fn);
+       return 0;
+}
+
 void test_checkout_crlf__initialize(void)
 {
+       git_buf reponame = GIT_BUF_INIT;
+
        g_repo = cl_git_sandbox_init("crlf");
 
+       /*
+        * remove the contents of the working directory so that we can
+        * check out over it.
+        */
+       cl_git_pass(git_buf_puts(&reponame, "crlf"));
+       cl_git_pass(git_path_direach(&reponame, 0, unlink_file, NULL));
+
        if (GIT_EOL_NATIVE == GIT_EOL_CRLF)
                systype = "windows";
        else
                systype = "posix";
+
+       git_buf_dispose(&reponame);
 }
 
 void test_checkout_crlf__cleanup(void)
@@ -29,7 +55,7 @@ void test_checkout_crlf__cleanup(void)
 
        if (expected_fixture.size) {
                cl_fixture_cleanup(expected_fixture.ptr);
-               git_buf_free(&expected_fixture);
+               git_buf_dispose(&expected_fixture);
        }
 }
 
@@ -47,15 +73,19 @@ static int compare_file(void *payload, git_buf *actual_path)
        git_buf expected_contents = GIT_BUF_INIT;
        struct compare_data *cd = payload;
        bool failed = true;
+       int cmp_git, cmp_gitattributes;
+       char *basename;
+
+       basename = git_path_basename(actual_path->ptr);
+       cmp_git = strcmp(basename, ".git");
+       cmp_gitattributes = strcmp(basename, ".gitattributes");
 
-       if (strcmp(git_path_basename(actual_path->ptr), ".git") == 0 ||
-               strcmp(git_path_basename(actual_path->ptr), ".gitattributes") == 0) {
+       if (cmp_git == 0 || cmp_gitattributes == 0) {
                failed = false;
                goto done;
        }
 
-       cl_git_pass(git_buf_joinpath(&expected_path, cd->dirname,
-               git_path_basename(actual_path->ptr)));
+       cl_git_pass(git_buf_joinpath(&expected_path, cd->dirname, basename));
 
        if (!git_path_isfile(expected_path.ptr) ||
                !git_path_isfile(actual_path->ptr))
@@ -78,14 +108,15 @@ done:
                git_buf details = GIT_BUF_INIT;
                git_buf_printf(&details, "filename=%s, system=%s, autocrlf=%s, attrs={%s}",
                        git_path_basename(actual_path->ptr), systype, cd->autocrlf, cd->attrs);
-               clar__fail(__FILE__, __LINE__,
+               clar__fail(__FILE__, __func__, __LINE__,
                        "checked out contents did not match expected", details.ptr, 0);
-               git_buf_free(&details);
+               git_buf_dispose(&details);
        }
 
-       git_buf_free(&expected_contents);
-       git_buf_free(&actual_contents);
-       git_buf_free(&expected_path);
+       git__free(basename);
+       git_buf_dispose(&expected_contents);
+       git_buf_dispose(&actual_contents);
+       git_buf_dispose(&expected_path);
 
        return 0;
 }
@@ -94,66 +125,77 @@ static void test_checkout(const char *autocrlf, const char *attrs)
 {
        git_buf attrbuf = GIT_BUF_INIT;
        git_buf expected_dirname = GIT_BUF_INIT;
+       git_buf systype_and_direction = GIT_BUF_INIT;
        git_buf sandboxname = GIT_BUF_INIT;
        git_buf reponame = GIT_BUF_INIT;
        git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
        struct compare_data compare_data = { NULL, autocrlf, attrs };
        const char *c;
 
-       git_buf_puts(&reponame, "crlf");
+       cl_git_pass(git_buf_puts(&reponame, "crlf"));
+
+       cl_git_pass(git_buf_puts(&systype_and_direction, systype));
+       cl_git_pass(git_buf_puts(&systype_and_direction, "_to_workdir"));
 
-       git_buf_puts(&sandboxname, "autocrlf_");
-       git_buf_puts(&sandboxname, autocrlf);
+       cl_git_pass(git_buf_puts(&sandboxname, "autocrlf_"));
+       cl_git_pass(git_buf_puts(&sandboxname, autocrlf));
 
        if (*attrs) {
-               git_buf_puts(&sandboxname, ",");
+               cl_git_pass(git_buf_puts(&sandboxname, ","));
 
                for (c = attrs; *c; c++) {
                        if (*c == ' ')
-                               git_buf_putc(&sandboxname, ',');
+                               cl_git_pass(git_buf_putc(&sandboxname, ','));
                        else if (*c == '=')
-                               git_buf_putc(&sandboxname, '_');
+                               cl_git_pass(git_buf_putc(&sandboxname, '_'));
                        else
-                               git_buf_putc(&sandboxname, *c);
+                               cl_git_pass(git_buf_putc(&sandboxname, *c));
                }
 
-               git_buf_printf(&attrbuf, "* %s\n", attrs);
+               cl_git_pass(git_buf_printf(&attrbuf, "* %s\n", attrs));
                cl_git_mkfile("crlf/.gitattributes", attrbuf.ptr);
        }
 
        cl_repo_set_string(g_repo, "core.autocrlf", autocrlf);
 
-       git_buf_joinpath(&expected_dirname, systype, sandboxname.ptr);
-       git_buf_joinpath(&expected_fixture, "crlf_data", expected_dirname.ptr);
+       cl_git_pass(git_buf_joinpath(&expected_dirname, systype_and_direction.ptr, sandboxname.ptr));
+       cl_git_pass(git_buf_joinpath(&expected_fixture, "crlf_data", expected_dirname.ptr));
        cl_fixture_sandbox(expected_fixture.ptr);
 
        opts.checkout_strategy = GIT_CHECKOUT_FORCE;
-       git_checkout_head(g_repo, &opts);
+       cl_git_pass(git_checkout_head(g_repo, &opts));
 
        compare_data.dirname = sandboxname.ptr;
        cl_git_pass(git_path_direach(&reponame, 0, compare_file, &compare_data));
 
        cl_fixture_cleanup(expected_fixture.ptr);
-       git_buf_free(&expected_fixture);
-
-       git_buf_free(&attrbuf);
-       git_buf_free(&expected_fixture);
-       git_buf_free(&expected_dirname);
-       git_buf_free(&sandboxname);
-       git_buf_free(&reponame);
+       git_buf_dispose(&expected_fixture);
+
+       git_buf_dispose(&attrbuf);
+       git_buf_dispose(&expected_fixture);
+       git_buf_dispose(&expected_dirname);
+       git_buf_dispose(&sandboxname);
+       git_buf_dispose(&systype_and_direction);
+       git_buf_dispose(&reponame);
 }
 
 static void empty_workdir(const char *name)
 {
        git_vector contents = GIT_VECTOR_INIT;
+       char *basename;
+       int cmp;
        size_t i;
        const char *fn;
 
-       git_path_dirload(&contents, name, 0, 0);
+       cl_git_pass(git_path_dirload(&contents, name, 0, 0));
        git_vector_foreach(&contents, i, fn) {
-               if (strncasecmp(git_path_basename(fn), ".git", 4) == 0)
-                       continue;
-               p_unlink(fn);
+               cl_assert(basename = git_path_basename(fn));
+               cmp = strncasecmp(basename, ".git", 4);
+
+               git__free(basename);
+
+               if (cmp)
+                       cl_git_pass(p_unlink(fn));
        }
        git_vector_free_deep(&contents);
 }
@@ -163,7 +205,7 @@ void test_checkout_crlf__matches_core_git(void)
        const char *autocrlf[] = { "true", "false", "input", NULL };
        const char *attrs[] = { "", "-crlf", "-text", "eol=crlf", "eol=lf",
                "text", "text eol=crlf", "text eol=lf",
-               "text=auto", "text=auto eol=crlf", "text=auto eol=lf", 
+               "text=auto", "text=auto eol=crlf", "text=auto eol=lf",
                NULL };
        const char **a, **b;
 
@@ -268,6 +310,7 @@ void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
 void test_checkout_crlf__with_ident(void)
 {
        git_index *index;
+       const git_index_entry *entry;
        git_blob *blob;
        git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
        opts.checkout_strategy = GIT_CHECKOUT_FORCE;
@@ -300,14 +343,14 @@ void test_checkout_crlf__with_ident(void)
 
        /* check that blobs have $Id$ */
 
-       cl_git_pass(git_blob_lookup(&blob, g_repo,
-               & git_index_get_bypath(index, "lf.ident", 0)->id));
+       cl_assert((entry = git_index_get_bypath(index, "lf.ident", 0)));
+       cl_git_pass(git_blob_lookup(&blob, g_repo, &entry->id));
        cl_assert_equal_s(
                ALL_LF_TEXT_RAW "\n$Id$\n", git_blob_rawcontent(blob));
        git_blob_free(blob);
 
-       cl_git_pass(git_blob_lookup(&blob, g_repo,
-               & git_index_get_bypath(index, "more2.identcrlf", 0)->id));
+       cl_assert((entry = git_index_get_bypath(index, "more2.identcrlf", 0)));
+       cl_git_pass(git_blob_lookup(&blob, g_repo, &entry->id));
        cl_assert_equal_s(
                "\n$Id$\n" MORE_CRLF_TEXT_AS_LF, git_blob_rawcontent(blob));
        git_blob_free(blob);
@@ -324,7 +367,7 @@ void test_checkout_crlf__with_ident(void)
        p_unlink("crlf/more1.identlf");
        p_unlink("crlf/more2.identcrlf");
 
-       git_checkout_head(g_repo, &opts);
+       cl_git_pass(git_checkout_head(g_repo, &opts));
 
        cl_assert_equal_file(
                ALL_LF_TEXT_AS_CRLF
@@ -353,7 +396,7 @@ void test_checkout_crlf__autocrlf_false_no_attrs(void)
 
        cl_repo_set_bool(g_repo, "core.autocrlf", false);
 
-       git_checkout_head(g_repo, &opts);
+       cl_git_pass(git_checkout_head(g_repo, &opts));
 
        check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
        check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
@@ -366,7 +409,7 @@ void test_checkout_crlf__autocrlf_true_no_attrs(void)
 
        cl_repo_set_bool(g_repo, "core.autocrlf", true);
 
-       git_checkout_head(g_repo, &opts);
+       cl_git_pass(git_checkout_head(g_repo, &opts));
 
        check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
        check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
@@ -379,7 +422,7 @@ void test_checkout_crlf__autocrlf_input_no_attrs(void)
 
        cl_repo_set_string(g_repo, "core.autocrlf", "input");
 
-       git_checkout_head(g_repo, &opts);
+       cl_git_pass(git_checkout_head(g_repo, &opts));
 
        check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
        check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
@@ -394,7 +437,7 @@ void test_checkout_crlf__autocrlf_false_text_auto_attr(void)
 
        cl_repo_set_bool(g_repo, "core.autocrlf", false);
 
-       git_checkout_head(g_repo, &opts);
+       cl_git_pass(git_checkout_head(g_repo, &opts));
 
        if (GIT_EOL_NATIVE == GIT_EOL_CRLF) {
                check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
@@ -414,7 +457,7 @@ void test_checkout_crlf__autocrlf_true_text_auto_attr(void)
 
        cl_repo_set_bool(g_repo, "core.autocrlf", true);
 
-       git_checkout_head(g_repo, &opts);
+       cl_git_pass(git_checkout_head(g_repo, &opts));
 
        check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
        check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
@@ -429,7 +472,7 @@ void test_checkout_crlf__autocrlf_input_text_auto_attr(void)
 
        cl_repo_set_string(g_repo, "core.autocrlf", "input");
 
-       git_checkout_head(g_repo, &opts);
+       cl_git_pass(git_checkout_head(g_repo, &opts));
 
        check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
        check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
@@ -442,8 +485,8 @@ void test_checkout_crlf__can_write_empty_file(void)
 
        cl_repo_set_bool(g_repo, "core.autocrlf", true);
 
-       git_repository_set_head(g_repo, "refs/heads/empty-files");
-       git_checkout_head(g_repo, &opts);
+       cl_git_pass(git_repository_set_head(g_repo, "refs/heads/empty-files"));
+       cl_git_pass(git_checkout_head(g_repo, &opts));
 
        check_file_contents("./crlf/test1.txt", "");