Commit 24393ea6d349c49c2c36eae7d51a367ef0e1b3df

Ben Straub 2012-12-13T09:14:56

Stop premature remote freeing when cloning

diff --git a/examples/network/clone.c b/examples/network/clone.c
index 52146a2..f8a5ec4 100644
--- a/examples/network/clone.c
+++ b/examples/network/clone.c
@@ -82,6 +82,7 @@ int do_clone(git_repository *repo, int argc, char **argv)
 
 	// Do the clone
 	error = git_clone(&cloned_repo, origin, path, &checkout_opts, &fetch_progress, &pd);
+	git_remote_free(origin);
 	printf("\n");
 	if (error != 0) {
 		const git_error *err = giterr_last();
diff --git a/src/clone.c b/src/clone.c
index 55638fd..aa6c43f 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -289,7 +289,6 @@ static int setup_remotes_and_fetch(
 			}
 			git_remote_disconnect(origin);
 		}
-		git_remote_free(origin);
 	}
 
 	return retcode;
diff --git a/tests-clar/clone/network.c b/tests-clar/clone/network.c
index 018f464..15ad95b 100644
--- a/tests-clar/clone/network.c
+++ b/tests-clar/clone/network.c
@@ -17,6 +17,12 @@ void test_clone_network__initialize(void)
 	cl_git_pass(git_remote_new(&g_origin, NULL, "origin", LIVE_REPO_URL, GIT_REMOTE_DEFAULT_FETCH));
 }
 
+void test_clone_network__cleanup(void)
+{
+	git_remote_free(g_origin);
+	g_origin = NULL;
+}
+
 static void cleanup_repository(void *path)
 {
 	if (g_repo) {
diff --git a/tests-clar/clone/nonetwork.c b/tests-clar/clone/nonetwork.c
index af12981..373b296 100644
--- a/tests-clar/clone/nonetwork.c
+++ b/tests-clar/clone/nonetwork.c
@@ -14,6 +14,12 @@ void test_clone_nonetwork__initialize(void)
 	cl_git_pass(git_remote_new(&g_origin, NULL, "origin", cl_git_fixture_url("testrepo.git"), GIT_REMOTE_DEFAULT_FETCH));
 }
 
+void test_clone_nonetwork__cleanup(void)
+{
+	git_remote_free(g_origin);
+	g_origin = NULL;
+}
+
 static void cleanup_repository(void *path)
 {
 	if (g_repo) {
@@ -56,7 +62,6 @@ void test_clone_nonetwork__fail_when_the_target_is_a_file(void)
 
 	cl_git_mkfile("./foo", "Bar!");
 	cl_git_fail(git_clone(&g_repo, g_origin, "./foo", NULL, NULL, NULL));
-	git_remote_free(g_origin);
 }
 
 void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(void)
@@ -66,5 +71,4 @@ void test_clone_nonetwork__fail_with_already_existing_but_non_empty_directory(vo
 	p_mkdir("./foo", GIT_DIR_MODE);
 	cl_git_mkfile("./foo/bar", "Baz!");
 	cl_git_fail(git_clone(&g_repo, g_origin, "./foo", NULL, NULL, NULL));
-	git_remote_free(g_origin);
 }