summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authornulltoken <emeric.fermas@gmail.com>2013-11-15 15:36:37 +0000
committerCarlos Martín Nieto <cmn@dwim.me>2014-04-30 11:45:49 +0200
commit40e48ea40f5dfe0fbf786efc89d4cf297f4525e1 (patch)
tree616c1173448659d160f8b46a736eafb3db9a7333 /src
parent48ebea662a33a3b918143c014dde88e58e6d0a75 (diff)
downloadlibgit2-40e48ea40f5dfe0fbf786efc89d4cf297f4525e1.tar.gz
remote: Introduce git_remote_delete()
Diffstat (limited to 'src')
-rw-r--r--src/remote.c117
1 files changed, 114 insertions, 3 deletions
diff --git a/src/remote.c b/src/remote.c
index ea638e37..8bd52e7f 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -1303,13 +1303,14 @@ static int rename_remote_config_section(
if (git_buf_printf(&old_section_name, "remote.%s", old_name) < 0)
goto cleanup;
- if (git_buf_printf(&new_section_name, "remote.%s", new_name) < 0)
- goto cleanup;
+ if (new_name &&
+ (git_buf_printf(&new_section_name, "remote.%s", new_name) < 0))
+ goto cleanup;
error = git_config_rename_section(
repo,
git_buf_cstr(&old_section_name),
- git_buf_cstr(&new_section_name));
+ new_name ? git_buf_cstr(&new_section_name) : NULL);
cleanup:
git_buf_free(&old_section_name);
@@ -1747,3 +1748,113 @@ int git_remote_init_callbacks(git_remote_callbacks* opts, int version)
return 0;
}
}
+
+struct branch_removal_data {
+ git_vector branches;
+ const char *name;
+};
+
+static int retrieve_branches_cb(
+ const git_config_entry *entry,
+ void *payload)
+{
+ int error;
+ struct branch_removal_data *data = (struct branch_removal_data *)payload;
+
+ if (strcmp(data->name, entry->value))
+ return 0;
+
+ error = git_vector_insert(
+ &data->branches,
+ git__strndup(
+ entry->name + strlen("branch."),
+ strlen(entry->name) - strlen("branch.") - strlen(".remote")));
+
+ return error;
+}
+
+static int delete_branch_remote_config_entry(
+ git_config *config,
+ const char *branch_name)
+{
+ int error;
+
+ git_buf config_entry = GIT_BUF_INIT;
+
+ if (git_buf_printf(&config_entry, "branch.%s.%s", branch_name, "remote") < 0)
+ return -1;
+
+ if ((error = git_config_delete_entry(config, git_buf_cstr(&config_entry))) < 0)
+ goto cleanup;
+
+ git_buf_clear(&config_entry);
+
+ if (git_buf_printf(&config_entry, "branch.%s.%s", branch_name, "merge") < 0)
+ return -1;
+
+ error = git_config_delete_entry(config, git_buf_cstr(&config_entry));
+
+cleanup:
+ git_buf_free(&config_entry);
+
+ return error;
+}
+
+static int remove_branch_config_related_entries(
+ git_repository *repo,
+ const char *remote_name)
+{
+ int error;
+ git_config *config;
+ size_t i;
+ char *branch_name;
+ struct branch_removal_data data;
+
+ if ((error = git_repository_config__weakptr(&config, repo)) < 0)
+ return error;
+
+ if ((error = git_vector_init(&data.branches, 4, git__strcmp_cb)) < 0)
+ return error;
+
+ data.name = remote_name;
+
+ error = git_config_foreach_match(
+ config, "branch\\..+\\.remote", retrieve_branches_cb, &data);
+
+ git_vector_foreach(&data.branches, i, branch_name) {
+ if (!error)
+ error = delete_branch_remote_config_entry(config, branch_name);
+
+ git__free(branch_name);
+ }
+
+ git_vector_free(&data.branches);
+ return error;
+}
+
+int git_remote_delete(git_remote *remote)
+{
+ int error;
+ git_repository *repo;
+
+ assert(remote);
+
+ if (!remote->name) {
+ giterr_set(GITERR_INVALID, "Can't delete an anonymous remote.");
+ return -1;
+ }
+
+ 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;
+
+ git_remote_free(remote);
+
+ return 0;
+}