]>
git.proxmox.com Git - libgit2.git/blob - tests/submodule/add.c
1 #include "clar_libgit2.h"
4 #include "submodule_helpers.h"
5 #include "config/config_helpers.h"
7 #include "repository.h"
8 #include "git2/sys/commit.h"
10 static git_repository
*g_repo
= NULL
;
11 static const char *valid_blob_id
= "fa49b077972391ad58037050f2a75f74e3671e92";
13 void test_submodule_add__cleanup(void)
15 cl_git_sandbox_cleanup();
18 static void assert_submodule_url(const char* name
, const char *url
)
20 git_buf key
= GIT_BUF_INIT
;
23 cl_git_pass(git_buf_printf(&key
, "submodule.%s.url", name
));
24 assert_config_entry_value(g_repo
, git_buf_cstr(&key
), url
);
26 git_buf_dispose(&key
);
29 void test_submodule_add__url_absolute(void)
33 git_buf dot_git_content
= GIT_BUF_INIT
;
35 g_repo
= setup_fixture_submod2();
37 /* re-add existing submodule */
40 git_submodule_add_setup(NULL
, g_repo
, "whatever", "sm_unchanged", 1));
42 /* add a submodule using a gitlink */
45 git_submodule_add_setup(&sm
, g_repo
, "https://github.com/libgit2/libgit2.git", "sm_libgit2", 1)
47 git_submodule_free(sm
);
49 cl_assert(git_path_isfile("submod2/" "sm_libgit2" "/.git"));
51 cl_assert(git_path_isdir("submod2/.git/modules"));
52 cl_assert(git_path_isdir("submod2/.git/modules/" "sm_libgit2"));
53 cl_assert(git_path_isfile("submod2/.git/modules/" "sm_libgit2" "/HEAD"));
54 assert_submodule_url("sm_libgit2", "https://github.com/libgit2/libgit2.git");
56 cl_git_pass(git_repository_open(&repo
, "submod2/" "sm_libgit2"));
58 /* Verify worktree path is relative */
59 assert_config_entry_value(repo
, "core.worktree", "../../../sm_libgit2/");
61 /* Verify gitdir path is relative */
62 cl_git_pass(git_futils_readbuffer(&dot_git_content
, "submod2/" "sm_libgit2" "/.git"));
63 cl_assert_equal_s("gitdir: ../.git/modules/sm_libgit2/", dot_git_content
.ptr
);
65 git_repository_free(repo
);
66 git_buf_dispose(&dot_git_content
);
68 /* add a submodule not using a gitlink */
71 git_submodule_add_setup(&sm
, g_repo
, "https://github.com/libgit2/libgit2.git", "sm_libgit2b", 0)
73 git_submodule_free(sm
);
75 cl_assert(git_path_isdir("submod2/" "sm_libgit2b" "/.git"));
76 cl_assert(git_path_isfile("submod2/" "sm_libgit2b" "/.git/HEAD"));
77 cl_assert(!git_path_exists("submod2/.git/modules/" "sm_libgit2b"));
78 assert_submodule_url("sm_libgit2b", "https://github.com/libgit2/libgit2.git");
81 void test_submodule_add__url_relative(void)
85 git_strarray problems
= {0};
87 /* default remote url is https://github.com/libgit2/false.git */
88 g_repo
= cl_git_sandbox_init("testrepo2");
90 /* make sure we don't default to origin - rename origin -> test_remote */
91 cl_git_pass(git_remote_rename(&problems
, g_repo
, "origin", "test_remote"));
92 cl_assert_equal_i(0, problems
.count
);
93 git_strarray_dispose(&problems
);
94 cl_git_fail(git_remote_lookup(&remote
, g_repo
, "origin"));
97 git_submodule_add_setup(&sm
, g_repo
, "../TestGitRepository", "TestGitRepository", 1)
99 git_submodule_free(sm
);
101 assert_submodule_url("TestGitRepository", "https://github.com/libgit2/TestGitRepository");
104 void test_submodule_add__url_relative_to_origin(void)
108 /* default remote url is https://github.com/libgit2/false.git */
109 g_repo
= cl_git_sandbox_init("testrepo2");
112 git_submodule_add_setup(&sm
, g_repo
, "../TestGitRepository", "TestGitRepository", 1)
114 git_submodule_free(sm
);
116 assert_submodule_url("TestGitRepository", "https://github.com/libgit2/TestGitRepository");
119 void test_submodule_add__url_relative_to_workdir(void)
123 /* In this repo, HEAD (master) has no remote tracking branc h*/
124 g_repo
= cl_git_sandbox_init("testrepo");
127 git_submodule_add_setup(&sm
, g_repo
, "./", "TestGitRepository", 1)
129 git_submodule_free(sm
);
131 assert_submodule_url("TestGitRepository", git_repository_workdir(g_repo
));
134 static void test_add_entry(
140 git_index_entry entry
= {{0}};
142 cl_git_pass(git_oid_fromstr(&entry
.id
, idstr
));
147 cl_git_pass(git_index_add(index
, &entry
));
150 void test_submodule_add__path_exists_in_index(void)
154 git_buf filename
= GIT_BUF_INIT
;
156 g_repo
= cl_git_sandbox_init("testrepo");
158 cl_git_pass(git_buf_joinpath(&filename
, "subdirectory", "test.txt"));
160 cl_git_pass(git_repository_index__weakptr(&index
, g_repo
));
162 test_add_entry(index
, valid_blob_id
, filename
.ptr
, GIT_FILEMODE_BLOB
);
164 cl_git_fail_with(git_submodule_add_setup(&sm
, g_repo
, "./", "subdirectory", 1), GIT_EEXISTS
);
166 git_submodule_free(sm
);
167 git_buf_dispose(&filename
);
170 void test_submodule_add__file_exists_in_index(void)
174 git_buf name
= GIT_BUF_INIT
;
176 g_repo
= cl_git_sandbox_init("testrepo");
178 cl_git_pass(git_repository_index__weakptr(&index
, g_repo
));
180 test_add_entry(index
, valid_blob_id
, "subdirectory", GIT_FILEMODE_BLOB
);
182 cl_git_fail_with(git_submodule_add_setup(&sm
, g_repo
, "./", "subdirectory", 1), GIT_EEXISTS
);
184 git_submodule_free(sm
);
185 git_buf_dispose(&name
);
188 void test_submodule_add__submodule_clone(void)
190 git_oid tree_id
, commit_id
;
195 g_repo
= cl_git_sandbox_init("empty_standard_repo");
197 /* Create the submodule structure, clone into it and finalize */
198 cl_git_pass(git_submodule_add_setup(&sm
, g_repo
, cl_fixture("testrepo.git"), "testrepo-add", true));
199 cl_git_pass(git_submodule_clone(NULL
, sm
, NULL
));
200 cl_git_pass(git_submodule_add_finalize(sm
));
202 /* Create the submodule commit */
203 cl_git_pass(git_repository_index(&index
, g_repo
));
204 cl_git_pass(git_index_write_tree(&tree_id
, index
));
205 cl_git_pass(git_signature_now(&sig
, "Submoduler", "submoduler@local"));
206 cl_git_pass(git_commit_create_from_ids(&commit_id
, g_repo
, "HEAD", sig
, sig
, NULL
, "A submodule\n",
209 assert_submodule_exists(g_repo
, "testrepo-add");
211 git_signature_free(sig
);
212 git_submodule_free(sm
);
213 git_index_free(index
);
216 void test_submodule_add__submodule_clone_into_nonempty_dir_succeeds(void)
220 g_repo
= cl_git_sandbox_init("empty_standard_repo");
222 cl_git_pass(p_mkdir("empty_standard_repo/sm", 0777));
223 cl_git_mkfile("empty_standard_repo/sm/foobar", "");
225 /* Create the submodule structure, clone into it and finalize */
226 cl_git_pass(git_submodule_add_setup(&sm
, g_repo
, cl_fixture("testrepo.git"), "sm", true));
227 cl_git_pass(git_submodule_clone(NULL
, sm
, NULL
));
228 cl_git_pass(git_submodule_add_finalize(sm
));
230 cl_assert(git_path_exists("empty_standard_repo/sm/foobar"));
232 assert_submodule_exists(g_repo
, "sm");
234 git_submodule_free(sm
);
237 void test_submodule_add__submodule_clone_twice_fails(void)
241 g_repo
= cl_git_sandbox_init("empty_standard_repo");
243 /* Create the submodule structure, clone into it and finalize */
244 cl_git_pass(git_submodule_add_setup(&sm
, g_repo
, cl_fixture("testrepo.git"), "sm", true));
245 cl_git_pass(git_submodule_clone(NULL
, sm
, NULL
));
246 cl_git_pass(git_submodule_add_finalize(sm
));
248 cl_git_fail(git_submodule_clone(NULL
, sm
, NULL
));
250 git_submodule_free(sm
);