Skip to content

Commit 118f8b2

Browse files
bonziniJunio C Hamano
authored andcommitted
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 <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 253e772 commit 118f8b2

File tree

4 files changed

+74
-25
lines changed

4 files changed

+74
-25
lines changed

Documentation/git-config.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ SYNOPSIS
1616
'git-config' [--global] [type] --get-all name [value_regex]
1717
'git-config' [--global] [type] --unset name [value_regex]
1818
'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
1921
'git-config' [--global] -l | --list
2022

2123
DESCRIPTION
@@ -74,6 +76,12 @@ OPTIONS
7476
--global::
7577
Use global ~/.gitconfig file rather than the repository .git/config.
7678

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+
7785
--unset::
7886
Remove the line matching the key from config file.
7987

builtin-config.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "cache.h"
33

44
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";
66

77
static char *key;
88
static regex_t *key_regexp;
@@ -168,6 +168,19 @@ int cmd_config(int argc, const char **argv, const char *prefix)
168168
}
169169
return 0;
170170
}
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+
}
171184
else
172185
break;
173186
argc--;

config.c

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -854,9 +854,37 @@ int git_config_set_multivar(const char* key, const char* value,
854854

855855
}
856856

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 */
857885
int git_config_rename_section(const char *old_name, const char *new_name)
858886
{
859-
int ret = 0;
887+
int ret = 0, remove = 0;
860888
char *config_filename;
861889
struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
862890
int out_fd;
@@ -887,39 +915,23 @@ int git_config_rename_section(const char *old_name, const char *new_name)
887915
; /* do nothing */
888916
if (buf[i] == '[') {
889917
/* 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;
900922
continue;
901923
}
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++;
915924
store.baselen = strlen(new_name);
916925
if (!store_write_section(out_fd, new_name)) {
917926
ret = write_error();
918927
goto out;
919928
}
920929
continue;
921930
}
931+
remove = 0;
922932
}
933+
if (remove)
934+
continue;
923935
length = strlen(buf);
924936
if (write_in_full(out_fd, buf, length) != length) {
925937
ret = write_error();

t/t1300-repo-config.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,22 @@ EOF
391391

392392
test_expect_success "rename succeeded" "diff -u expect .git/config"
393393

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+
394410
test_expect_success numbers '
395411
396412
git-config kilo.gram 1k &&

0 commit comments

Comments
 (0)