Skip to content

Commit 3418e96

Browse files
pks-tgitster
authored andcommitted
builtin/config: introduce "rename-section" subcommand
Introduce a new "rename-section" subcommand to git-config(1). Please refer to preceding commits regarding the motivation behind this change. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 95ea69c commit 3418e96

File tree

3 files changed

+50
-15
lines changed

3 files changed

+50
-15
lines changed

Documentation/git-config.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ SYNOPSIS
1313
'git config get' [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<value>] [--fixed-value] [--default=<default>] <name>
1414
'git config set' [<file-option>] [--type=<type>] [--all] [--value=<value>] [--fixed-value] <name> <value>
1515
'git config unset' [<file-option>] [--all] [--value=<value>] [--fixed-value] <name> <value>
16-
'git config' [<file-option>] --rename-section <old-name> <new-name>
16+
'git config rename-section' [<file-option>] <old-name> <new-name>
1717
'git config' [<file-option>] --remove-section <name>
1818
'git config' [<file-option>] --get-colorbool <name> [<stdout-is-tty>]
1919
'git config' [<file-option>] -e | --edit
@@ -92,6 +92,9 @@ unset::
9292
multi-valued config options, whereas `--value` will unset all config
9393
options whose values match the given pattern.
9494

95+
rename-section::
96+
Rename the given section to a new name.
97+
9598
[[OPTIONS]]
9699
OPTIONS
97100
-------
@@ -192,9 +195,6 @@ See also <<FILES>>.
192195
--remove-section::
193196
Remove the given section from the configuration file.
194197

195-
--rename-section::
196-
Rename the given section to a new name.
197-
198198
--fixed-value::
199199
When used with the `value-pattern` argument, treat `value-pattern` as
200200
an exact string instead of a regular expression. This will restrict
@@ -330,6 +330,9 @@ recommended to migrate to the new syntax.
330330
--unset-all <name> [<value-pattern>]::
331331
Replaced by `git config unset [--value=<pattern>] --all <name>`.
332332

333+
--rename-section <old-name> <new-name>::
334+
Replaced by `git config rename-section <old-name> <new-name>`.
335+
333336
CONFIGURATION
334337
-------------
335338
`pager.config` is only respected when listing configuration, i.e., when

builtin/config.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ static const char *const builtin_config_usage[] = {
2020
N_("git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<value>] [--fixed-value] [--default=<default>] <name>"),
2121
N_("git config set [<file-option>] [--type=<type>] [--all] [--value=<value>] [--fixed-value] <name> <value>"),
2222
N_("git config unset [<file-option>] [--all] [--value=<value>] [--fixed-value] <name> <value>"),
23+
N_("git config rename-section [<file-option>] <old-name> <new-name>"),
2324
NULL
2425
};
2526

@@ -43,6 +44,11 @@ static const char *const builtin_config_unset_usage[] = {
4344
NULL
4445
};
4546

47+
static const char *const builtin_config_rename_section_usage[] = {
48+
N_("git config rename-section [<file-option>] <old-name> <new-name>"),
49+
NULL
50+
};
51+
4652
static char *key;
4753
static regex_t *key_regexp;
4854
static const char *value_pattern;
@@ -949,11 +955,37 @@ static int cmd_config_unset(int argc, const char **argv, const char *prefix)
949955
NULL, NULL);
950956
}
951957

958+
static int cmd_config_rename_section(int argc, const char **argv, const char *prefix)
959+
{
960+
struct option opts[] = {
961+
CONFIG_LOCATION_OPTIONS,
962+
OPT_END(),
963+
};
964+
int ret;
965+
966+
argc = parse_options(argc, argv, prefix, opts, builtin_config_rename_section_usage,
967+
PARSE_OPT_STOP_AT_NON_OPTION);
968+
check_write();
969+
check_argc(argc, 2, 2);
970+
971+
handle_config_location(prefix);
972+
973+
ret = git_config_rename_section_in_file(given_config_source.file,
974+
argv[0], argv[1]);
975+
if (ret < 0)
976+
return ret;
977+
else if (!ret)
978+
die(_("no such section: %s"), argv[0]);
979+
980+
return 0;
981+
}
982+
952983
static struct option builtin_subcommand_options[] = {
953984
OPT_SUBCOMMAND("list", &subcommand, cmd_config_list),
954985
OPT_SUBCOMMAND("get", &subcommand, cmd_config_get),
955986
OPT_SUBCOMMAND("set", &subcommand, cmd_config_set),
956987
OPT_SUBCOMMAND("unset", &subcommand, cmd_config_unset),
988+
OPT_SUBCOMMAND("rename-section", &subcommand, cmd_config_rename_section),
957989
OPT_END(),
958990
};
959991

t/t1300-config.sh

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ weird
699699
EOF
700700

701701
test_expect_success 'rename section' '
702-
git config --rename-section branch.eins branch.zwei
702+
git config ${mode_prefix}rename-section branch.eins branch.zwei
703703
'
704704

705705
cat > expect << EOF
@@ -718,7 +718,7 @@ test_expect_success 'rename succeeded' '
718718
'
719719

720720
test_expect_success 'rename non-existing section' '
721-
test_must_fail git config --rename-section \
721+
test_must_fail git config ${mode_prefix}rename-section \
722722
branch."world domination" branch.drei
723723
'
724724

@@ -727,7 +727,7 @@ test_expect_success 'rename succeeded' '
727727
'
728728

729729
test_expect_success 'rename another section' '
730-
git config --rename-section branch."1 234 blabl/a" branch.drei
730+
git config ${mode_prefix}rename-section branch."1 234 blabl/a" branch.drei
731731
'
732732

733733
cat > expect << EOF
@@ -750,7 +750,7 @@ cat >> .git/config << EOF
750750
EOF
751751

752752
test_expect_success 'rename a section with a var on the same line' '
753-
git config --rename-section branch.vier branch.zwei
753+
git config ${mode_prefix}rename-section branch.vier branch.zwei
754754
'
755755

756756
cat > expect << EOF
@@ -771,11 +771,11 @@ test_expect_success 'rename succeeded' '
771771
'
772772

773773
test_expect_success 'renaming empty section name is rejected' '
774-
test_must_fail git config --rename-section branch.zwei ""
774+
test_must_fail git config ${mode_prefix}rename-section branch.zwei ""
775775
'
776776

777777
test_expect_success 'renaming to bogus section is rejected' '
778-
test_must_fail git config --rename-section branch.zwei "bogus name"
778+
test_must_fail git config ${mode_prefix}rename-section branch.zwei "bogus name"
779779
'
780780

781781
test_expect_success 'renaming a section with a long line' '
@@ -784,7 +784,7 @@ test_expect_success 'renaming a section with a long line' '
784784
printf " c = d %1024s [a] e = f\\n" " " &&
785785
printf "[a] g = h\\n"
786786
} >y &&
787-
git config -f y --rename-section a xyz &&
787+
git config ${mode_prefix}rename-section -f y a xyz &&
788788
test_must_fail git config -f y b.e
789789
'
790790

@@ -794,7 +794,7 @@ test_expect_success 'renaming an embedded section with a long line' '
794794
printf " c = d %1024s [a] [foo] e = f\\n" " " &&
795795
printf "[a] g = h\\n"
796796
} >y &&
797-
git config -f y --rename-section a xyz &&
797+
git config ${mode_prefix}rename-section -f y a xyz &&
798798
test_must_fail git config -f y foo.e
799799
'
800800

@@ -804,7 +804,7 @@ test_expect_success 'renaming a section with an overly-long line' '
804804
printf " c = d %525000s e" " " &&
805805
printf "[a] g = h\\n"
806806
} >y &&
807-
test_must_fail git config -f y --rename-section a xyz 2>err &&
807+
test_must_fail git config ${mode_prefix}rename-section -f y a xyz 2>err &&
808808
grep "refusing to work with overly long line in .y. on line 2" err
809809
'
810810

@@ -2112,7 +2112,7 @@ test_expect_success POSIXPERM,PERL 'preserves existing permissions' '
21122112
git config imap.pass Hunter2 &&
21132113
perl -e \
21142114
"die q(badset) if ((stat(q(.git/config)))[2] & 07777) != 0600" &&
2115-
git config --rename-section imap pop &&
2115+
git config ${mode_prefix}rename-section imap pop &&
21162116
perl -e \
21172117
"die q(badrename) if ((stat(q(.git/config)))[2] & 07777) != 0600"
21182118
'
@@ -2601,7 +2601,7 @@ test_expect_success 'refuse --fixed-value for incompatible actions' '
26012601
test_must_fail git config --file=config --fixed-value --add dev.null bogus &&
26022602
test_must_fail git config --file=config --fixed-value --get-urlmatch dev.null bogus &&
26032603
test_must_fail git config --file=config --fixed-value --get-urlmatch dev.null bogus &&
2604-
test_must_fail git config --file=config --fixed-value --rename-section dev null &&
2604+
test_must_fail git config ${mode_prefix}rename-section --file=config --fixed-value dev null &&
26052605
test_must_fail git config --file=config --fixed-value --remove-section dev &&
26062606
test_must_fail git config ${mode_prefix}list --file=config --fixed-value &&
26072607
test_must_fail git config --file=config --fixed-value --get-color dev.null &&

0 commit comments

Comments
 (0)