submodule: make `_set_update_fetch_recurse_submodules()` affect the config Similarly to the other ones. In this test we copy over testing `RECURSE_YES` which shows an error in our handling of the `YES` variant which we may have to port to the rest.
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
diff --git a/include/git2/submodule.h b/include/git2/submodule.h
index d36884a..6357215 100644
--- a/include/git2/submodule.h
+++ b/include/git2/submodule.h
@@ -519,18 +519,18 @@ GIT_EXTERN(git_submodule_recurse_t) git_submodule_fetch_recurse_submodules(
git_submodule *submodule);
/**
- * Set the fetchRecurseSubmodules rule for a submodule.
+ * Set the fetchRecurseSubmodules rule for a submodule in the configuration
*
- * This sets the submodule.<name>.fetchRecurseSubmodules value for
- * the submodule. You should call `git_submodule_save()` if you want
- * to persist the new value.
+ * This setting won't affect any existing instances.
*
- * @param submodule The submodule to modify
+ * @param repo the repository to affect
+ * @param name the submodule to configure
* @param fetch_recurse_submodules Boolean value
* @return old value for fetchRecurseSubmodules
*/
-GIT_EXTERN(git_submodule_recurse_t) git_submodule_set_fetch_recurse_submodules(
- git_submodule *submodule,
+GIT_EXTERN(int) git_submodule_set_fetch_recurse_submodules(
+ git_repository *repo,
+ const char *name,
git_submodule_recurse_t fetch_recurse_submodules);
/**
diff --git a/src/submodule.c b/src/submodule.c
index 6d2d095..7576ead 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -769,7 +769,6 @@ int git_submodule_save(git_submodule *submodule)
int error = 0;
git_config_backend *mods;
git_buf key = GIT_BUF_INIT;
- const char *val;
assert(submodule);
@@ -806,12 +805,6 @@ int git_submodule_save(git_submodule *submodule)
if (error < 0)
goto cleanup;
- if (!(error = submodule_config_key_trunc_puts(&key, "fetchRecurseSubmodules")) &&
- (val = git_submodule_recurse_to_str(submodule->fetch_recurse)) != NULL)
- error = git_config_file_set_string(mods, key.ptr, val);
- if (error < 0)
- goto cleanup;
-
/* update internal defaults */
submodule->ignore_default = submodule->ignore;
@@ -965,7 +958,11 @@ static int write_var(git_repository *repo, const char *name, const char *var, co
if ((error = git_buf_printf(&key, "submodule.%s.%s", name, var)) < 0)
goto cleanup;
- error = git_config_file_set_string(mods, key.ptr, val);
+ if (val)
+ error = git_config_file_set_string(mods, key.ptr, val);
+ else
+ error = git_config_file_delete(mods, key.ptr);
+
git_buf_free(&key);
cleanup:
@@ -1019,20 +1016,31 @@ git_submodule_recurse_t git_submodule_fetch_recurse_submodules(
return submodule->fetch_recurse;
}
-git_submodule_recurse_t git_submodule_set_fetch_recurse_submodules(
- git_submodule *submodule,
- git_submodule_recurse_t fetch_recurse_submodules)
+int git_submodule_set_fetch_recurse_submodules(git_repository *repo, const char *name, git_submodule_recurse_t recurse)
{
- git_submodule_recurse_t old;
+ const char *val;
+ int error;
- assert(submodule);
+ assert(repo && name);
+
+ val = git_submodule_recurse_to_str(recurse);
+ if (!val) {
+ switch (recurse) {
+ case GIT_SUBMODULE_RECURSE_YES:
+ val = "true";
+ break;
+ case GIT_SUBMODULE_RECURSE_NO:
+ val = NULL;
+ break;
+ default:
+ giterr_set(GITERR_SUBMODULE, "invalid recurse value");
+ return -1;
+ }
+ }
- if (fetch_recurse_submodules == GIT_SUBMODULE_RECURSE_RESET)
- fetch_recurse_submodules = submodule->fetch_recurse_default;
+ error = write_var(repo, name, "fetchRecurseSubmodules", val);
- old = submodule->fetch_recurse;
- submodule->fetch_recurse = fetch_recurse_submodules;
- return old;
+ return error;
}
static int submodule_repo_create(
diff --git a/tests/submodule/modify.c b/tests/submodule/modify.c
index a44e791..e261147 100644
--- a/tests/submodule/modify.c
+++ b/tests/submodule/modify.c
@@ -150,12 +150,27 @@ void test_submodule_modify__set_update(void)
git_submodule_free(sm);
}
+void test_submodule_modify__set_fetch_recurse_submodules(void)
+{
+ git_submodule *sm;
+
+ cl_git_pass(git_submodule_set_fetch_recurse_submodules(g_repo, "sm_changed_head", GIT_SUBMODULE_RECURSE_YES));
+
+ cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+ cl_assert_equal_i(GIT_SUBMODULE_RECURSE_YES, git_submodule_fetch_recurse_submodules(sm));
+ git_submodule_free(sm);
+
+ git_submodule_set_fetch_recurse_submodules(g_repo, "sm_changed_head", GIT_SUBMODULE_RECURSE_ONDEMAND);
+ cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+ cl_assert_equal_i(GIT_SUBMODULE_RECURSE_ONDEMAND, git_submodule_fetch_recurse_submodules(sm));
+ git_submodule_free(sm);
+}
+
void test_submodule_modify__edit_and_save(void)
{
git_submodule *sm1, *sm2;
char *old_url, *old_branch;
git_repository *r2;
- git_submodule_recurse_t old_fetchrecurse;
cl_git_pass(git_submodule_lookup(&sm1, g_repo, "sm_changed_head"));
@@ -165,43 +180,29 @@ void test_submodule_modify__edit_and_save(void)
/* modify properties of submodule */
cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL));
cl_git_pass(git_submodule_set_branch(sm1, SM_LIBGIT2_BRANCH));
- old_fetchrecurse = git_submodule_set_fetch_recurse_submodules(
- sm1, GIT_SUBMODULE_RECURSE_YES);
-
cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1));
cl_assert_equal_s(SM_LIBGIT2_BRANCH, git_submodule_branch(sm1));
- cl_assert_equal_i(
- GIT_SUBMODULE_RECURSE_YES, git_submodule_fetch_recurse_submodules(sm1));
/* revert without saving (and confirm setters return old value) */
cl_git_pass(git_submodule_set_url(sm1, old_url));
cl_git_pass(git_submodule_set_branch(sm1, old_branch));
- cl_assert_equal_i(
- GIT_SUBMODULE_RECURSE_YES, git_submodule_set_fetch_recurse_submodules(
- sm1, GIT_SUBMODULE_RECURSE_RESET));
/* check that revert was successful */
cl_assert_equal_s(old_url, git_submodule_url(sm1));
cl_assert_equal_s(old_branch, git_submodule_branch(sm1));
- cl_assert_equal_i(
- old_fetchrecurse, git_submodule_fetch_recurse_submodules(sm1));
/* modify properties of submodule (again) */
cl_git_pass(git_submodule_set_url(sm1, SM_LIBGIT2_URL));
cl_git_pass(git_submodule_set_branch(sm1, SM_LIBGIT2_BRANCH));
- git_submodule_set_fetch_recurse_submodules(sm1, GIT_SUBMODULE_RECURSE_YES);
/* call save */
cl_git_pass(git_submodule_save(sm1));
- cl_assert_equal_i(GIT_SUBMODULE_RECURSE_YES, git_submodule_fetch_recurse_submodules(sm1));
-
/* call reload and check that the new values are loaded */
cl_git_pass(git_submodule_reload(sm1, 0));
cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm1));
cl_assert_equal_s(SM_LIBGIT2_BRANCH, git_submodule_branch(sm1));
- cl_assert_equal_i(GIT_SUBMODULE_RECURSE_YES, git_submodule_fetch_recurse_submodules(sm1));
/* unset branch again and verify that the property is deleted in config */
cl_git_pass(git_submodule_set_branch(sm1, NULL));
@@ -214,19 +215,6 @@ void test_submodule_modify__edit_and_save(void)
cl_git_pass(git_submodule_lookup(&sm2, r2, "sm_changed_head"));
cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm2));
- cl_assert_equal_i(
- GIT_SUBMODULE_RECURSE_NO, git_submodule_fetch_recurse_submodules(sm2));
-
- /* set fetchRecurseSubmodules on-demand */
- cl_git_pass(git_submodule_reload(sm1, 0));
- git_submodule_set_fetch_recurse_submodules(sm1, GIT_SUBMODULE_RECURSE_ONDEMAND);
- cl_assert_equal_i(
- GIT_SUBMODULE_RECURSE_ONDEMAND, git_submodule_fetch_recurse_submodules(sm1));
- /* call save */
- cl_git_pass(git_submodule_save(sm1));
- cl_git_pass(git_submodule_reload(sm1, 0));
- cl_assert_equal_i(
- GIT_SUBMODULE_RECURSE_ONDEMAND, git_submodule_fetch_recurse_submodules(sm1));
git_submodule_free(sm1);
git_submodule_free(sm2);