diff options
| author | Russell Belfer <rb@github.com> | 2012-10-25 10:55:03 -0700 |
|---|---|---|
| committer | Russell Belfer <rb@github.com> | 2012-10-25 10:55:03 -0700 |
| commit | 1b9346897b3be1c8f659e12a6ee84c74858fc32e (patch) | |
| tree | 16b415e58f1343d45111376eae4ecd7ebc77e3d3 /src/config.c | |
| parent | 505f37b41a7d80c539a33d28aec947cd87bb1b6c (diff) | |
| parent | fcccf3045f6fbeae5139af7263c2ab986818f154 (diff) | |
| download | libgit2-1b9346897b3be1c8f659e12a6ee84c74858fc32e.tar.gz | |
Merge pull request #925 from nulltoken/topic/moving-branch-updates-config
Updates config upon moving and deletion of branches
Diffstat (limited to 'src/config.c')
| -rw-r--r-- | src/config.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/config.c b/src/config.c index c85695f19..937510d7e 100644 --- a/src/config.c +++ b/src/config.c @@ -720,3 +720,81 @@ fail_parse: giterr_set(GITERR_CONFIG, "Failed to parse '%s' as a 32-bit integer", value); return -1; } + +struct rename_data +{ + git_config *config; + const char *old_name; + const char *new_name; +}; + +static int rename_config_entries_cb( + const git_config_entry *entry, + void *payload) +{ + struct rename_data *data = (struct rename_data *)payload; + + if (data->new_name != NULL) { + git_buf name = GIT_BUF_INIT; + int error; + + if (git_buf_printf( + &name, + "%s.%s", + data->new_name, + entry->name + strlen(data->old_name) + 1) < 0) + return -1; + + error = git_config_set_string( + data->config, + git_buf_cstr(&name), + entry->value); + + git_buf_free(&name); + + if (error) + return error; + } + + return git_config_delete(data->config, entry->name); +} + +int git_config_rename_section( + git_repository *repo, + const char *old_section_name, + const char *new_section_name) +{ + git_config *config; + git_buf pattern = GIT_BUF_INIT; + int error = -1; + struct rename_data data; + + git_buf_puts_escape_regex(&pattern, old_section_name); + git_buf_puts(&pattern, "\\..+"); + if (git_buf_oom(&pattern)) + goto cleanup; + + if (git_repository_config__weakptr(&config, repo) < 0) + goto cleanup; + + data.config = config; + data.old_name = old_section_name; + data.new_name = new_section_name; + + if ((error = git_config_foreach_match( + config, + git_buf_cstr(&pattern), + rename_config_entries_cb, &data)) < 0) { + giterr_set(GITERR_CONFIG, + "Cannot rename config section '%s' to '%s'", + old_section_name, + new_section_name); + goto cleanup; + } + + error = 0; + +cleanup: + git_buf_free(&pattern); + return error; +} |
