git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Patrick Steinhardt <ps@pks.im>
To: git@vger.kernel.org
Cc: "Kristoffer Haugsbakk" <code@khaugsbakk.name>,
	"Taylor Blau" <me@ttaylorr.com>,
	"Jean-Noël AVILA" <jn.avila@free.fr>,
	"Eric Sunshine" <sunshine@sunshineco.com>,
	"Junio C Hamano" <gitster@pobox.com>
Subject: [PATCH v3 12/13] builtin/config: introduce "edit" subcommand
Date: Wed, 27 Mar 2024 09:46:59 +0100	[thread overview]
Message-ID: <4a6512c88af82013b8a08767515f5715af05c6bb.1711527812.git.ps@pks.im> (raw)
In-Reply-To: <cover.1711527811.git.ps@pks.im>

[-- Attachment #1: Type: text/plain, Size: 7247 bytes --]

Introduce a new "edit" 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 | 17 ++++----
 builtin/config.c             | 81 ++++++++++++++++++++++++------------
 t/t1300-config.sh            |  6 +--
 3 files changed, 68 insertions(+), 36 deletions(-)

diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt
index b9ca5e83a2..448bf43bb3 100644
--- a/Documentation/git-config.txt
+++ b/Documentation/git-config.txt
@@ -15,8 +15,8 @@ SYNOPSIS
 'git config unset' [<file-option>] [--all] [--value=<value>] [--fixed-value] <name> <value>
 'git config rename-section' [<file-option>] <old-name> <new-name>
 'git config remove-section' [<file-option>] <name>
+'git config edit' [<file-option>]
 'git config' [<file-option>] --get-colorbool <name> [<stdout-is-tty>]
-'git config' [<file-option>] -e | --edit
 
 DESCRIPTION
 -----------
@@ -98,6 +98,11 @@ rename-section::
 remove-section::
 	Remove the given section from the configuration file.
 
+edit::
+	Opens an editor to modify the specified config file; either
+	`--system`, `--global`, `--local` (default), `--worktree`, or
+	`--file <config-file>`.
+
 [[OPTIONS]]
 OPTIONS
 -------
@@ -262,12 +267,6 @@ Valid `<type>`'s include:
 	When the color setting for `name` is undefined, the command uses
 	`color.ui` as fallback.
 
--e::
---edit::
-	Opens an editor to modify the specified config file; either
-	`--system`, `--global`, `--local` (default), `--worktree`, or
-	`--file <config-file>`.
-
 --[no-]includes::
 	Respect `include.*` directives in config files when looking up
 	values. Defaults to `off` when a specific file is given (e.g.,
@@ -324,6 +323,10 @@ recommended to migrate to the new syntax.
 --remove-section <name>::
 	Replaced by `git config remove-section <name>`.
 
+-e::
+--edit::
+	Replaced by `git config edit`.
+
 CONFIGURATION
 -------------
 `pager.config` is only respected when listing configuration, i.e., when
diff --git a/builtin/config.c b/builtin/config.c
index 6be6717f26..b8cb940b1e 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -22,6 +22,7 @@ static const char *const builtin_config_usage[] = {
 	N_("git config unset [<file-option>] [--all] [--value=<value>] [--fixed-value] <name> <value>"),
 	N_("git config rename-section [<file-option>] <old-name> <new-name>"),
 	N_("git config remove-section [<file-option>] <name>"),
+	N_("git config edit [<file-option>]"),
 	NULL
 };
 
@@ -55,6 +56,11 @@ static const char *const builtin_config_remove_section_usage[] = {
 	NULL
 };
 
+static const char *const builtin_config_edit_usage[] = {
+	N_("git config edit [<file-option>]"),
+	NULL
+};
+
 static char *key;
 static regex_t *key_regexp;
 static const char *value_pattern;
@@ -1002,6 +1008,53 @@ static int cmd_config_remove_section(int argc, const char **argv, const char *pr
 	return 0;
 }
 
+static int show_editor(void)
+{
+	char *config_file;
+
+	if (!given_config_source.file && !startup_info->have_repository)
+		die(_("not in a git directory"));
+	if (given_config_source.use_stdin)
+		die(_("editing stdin is not supported"));
+	if (given_config_source.blob)
+		die(_("editing blobs is not supported"));
+	git_config(git_default_config, NULL);
+	config_file = given_config_source.file ?
+			xstrdup(given_config_source.file) :
+			git_pathdup("config");
+	if (use_global_config) {
+		int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
+		if (fd >= 0) {
+			char *content = default_user_config();
+			write_str_in_full(fd, content);
+			free(content);
+			close(fd);
+		}
+		else if (errno != EEXIST)
+			die_errno(_("cannot create configuration file %s"), config_file);
+	}
+	launch_editor(config_file, NULL, NULL);
+	free(config_file);
+
+	return 0;
+}
+
+static int cmd_config_edit(int argc, const char **argv, const char *prefix)
+{
+	struct option opts[] = {
+		CONFIG_LOCATION_OPTIONS,
+		OPT_END(),
+	};
+
+	argc = parse_options(argc, argv, prefix, opts, builtin_config_edit_usage, 0);
+	check_write();
+	check_argc(argc, 0, 0);
+
+	handle_config_location(prefix);
+
+	return show_editor();
+}
+
 static struct option builtin_subcommand_options[] = {
 	OPT_SUBCOMMAND("list", &subcommand, cmd_config_list),
 	OPT_SUBCOMMAND("get", &subcommand, cmd_config_get),
@@ -1009,6 +1062,7 @@ static struct option builtin_subcommand_options[] = {
 	OPT_SUBCOMMAND("unset", &subcommand, cmd_config_unset),
 	OPT_SUBCOMMAND("rename-section", &subcommand, cmd_config_rename_section),
 	OPT_SUBCOMMAND("remove-section", &subcommand, cmd_config_remove_section),
+	OPT_SUBCOMMAND("edit", &subcommand, cmd_config_edit),
 	OPT_END(),
 };
 
@@ -1127,32 +1181,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 		}
 	}
 	else if (actions == ACTION_EDIT) {
-		char *config_file;
-
-		check_argc(argc, 0, 0);
-		if (!given_config_source.file && !startup_info->have_repository)
-			die(_("not in a git directory"));
-		if (given_config_source.use_stdin)
-			die(_("editing stdin is not supported"));
-		if (given_config_source.blob)
-			die(_("editing blobs is not supported"));
-		git_config(git_default_config, NULL);
-		config_file = given_config_source.file ?
-				xstrdup(given_config_source.file) :
-				git_pathdup("config");
-		if (use_global_config) {
-			int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
-			if (fd >= 0) {
-				char *content = default_user_config();
-				write_str_in_full(fd, content);
-				free(content);
-				close(fd);
-			}
-			else if (errno != EEXIST)
-				die_errno(_("cannot create configuration file %s"), config_file);
-		}
-		launch_editor(config_file, NULL, NULL);
-		free(config_file);
+		ret = show_editor();
 	}
 	else if (actions == ACTION_SET) {
 		check_write();
diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index 2f8d0310b1..3049b61533 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -556,7 +556,7 @@ test_expect_success 'setting a value in stdin is an error' '
 '
 
 test_expect_success 'editing stdin is an error' '
-	test_must_fail git config --file - --edit
+	test_must_fail git config ${mode_prefix}edit --file -
 '
 
 test_expect_success 'refer config from subdirectory' '
@@ -1642,7 +1642,7 @@ test_expect_success 'command line overrides environment config' '
 test_expect_success 'git config --edit works' '
 	git config -f tmp test.value no &&
 	echo test.value=yes >expect &&
-	GIT_EDITOR="echo [test]value=yes >" git config -f tmp --edit &&
+	GIT_EDITOR="echo [test]value=yes >" git config ${mode_prefix}edit -f tmp &&
 	git config ${mode_prefix}list -f tmp >actual &&
 	test_cmp expect actual
 '
@@ -1651,7 +1651,7 @@ test_expect_success 'git config --edit respects core.editor' '
 	git config -f tmp test.value no &&
 	echo test.value=yes >expect &&
 	test_config core.editor "echo [test]value=yes >" &&
-	git config -f tmp --edit &&
+	git config ${mode_prefix}edit -f tmp &&
 	git config ${mode_prefix}list -f tmp >actual &&
 	test_cmp expect actual
 '
-- 
2.44.GIT


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  parent reply	other threads:[~2024-03-27  8:47 UTC|newest]

Thread overview: 113+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-06 11:31 [PATCH 0/8] builtin/config: introduce subcommands Patrick Steinhardt
2024-03-06 11:31 ` [PATCH 1/8] builtin/config: move option array around Patrick Steinhardt
2024-03-06 11:31 ` [PATCH 2/8] builtin/config: move "fixed-value" option to correct group Patrick Steinhardt
2024-03-06 11:31 ` [PATCH 3/8] builtin/config: use `OPT_CMDMODE()` to specify modes Patrick Steinhardt
2024-03-06 23:52   ` Taylor Blau
2024-03-07  7:02     ` Patrick Steinhardt
2024-03-06 11:31 ` [PATCH 4/8] builtin/config: move modes into separate functions Patrick Steinhardt
2024-03-06 11:31 ` [PATCH 5/8] builtin/config: track subcommands by action Patrick Steinhardt
2024-03-06 21:54   ` Jean-Noël AVILA
2024-03-07  6:37     ` Patrick Steinhardt
2024-03-07  0:10   ` Taylor Blau
2024-03-07  6:36     ` Patrick Steinhardt
2024-03-06 11:31 ` [PATCH 6/8] builtin/config: introduce subcommands Patrick Steinhardt
2024-03-06 21:38   ` Karthik Nayak
2024-03-07  7:14     ` Patrick Steinhardt
2024-03-06 11:31 ` [PATCH 7/8] t1300: exercise both old- and new-style modes Patrick Steinhardt
2024-03-06 11:32 ` [PATCH 8/8] Documentation/git-config: update to new-style syntax Patrick Steinhardt
2024-03-07  6:57   ` Eric Sunshine
2024-03-07  7:33     ` Patrick Steinhardt
2024-03-06 17:06 ` [PATCH 0/8] builtin/config: introduce subcommands Junio C Hamano
2024-03-06 23:46   ` Taylor Blau
2024-03-06 23:52     ` Junio C Hamano
2024-03-07  0:13       ` Taylor Blau
2024-03-07  0:31     ` Dragan Simic
2024-03-07  6:31     ` Patrick Steinhardt
2024-03-07 13:22       ` Junio C Hamano
2024-03-06 22:49 ` Kristoffer Haugsbakk
2024-03-11 23:19 ` [PATCH v2 00/13] " Patrick Steinhardt
2024-03-11 23:19   ` [PATCH v2 01/13] builtin/config: move option array around Patrick Steinhardt
2024-03-11 23:19   ` [PATCH v2 02/13] builtin/config: move "fixed-value" option to correct group Patrick Steinhardt
2024-03-11 23:20   ` [PATCH v2 03/13] builtin/config: use `OPT_CMDMODE()` to specify modes Patrick Steinhardt
2024-03-11 23:20   ` [PATCH v2 04/13] builtin/config: pull out function to handle config location Patrick Steinhardt
2024-03-11 23:20   ` [PATCH v2 05/13] builtin/config: pull out function to handle `--null` Patrick Steinhardt
2024-03-11 23:20   ` [PATCH v2 06/13] builtin/config: introduce "list" subcommand Patrick Steinhardt
2024-03-13  2:45     ` Eric Sunshine
2024-03-27  8:42       ` Patrick Steinhardt
2024-03-11 23:20   ` [PATCH v2 07/13] builtin/config: introduce "get" subcommand Patrick Steinhardt
2024-03-13  3:11     ` Eric Sunshine
2024-03-27  8:42       ` Patrick Steinhardt
2024-03-11 23:20   ` [PATCH v2 08/13] builtin/config: introduce "set" subcommand Patrick Steinhardt
2024-03-11 23:21   ` [PATCH v2 09/13] builtin/config: introduce "unset" subcommand Patrick Steinhardt
2024-03-11 23:21   ` [PATCH v2 10/13] builtin/config: introduce "rename-section" subcommand Patrick Steinhardt
2024-03-11 23:21   ` [PATCH v2 11/13] builtin/config: introduce "remove-section" subcommand Patrick Steinhardt
2024-03-11 23:21   ` [PATCH v2 12/13] builtin/config: introduce "edit" subcommand Patrick Steinhardt
2024-03-11 23:21   ` [PATCH v2 13/13] builtin/config: display subcommand help Patrick Steinhardt
2024-03-27  8:46 ` [PATCH v3 00/13] builtin/config: introduce subcommands Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 01/13] builtin/config: move option array around Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 02/13] builtin/config: move "fixed-value" option to correct group Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 03/13] builtin/config: use `OPT_CMDMODE()` to specify modes Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 04/13] builtin/config: pull out function to handle config location Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 05/13] builtin/config: pull out function to handle `--null` Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 06/13] builtin/config: introduce "list" subcommand Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 07/13] builtin/config: introduce "get" subcommand Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 08/13] builtin/config: introduce "set" subcommand Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 09/13] builtin/config: introduce "unset" subcommand Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 10/13] builtin/config: introduce "rename-section" subcommand Patrick Steinhardt
2024-03-27  8:46   ` [PATCH v3 11/13] builtin/config: introduce "remove-section" subcommand Patrick Steinhardt
2024-03-27  8:46   ` Patrick Steinhardt [this message]
2024-03-27  8:47   ` [PATCH v3 13/13] builtin/config: display subcommand help Patrick Steinhardt
2024-03-27  8:53   ` [PATCH v3 00/13] builtin/config: introduce subcommands Eric Sunshine
2024-03-27  9:16     ` Patrick Steinhardt
2024-05-03  9:56 ` [PATCH v4 00/14] " Patrick Steinhardt
2024-05-03  9:56   ` [PATCH v4 01/14] config: clarify memory ownership when preparing comment strings Patrick Steinhardt
2024-05-03 10:13     ` Kristoffer Haugsbakk
2024-05-03  9:56   ` [PATCH v4 02/14] builtin/config: move option array around Patrick Steinhardt
2024-05-03  9:56   ` [PATCH v4 03/14] builtin/config: move "fixed-value" option to correct group Patrick Steinhardt
2024-05-03 12:28     ` Karthik Nayak
2024-05-06  9:34       ` Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 04/14] builtin/config: use `OPT_CMDMODE()` to specify modes Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 05/14] builtin/config: pull out function to handle config location Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 06/14] builtin/config: pull out function to handle `--null` Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 07/14] builtin/config: introduce "list" subcommand Patrick Steinhardt
2024-05-03 13:08     ` Karthik Nayak
2024-05-03 13:13       ` rsbecker
2024-05-03 16:01         ` Junio C Hamano
2024-05-06  7:51           ` Patrick Steinhardt
2024-05-06 17:13             ` Junio C Hamano
2024-05-06 18:33               ` rsbecker
2024-05-06 18:45                 ` Dragan Simic
2024-05-07  6:20                 ` Kristoffer Haugsbakk
2024-05-06 21:33               ` Git 3.0? Junio C Hamano
2024-05-07  4:18                 ` Patrick Steinhardt
2024-05-07  4:02               ` [PATCH v4 07/14] builtin/config: introduce "list" subcommand Patrick Steinhardt
2024-05-06  7:58       ` Patrick Steinhardt
2024-05-06 11:26         ` Karthik Nayak
2024-05-03  9:57   ` [PATCH v4 08/14] builtin/config: introduce "get" subcommand Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 09/14] builtin/config: introduce "set" subcommand Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 10/14] builtin/config: introduce "unset" subcommand Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 11/14] builtin/config: introduce "rename-section" subcommand Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 12/14] builtin/config: introduce "remove-section" subcommand Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 13/14] builtin/config: introduce "edit" subcommand Patrick Steinhardt
2024-05-03  9:57   ` [PATCH v4 14/14] builtin/config: display subcommand help Patrick Steinhardt
2024-05-03 13:36   ` [PATCH v4 00/14] builtin/config: introduce subcommands Dragan Simic
2024-05-03 16:09   ` Junio C Hamano
2024-05-06  8:55 ` [PATCH v5 " Patrick Steinhardt
2024-05-06  8:55   ` [PATCH v5 01/14] config: clarify memory ownership when preparing comment strings Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 02/14] builtin/config: move option array around Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 03/14] builtin/config: move "fixed-value" option to correct group Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 04/14] builtin/config: use `OPT_CMDMODE()` to specify modes Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 05/14] builtin/config: pull out function to handle config location Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 06/14] builtin/config: pull out function to handle `--null` Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 07/14] builtin/config: introduce "list" subcommand Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 08/14] builtin/config: introduce "get" subcommand Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 09/14] builtin/config: introduce "set" subcommand Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 10/14] builtin/config: introduce "unset" subcommand Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 11/14] builtin/config: introduce "rename-section" subcommand Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 12/14] builtin/config: introduce "remove-section" subcommand Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 13/14] builtin/config: introduce "edit" subcommand Patrick Steinhardt
2024-05-06  8:56   ` [PATCH v5 14/14] builtin/config: display subcommand help Patrick Steinhardt
2024-05-06 11:30   ` [PATCH v5 00/14] builtin/config: introduce subcommands Karthik Nayak
2024-05-06 20:21   ` Taylor Blau
2024-05-06 20:38     ` rsbecker
2024-05-07  4:07       ` Patrick Steinhardt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4a6512c88af82013b8a08767515f5715af05c6bb.1711527812.git.ps@pks.im \
    --to=ps@pks.im \
    --cc=code@khaugsbakk.name \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jn.avila@free.fr \
    --cc=me@ttaylorr.com \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).