Commit ec8a949a58864272860a4838c6b3d862beda7076

Carlos Martín Nieto 2014-04-30T09:20:03

remote: remove remote-tracking branches on delete When we delete a remote, we also need to go through its fetch refspecs and remove the references they create locally.

diff --git a/src/remote.c b/src/remote.c
index ca1099a..f553753 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -1813,6 +1813,53 @@ static int remove_branch_config_related_entries(
 	return error;
 }
 
+static int remove_refs(git_repository *repo, const char *glob)
+{
+	git_reference_iterator *iter;
+	const char *name;
+	int error;
+
+	if ((error = git_reference_iterator_glob_new(&iter, repo, glob)) < 0)
+		return error;
+
+	while ((error = git_reference_next_name(&name, iter)) == 0) {
+		if ((error = git_reference_remove(repo, name)) < 0)
+			break;
+	}
+	git_reference_iterator_free(iter);
+
+	if (error == GIT_ITEROVER)
+		error = 0;
+
+	return error;
+}
+
+static int remove_remote_tracking(git_repository *repo, const char *remote_name)
+{
+	git_remote *remote;
+	int error;
+	size_t i, count;
+
+	/* we want to use what's on the config, regardless of changes to the instance in memory */
+	if ((error = git_remote_load(&remote, repo, remote_name)) < 0)
+		return error;
+
+	count = git_remote_refspec_count(remote);
+	for (i = 0; i < count; i++) {
+		const git_refspec *refspec = git_remote_get_refspec(remote, i);
+
+		/* shouldn't ever actually happen */
+		if (refspec == NULL)
+			continue;
+
+		if ((error = remove_refs(repo, git_refspec_dst(refspec))) < 0)
+			break;
+	}
+
+	git_remote_free(remote);
+	return error;
+}
+
 int git_remote_delete(git_remote *remote)
 {
 	int error;
@@ -1827,14 +1874,17 @@ int git_remote_delete(git_remote *remote)
 
 	repo = git_remote_owner(remote);
 
-	if ((error = rename_remote_config_section(
-		repo, git_remote_name(remote), NULL)) < 0)
-		return error;
-
 	if ((error = remove_branch_config_related_entries(repo,
 		git_remote_name(remote))) < 0)
 		return error;
 
+	if ((error = remove_remote_tracking(repo, git_remote_name(remote))) < 0)
+		return error;
+
+	if ((error = rename_remote_config_section(
+		repo, git_remote_name(remote), NULL)) < 0)
+		return error;
+
 	git_remote_free(remote);
 
 	return 0;
diff --git a/tests/network/remote/delete.c b/tests/network/remote/delete.c
index 5bf944c..db55b07 100644
--- a/tests/network/remote/delete.c
+++ b/tests/network/remote/delete.c
@@ -27,14 +27,18 @@ void test_network_remote_delete__cannot_delete_an_anonymous_remote(void)
 	cl_git_fail(git_remote_delete(remote));
 
 	git_remote_free(remote);
+	git_remote_free(_remote);
 }
 
-void test_network_remote_delete__deleting_a_remote_removes_the_remote_tracking_references(void)
+void test_network_remote_delete__remove_remote_tracking_branches(void)
 {
-	cl_assert(false);
+	git_reference *ref;
+
+	cl_git_pass(git_remote_delete(_remote));
+	cl_git_fail_with(GIT_ENOTFOUND, git_reference_lookup(&ref, _repo, "refs/remotes/test/master"));
 }
 
-void test_network_remote_delete__deleting_a_remote_removes_the_remote_configuration_settings(void)
+void test_network_remote_delete__remove_remote_configuration_settings(void)
 {
 	cl_assert(count_config_entries_match(_repo, "remote\\.test\\.+") > 0);
 
@@ -43,7 +47,7 @@ void test_network_remote_delete__deleting_a_remote_removes_the_remote_configurat
 	cl_assert_equal_i(0, count_config_entries_match(_repo, "remote\\.test\\.+"));
 }
 
-void test_network_remote_delete__deleting_a_remote_removes_the_branch_remote_configuration_settings(void)
+void test_network_remote_delete__remove_branch_upstream_configuration_settings(void)
 {
 	assert_config_entry_existence(_repo, "branch.mergeless.remote", true);
 	assert_config_entry_existence(_repo, "branch.master.remote", true);