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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed May 6, 2024 at 10:56 UTC 3418e96f37b3851140e0c66fed8feae7593b7a25
3 files changed +50 -15
Documentation/git-config.txt
+7 -4
@@ -13,7 +13,7 @@ SYNOPSIS
13 'git config get' [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<value>] [--fixed-value] [--default=<default>] <name>
14 'git config set' [<file-option>] [--type=<type>] [--all] [--value=<value>] [--fixed-value] <name> <value>
15 '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>
17 'git config' [<file-option>] --remove-section <name>
18 'git config' [<file-option>] --get-colorbool <name> [<stdout-is-tty>]
19 'git config' [<file-option>] -e | --edit
@@ -92,6 +92,9 @@ unset::
92 multi-valued config options, whereas `--value` will unset all config
93 options whose values match the given pattern.
94
95 +rename-section::
96 + Rename the given section to a new name.
97 +
98 [[OPTIONS]]
99 OPTIONS
100 -------
@@ -192,9 +195,6 @@ See also <<FILES>>.
195 --remove-section::
196 Remove the given section from the configuration file.
197
195 ---rename-section::
196 - Rename the given section to a new name.
197 -
198 --fixed-value::
199 When used with the `value-pattern` argument, treat `value-pattern` as
200 an exact string instead of a regular expression. This will restrict
@@ -330,6 +330,9 @@ recommended to migrate to the new syntax.
330 --unset-all <name> [<value-pattern>]::
331 Replaced by `git config unset [--value=<pattern>] --all <name>`.
332
333 +--rename-section <old-name> <new-name>::
334 + Replaced by `git config rename-section <old-name> <new-name>`.
335 +
336 CONFIGURATION
337 -------------
338 `pager.config` is only respected when listing configuration, i.e., when
builtin/config.c
+32
@@ -20,6 +20,7 @@ static const char *const builtin_config_usage[] = {
20 N_("git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<value>] [--fixed-value] [--default=<default>] <name>"),
21 N_("git config set [<file-option>] [--type=<type>] [--all] [--value=<value>] [--fixed-value] <name> <value>"),
22 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>"),
24 NULL
25 };
26
@@ -43,6 +44,11 @@ static const char *const builtin_config_unset_usage[] = {
44 NULL
45 };
46
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 +
52 static char *key;
53 static regex_t *key_regexp;
54 static const char *value_pattern;
@@ -949,11 +955,37 @@ static int cmd_config_unset(int argc, const char **argv, const char *prefix)
955 NULL, NULL);
956 }
957
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 +
983 static struct option builtin_subcommand_options[] = {
984 OPT_SUBCOMMAND("list", &subcommand, cmd_config_list),
985 OPT_SUBCOMMAND("get", &subcommand, cmd_config_get),
986 OPT_SUBCOMMAND("set", &subcommand, cmd_config_set),
987 OPT_SUBCOMMAND("unset", &subcommand, cmd_config_unset),
988 + OPT_SUBCOMMAND("rename-section", &subcommand, cmd_config_rename_section),
989 OPT_END(),
990 };
991
t/t1300-config.sh
+11 -11
@@ -699,7 +699,7 @@ weird
699 EOF
700
701 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
703 '
704
705 cat > expect << EOF
@@ -718,7 +718,7 @@ test_expect_success 'rename succeeded' '
718 '
719
720 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 \
722 branch."world domination" branch.drei
723 '
724
@@ -727,7 +727,7 @@ test_expect_success 'rename succeeded' '
727 '
728
729 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
731 '
732
733 cat > expect << EOF
@@ -750,7 +750,7 @@ cat >> .git/config << EOF
750 EOF
751
752 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
754 '
755
756 cat > expect << EOF
@@ -771,11 +771,11 @@ test_expect_success 'rename succeeded' '
771 '
772
773 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 ""
775 '
776
777 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"
779 '
780
781 test_expect_success 'renaming a section with a long line' '
@@ -784,7 +784,7 @@ test_expect_success 'renaming a section with a long line' '
784 printf " c = d %1024s [a] e = f\\n" " " &&
785 printf "[a] g = h\\n"
786 } >y &&
787 - git config -f y --rename-section a xyz &&
787 + git config ${mode_prefix}rename-section -f y a xyz &&
788 test_must_fail git config -f y b.e
789 '
790
@@ -794,7 +794,7 @@ test_expect_success 'renaming an embedded section with a long line' '
794 printf " c = d %1024s [a] [foo] e = f\\n" " " &&
795 printf "[a] g = h\\n"
796 } >y &&
797 - git config -f y --rename-section a xyz &&
797 + git config ${mode_prefix}rename-section -f y a xyz &&
798 test_must_fail git config -f y foo.e
799 '
800
@@ -804,7 +804,7 @@ test_expect_success 'renaming a section with an overly-long line' '
804 printf " c = d %525000s e" " " &&
805 printf "[a] g = h\\n"
806 } >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 &&
808 grep "refusing to work with overly long line in .y. on line 2" err
809 '
810
@@ -2112,7 +2112,7 @@ test_expect_success POSIXPERM,PERL 'preserves existing permissions' '
2112 git config imap.pass Hunter2 &&
2113 perl -e \
2114 "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 &&
2116 perl -e \
2117 "die q(badrename) if ((stat(q(.git/config)))[2] & 07777) != 0600"
2118 '
@@ -2601,7 +2601,7 @@ test_expect_success 'refuse --fixed-value for incompatible actions' '
2601 test_must_fail git config --file=config --fixed-value --add dev.null bogus &&
2602 test_must_fail git config --file=config --fixed-value --get-urlmatch dev.null bogus &&
2603 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 &&
2605 test_must_fail git config --file=config --fixed-value --remove-section dev &&
2606 test_must_fail git config ${mode_prefix}list --file=config --fixed-value &&
2607 test_must_fail git config --file=config --fixed-value --get-color dev.null &&