submodule: make `_set_update()` affect the configuration Moving on with the removal of runtime-changing variables, the update setting for a remote is whatever it was when it was looked up.
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
diff --git a/include/git2/submodule.h b/include/git2/submodule.h
index 31218cc..d36884a 100644
--- a/include/git2/submodule.h
+++ b/include/git2/submodule.h
@@ -490,23 +490,18 @@ GIT_EXTERN(git_submodule_update_t) git_submodule_update_strategy(
git_submodule *submodule);
/**
- * Set the update rule for the submodule.
+ * Set the update rule for the submodule in the configuration
*
- * The initial value comes from the ".git/config" setting of
- * `submodule.$name.update` for this submodule (which is initialized from
- * the ".gitmodules" file). Using this function sets the update rule in
- * memory for the submodule. Call `git_submodule_save()` to write out the
- * new update rule.
+ * This setting won't affect any existing instances.
*
- * Calling this again with GIT_SUBMODULE_UPDATE_RESET or calling
- * `git_submodule_reload()` will revert the rule to the on disk value.
- *
- * @param submodule The submodule to update
+ * @param repo the repository to affect
+ * @param name the name of the submodule to configure
* @param update The new value to use
- * @return old value for update
+ * @return 0 or an error code
*/
-GIT_EXTERN(git_submodule_update_t) git_submodule_set_update(
- git_submodule *submodule,
+GIT_EXTERN(int) git_submodule_set_update(
+ git_repository *repo,
+ const char *name,
git_submodule_update_t update);
/**
diff --git a/src/submodule.c b/src/submodule.c
index f777085..6d2d095 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -806,12 +806,6 @@ int git_submodule_save(git_submodule *submodule)
if (error < 0)
goto cleanup;
- if (!(error = submodule_config_key_trunc_puts(&key, "update")) &&
- (val = git_submodule_update_to_str(submodule->update)) != NULL)
- error = git_config_file_set_string(mods, key.ptr, val);
- 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);
@@ -958,24 +952,17 @@ git_submodule_ignore_t git_submodule_ignore(git_submodule *submodule)
GIT_SUBMODULE_IGNORE_NONE : submodule->ignore;
}
-int git_submodule_set_ignore(git_repository *repo, const char *name, git_submodule_ignore_t ignore)
+static int write_var(git_repository *repo, const char *name, const char *var, const char *val)
{
git_buf key = GIT_BUF_INIT;
git_config_backend *mods;
- const char *val;
int error;
- val = git_submodule_ignore_to_str(ignore);
- if (!val) {
- giterr_set(GITERR_SUBMODULE, "invalid ignore value");
- return -1;
- }
-
mods = open_gitmodules(repo, GITMODULES_CREATE);
if (!mods)
return -1;
- if ((error = git_buf_printf(&key, "submodule.%s.ignore", name)) < 0)
+ if ((error = git_buf_printf(&key, "submodule.%s.%s", name, var)) < 0)
goto cleanup;
error = git_config_file_set_string(mods, key.ptr, val);
@@ -986,6 +973,22 @@ cleanup:
return error;
}
+int git_submodule_set_ignore(git_repository *repo, const char *name, git_submodule_ignore_t ignore)
+{
+ const char *val;
+ int error;
+
+ val = git_submodule_ignore_to_str(ignore);
+ if (!val) {
+ giterr_set(GITERR_SUBMODULE, "invalid ignore value");
+ return -1;
+ }
+
+ error = write_var(repo, name, "ignore", val);
+
+ return error;
+}
+
git_submodule_update_t git_submodule_update_strategy(git_submodule *submodule)
{
assert(submodule);
@@ -993,19 +996,20 @@ git_submodule_update_t git_submodule_update_strategy(git_submodule *submodule)
GIT_SUBMODULE_UPDATE_CHECKOUT : submodule->update;
}
-git_submodule_update_t git_submodule_set_update(
- git_submodule *submodule, git_submodule_update_t update)
+int git_submodule_set_update(git_repository *repo, const char *name, git_submodule_update_t update)
{
- git_submodule_update_t old;
+ const char *val;
+ int error;
- assert(submodule);
+ val = git_submodule_update_to_str(update);
+ if (!val) {
+ giterr_set(GITERR_SUBMODULE, "invalid update value");
+ return -1;
+ }
- if (update == GIT_SUBMODULE_UPDATE_RESET)
- update = submodule->update_default;
+ error = write_var(repo, name, "update", val);
- old = submodule->update;
- submodule->update = update;
- return old;
+ return error;
}
git_submodule_recurse_t git_submodule_fetch_recurse_submodules(
diff --git a/tests/submodule/modify.c b/tests/submodule/modify.c
index 65f656e..a44e791 100644
--- a/tests/submodule/modify.c
+++ b/tests/submodule/modify.c
@@ -139,11 +139,21 @@ void test_submodule_modify__set_ignore(void)
git_submodule_free(sm);
}
+void test_submodule_modify__set_update(void)
+{
+ git_submodule *sm;
+
+ cl_git_pass(git_submodule_set_update(g_repo, "sm_changed_head", GIT_SUBMODULE_UPDATE_REBASE));
+
+ cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head"));
+ cl_assert_equal_i(GIT_SUBMODULE_UPDATE_REBASE, git_submodule_update_strategy(sm));
+ git_submodule_free(sm);
+}
+
void test_submodule_modify__edit_and_save(void)
{
git_submodule *sm1, *sm2;
char *old_url, *old_branch;
- git_submodule_update_t old_update;
git_repository *r2;
git_submodule_recurse_t old_fetchrecurse;
@@ -155,51 +165,35 @@ 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_update = git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_REBASE);
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_UPDATE_REBASE, git_submodule_update_strategy(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_UPDATE_REBASE,
- git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_RESET));
- 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((int)old_update, (int)git_submodule_update_strategy(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_update(sm1, GIT_SUBMODULE_UPDATE_REBASE);
git_submodule_set_fetch_recurse_submodules(sm1, GIT_SUBMODULE_RECURSE_YES);
/* call save */
cl_git_pass(git_submodule_save(sm1));
- /* attempt to "revert" values */
- git_submodule_set_update(sm1, GIT_SUBMODULE_UPDATE_RESET);
-
- /* but ignore and update should NOT revert because the RESET
- * should now be the newly saved value...
- */
- cl_assert_equal_i(
- (int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update_strategy(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 */
@@ -207,8 +201,6 @@ void test_submodule_modify__edit_and_save(void)
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(
- (int)GIT_SUBMODULE_UPDATE_REBASE, (int)git_submodule_update_strategy(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 */
@@ -223,8 +215,6 @@ void test_submodule_modify__edit_and_save(void)
cl_assert_equal_s(SM_LIBGIT2_URL, git_submodule_url(sm2));
cl_assert_equal_i(
- GIT_SUBMODULE_UPDATE_REBASE, git_submodule_update_strategy(sm2));
- cl_assert_equal_i(
GIT_SUBMODULE_RECURSE_NO, git_submodule_fetch_recurse_submodules(sm2));
/* set fetchRecurseSubmodules on-demand */