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>
Johannes Schindelin committed
Dec 16, 2006 at 15:14 UTC
0667fcfb6271e9e1ea032a0e3a7d74c9d6a5fa8a
4 files changed
+126
-1
builtin-repo-config.c
+13
-1
@@ -3,7 +3,7 @@
3
#include <regex.h>
4
5
static const char git_config_set_usage[] =
6
-"git-repo-config [ --global ] [ --bool | --int ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --list";
6
+"git-repo-config [ --global ] [ --bool | --int ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --list";
7
8
static char *key;
9
static regex_t *key_regexp;
@@ -148,6 +148,18 @@ int cmd_repo_config(int argc, const char **argv, const char *prefix)
148
} else {
149
die("$HOME not set");
150
}
151
+ } else if (!strcmp(argv[1], "--rename-section")) {
152
+ int ret;
153
+ if (argc != 4)
154
+ usage(git_config_set_usage);
155
+ ret = git_config_rename_section(argv[2], argv[3]);
156
+ if (ret < 0)
157
+ return ret;
158
+ if (ret == 0) {
159
+ fprintf(stderr, "No such section!\n");
160
+ return 1;
161
+ }
162
+ return 0;
163
} else
164
break;
165
argc--;
cache.h
+1
@@ -404,6 +404,7 @@ extern int git_config_int(const char *, const char *);
404
extern int git_config_bool(const char *, const char *);
405
extern int git_config_set(const char *, const char *);
406
extern int git_config_set_multivar(const char *, const char *, const char *, int);
407
+extern int git_config_rename_section(const char *, const char *);
408
extern int check_repository_format_version(const char *var, const char *value);
409
410
#define MAX_GITNAME (1000)
config.c
+64
@@ -746,4 +746,68 @@ out_free:
746
return ret;
747
}
748
749
+int git_config_rename_section(const char *old_name, const char *new_name)
750
+{
751
+ int ret = 0;
752
+ const char *config_filename;
753
+ struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
754
+ int out_fd;
755
+ char buf[1024];
756
+
757
+ config_filename = getenv("GIT_CONFIG");
758
+ if (!config_filename) {
759
+ config_filename = getenv("GIT_CONFIG_LOCAL");
760
+ if (!config_filename)
761
+ config_filename = git_path("config");
762
+ }
763
+ config_filename = xstrdup(config_filename);
764
+ out_fd = hold_lock_file_for_update(lock, config_filename, 0);
765
+ if (out_fd < 0)
766
+ return error("Could not lock config file!");
767
+
768
+ if (!(config_file = fopen(config_filename, "rb")))
769
+ return error("Could not open config file!");
770
+
771
+ while (fgets(buf, sizeof(buf), config_file)) {
772
+ int i;
773
+ for (i = 0; buf[i] && isspace(buf[i]); i++)
774
+ ; /* do nothing */
775
+ if (buf[i] == '[') {
776
+ /* it's a section */
777
+ int j = 0, dot = 0;
778
+ for (i++; buf[i] && buf[i] != ']'; i++) {
779
+ if (!dot && isspace(buf[i])) {
780
+ dot = 1;
781
+ if (old_name[j++] != '.')
782
+ break;
783
+ for (i++; isspace(buf[i]); i++)
784
+ ; /* do nothing */
785
+ if (buf[i] != '"')
786
+ break;
787
+ continue;
788
+ }
789
+ if (buf[i] == '\\' && dot)
790
+ i++;
791
+ else if (buf[i] == '"' && dot) {
792
+ for (i++; isspace(buf[i]); i++)
793
+ ; /* do_nothing */
794
+ break;
795
+ }
796
+ if (buf[i] != old_name[j++])
797
+ break;
798
+ }
799
+ if (buf[i] == ']') {
800
+ /* old_name matches */
801
+ ret++;
802
+ store.baselen = strlen(new_name);
803
+ store_write_section(out_fd, new_name);
804
+ continue;
805
+ }
806
+ }
807
+ write(out_fd, buf, strlen(buf));
808
+ }
809
+ if (close(out_fd) || commit_lock_file(lock) < 0)
810
+ return error("Cannot commit config file!");
811
+ return ret;
812
+}
813
t/t1300-repo-config.sh
+48
@@ -343,5 +343,53 @@ EOF
343
344
test_expect_success '--set in alternative GIT_CONFIG' 'cmp other-config expect'
345
346
+cat > .git/config << EOF
347
+# Hallo
348
+ #Bello
349
+[branch "eins"]
350
+ x = 1
351
+[branch.eins]
352
+ y = 1
353
+ [branch "1 234 blabl/a"]
354
+weird
355
+EOF
356
+
357
+test_expect_success "rename section" \
358
+ "git-repo-config --rename-section branch.eins branch.zwei"
359
+
360
+cat > expect << EOF
361
+# Hallo
362
+ #Bello
363
+[branch "zwei"]
364
+ x = 1
365
+[branch "zwei"]
366
+ y = 1
367
+ [branch "1 234 blabl/a"]
368
+weird
369
+EOF
370
+
371
+test_expect_success "rename succeeded" "diff -u expect .git/config"
372
+
373
+test_expect_failure "rename non-existing section" \
374
+ 'git-repo-config --rename-section branch."world domination" branch.drei'
375
+
376
+test_expect_success "rename succeeded" "diff -u expect .git/config"
377
+
378
+test_expect_success "rename another section" \
379
+ 'git-repo-config --rename-section branch."1 234 blabl/a" branch.drei'
380
+
381
+cat > expect << EOF
382
+# Hallo
383
+ #Bello
384
+[branch "zwei"]
385
+ x = 1
386
+[branch "zwei"]
387
+ y = 1
388
+[branch "drei"]
389
+weird
390
+EOF
391
+
392
+test_expect_success "rename succeeded" "diff -u expect .git/config"
393
+
394
test_done
395