git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4 11/14] builtin/config: introduce "rename-section" subcommand

From
Patrick Steinhardt <ps@pks.im>
Date
May 3, 2024, 09:57 UTC
Message-ID
<f24008d3563a07445ebf8d63ec3a86f4c0983010.1714730170.git.ps@pks.im>
In-Reply-To
<cover.1714730169.git.ps@pks.im>

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>
---
 Documentation/git-config.txt | 11 +++++++----
 builtin/config.c             | 32 ++++++++++++++++++++++++++++++++
 t/t1300-config.sh            | 22 +++++++++++-----------
 3 files changed, 50 insertions(+), 15 deletions(-)
diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt
index 5575fe55ea..ede86ad085 100644
--- a/Documentation/git-config.txt
+++ b/Documentation/git-config.txt
@@ -13,7 +13,7 @@ SYNOPSIS
 'git config get' [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<value>] [--fixed-value] [--default=<default>] <name>
 'git config set' [<file-option>] [--type=<type>] [--all] [--value=<value>] [--fixed-value] <name> <value>
 'git config unset' [<file-option>] [--all] [--value=<value>] [--fixed-value] <name> <value>
-'git config' [<file-option>] --rename-section <old-name> <new-name>
+'git config rename-section' [<file-option>] <old-name> <new-name>
 'git config' [<file-option>] --remove-section <name>
 'git config' [<file-option>] --get-colorbool <name> [<stdout-is-tty>]
 'git config' [<file-option>] -e | --edit
@@ -92,6 +92,9 @@ unset::
 	multi-valued config options, whereas `--value` will unset all config
 	options whose values match the given pattern.
 
+rename-section::
+	Rename the given section to a new name.
+
 [[OPTIONS]]
 OPTIONS
 -------
@@ -192,9 +195,6 @@ See also <<FILES>>.
 --remove-section::
 	Remove the given section from the configuration file.
 
---rename-section::
-	Rename the given section to a new name.
-
 --fixed-value::
 	When used with the `value-pattern` argument, treat `value-pattern` as
 	an exact string instead of a regular expression. This will restrict
@@ -330,6 +330,9 @@ recommended to migrate to the new syntax.
 --unset-all <name> [<value-pattern>]::
 	Replaced by `git config unset [--value=<pattern>] --all <name>`.
 
+--rename-section <old-name> <new-name>::
+	Replaced by `git config rename-section <old-name> <new-name>`.
+
 CONFIGURATION
 -------------
 `pager.config` is only respected when listing configuration, i.e., when
diff --git a/builtin/config.c b/builtin/config.c
index 2578d4c930..a60dca9b2b 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -20,6 +20,7 @@ static const char *const builtin_config_usage[] = {
 	N_("git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<value>] [--fixed-value] [--default=<default>] <name>"),
 	N_("git config set [<file-option>] [--type=<type>] [--all] [--value=<value>] [--fixed-value] <name> <value>"),
 	N_("git config unset [<file-option>] [--all] [--value=<value>] [--fixed-value] <name> <value>"),
+	N_("git config rename-section [<file-option>] <old-name> <new-name>"),
 	NULL
 };
 
@@ -43,6 +44,11 @@ static const char *const builtin_config_unset_usage[] = {
 	NULL
 };
 
+static const char *const builtin_config_rename_section_usage[] = {
+	N_("git config rename-section [<file-option>] <old-name> <new-name>"),
+	NULL
+};
+
 static char *key;
 static regex_t *key_regexp;
 static const char *value_pattern;
@@ -949,11 +955,37 @@ static int cmd_config_unset(int argc, const char **argv, const char *prefix)
 						     NULL, NULL);
 }
 
+static int cmd_config_rename_section(int argc, const char **argv, const char *prefix)
+{
+	struct option opts[] = {
+		CONFIG_LOCATION_OPTIONS,
+		OPT_END(),
+	};
+	int ret;
+
+	argc = parse_options(argc, argv, prefix, opts, builtin_config_rename_section_usage,
+			     PARSE_OPT_STOP_AT_NON_OPTION);
+	check_write();
+	check_argc(argc, 2, 2);
+
+	handle_config_location(prefix);
+
+	ret = git_config_rename_section_in_file(given_config_source.file,
+						argv[0], argv[1]);
+	if (ret < 0)
+		return ret;
+	else if (!ret)
+		die(_("no such section: %s"), argv[0]);
+
+	return 0;
+}
+
 static struct option builtin_subcommand_options[] = {
 	OPT_SUBCOMMAND("list", &subcommand, cmd_config_list),
 	OPT_SUBCOMMAND("get", &subcommand, cmd_config_get),
 	OPT_SUBCOMMAND("set", &subcommand, cmd_config_set),
 	OPT_SUBCOMMAND("unset", &subcommand, cmd_config_unset),
+	OPT_SUBCOMMAND("rename-section", &subcommand, cmd_config_rename_section),
 	OPT_END(),
 };
 
diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index 2ab58bbd95..5d7b08fa4c 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -699,7 +699,7 @@ weird
 EOF
 
 test_expect_success 'rename section' '
-	git config --rename-section branch.eins branch.zwei
+	git config ${mode_prefix}rename-section branch.eins branch.zwei
 '
 
 cat > expect << EOF
@@ -718,7 +718,7 @@ test_expect_success 'rename succeeded' '
 '
 
 test_expect_success 'rename non-existing section' '
-	test_must_fail git config --rename-section \
+	test_must_fail git config ${mode_prefix}rename-section \
 		branch."world domination" branch.drei
 '
 
@@ -727,7 +727,7 @@ test_expect_success 'rename succeeded' '
 '
 
 test_expect_success 'rename another section' '
-	git config --rename-section branch."1 234 blabl/a" branch.drei
+	git config ${mode_prefix}rename-section branch."1 234 blabl/a" branch.drei
 '
 
 cat > expect << EOF
@@ -750,7 +750,7 @@ cat >> .git/config << EOF
 EOF
 
 test_expect_success 'rename a section with a var on the same line' '
-	git config --rename-section branch.vier branch.zwei
+	git config ${mode_prefix}rename-section branch.vier branch.zwei
 '
 
 cat > expect << EOF
@@ -771,11 +771,11 @@ test_expect_success 'rename succeeded' '
 '
 
 test_expect_success 'renaming empty section name is rejected' '
-	test_must_fail git config --rename-section branch.zwei ""
+	test_must_fail git config ${mode_prefix}rename-section branch.zwei ""
 '
 
 test_expect_success 'renaming to bogus section is rejected' '
-	test_must_fail git config --rename-section branch.zwei "bogus name"
+	test_must_fail git config ${mode_prefix}rename-section branch.zwei "bogus name"
 '
 
 test_expect_success 'renaming a section with a long line' '
@@ -784,7 +784,7 @@ test_expect_success 'renaming a section with a long line' '
 		printf "  c = d %1024s [a] e = f\\n" " " &&
 		printf "[a] g = h\\n"
 	} >y &&
-	git config -f y --rename-section a xyz &&
+	git config ${mode_prefix}rename-section -f y a xyz &&
 	test_must_fail git config -f y b.e
 '
 
@@ -794,7 +794,7 @@ test_expect_success 'renaming an embedded section with a long line' '
 		printf "  c = d %1024s [a] [foo] e = f\\n" " " &&
 		printf "[a] g = h\\n"
 	} >y &&
-	git config -f y --rename-section a xyz &&
+	git config ${mode_prefix}rename-section -f y a xyz &&
 	test_must_fail git config -f y foo.e
 '
 
@@ -804,7 +804,7 @@ test_expect_success 'renaming a section with an overly-long line' '
 		printf "  c = d %525000s e" " " &&
 		printf "[a] g = h\\n"
 	} >y &&
-	test_must_fail git config -f y --rename-section a xyz 2>err &&
+	test_must_fail git config ${mode_prefix}rename-section -f y a xyz 2>err &&
 	grep "refusing to work with overly long line in .y. on line 2" err
 '
 
@@ -2112,7 +2112,7 @@ test_expect_success POSIXPERM,PERL 'preserves existing permissions' '
 	git config imap.pass Hunter2 &&
 	perl -e \
 	  "die q(badset) if ((stat(q(.git/config)))[2] & 07777) != 0600" &&
-	git config --rename-section imap pop &&
+	git config ${mode_prefix}rename-section imap pop &&
 	perl -e \
 	  "die q(badrename) if ((stat(q(.git/config)))[2] & 07777) != 0600"
 '
@@ -2601,7 +2601,7 @@ test_expect_success 'refuse --fixed-value for incompatible actions' '
 	test_must_fail git config --file=config --fixed-value --add dev.null bogus &&
 	test_must_fail git config --file=config --fixed-value --get-urlmatch dev.null bogus &&
 	test_must_fail git config --file=config --fixed-value --get-urlmatch dev.null bogus &&
-	test_must_fail git config --file=config --fixed-value --rename-section dev null &&
+	test_must_fail git config ${mode_prefix}rename-section --file=config --fixed-value dev null &&
 	test_must_fail git config --file=config --fixed-value --remove-section dev &&
 	test_must_fail git config ${mode_prefix}list --file=config --fixed-value &&
 	test_must_fail git config --file=config --fixed-value --get-color dev.null &&
-- 
2.45.0
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 89 of 116 in “builtin/config: introduce subcommands”
  1. 0/8 builtin/config: introduce subcommandsPatrick Steinhardt, Mar 6, 2024
  2. 1/8 builtin/config: move option array aroundPatrick Steinhardt, Mar 6, 2024
  3. 2/8 builtin/config: move "fixed-value" option to correct groupPatrick Steinhardt, Mar 6, 2024
  4. 3/8 builtin/config: use `OPT_CMDMODE()` to specify modesPatrick Steinhardt, Mar 6, 2024
  5. Taylor BlauMar 6, 2024
  6. Patrick SteinhardtMar 7, 2024
  7. 4/8 builtin/config: move modes into separate functionsPatrick Steinhardt, Mar 6, 2024
  8. 5/8 builtin/config: track subcommands by actionPatrick Steinhardt, Mar 6, 2024
  9. Jean-Noël AVILAMar 6, 2024
  10. Patrick SteinhardtMar 7, 2024
  11. Taylor BlauMar 7, 2024
  12. Patrick SteinhardtMar 7, 2024
  13. 6/8 builtin/config: introduce subcommandsPatrick Steinhardt, Mar 6, 2024
  14. Karthik NayakMar 6, 2024
  15. Patrick SteinhardtMar 7, 2024
  16. 7/8 t1300: exercise both old- and new-style modesPatrick Steinhardt, Mar 6, 2024
  17. 8/8 Documentation/git-config: update to new-style syntaxPatrick Steinhardt, Mar 6, 2024
  18. Eric SunshineMar 7, 2024
  19. Patrick SteinhardtMar 7, 2024
  20. Junio C HamanoMar 6, 2024
  21. Taylor BlauMar 6, 2024
  22. Junio C HamanoMar 6, 2024
  23. Taylor BlauMar 7, 2024
  24. Dragan SimicMar 7, 2024
  25. Patrick SteinhardtMar 7, 2024
  26. Junio C HamanoMar 7, 2024
  27. Kristoffer HaugsbakkMar 6, 2024
  28. 00/13 builtin/config: introduce subcommandsPatrick Steinhardt, Mar 11, 2024
  29. 01/13 builtin/config: move option array aroundPatrick Steinhardt, Mar 11, 2024
  30. 02/13 builtin/config: move "fixed-value" option to correct groupPatrick Steinhardt, Mar 11, 2024
  31. 03/13 builtin/config: use `OPT_CMDMODE()` to specify modesPatrick Steinhardt, Mar 11, 2024
  32. 04/13 builtin/config: pull out function to handle config locationPatrick Steinhardt, Mar 11, 2024
  33. 05/13 builtin/config: pull out function to handle `--null`Patrick Steinhardt, Mar 11, 2024
  34. 06/13 builtin/config: introduce "list" subcommandPatrick Steinhardt, Mar 11, 2024
  35. Eric SunshineMar 13, 2024
  36. Patrick SteinhardtMar 27, 2024
  37. 07/13 builtin/config: introduce "get" subcommandPatrick Steinhardt, Mar 11, 2024
  38. Eric SunshineMar 13, 2024
  39. Patrick SteinhardtMar 27, 2024
  40. 08/13 builtin/config: introduce "set" subcommandPatrick Steinhardt, Mar 11, 2024
  41. 09/13 builtin/config: introduce "unset" subcommandPatrick Steinhardt, Mar 11, 2024
  42. 10/13 builtin/config: introduce "rename-section" subcommandPatrick Steinhardt, Mar 11, 2024
  43. 11/13 builtin/config: introduce "remove-section" subcommandPatrick Steinhardt, Mar 11, 2024
  44. 12/13 builtin/config: introduce "edit" subcommandPatrick Steinhardt, Mar 11, 2024
  45. 13/13 builtin/config: display subcommand helpPatrick Steinhardt, Mar 11, 2024
  46. 00/13 builtin/config: introduce subcommandsPatrick Steinhardt, Mar 27, 2024
  47. 01/13 builtin/config: move option array aroundPatrick Steinhardt, Mar 27, 2024
  48. 02/13 builtin/config: move "fixed-value" option to correct groupPatrick Steinhardt, Mar 27, 2024
  49. 03/13 builtin/config: use `OPT_CMDMODE()` to specify modesPatrick Steinhardt, Mar 27, 2024
  50. 04/13 builtin/config: pull out function to handle config locationPatrick Steinhardt, Mar 27, 2024
  51. 05/13 builtin/config: pull out function to handle `--null`Patrick Steinhardt, Mar 27, 2024
  52. 06/13 builtin/config: introduce "list" subcommandPatrick Steinhardt, Mar 27, 2024
  53. 07/13 builtin/config: introduce "get" subcommandPatrick Steinhardt, Mar 27, 2024
  54. 08/13 builtin/config: introduce "set" subcommandPatrick Steinhardt, Mar 27, 2024
  55. 09/13 builtin/config: introduce "unset" subcommandPatrick Steinhardt, Mar 27, 2024
  56. 10/13 builtin/config: introduce "rename-section" subcommandPatrick Steinhardt, Mar 27, 2024
  57. 11/13 builtin/config: introduce "remove-section" subcommandPatrick Steinhardt, Mar 27, 2024
  58. 12/13 builtin/config: introduce "edit" subcommandPatrick Steinhardt, Mar 27, 2024
  59. 13/13 builtin/config: display subcommand helpPatrick Steinhardt, Mar 27, 2024
  60. Eric SunshineMar 27, 2024
  61. Patrick SteinhardtMar 27, 2024
  62. 00/14 builtin/config: introduce subcommandsPatrick Steinhardt, May 3, 2024
  63. 01/14 config: clarify memory ownership when preparing comment stringsPatrick Steinhardt, May 3, 2024
  64. Kristoffer HaugsbakkMay 3, 2024
  65. 02/14 builtin/config: move option array aroundPatrick Steinhardt, May 3, 2024
  66. 03/14 builtin/config: move "fixed-value" option to correct groupPatrick Steinhardt, May 3, 2024
  67. Karthik NayakMay 3, 2024
  68. Patrick SteinhardtMay 6, 2024
  69. 04/14 builtin/config: use `OPT_CMDMODE()` to specify modesPatrick Steinhardt, May 3, 2024
  70. 05/14 builtin/config: pull out function to handle config locationPatrick Steinhardt, May 3, 2024
  71. 06/14 builtin/config: pull out function to handle `--null`Patrick Steinhardt, May 3, 2024
  72. 07/14 builtin/config: introduce "list" subcommandPatrick Steinhardt, May 3, 2024
  73. Karthik NayakMay 3, 2024
  74. rsbecker@nexbridge.comMay 3, 2024
  75. Junio C HamanoMay 3, 2024
  76. Patrick SteinhardtMay 6, 2024
  77. Junio C HamanoMay 6, 2024
  78. rsbecker@nexbridge.comMay 6, 2024
  79. Dragan SimicMay 6, 2024
  80. Kristoffer HaugsbakkMay 7, 2024
  81. Git 3.0?Junio C Hamano, May 6, 2024
  82. Patrick SteinhardtMay 7, 2024
  83. Patrick SteinhardtMay 7, 2024
  84. Patrick SteinhardtMay 6, 2024
  85. Karthik NayakMay 6, 2024
  86. 08/14 builtin/config: introduce "get" subcommandPatrick Steinhardt, May 3, 2024
  87. 09/14 builtin/config: introduce "set" subcommandPatrick Steinhardt, May 3, 2024
  88. 10/14 builtin/config: introduce "unset" subcommandPatrick Steinhardt, May 3, 2024
  89. 11/14 builtin/config: introduce "rename-section" subcommandPatrick Steinhardt, May 3, 2024
  90. 12/14 builtin/config: introduce "remove-section" subcommandPatrick Steinhardt, May 3, 2024
  91. 13/14 builtin/config: introduce "edit" subcommandPatrick Steinhardt, May 3, 2024
  92. 14/14 builtin/config: display subcommand helpPatrick Steinhardt, May 3, 2024
  93. Dragan SimicMay 3, 2024
  94. Junio C HamanoMay 3, 2024
  95. 00/14 builtin/config: introduce subcommandsPatrick Steinhardt, May 6, 2024
  96. 01/14 config: clarify memory ownership when preparing comment stringsPatrick Steinhardt, May 6, 2024
  97. 02/14 builtin/config: move option array aroundPatrick Steinhardt, May 6, 2024
  98. 03/14 builtin/config: move "fixed-value" option to correct groupPatrick Steinhardt, May 6, 2024
  99. 04/14 builtin/config: use `OPT_CMDMODE()` to specify modesPatrick Steinhardt, May 6, 2024
  100. 05/14 builtin/config: pull out function to handle config locationPatrick Steinhardt, May 6, 2024
  101. 06/14 builtin/config: pull out function to handle `--null`Patrick Steinhardt, May 6, 2024
  102. 07/14 builtin/config: introduce "list" subcommandPatrick Steinhardt, May 6, 2024
  103. 08/14 builtin/config: introduce "get" subcommandPatrick Steinhardt, May 6, 2024
  104. SZEDER GáborAug 3, 2025
  105. Junio C HamanoAug 3, 2025
  106. Patrick SteinhardtSep 11, 2025
  107. 09/14 builtin/config: introduce "set" subcommandPatrick Steinhardt, May 6, 2024
  108. 10/14 builtin/config: introduce "unset" subcommandPatrick Steinhardt, May 6, 2024
  109. 11/14 builtin/config: introduce "rename-section" subcommandPatrick Steinhardt, May 6, 2024
  110. 12/14 builtin/config: introduce "remove-section" subcommandPatrick Steinhardt, May 6, 2024
  111. 13/14 builtin/config: introduce "edit" subcommandPatrick Steinhardt, May 6, 2024
  112. 14/14 builtin/config: display subcommand helpPatrick Steinhardt, May 6, 2024
  113. Karthik NayakMay 6, 2024
  114. Taylor BlauMay 6, 2024
  115. rsbecker@nexbridge.comMay 6, 2024
  116. Patrick SteinhardtMay 7, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.