|
5 | 5 | #include "config/config_helpers.h" |
6 | 6 | #include "futils.h" |
7 | 7 | #include "repository.h" |
| 8 | +#include "git2/sys/commit.h" |
8 | 9 |
|
9 | 10 | static git_repository *g_repo = NULL; |
10 | 11 | static const char *valid_blob_id = "fa49b077972391ad58037050f2a75f74e3671e92"; |
@@ -183,3 +184,83 @@ void test_submodule_add__file_exists_in_index(void) |
183 | 184 | git_submodule_free(sm); |
184 | 185 | git_buf_dispose(&name); |
185 | 186 | } |
| 187 | + |
| 188 | +static int just_return_origin(git_remote **out, git_repository *repo, const char *name, const char *url, void *payload) |
| 189 | +{ |
| 190 | + GIT_UNUSED(url); GIT_UNUSED(payload); |
| 191 | + |
| 192 | + return git_remote_lookup(out, repo, name); |
| 193 | +} |
| 194 | + |
| 195 | +static int just_return_repo(git_repository **out, const char *path, int bare, void *payload) |
| 196 | +{ |
| 197 | + git_submodule *sm = payload; |
| 198 | + |
| 199 | + GIT_UNUSED(path); GIT_UNUSED(bare); |
| 200 | + |
| 201 | + return git_submodule_open(out, sm); |
| 202 | +} |
| 203 | + |
| 204 | +void test_submodule_add__homemade_clone(void) |
| 205 | +{ |
| 206 | + git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; |
| 207 | + git_index *index; |
| 208 | + git_oid tree_id, commit_id; |
| 209 | + git_submodule *sm; |
| 210 | + git_signature *sig; |
| 211 | + git_repository *sm_repo; |
| 212 | + |
| 213 | + cl_git_pass(git_repository_init(&g_repo, "willaddsubmodule", false)); |
| 214 | + |
| 215 | + /* Create the submodule structure, clone into it and finalize */ |
| 216 | + cl_git_pass(git_submodule_add_setup(&sm, g_repo, cl_fixture("testrepo.git"), "testrepo", true)); |
| 217 | + |
| 218 | + clone_opts.repository_cb = just_return_repo; |
| 219 | + clone_opts.repository_cb_payload = sm; |
| 220 | + clone_opts.remote_cb = just_return_origin; |
| 221 | + clone_opts.remote_cb_payload = sm; |
| 222 | + cl_git_pass(git_clone(&sm_repo, cl_fixture("testrepo.git"), "testrepo", &clone_opts)); |
| 223 | + cl_git_pass(git_submodule_add_finalize(sm)); |
| 224 | + git_repository_free(sm_repo); |
| 225 | + git_submodule_free(sm); |
| 226 | + |
| 227 | + cl_git_pass(git_repository_index(&index, g_repo)); |
| 228 | + cl_git_pass(git_index_write_tree(&tree_id, index)); |
| 229 | + git_index_free(index); |
| 230 | + |
| 231 | + cl_git_pass(git_signature_now(&sig, "Submoduler", "submoduler@local")); |
| 232 | + cl_git_pass(git_commit_create_from_ids(&commit_id, g_repo, "HEAD", sig, sig, NULL, "A submodule\n", |
| 233 | + &tree_id, 0, NULL)); |
| 234 | + |
| 235 | + git_signature_free(sig); |
| 236 | + |
| 237 | + assert_submodule_exists(g_repo, "testrepo"); |
| 238 | +} |
| 239 | + |
| 240 | +void test_submodule_add__submodule_clone(void) |
| 241 | +{ |
| 242 | + git_index *index; |
| 243 | + git_oid tree_id, commit_id; |
| 244 | + git_submodule *sm; |
| 245 | + git_signature *sig; |
| 246 | + |
| 247 | + cl_git_pass(git_repository_init(&g_repo, "willaddsubmodule-add", false)); |
| 248 | + |
| 249 | + /* Create the submodule structure, clone into it and finalize */ |
| 250 | + cl_git_pass(git_submodule_add_setup(&sm, g_repo, cl_fixture("testrepo.git"), "testrepo-add", true)); |
| 251 | + cl_git_pass(git_submodule_clone(NULL, sm, NULL)); |
| 252 | + cl_git_pass(git_submodule_add_finalize(sm)); |
| 253 | + git_submodule_free(sm); |
| 254 | + |
| 255 | + cl_git_pass(git_repository_index(&index, g_repo)); |
| 256 | + cl_git_pass(git_index_write_tree(&tree_id, index)); |
| 257 | + git_index_free(index); |
| 258 | + |
| 259 | + cl_git_pass(git_signature_now(&sig, "Submoduler", "submoduler@local")); |
| 260 | + cl_git_pass(git_commit_create_from_ids(&commit_id, g_repo, "HEAD", sig, sig, NULL, "A submodule\n", |
| 261 | + &tree_id, 0, NULL)); |
| 262 | + |
| 263 | + git_signature_free(sig); |
| 264 | + |
| 265 | + assert_submodule_exists(g_repo, "testrepo-add"); |
| 266 | +} |
0 commit comments