Commit 413d55638483678357ebcb8c26911cf944be95cc

Sascha Cunz 2012-07-25T02:10:35

Remotes: Save a cleaned pushurl (by deleting it from the config)

diff --git a/src/remote.c b/src/remote.c
index cdd593c..bee1ab6 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -207,15 +207,24 @@ int git_remote_save(const git_remote *remote)
 		return -1;
 	}
 
-	if (remote->pushurl) {
-		git_buf_clear(&buf);
-		if (git_buf_printf(&buf, "remote.%s.pushurl", remote->name) < 0)
-			return -1;
+	git_buf_clear(&buf);
+	if (git_buf_printf(&buf, "remote.%s.pushurl", remote->name) < 0)
+		return -1;
 
+	if (remote->pushurl) {
 		if (git_config_set_string(config, git_buf_cstr(&buf), remote->pushurl) < 0) {
 			git_buf_free(&buf);
 			return -1;
 		}
+	} else {
+		int error = git_config_delete(config, git_buf_cstr(&buf));
+		if (error == GIT_ENOTFOUND) {
+			error = 0;
+		}
+		if (error < 0) {
+			git_buf_free(&buf);
+			return -1;
+		}
 	}
 
 	if (remote->fetch.src != NULL && remote->fetch.dst != NULL) {
diff --git a/tests-clar/network/remotes.c b/tests-clar/network/remotes.c
index 61b29b8..3d989c1 100644
--- a/tests-clar/network/remotes.c
+++ b/tests-clar/network/remotes.c
@@ -120,6 +120,15 @@ void test_network_remotes__save(void)
 
 	cl_assert_equal_s(git_remote_url(_remote), "git://github.com/libgit2/libgit2");
 	cl_assert_equal_s(git_remote_pushurl(_remote), "git://github.com/libgit2/libgit2_push");
+
+	/* remove the pushurl again and see if we can save that too */
+	cl_git_pass(git_remote_set_pushurl(_remote, NULL));
+	cl_git_pass(git_remote_save(_remote));
+	git_remote_free(_remote);
+	_remote = NULL;
+
+	cl_git_pass(git_remote_load(&_remote, _repo, "upstream"));
+	cl_assert(git_remote_pushurl(_remote) == NULL);
 }
 
 void test_network_remotes__fnmatch(void)