{"thread":{"id":"43616","subject":"[PATCH 1/2] add a function to rename sections in the config","startedAt":"2006-12-16T14:14:14Z","lastAt":"2006-12-16T14:14:14Z","messageCount":1,"participants":["Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"296002","messageId":"Pine.LNX.4.63.0612161513480.3635@wbgn013.biozentrum.uni-wuerzburg.de","threadId":"43616","inReplyTo":null,"subject":"[PATCH 1/2] add a function to rename sections in the config","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2006-12-16T14:14:14Z","receivedAt":"2006-12-16T14:14:14Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"\nGiven a config like this:\n\n\t# A config\n\t[very.interesting.section]\n\t\tnot\n\nThe command\n\n\t$ git repo-config --rename-section very.interesting.section bla.1\n\nwill lead to this config:\n\n\t# A config\n\t[bla \"1\"]\n\t\tnot\n\nSigned-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>\n---\n builtin-repo-config.c  |   14 +++++++++-\n cache.h                |    1 +\n config.c               |   64 ++++++++++++++++++++++++++++++++++++++++++++++++\n t/t1300-repo-config.sh |   48 ++++++++++++++++++++++++++++++++++++\n 4 files changed, 126 insertions(+), 1 deletions(-)\n\ndiff --git a/builtin-repo-config.c b/builtin-repo-config.c\nindex 7b6e572..a6b99ed 100644\n--- a/builtin-repo-config.c\n+++ b/builtin-repo-config.c\n@@ -3,7 +3,7 @@\n #include <regex.h>\n \n static const char git_config_set_usage[] =\n-\"git-repo-config [ --global ] [ --bool | --int ] [--get | --get-all | --get-regexp | --replace-all | --unset | --unset-all] name [value [value_regex]] | --list\";\n+\"git-repo-config [ --global ] [ --bool | --int ] [--get | --get-all | --get-regexp | --replace-all | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --list\";\n \n static char *key;\n static regex_t *key_regexp;\n@@ -148,6 +148,18 @@ int cmd_repo_config(int argc, const char **argv, const char *prefix)\n \t\t\t} else {\n \t\t\t\tdie(\"$HOME not set\");\n \t\t\t}\n+\t\t} else if (!strcmp(argv[1], \"--rename-section\")) {\n+\t\t\tint ret;\n+\t\t\tif (argc != 4)\n+\t\t\t\tusage(git_config_set_usage);\n+\t\t\tret = git_config_rename_section(argv[2], argv[3]);\n+\t\t\tif (ret < 0)\n+\t\t\t\treturn ret;\n+\t\t\tif (ret == 0) {\n+\t\t\t\tfprintf(stderr, \"No such section!\\n\");\n+\t\t\t\treturn 1;\n+\t\t\t}\n+\t\t\treturn 0;\n \t\t} else\n \t\t\tbreak;\n \t\targc--;\ndiff --git a/cache.h b/cache.h\nindex 40afee3..a2ffa23 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -403,6 +403,7 @@ extern int git_config_int(const char *, const char *);\n extern int git_config_bool(const char *, const char *);\n extern int git_config_set(const char *, const char *);\n extern int git_config_set_multivar(const char *, const char *, const char *, int);\n+extern int git_config_rename_section(const char *, const char *);\n extern int check_repository_format_version(const char *var, const char *value);\n \n #define MAX_GITNAME (1000)\ndiff --git a/config.c b/config.c\nindex 1bdef44..663993f 100644\n--- a/config.c\n+++ b/config.c\n@@ -746,4 +746,68 @@ out_free:\n \treturn ret;\n }\n \n+int git_config_rename_section(const char *old_name, const char *new_name)\n+{\n+\tint ret = 0;\n+\tconst char *config_filename;\n+\tstruct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);\n+\tint out_fd;\n+\tchar buf[1024];\n+\n+\tconfig_filename = getenv(\"GIT_CONFIG\");\n+\tif (!config_filename) {\n+\t\tconfig_filename = getenv(\"GIT_CONFIG_LOCAL\");\n+\t\tif (!config_filename)\n+\t\t\tconfig_filename  = git_path(\"config\");\n+\t}\n+\tconfig_filename = xstrdup(config_filename);\n+\tout_fd = hold_lock_file_for_update(lock, config_filename, 0);\n+\tif (out_fd < 0)\n+\t\treturn error(\"Could not lock config file!\");\n+\n+\tif (!(config_file = fopen(config_filename, \"rb\")))\n+\t\treturn error(\"Could not open config file!\");\n+\n+\twhile (fgets(buf, sizeof(buf), config_file)) {\n+\t\tint i;\n+\t\tfor (i = 0; buf[i] && isspace(buf[i]); i++)\n+\t\t\t; /* do nothing */\n+\t\tif (buf[i] == '[') {\n+\t\t\t/* it's a section */\n+\t\t\tint j = 0, dot = 0;\n+\t\t\tfor (i++; buf[i] && buf[i] != ']'; i++) {\n+\t\t\t\tif (!dot && isspace(buf[i])) {\n+\t\t\t\t\tdot = 1;\n+\t\t\t\t\tif (old_name[j++] != '.')\n+\t\t\t\t\t\tbreak;\n+\t\t\t\t\tfor (i++; isspace(buf[i]); i++)\n+\t\t\t\t\t\t; /* do nothing */\n+\t\t\t\t\tif (buf[i] != '\"')\n+\t\t\t\t\t\tbreak;\n+\t\t\t\t\tcontinue;\n+\t\t\t\t}\n+\t\t\t\tif (buf[i] == '\\\\' && dot)\n+\t\t\t\t\ti++;\n+\t\t\t\telse if (buf[i] == '\"' && dot) {\n+\t\t\t\t\tfor (i++; isspace(buf[i]); i++)\n+\t\t\t\t\t\t; /* do_nothing */\n+\t\t\t\t\tbreak;\n+\t\t\t\t}\n+\t\t\t\tif (buf[i] != old_name[j++])\n+\t\t\t\t\tbreak;\n+\t\t\t}\n+\t\t\tif (buf[i] == ']') {\n+\t\t\t\t/* old_name matches */\n+\t\t\t\tret++;\n+\t\t\t\tstore.baselen = strlen(new_name);\n+\t\t\t\tstore_write_section(out_fd, new_name);\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t}\n+\t\twrite(out_fd, buf, strlen(buf));\n+\t}\n+\tif (close(out_fd) || commit_lock_file(lock) < 0)\n+\t\treturn error(\"Cannot commit config file!\");\n+\treturn ret;\n+}\n \ndiff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh\nindex 0de2497..b643cad 100755\n--- a/t/t1300-repo-config.sh\n+++ b/t/t1300-repo-config.sh\n@@ -333,5 +333,53 @@ EOF\n \n test_expect_success '--set in alternative GIT_CONFIG' 'cmp other-config expect'\n \n+cat > .git/config << EOF\n+# Hallo\n+\t#Bello\n+[branch \"eins\"]\n+\tx = 1\n+[branch.eins]\n+\ty = 1\n+\t[branch \"1 234 blabl/a\"]\n+weird\n+EOF\n+\n+test_expect_success \"rename section\" \\\n+\t\"git-repo-config --rename-section branch.eins branch.zwei\"\n+\n+cat > expect << EOF\n+# Hallo\n+\t#Bello\n+[branch \"zwei\"]\n+\tx = 1\n+[branch \"zwei\"]\n+\ty = 1\n+\t[branch \"1 234 blabl/a\"]\n+weird\n+EOF\n+\n+test_expect_success \"rename succeeded\" \"diff -u expect .git/config\"\n+\n+test_expect_failure \"rename non-existing section\" \\\n+\t'git-repo-config --rename-section branch.\"world domination\" branch.drei'\n+\n+test_expect_success \"rename succeeded\" \"diff -u expect .git/config\"\n+\n+test_expect_success \"rename another section\" \\\n+\t'git-repo-config --rename-section branch.\"1 234 blabl/a\" branch.drei'\n+\n+cat > expect << EOF\n+# Hallo\n+\t#Bello\n+[branch \"zwei\"]\n+\tx = 1\n+[branch \"zwei\"]\n+\ty = 1\n+[branch \"drei\"]\n+weird\n+EOF\n+\n+test_expect_success \"rename succeeded\" \"diff -u expect .git/config\"\n+\n test_done\n \n-- \n1.4.4.1.g0604-dirty\n"}]}