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.
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
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);