Merge pull request #4637 from tiennou/feature/submodule-easy-clone Provide a wrapper for simple submodule clone steps
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
diff --git a/include/git2/submodule.h b/include/git2/submodule.h
index 62f250b..bedd76d 100644
--- a/include/git2/submodule.h
+++ b/include/git2/submodule.h
@@ -263,7 +263,8 @@ GIT_EXTERN(int) git_submodule_foreach(
* from the working directory to the new repo.
*
* To fully emulate "git submodule add" call this function, then open the
- * submodule repo and perform the clone step as needed. Lastly, call
+ * submodule repo and perform the clone step as needed (if you don't need
+ * anything custom see `git_submodule_add_clone()`). Lastly, call
* `git_submodule_add_finalize()` to wrap up adding the new submodule and
* .gitmodules to the index to be ready to commit.
*
@@ -286,6 +287,22 @@ GIT_EXTERN(int) git_submodule_add_setup(
int use_gitlink);
/**
+ * Perform the clone step for a newly created submodule.
+ *
+ * This performs the necessary `git_clone` to setup a newly-created submodule.
+ *
+ * @param out The newly created repository object. Optional.
+ * @param submodule The submodule currently waiting for its clone.
+ * @param opts The options to use.
+ *
+ * @return 0 on success, -1 on other errors (see git_clone).
+ */
+GIT_EXTERN(int) git_submodule_clone(
+ git_repository **out,
+ git_submodule *submodule,
+ const git_submodule_update_options *opts);
+
+/**
* Resolve the setup of a new git submodule.
*
* This should be called on a submodule once you have called add setup
diff --git a/src/clone.c b/src/clone.c
index e0b43f1..e8972b4 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -382,11 +382,12 @@ done:
return is_local;
}
-int git_clone(
+static int git__clone(
git_repository **out,
const char *url,
const char *local_path,
- const git_clone_options *_options)
+ const git_clone_options *_options,
+ int use_existing)
{
int error = 0;
git_repository *repo = NULL;
@@ -403,7 +404,7 @@ int git_clone(
GIT_ERROR_CHECK_VERSION(&options, GIT_CLONE_OPTIONS_VERSION, "git_clone_options");
/* Only clone to a new directory or an empty directory */
- if (git_path_exists(local_path) && !git_path_is_empty_dir(local_path)) {
+ if (git_path_exists(local_path) && !use_existing && !git_path_is_empty_dir(local_path)) {
git_error_set(GIT_ERROR_INVALID,
"'%s' exists and is not an empty directory", local_path);
return GIT_EEXISTS;
@@ -455,6 +456,24 @@ int git_clone(
return error;
}
+int git_clone(
+ git_repository **out,
+ const char *url,
+ const char *local_path,
+ const git_clone_options *_options)
+{
+ return git__clone(out, url, local_path, _options, 0);
+}
+
+int git_clone__submodule(
+ git_repository **out,
+ const char *url,
+ const char *local_path,
+ const git_clone_options *_options)
+{
+ return git__clone(out, url, local_path, _options, 1);
+}
+
int git_clone_options_init(git_clone_options *opts, unsigned int version)
{
GIT_INIT_STRUCTURE_FROM_TEMPLATE(
diff --git a/src/clone.h b/src/clone.h
index 864b590..7d73cab 100644
--- a/src/clone.h
+++ b/src/clone.h
@@ -11,6 +11,10 @@
#include "git2/clone.h"
+extern int git_clone__submodule(git_repository **out,
+ const char *url, const char *local_path,
+ const git_clone_options *_options);
+
extern int git_clone__should_clone_local(const char *url, git_clone_local_t local);
#endif
diff --git a/src/submodule.c b/src/submodule.c
index cf89fc9..d12dbcf 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -23,6 +23,7 @@
#include "path.h"
#include "index.h"
#include "worktree.h"
+#include "clone.h"
#define GIT_MODULES_FILE ".gitmodules"
@@ -815,6 +816,64 @@ done:
return error;
}
+static int clone_return_origin(git_remote **out, git_repository *repo, const char *name, const char *url, void *payload)
+{
+ GIT_UNUSED(url);
+ GIT_UNUSED(payload);
+ return git_remote_lookup(out, repo, name);
+}
+
+static int clone_return_repo(git_repository **out, const char *path, int bare, void *payload)
+{
+ git_submodule *sm = payload;
+
+ GIT_UNUSED(path);
+ GIT_UNUSED(bare);
+ return git_submodule_open(out, sm);
+}
+
+int git_submodule_clone(git_repository **out, git_submodule *submodule, const git_submodule_update_options *given_opts)
+{
+ int error;
+ git_repository *clone;
+ git_buf rel_path = GIT_BUF_INIT;
+ git_submodule_update_options sub_opts = GIT_SUBMODULE_UPDATE_OPTIONS_INIT;
+ git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
+
+ assert(submodule);
+
+ if (given_opts)
+ memcpy(&sub_opts, given_opts, sizeof(sub_opts));
+
+ GIT_ERROR_CHECK_VERSION(&sub_opts, GIT_SUBMODULE_UPDATE_OPTIONS_VERSION, "git_submodule_update_options");
+
+ memcpy(&opts.checkout_opts, &sub_opts.checkout_opts, sizeof(sub_opts.checkout_opts));
+ memcpy(&opts.fetch_opts, &sub_opts.fetch_opts, sizeof(sub_opts.fetch_opts));
+ opts.repository_cb = clone_return_repo;
+ opts.repository_cb_payload = submodule;
+ opts.remote_cb = clone_return_origin;
+ opts.remote_cb_payload = submodule;
+
+ git_buf_puts(&rel_path, git_repository_workdir(git_submodule_owner(submodule)));
+ git_buf_joinpath(&rel_path, git_buf_cstr(&rel_path), git_submodule_path(submodule));
+
+ GIT_ERROR_CHECK_ALLOC_BUF(&rel_path);
+
+ error = git_clone__submodule(&clone, git_submodule_url(submodule), git_buf_cstr(&rel_path), &opts);
+ if (error < 0)
+ goto cleanup;
+
+ if (!out)
+ git_repository_free(clone);
+ else
+ *out = clone;
+
+cleanup:
+ git_buf_dispose(&rel_path);
+
+ return error;
+}
+
int git_submodule_add_finalize(git_submodule *sm)
{
int error;
diff --git a/tests/clone/nonetwork.c b/tests/clone/nonetwork.c
index 2b8081f..7ca4908 100644
--- a/tests/clone/nonetwork.c
+++ b/tests/clone/nonetwork.c
@@ -1,7 +1,6 @@
#include "clar_libgit2.h"
#include "git2/clone.h"
-#include "git2/sys/commit.h"
#include "../submodule/submodule_helpers.h"
#include "remote.h"
#include "futils.h"
@@ -352,56 +351,3 @@ void test_clone_nonetwork__clone_from_empty_sets_upstream(void)
git_repository_free(repo);
cl_fixture_cleanup("./repowithunborn");
}
-
-static int just_return_origin(git_remote **out, git_repository *repo, const char *name, const char *url, void *payload)
-{
- GIT_UNUSED(url); GIT_UNUSED(payload);
-
- return git_remote_lookup(out, repo, name);
-}
-
-static int just_return_repo(git_repository **out, const char *path, int bare, void *payload)
-{
- git_submodule *sm = payload;
-
- GIT_UNUSED(path); GIT_UNUSED(bare);
-
- return git_submodule_open(out, sm);
-}
-
-void test_clone_nonetwork__clone_submodule(void)
-{
- git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT;
- git_index *index;
- git_oid tree_id, commit_id;
- git_submodule *sm;
- git_signature *sig;
- git_repository *sm_repo;
-
- cl_git_pass(git_repository_init(&g_repo, "willaddsubmodule", false));
-
-
- /* Create the submodule structure, clone into it and finalize */
- cl_git_pass(git_submodule_add_setup(&sm, g_repo, cl_fixture("testrepo.git"), "testrepo", true));
-
- clone_opts.repository_cb = just_return_repo;
- clone_opts.repository_cb_payload = sm;
- clone_opts.remote_cb = just_return_origin;
- clone_opts.remote_cb_payload = sm;
- cl_git_pass(git_clone(&sm_repo, cl_fixture("testrepo.git"), "testrepo", &clone_opts));
- cl_git_pass(git_submodule_add_finalize(sm));
- git_repository_free(sm_repo);
- git_submodule_free(sm);
-
- cl_git_pass(git_repository_index(&index, g_repo));
- cl_git_pass(git_index_write_tree(&tree_id, index));
- git_index_free(index);
-
- cl_git_pass(git_signature_now(&sig, "Submoduler", "submoduler@local"));
- cl_git_pass(git_commit_create_from_ids(&commit_id, g_repo, "HEAD", sig, sig, NULL, "A submodule\n",
- &tree_id, 0, NULL));
-
- git_signature_free(sig);
-
- assert_submodule_exists(g_repo, "testrepo");
-}
diff --git a/tests/submodule/add.c b/tests/submodule/add.c
index b251b33..f4d1e3b 100644
--- a/tests/submodule/add.c
+++ b/tests/submodule/add.c
@@ -5,6 +5,7 @@
#include "config/config_helpers.h"
#include "futils.h"
#include "repository.h"
+#include "git2/sys/commit.h"
static git_repository *g_repo = NULL;
static const char *valid_blob_id = "fa49b077972391ad58037050f2a75f74e3671e92";
@@ -183,3 +184,68 @@ void test_submodule_add__file_exists_in_index(void)
git_submodule_free(sm);
git_buf_dispose(&name);
}
+
+void test_submodule_add__submodule_clone(void)
+{
+ git_oid tree_id, commit_id;
+ git_signature *sig;
+ git_submodule *sm;
+ git_index *index;
+
+ g_repo = cl_git_sandbox_init("empty_standard_repo");
+
+ /* Create the submodule structure, clone into it and finalize */
+ cl_git_pass(git_submodule_add_setup(&sm, g_repo, cl_fixture("testrepo.git"), "testrepo-add", true));
+ cl_git_pass(git_submodule_clone(NULL, sm, NULL));
+ cl_git_pass(git_submodule_add_finalize(sm));
+
+ /* Create the submodule commit */
+ cl_git_pass(git_repository_index(&index, g_repo));
+ cl_git_pass(git_index_write_tree(&tree_id, index));
+ cl_git_pass(git_signature_now(&sig, "Submoduler", "submoduler@local"));
+ cl_git_pass(git_commit_create_from_ids(&commit_id, g_repo, "HEAD", sig, sig, NULL, "A submodule\n",
+ &tree_id, 0, NULL));
+
+ assert_submodule_exists(g_repo, "testrepo-add");
+
+ git_signature_free(sig);
+ git_submodule_free(sm);
+ git_index_free(index);
+}
+
+void test_submodule_add__submodule_clone_into_nonempty_dir_succeeds(void)
+{
+ git_submodule *sm;
+
+ g_repo = cl_git_sandbox_init("empty_standard_repo");
+
+ cl_git_pass(p_mkdir("empty_standard_repo/sm", 0777));
+ cl_git_mkfile("empty_standard_repo/sm/foobar", "");
+
+ /* Create the submodule structure, clone into it and finalize */
+ cl_git_pass(git_submodule_add_setup(&sm, g_repo, cl_fixture("testrepo.git"), "sm", true));
+ cl_git_pass(git_submodule_clone(NULL, sm, NULL));
+ cl_git_pass(git_submodule_add_finalize(sm));
+
+ cl_assert(git_path_exists("empty_standard_repo/sm/foobar"));
+
+ assert_submodule_exists(g_repo, "sm");
+
+ git_submodule_free(sm);
+}
+
+void test_submodule_add__submodule_clone_twice_fails(void)
+{
+ git_submodule *sm;
+
+ g_repo = cl_git_sandbox_init("empty_standard_repo");
+
+ /* Create the submodule structure, clone into it and finalize */
+ cl_git_pass(git_submodule_add_setup(&sm, g_repo, cl_fixture("testrepo.git"), "sm", true));
+ cl_git_pass(git_submodule_clone(NULL, sm, NULL));
+ cl_git_pass(git_submodule_add_finalize(sm));
+
+ cl_git_fail(git_submodule_clone(NULL, sm, NULL));
+
+ git_submodule_free(sm);
+}