From patchwork Mon May 13 10:22:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13663258 Received: from wfhigh6-smtp.messagingengine.com (wfhigh6-smtp.messagingengine.com [64.147.123.157]) (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 0E4AA146D76 for ; Mon, 13 May 2024 10:22:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.123.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715595734; cv=none; b=KsBu3a6UUUoghf7dJnBnaXDamMZHROhzsVsR21UZX0SgzZFMTAVhm7RylUkT63xrgWzLZOlYLQNCP+226K97DIVzE5VJftleh0SLGRXqsehXEzFikOSOpJMAd5GT6B69pPhOvst6TrKjhOkLYrGfqh164S/18TJkIHOMjq8PjPk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715595734; c=relaxed/simple; bh=xWJOfc864Bk7vru1mAPK8FXw8+h9n45qB4ToOyJz/A4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=rtOYPkd5du6hyyh0J4QNO3G41htj8MvlINwotsKcc86LiUTOXIYTZ3WwR4yqjMKX1b8RivcolMrPbrOyRox5by+qLCwNIRRZVmji843jaRBYmMrZmHez2AFGTOc+1fd6ZqO2SYbVkI16oH7mXDAfxjVdJmdMO5GQBLRdtQwRG9Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject 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=LO3h1cRn; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=KkaPkKBK; arc=none smtp.client-ip=64.147.123.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject 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="LO3h1cRn"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="KkaPkKBK" Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailfhigh.west.internal (Postfix) with ESMTP id 1834B1800076; Mon, 13 May 2024 06:22:12 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Mon, 13 May 2024 06:22:12 -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=1715595731; x=1715682131; bh=1q8INptvvm EodmPr7DQ/PNMt/0rC/CZE0a1/eh86yfk=; b=LO3h1cRnIZ2ZxEB0MPn4v0jt34 NW2DNz8Rg0gtfbGMnPlg34OhY+vtkvxezPLQwdSbhsqamb801Gpnuy9YUCy/u/lK mddpl0K705FTVM4qKl/qB2mTq65TefeYdETTf4qN0C/2hVog05vIKUfUxuGATrWP GWO59OnSPo2dAu7tU4Pm0ZaD026DCC0nUHAypidMDnT6EWqeFHPn4fy/wRUiMelg gBvmCuRhJ+Y8y53dlS0yEiTSWsY4KhmWUN5Mccx1oYO6KTfvofWvoP8gC4h0fLin NDrgF9WmVzN/7VB6TF2JX7Qn2vZOSTB79WB9dMuDbmTXdR56yjxaa+HPK11g== 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=1715595731; x=1715682131; bh=1q8INptvvmEodmPr7DQ/PNMt/0rC /CZE0a1/eh86yfk=; b=KkaPkKBKy7L4Xa7BNTd5a6jn7m4FOkg5mpNIlPfiG4sd 7a1YLsSy36he9cy2jRs43aqsToK//Nggfkpcad6SLjwNVazZAOtjG7W+sJofa2lc bGuPMA8PDNistcBwLFhk2dHC8sIUWpCdH9YGnJowbkoZzKFAMq9Yb4U0zq+krS21 AtSd9Oeit0byiPCV9xfIoFbeQgZhZEaXaGB042cNvl0DVVEERqIdNVoOKU/9oRuo B91pyVXiLjnkvY96qo8op2culcz2eaYYQwrnNsJEFd7o5EON2SPhdOpS4BrthDKQ /2vXXzijrOjSiou958kgOsJQ9KwHkds50yl7tpI0wA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdeggedgvdeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 13 May 2024 06:22:10 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 58c8a9ae (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Mon, 13 May 2024 10:21:52 +0000 (UTC) Date: Mon, 13 May 2024 12:22:08 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , Kyle Lippincott , Eric Sunshine Subject: [PATCH v2 02/21] builtin/config: move legacy mode into its own function Message-ID: <663e1f74f82b8c1acdd0c35ad87736c071eafb33.1715595550.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: In `cmd_config()` we first try to parse the provided arguments as subcommands and, if this is successful, call the respective functions of that subcommand. Otherwise we continue with the "legacy" mode that uses implicit actions and/or flags. Disentangle this by moving the legacy mode into its own function. This allows us to move the options into the respective functions and clearly separates concerns. Signed-off-by: Patrick Steinhardt --- builtin/config.c | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/builtin/config.c b/builtin/config.c index 3a71d3253f..f6c7e7a082 100644 --- a/builtin/config.c +++ b/builtin/config.c @@ -1069,31 +1069,13 @@ static struct option builtin_subcommand_options[] = { OPT_END(), }; -int cmd_config(int argc, const char **argv, const char *prefix) +static int cmd_config_actions(int argc, const char **argv, const char *prefix) { char *value = NULL, *comment = NULL; int flags = 0; int ret = 0; struct key_value_info default_kvi = KVI_INIT; - given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT)); - - /* - * This is somewhat hacky: we first parse the command line while - * keeping all args intact in order to determine whether a subcommand - * has been specified. If so, we re-parse it a second time, but this - * time we drop KEEP_ARGV0. This is so that we don't munge the command - * line in case no subcommand was given, which would otherwise confuse - * us when parsing the legacy-style modes that don't use subcommands. - */ - argc = parse_options(argc, argv, prefix, builtin_subcommand_options, builtin_config_usage, - PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_ARGV0|PARSE_OPT_KEEP_UNKNOWN_OPT); - if (subcommand) { - argc = parse_options(argc, argv, prefix, builtin_subcommand_options, builtin_config_usage, - PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_UNKNOWN_OPT); - return subcommand(argc, argv, prefix); - } - argc = parse_options(argc, argv, prefix, builtin_config_options, builtin_config_usage, PARSE_OPT_STOP_AT_NON_OPTION); @@ -1306,3 +1288,26 @@ int cmd_config(int argc, const char **argv, const char *prefix) free(value); return ret; } + +int cmd_config(int argc, const char **argv, const char *prefix) +{ + given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT)); + + /* + * This is somewhat hacky: we first parse the command line while + * keeping all args intact in order to determine whether a subcommand + * has been specified. If so, we re-parse it a second time, but this + * time we drop KEEP_ARGV0. This is so that we don't munge the command + * line in case no subcommand was given, which would otherwise confuse + * us when parsing the legacy-style modes that don't use subcommands. + */ + argc = parse_options(argc, argv, prefix, builtin_subcommand_options, builtin_config_usage, + PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_ARGV0|PARSE_OPT_KEEP_UNKNOWN_OPT); + if (subcommand) { + argc = parse_options(argc, argv, prefix, builtin_subcommand_options, builtin_config_usage, + PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_UNKNOWN_OPT); + return subcommand(argc, argv, prefix); + } + + return cmd_config_actions(argc, argv, prefix); +}