git-config: document --rename-section, provide --remove-section
This patch documents the previously undocumented option --rename-section and adds a new option to zap an entire section. Signed-off-by: Paolo Bonzini <bonzini@gnu.org> Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
Paolo Bonzini committed
Mar 2, 2007 at 21:53 UTC
118f8b241355b38cd21e644e8620d81f10190627
4 files changed
+74
-25
Documentation/git-config.txt
+8
@@ -16,6 +16,8 @@ SYNOPSIS
16
'git-config' [--global] [type] --get-all name [value_regex]
17
'git-config' [--global] [type] --unset name [value_regex]
18
'git-config' [--global] [type] --unset-all name [value_regex]
19
+'git-config' [--global] [type] --rename-section old_name new_name
20
+'git-config' [--global] [type] --remove-section name
21
'git-config' [--global] -l | --list
22
23
DESCRIPTION
@@ -74,6 +76,12 @@ OPTIONS
76
--global::
77
Use global ~/.gitconfig file rather than the repository .git/config.
78
79
+--remove-section::
80
+ Remove the given section from the configuration file.
81
+
82
+--rename-section::
83
+ Rename the given section to a new name.
84
+
85
--unset::
86
Remove the line matching the key from config file.
87
builtin-config.c
+14
-1
@@ -2,7 +2,7 @@
2
#include "cache.h"
3
4
static const char git_config_set_usage[] =
5
-"git-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";
5
+"git-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 | --remove-section name | --list";
6
7
static char *key;
8
static regex_t *key_regexp;
@@ -168,6 +168,19 @@ int cmd_config(int argc, const char **argv, const char *prefix)
168
}
169
return 0;
170
}
171
+ else if (!strcmp(argv[1], "--remove-section")) {
172
+ int ret;
173
+ if (argc != 3)
174
+ usage(git_config_set_usage);
175
+ ret = git_config_rename_section(argv[2], NULL);
176
+ if (ret < 0)
177
+ return ret;
178
+ if (ret == 0) {
179
+ fprintf(stderr, "No such section!\n");
180
+ return 1;
181
+ }
182
+ return 0;
183
+ }
184
else
185
break;
186
argc--;
config.c
+36
-24
@@ -854,9 +854,37 @@ write_err_out:
854
855
}
856
857
+static int section_name_match (const char *buf, const char *name)
858
+{
859
+ int i = 0, j = 0, dot = 0;
860
+ for (; buf[i] && buf[i] != ']'; i++) {
861
+ if (!dot && isspace(buf[i])) {
862
+ dot = 1;
863
+ if (name[j++] != '.')
864
+ break;
865
+ for (i++; isspace(buf[i]); i++)
866
+ ; /* do nothing */
867
+ if (buf[i] != '"')
868
+ break;
869
+ continue;
870
+ }
871
+ if (buf[i] == '\\' && dot)
872
+ i++;
873
+ else if (buf[i] == '"' && dot) {
874
+ for (i++; isspace(buf[i]); i++)
875
+ ; /* do_nothing */
876
+ break;
877
+ }
878
+ if (buf[i] != name[j++])
879
+ break;
880
+ }
881
+ return (buf[i] == ']' && name[j] == 0);
882
+}
883
+
884
+/* if new_name == NULL, the section is removed instead */
885
int git_config_rename_section(const char *old_name, const char *new_name)
886
{
859
- int ret = 0;
887
+ int ret = 0, remove = 0;
888
char *config_filename;
889
struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
890
int out_fd;
@@ -887,31 +915,12 @@ int git_config_rename_section(const char *old_name, const char *new_name)
915
; /* do nothing */
916
if (buf[i] == '[') {
917
/* it's a section */
890
- int j = 0, dot = 0;
891
- for (i++; buf[i] && buf[i] != ']'; i++) {
892
- if (!dot && isspace(buf[i])) {
893
- dot = 1;
894
- if (old_name[j++] != '.')
895
- break;
896
- for (i++; isspace(buf[i]); i++)
897
- ; /* do nothing */
898
- if (buf[i] != '"')
899
- break;
918
+ if (section_name_match (&buf[i+1], old_name)) {
919
+ ret++;
920
+ if (new_name == NULL) {
921
+ remove = 1;
922
continue;
923
}
902
- if (buf[i] == '\\' && dot)
903
- i++;
904
- else if (buf[i] == '"' && dot) {
905
- for (i++; isspace(buf[i]); i++)
906
- ; /* do_nothing */
907
- break;
908
- }
909
- if (buf[i] != old_name[j++])
910
- break;
911
- }
912
- if (buf[i] == ']' && old_name[j] == 0) {
913
- /* old_name matches */
914
- ret++;
924
store.baselen = strlen(new_name);
925
if (!store_write_section(out_fd, new_name)) {
926
ret = write_error();
@@ -919,7 +928,10 @@ int git_config_rename_section(const char *old_name, const char *new_name)
928
}
929
continue;
930
}
931
+ remove = 0;
932
}
933
+ if (remove)
934
+ continue;
935
length = strlen(buf);
936
if (write_in_full(out_fd, buf, length) != length) {
937
ret = write_error();
t/t1300-repo-config.sh
+16
@@ -391,6 +391,22 @@ EOF
391
392
test_expect_success "rename succeeded" "diff -u expect .git/config"
393
394
+cat >> .git/config << EOF
395
+ [branch "zwei"] a = 1 [branch "vier"]
396
+EOF
397
+
398
+test_expect_success "remove section" "git config --remove-section branch.zwei"
399
+
400
+cat > expect << EOF
401
+# Hallo
402
+ #Bello
403
+[branch "drei"]
404
+weird
405
+EOF
406
+
407
+test_expect_success "section was removed properly" \
408
+ "diff -u expect .git/config"
409
+
410
test_expect_success numbers '
411
412
git-config kilo.gram 1k &&