summaryrefslogtreecommitdiff
path: root/config.c
diff options
context:
space:
mode:
authorJohannes Schindelin <Johannes.Schindelin@gmx.de>2006-12-16 15:14:14 +0100
committerJunio C Hamano <junkio@cox.net>2006-12-16 13:28:20 -0800
commit0667fcfb6271e9e1ea032a0e3a7d74c9d6a5fa8a (patch)
tree56b2353219b7d37819c93714618caaa344635904 /config.c
parentb1bfcae438adb485bb66e2f59396373809e346e6 (diff)
downloadgit-0667fcfb6271e9e1ea032a0e3a7d74c9d6a5fa8a.tar.gz
add a function to rename sections in the config
Given a config like this: # A config [very.interesting.section] not The command $ git repo-config --rename-section very.interesting.section bla.1 will lead to this config: # A config [bla "1"] not Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
Diffstat (limited to 'config.c')
-rw-r--r--config.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/config.c b/config.c
index 1bdef44a3a..663993fefa 100644
--- a/config.c
+++ b/config.c
@@ -746,4 +746,68 @@ out_free:
return ret;
}
+int git_config_rename_section(const char *old_name, const char *new_name)
+{
+ int ret = 0;
+ const char *config_filename;
+ struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
+ int out_fd;
+ char buf[1024];
+
+ config_filename = getenv("GIT_CONFIG");
+ if (!config_filename) {
+ config_filename = getenv("GIT_CONFIG_LOCAL");
+ if (!config_filename)
+ config_filename = git_path("config");
+ }
+ config_filename = xstrdup(config_filename);
+ out_fd = hold_lock_file_for_update(lock, config_filename, 0);
+ if (out_fd < 0)
+ return error("Could not lock config file!");
+
+ if (!(config_file = fopen(config_filename, "rb")))
+ return error("Could not open config file!");
+
+ while (fgets(buf, sizeof(buf), config_file)) {
+ int i;
+ for (i = 0; buf[i] && isspace(buf[i]); i++)
+ ; /* do nothing */
+ if (buf[i] == '[') {
+ /* it's a section */
+ int j = 0, dot = 0;
+ for (i++; buf[i] && buf[i] != ']'; i++) {
+ if (!dot && isspace(buf[i])) {
+ dot = 1;
+ if (old_name[j++] != '.')
+ break;
+ for (i++; isspace(buf[i]); i++)
+ ; /* do nothing */
+ if (buf[i] != '"')
+ break;
+ continue;
+ }
+ if (buf[i] == '\\' && dot)
+ i++;
+ else if (buf[i] == '"' && dot) {
+ for (i++; isspace(buf[i]); i++)
+ ; /* do_nothing */
+ break;
+ }
+ if (buf[i] != old_name[j++])
+ break;
+ }
+ if (buf[i] == ']') {
+ /* old_name matches */
+ ret++;
+ store.baselen = strlen(new_name);
+ store_write_section(out_fd, new_name);
+ continue;
+ }
+ }
+ write(out_fd, buf, strlen(buf));
+ }
+ if (close(out_fd) || commit_lock_file(lock) < 0)
+ return error("Cannot commit config file!");
+ return ret;
+}