builtin/config: introduce "remove-section" subcommand

Introduce a new "remove-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 15dad20c3f6ac2a12c9141d97d4f9dd29ce985f4
3 files changed +41 -6
Documentation/git-config.txt
+7 -4
@@ -14,7 +14,7 @@ SYNOPSIS
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 rename-section' [<file-option>] <old-name> <new-name>
17 -'git config' [<file-option>] --remove-section <name>
17 +'git config remove-section' [<file-option>] <name>
18 'git config' [<file-option>] --get-colorbool <name> [<stdout-is-tty>]
19 'git config' [<file-option>] -e | --edit
20
@@ -95,6 +95,9 @@ unset::
95 rename-section::
96 Rename the given section to a new name.
97
98 +remove-section::
99 + Remove the given section from the configuration file.
100 +
101 [[OPTIONS]]
102 OPTIONS
103 -------
@@ -192,9 +195,6 @@ See also <<FILES>>.
195 section in linkgit:gitrevisions[7] for a more complete list of
196 ways to spell blob names.
197
195 ---remove-section::
196 - Remove the given section from the configuration file.
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
@@ -333,6 +333,9 @@ recommended to migrate to the new syntax.
333 --rename-section <old-name> <new-name>::
334 Replaced by `git config rename-section <old-name> <new-name>`.
335
336 +--remove-section <name>::
337 + Replaced by `git config remove-section <name>`.
338 +
339 CONFIGURATION
340 -------------
341 `pager.config` is only respected when listing configuration, i.e., when
builtin/config.c
+32
@@ -21,6 +21,7 @@ static const char *const builtin_config_usage[] = {
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 + N_("git config remove-section [<file-option>] <name>"),
25 NULL
26 };
27
@@ -49,6 +50,11 @@ static const char *const builtin_config_rename_section_usage[] = {
50 NULL
51 };
52
53 +static const char *const builtin_config_remove_section_usage[] = {
54 + N_("git config remove-section [<file-option>] <name>"),
55 + NULL
56 +};
57 +
58 static char *key;
59 static regex_t *key_regexp;
60 static const char *value_pattern;
@@ -980,12 +986,38 @@ static int cmd_config_rename_section(int argc, const char **argv, const char *pr
986 return 0;
987 }
988
989 +static int cmd_config_remove_section(int argc, const char **argv, const char *prefix)
990 +{
991 + struct option opts[] = {
992 + CONFIG_LOCATION_OPTIONS,
993 + OPT_END(),
994 + };
995 + int ret;
996 +
997 + argc = parse_options(argc, argv, prefix, opts, builtin_config_remove_section_usage,
998 + PARSE_OPT_STOP_AT_NON_OPTION);
999 + check_write();
1000 + check_argc(argc, 1, 1);
1001 +
1002 + handle_config_location(prefix);
1003 +
1004 + ret = git_config_rename_section_in_file(given_config_source.file,
1005 + argv[0], NULL);
1006 + if (ret < 0)
1007 + return ret;
1008 + else if (!ret)
1009 + die(_("no such section: %s"), argv[0]);
1010 +
1011 + return 0;
1012 +}
1013 +
1014 static struct option builtin_subcommand_options[] = {
1015 OPT_SUBCOMMAND("list", &subcommand, cmd_config_list),
1016 OPT_SUBCOMMAND("get", &subcommand, cmd_config_get),
1017 OPT_SUBCOMMAND("set", &subcommand, cmd_config_set),
1018 OPT_SUBCOMMAND("unset", &subcommand, cmd_config_unset),
1019 OPT_SUBCOMMAND("rename-section", &subcommand, cmd_config_rename_section),
1020 + OPT_SUBCOMMAND("remove-section", &subcommand, cmd_config_remove_section),
1021 OPT_END(),
1022 };
1023
t/t1300-config.sh
+2 -2
@@ -813,7 +813,7 @@ cat >> .git/config << EOF
813 EOF
814
815 test_expect_success 'remove section' '
816 - git config --remove-section branch.zwei
816 + git config ${mode_prefix}remove-section branch.zwei
817 '
818
819 cat > expect << EOF
@@ -2602,7 +2602,7 @@ test_expect_success 'refuse --fixed-value for incompatible actions' '
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 ${mode_prefix}rename-section --file=config --fixed-value dev null &&
2605 - test_must_fail git config --file=config --fixed-value --remove-section dev &&
2605 + test_must_fail git config ${mode_prefix}remove-section --file=config --fixed-value 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 &&
2608 test_must_fail git config --file=config --fixed-value --get-colorbool dev.null &&