Test (and fix) the git_submodule_sync changes I wrote this stuff a while ago and forgot to write tests. Wanted to do so now to wrap up the PR and immediately found problems.
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
diff --git a/src/submodule.c b/src/submodule.c
index e49f096..bea096d 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -849,12 +849,21 @@ int git_submodule_sync(git_submodule *sm)
(sm->flags & GIT_SUBMODULE_STATUS_IN_WD) != 0 &&
!(error = git_submodule_open(&smrepo, sm)))
{
- if ((error = lookup_head_remote_key(&key, smrepo)) < 0) {
+ git_buf remote_name = GIT_BUF_INIT;
+
+ if ((error = git_repository_config__weakptr(&cfg, smrepo)) < 0)
+ /* return error from reading submodule config */;
+ else if ((error = lookup_head_remote_key(&remote_name, smrepo)) < 0) {
giterr_clear();
- git_buf_sets(&key, "branch.origin.remote");
+ error = git_buf_sets(&key, "branch.origin.remote");
+ } else {
+ error = git_buf_join3(
+ &key, '.', "branch", remote_name.ptr, "remote");
+ git_buf_free(&remote_name);
}
- error = git_config__update_entry(cfg, key.ptr, sm->url, true, true);
+ if (!error)
+ error = git_config__update_entry(cfg, key.ptr, sm->url, true, false);
git_repository_free(smrepo);
}
diff --git a/tests/submodule/modify.c b/tests/submodule/modify.c
index 7e76f35..582d416 100644
--- a/tests/submodule/modify.c
+++ b/tests/submodule/modify.c
@@ -69,6 +69,26 @@ static int sync_one_submodule(
return git_submodule_sync(sm);
}
+static void assert_submodule_url_is_synced(
+ git_submodule *sm, const char *parent_key, const char *child_key)
+{
+ git_config *cfg;
+ const char *str;
+ git_repository *smrepo;
+
+ cl_git_pass(git_repository_config(&cfg, g_repo));
+ cl_git_pass(git_config_get_string(&str, cfg, parent_key));
+ cl_assert_equal_s(git_submodule_url(sm), str);
+ git_config_free(cfg);
+
+ cl_git_pass(git_submodule_open(&smrepo, sm));
+ cl_git_pass(git_repository_config(&cfg, smrepo));
+ cl_git_pass(git_config_get_string(&str, cfg, child_key));
+ cl_assert_equal_s(git_submodule_url(sm), str);
+ git_config_free(cfg);
+ git_repository_free(smrepo);
+}
+
void test_submodule_modify__sync(void)
{
git_submodule *sm1, *sm2, *sm3;
@@ -104,14 +124,12 @@ void test_submodule_modify__sync(void)
cl_git_pass(git_submodule_foreach(g_repo, sync_one_submodule, NULL));
/* check that submodule config is updated */
- cl_git_pass(git_repository_config(&cfg, g_repo));
- cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM1".url"));
- cl_assert_equal_s(git_submodule_url(sm1), str);
- cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM2".url"));
- cl_assert_equal_s(git_submodule_url(sm2), str);
- cl_git_pass(git_config_get_string(&str, cfg, "submodule."SM3".url"));
- cl_assert_equal_s(git_submodule_url(sm3), str);
- git_config_free(cfg);
+ assert_submodule_url_is_synced(
+ sm1, "submodule."SM1".url", "branch.origin.remote");
+ assert_submodule_url_is_synced(
+ sm2, "submodule."SM2".url", "branch.origin.remote");
+ assert_submodule_url_is_synced(
+ sm3, "submodule."SM3".url", "branch.origin.remote");
git_submodule_free(sm1);
git_submodule_free(sm2);