]> git.proxmox.com Git - libgit2.git/blobdiff - tests/status/submodules.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / tests / status / submodules.c
index 6d0d63a5f6f52178475d7bc7b531a3a9b5fa8844..d223657b4ead9877756f8f741c4e4931f0f53ee3 100644 (file)
@@ -1,7 +1,5 @@
 #include "clar_libgit2.h"
-#include "buffer.h"
-#include "path.h"
-#include "posix.h"
+#include "futils.h"
 #include "status_helpers.h"
 #include "../submodule/submodule_helpers.h"
 
@@ -38,9 +36,9 @@ void test_status_submodules__0(void)
 
        g_repo = setup_fixture_submodules();
 
-       cl_assert(git_path_isdir("submodules/.git"));
-       cl_assert(git_path_isdir("submodules/testrepo/.git"));
-       cl_assert(git_path_isfile("submodules/.gitmodules"));
+       cl_assert(git_fs_path_isdir("submodules/.git"));
+       cl_assert(git_fs_path_isdir("submodules/testrepo/.git"));
+       cl_assert(git_fs_path_isfile("submodules/.gitmodules"));
 
        cl_git_pass(
                git_status_foreach(g_repo, cb_status__count, &counts)
@@ -73,12 +71,12 @@ static int cb_status__match(const char *p, unsigned int s, void *payload)
        int idx = counts->entry_count++;
 
        clar__assert_equal(
-               counts->file, counts->line,
+               counts->file, counts->func, counts->line,
                "Status path mismatch", 1,
                "%s", counts->expected_paths[idx], p);
 
        clar__assert_equal(
-               counts->file, counts->line,
+               counts->file, counts->func, counts->line,
                "Status code mismatch", 1,
                "%o", counts->expected_statuses[idx], s);
 
@@ -91,9 +89,9 @@ void test_status_submodules__1(void)
 
        g_repo = setup_fixture_submodules();
 
-       cl_assert(git_path_isdir("submodules/.git"));
-       cl_assert(git_path_isdir("submodules/testrepo/.git"));
-       cl_assert(git_path_isfile("submodules/.gitmodules"));
+       cl_assert(git_fs_path_isdir("submodules/.git"));
+       cl_assert(git_fs_path_isdir("submodules/testrepo/.git"));
+       cl_assert(git_fs_path_isfile("submodules/.gitmodules"));
 
        status_counts_init(counts, expected_files, expected_status);
 
@@ -145,7 +143,7 @@ void test_status_submodules__moved_head(void)
        /* move submodule HEAD to c47800c7266a2be04c571c04d5a6614691ea99bd */
        cl_git_pass(
                git_oid_fromstr(&oid, "c47800c7266a2be04c571c04d5a6614691ea99bd"));
-       cl_git_pass(git_repository_set_head_detached(smrepo, &oid, NULL, NULL));
+       cl_git_pass(git_repository_set_head_detached(smrepo, &oid));
 
        /* first do a normal status, which should now include the submodule */
 
@@ -389,3 +387,177 @@ void test_status_submodules__contained_untracked_repo(void)
                g_repo, &opts, cb_status__match, &counts));
        cl_assert_equal_i(5, counts.entry_count);
 }
+
+void test_status_submodules__broken_stuff_that_git_allows(void)
+{
+       git_status_options opts = GIT_STATUS_OPTIONS_INIT;
+       status_entry_counts counts;
+       git_repository *contained;
+       static const char *expected_files_with_broken[] = {
+               ".gitmodules",
+               "added",
+               "broken/tracked",
+               "deleted",
+               "ignored",
+               "modified",
+               "untracked"
+       };
+       static unsigned int expected_status_with_broken[] = {
+               GIT_STATUS_WT_MODIFIED,
+               GIT_STATUS_INDEX_NEW,
+               GIT_STATUS_INDEX_NEW,
+               GIT_STATUS_INDEX_DELETED,
+               GIT_STATUS_IGNORED,
+               GIT_STATUS_WT_MODIFIED,
+               GIT_STATUS_WT_NEW,
+       };
+
+       g_repo = setup_fixture_submodules();
+
+       opts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+               GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS |
+               GIT_STATUS_OPT_INCLUDE_IGNORED;
+
+       /* make a directory and stick a tracked item into the index */
+       {
+               git_index *idx;
+               cl_must_pass(p_mkdir("submodules/broken", 0777));
+               cl_git_mkfile("submodules/broken/tracked", "tracked content");
+               cl_git_pass(git_repository_index(&idx, g_repo));
+               cl_git_pass(git_index_add_bypath(idx, "broken/tracked"));
+               cl_git_pass(git_index_write(idx));
+               git_index_free(idx);
+       }
+
+       status_counts_init(
+               counts, expected_files_with_broken, expected_status_with_broken);
+       cl_git_pass(git_status_foreach_ext(
+               g_repo, &opts, cb_status__match, &counts));
+       cl_assert_equal_i(7, counts.entry_count);
+
+       /* directory with tracked items that looks a little bit like a repo */
+
+       cl_must_pass(p_mkdir("submodules/broken/.git", 0777));
+       cl_must_pass(p_mkdir("submodules/broken/.git/info", 0777));
+       cl_git_mkfile("submodules/broken/.git/info/exclude", "# bogus");
+
+       status_counts_init(
+               counts, expected_files_with_broken, expected_status_with_broken);
+       cl_git_pass(git_status_foreach_ext(
+               g_repo, &opts, cb_status__match, &counts));
+       cl_assert_equal_i(7, counts.entry_count);
+
+       /* directory with tracked items that is a repo */
+
+       cl_git_pass(git_futils_rmdir_r(
+               "submodules/broken/.git", NULL, GIT_RMDIR_REMOVE_FILES));
+       cl_git_pass(git_repository_init(&contained, "submodules/broken", false));
+       git_repository_free(contained);
+
+       status_counts_init(
+               counts, expected_files_with_broken, expected_status_with_broken);
+       cl_git_pass(git_status_foreach_ext(
+               g_repo, &opts, cb_status__match, &counts));
+       cl_assert_equal_i(7, counts.entry_count);
+
+       /* directory with tracked items that claims to be a submodule but is not */
+
+       cl_git_pass(git_futils_rmdir_r(
+               "submodules/broken/.git", NULL, GIT_RMDIR_REMOVE_FILES));
+       cl_git_append2file("submodules/.gitmodules",
+               "\n[submodule \"broken\"]\n"
+               "\tpath = broken\n"
+               "\turl = https://github.com/not/used\n\n");
+
+       status_counts_init(
+               counts, expected_files_with_broken, expected_status_with_broken);
+       cl_git_pass(git_status_foreach_ext(
+               g_repo, &opts, cb_status__match, &counts));
+       cl_assert_equal_i(7, counts.entry_count);
+}
+
+void test_status_submodules__entry_but_dir_tracked(void)
+{
+       git_repository *repo;
+       git_status_list *status;
+       git_diff *diff;
+       git_index *index;
+       git_tree *tree;
+
+       cl_git_pass(git_repository_init(&repo, "mixed-submodule", 0));
+       cl_git_mkfile("mixed-submodule/.gitmodules", "[submodule \"sub\"]\n path = sub\n url = ../foo\n");
+       cl_git_pass(p_mkdir("mixed-submodule/sub", 0777));
+       cl_git_mkfile("mixed-submodule/sub/file", "");
+
+       /* Create the commit with sub/file as a file, and an entry for sub in the modules list */
+       {
+               git_oid tree_id, commit_id;
+               git_signature *sig;
+               git_reference *ref;
+
+               cl_git_pass(git_repository_index(&index, repo));
+               cl_git_pass(git_index_add_bypath(index, ".gitmodules"));
+               cl_git_pass(git_index_add_bypath(index, "sub/file"));
+               cl_git_pass(git_index_write(index));
+               cl_git_pass(git_index_write_tree(&tree_id, index));
+               cl_git_pass(git_signature_now(&sig, "Sloppy Submoduler", "sloppy@example.com"));
+               cl_git_pass(git_tree_lookup(&tree, repo, &tree_id));
+               cl_git_pass(git_commit_create(&commit_id, repo, NULL, sig, sig, NULL, "message", tree, 0, NULL));
+               cl_git_pass(git_reference_create(&ref, repo, "refs/heads/master", &commit_id, 1, "commit: foo"));
+               git_reference_free(ref);
+               git_signature_free(sig);
+       }
+
+       cl_git_pass(git_diff_tree_to_index(&diff, repo, tree, index, NULL));
+       cl_assert_equal_i(0, git_diff_num_deltas(diff));
+       git_diff_free(diff);
+
+       cl_git_pass(git_diff_index_to_workdir(&diff, repo, index, NULL));
+       cl_assert_equal_i(0, git_diff_num_deltas(diff));
+       git_diff_free(diff);
+
+       cl_git_pass(git_status_list_new(&status, repo, NULL));
+       cl_assert_equal_i(0, git_status_list_entrycount(status));
+
+       git_status_list_free(status);
+       git_index_free(index);
+       git_tree_free(tree);
+       git_repository_free(repo);
+}
+
+void test_status_submodules__mixed_case(void)
+{
+       git_status_list *status;
+       git_status_options status_opts = GIT_STATUS_OPTIONS_INIT;
+       const git_status_entry *s;
+       size_t i;
+
+       status_opts.flags =
+               GIT_STATUS_OPT_INCLUDE_UNTRACKED |
+               GIT_STATUS_OPT_INCLUDE_IGNORED |
+               GIT_STATUS_OPT_INCLUDE_UNMODIFIED |
+               GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS |
+               GIT_STATUS_OPT_RECURSE_IGNORED_DIRS |
+               GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
+               GIT_STATUS_OPT_RENAMES_INDEX_TO_WORKDIR |
+               GIT_STATUS_OPT_RENAMES_FROM_REWRITES |
+               GIT_STATUS_OPT_INCLUDE_UNREADABLE |
+               GIT_STATUS_OPT_INCLUDE_UNREADABLE_AS_UNTRACKED;
+
+    g_repo = setup_fixture_submod3();
+
+       cl_git_pass(git_status_list_new(&status, g_repo, &status_opts));
+
+       for (i = 0; i < git_status_list_entrycount(status); i++) {
+               s = git_status_byindex(status, i);
+
+               if (s->head_to_index &&
+                       strcmp(s->head_to_index->old_file.path, ".gitmodules") == 0)
+                       continue;
+
+               cl_assert_equal_i(0, s->status);
+       }
+
+       git_status_list_free(status);
+}
+