From patchwork Mon May 6 08:56:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13655183 Received: from wfout3-smtp.messagingengine.com (wfout3-smtp.messagingengine.com [64.147.123.146]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A30E41422A7 for ; Mon, 6 May 2024 08:56:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.123.146 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985820; cv=none; b=oCW6zAIEYz7oaeOGu7lHVEsGqwHqUj0iYrPcOeWpbpGjYiatHTPeHOkNt6RWdycfTJQqi7+NR6dY0At4jLFNVLctn9U7vlZIDCFgLVuapvuqPHJv0dgF1tZrfwj+4WjJvSHv7JlNL52KdmaCI4FhgV3OTSLFgyLssW3B9e6x+bY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714985820; c=relaxed/simple; bh=O6S9J94zeCk05aeNBKP3r0L01W0EWtZYt3fBwxiB4zk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=lmN1CI/v27Pyerh/2c0ZHCyJIygIXBy0f4bF9V2cInFF/fN0Tf6GjaF9fGkkNi/ROjlvSC3IHn1iWmJJgnPvm+popy1LLF2pPtu3V89RsDMeKCYkYWYy7QdciInID37bU3/2wOp9Mb750moLgmZBZsEcbgwSIP+NeDFVGVoDqY8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im; spf=pass smtp.mailfrom=pks.im; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b=Rhi4ksnf; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=b55OmuCy; arc=none smtp.client-ip=64.147.123.146 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pks.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pks.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="Rhi4ksnf"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="b55OmuCy" Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailfout.west.internal (Postfix) with ESMTP id 927181C0012E; Mon, 6 May 2024 04:56:57 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Mon, 06 May 2024 04:56:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pks.im; h=cc:cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1714985817; x=1715072217; bh=b7iJmiMKCl Coxu6dfljLANIrxXolpcvQRNPRmVVXYZA=; b=Rhi4ksnf12NPRnlk0tS80P6ZZ1 LBQJLQ7yuC7QoDUXnMA99T1iiwEj+3cWfaLvciH1Oh9ANtrx94N9sl8f1CYys62O OWXi62TU3c13eTNPxtHNJxbWiDBG5L7+78bCI+RPQWLzY5l9lJyN6lfYY57xbRvi KZyED3PEdq68ihKlwnqVZwKr89usxlH1AmHWUiZt8gTpRVBAphnAwb3/vbP5m7ah 7MrdcGJI6YEHkZkUrkY9opULCDTS1SuGsbbBWn8di9o68kZXq1t42taayDjpUris 2YLyEP93VAl1CWseDfDVpsjxqqFuO9eVM6D89D1M0s+cRJ/i47Es6ODq2H/g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1714985817; x=1715072217; bh=b7iJmiMKClCoxu6dfljLANIrxXol pcvQRNPRmVVXYZA=; b=b55OmuCyhpxtHSEK03/uv9z/yLkz5A47q6d/jF24/2hh RHuoIXTECWOEFd4VPQW5EiLSA503eZT8ynsaaZSsdi6YTfFavubm6rg48urwnsHF bQ5toNVYENnVti/kW3xItKpGukeEgzgYdz+3Z+ZHQtgECjhvWkWBb5P4wssCOPTO PWeA21m+U9cEASuET/zSdPu2hKHzmH595YSaL5fAGSylEzMFb6IvpBROuvDZ3TCW fsDpRWdVZO966veX6EsFwTXcfpVWy7fd5kTja58U0qQXJSls8QAWaCFk2Z6Z2g41 Dp3BFfiPEr+Mb4ypALOUz8MaddIBMr1DJ7i20LVjbg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvddviedguddtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 6 May 2024 04:56:55 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id d5c094d3 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Mon, 6 May 2024 08:56:49 +0000 (UTC) Date: Mon, 6 May 2024 10:56:52 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Kristoffer Haugsbakk , Taylor Blau , =?iso-8859-1?q?Jean-No=EBl?= AVILA , Eric Sunshine , Junio C Hamano , Dragan Simic , rsbecker@nexbridge.com Subject: [PATCH v5 13/14] builtin/config: introduce "edit" subcommand Message-ID: <1893c23afc982e1d442f50b5e4b8ab0073aaa05d.1714982328.git.ps@pks.im> References: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: 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 --- 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 ef46520c15..65c645d461 100644 --- a/Documentation/git-config.txt +++ b/Documentation/git-config.txt @@ -15,8 +15,8 @@ SYNOPSIS 'git config unset' [] [--all] [--value=] [--fixed-value] 'git config rename-section' [] 'git config remove-section' [] +'git config edit' [] 'git config' [] --get-colorbool [] -'git config' [] -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 `. + [[OPTIONS]] OPTIONS ------- @@ -274,12 +279,6 @@ Valid ``'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 `. - --[no-]includes:: Respect `include.*` directives in config files when looking up values. Defaults to `off` when a specific file is given (e.g., @@ -336,6 +335,10 @@ recommended to migrate to the new syntax. --remove-section :: Replaced by `git config remove-section `. +-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 cea03fb517..8f7fa8f31a 100644 --- a/builtin/config.c +++ b/builtin/config.c @@ -22,6 +22,7 @@ static const char *const builtin_config_usage[] = { N_("git config unset [] [--all] [--value=] [--fixed-value] "), N_("git config rename-section [] "), N_("git config remove-section [] "), + N_("git config edit []"), 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 []"), + NULL +}; + static char *key; static regex_t *key_regexp; static const char *value_pattern; @@ -1011,6 +1017,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), @@ -1018,6 +1071,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(), }; @@ -1144,32 +1198,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 9d71ec1260..f3c4d28e06 100755 --- a/t/t1300-config.sh +++ b/t/t1300-config.sh @@ -666,7 +666,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' ' @@ -1768,7 +1768,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 ' @@ -1777,7 +1777,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 '