Commit eedeeb9e8f708e9f60568adc4a63307754a603f5

Russell Belfer 2014-04-03T11:58:51

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.

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);