Skip to content

Commit bd1fc62

Browse files
author
Junio C Hamano
committed
Merge branch 'js/config-rename'
* js/config-rename: git-config: document --rename-section, provide --remove-section
2 parents f45fa2a + 118f8b2 commit bd1fc62

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
@@ -861,9 +861,37 @@ int git_config_set_multivar(const char* key, const char* value,
861861

862862
}
863863

864+
static int section_name_match (const char *buf, const char *name)
865+
{
866+
int i = 0, j = 0, dot = 0;
867+
for (; buf[i] && buf[i] != ']'; i++) {
868+
if (!dot && isspace(buf[i])) {
869+
dot = 1;
870+
if (name[j++] != '.')
871+
break;
872+
for (i++; isspace(buf[i]); i++)
873+
; /* do nothing */
874+
if (buf[i] != '"')
875+
break;
876+
continue;
877+
}
878+
if (buf[i] == '\\' && dot)
879+
i++;
880+
else if (buf[i] == '"' && dot) {
881+
for (i++; isspace(buf[i]); i++)
882+
; /* do_nothing */
883+
break;
884+
}
885+
if (buf[i] != name[j++])
886+
break;
887+
}
888+
return (buf[i] == ']' && name[j] == 0);
889+
}
890+
891+
/* if new_name == NULL, the section is removed instead */
864892
int git_config_rename_section(const char *old_name, const char *new_name)
865893
{
866-
int ret = 0;
894+
int ret = 0, remove = 0;
867895
char *config_filename;
868896
struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
869897
int out_fd;
@@ -894,39 +922,23 @@ int git_config_rename_section(const char *old_name, const char *new_name)
894922
; /* do nothing */
895923
if (buf[i] == '[') {
896924
/* it's a section */
897-
int j = 0, dot = 0;
898-
for (i++; buf[i] && buf[i] != ']'; i++) {
899-
if (!dot && isspace(buf[i])) {
900-
dot = 1;
901-
if (old_name[j++] != '.')
902-
break;
903-
for (i++; isspace(buf[i]); i++)
904-
; /* do nothing */
905-
if (buf[i] != '"')
906-
break;
925+
if (section_name_match (&buf[i+1], old_name)) {
926+
ret++;
927+
if (new_name == NULL) {
928+
remove = 1;
907929
continue;
908930
}
909-
if (buf[i] == '\\' && dot)
910-
i++;
911-
else if (buf[i] == '"' && dot) {
912-
for (i++; isspace(buf[i]); i++)
913-
; /* do_nothing */
914-
break;
915-
}
916-
if (buf[i] != old_name[j++])
917-
break;
918-
}
919-
if (buf[i] == ']' && old_name[j] == 0) {
920-
/* old_name matches */
921-
ret++;
922931
store.baselen = strlen(new_name);
923932
if (!store_write_section(out_fd, new_name)) {
924933
ret = write_error();
925934
goto out;
926935
}
927936
continue;
928937
}
938+
remove = 0;
929939
}
940+
if (remove)
941+
continue;
930942
length = strlen(buf);
931943
if (write_in_full(out_fd, buf, length) != length) {
932944
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)