From patchwork Mon May 13 10:23:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13663276 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 9465114C588 for ; Mon, 13 May 2024 10:23:45 +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=1715595827; cv=none; b=Ln+kgKRdIZ+lyER/GRS6LDZipe9VgUSqrceGUatVs86RlWqqEzZPJKdEt4zsIx8sBeQgRydJRoTHyZnC1bWY8SJpQU9dKDonaAxZ2rMrja8viD2pc4xjllwWUJfgSXkjwkYqa5I5ooeBxBsliGUZdYaXBr075SI76fCLzaUG1xI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715595827; c=relaxed/simple; bh=nsIBHI3VrhrVls4sBT6lfQiJr7GZe2GYOvOJo9IOJRM=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=tQWCLvQCWAHe8I7PrMRfxi1h7RvcBkSFi/t3NzfXcoQdNvnSYtKdWFcCzWYTW8955YCZhAXzHTG+5e7hJ/3ZLUFlEQZTWJ5vh2inbA9TFq7djRydkMMcrMUF5ssa6DyCbVSIU/rBoCnwuVwUPQC3dLcYwBzXuAz5NbJc82NzDZ4= 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=dlFuCGE0; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=TVAp6nbz; arc=none smtp.client-ip=64.147.123.146 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="dlFuCGE0"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="TVAp6nbz" Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailfout.west.internal (Postfix) with ESMTP id E29791C000E9; Mon, 13 May 2024 06:23:44 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Mon, 13 May 2024 06:23:45 -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=1715595824; x=1715682224; bh=CoKr9B+tfN 7alSnQuVPW0COjLhKGVInyuSu+7G4om0A=; b=dlFuCGE0eficxcC/SMmbDgRPa0 JyAr45T6UZh6Id2+ceYbNTC4dHfAEplbGFGuLpCC3li6TsbzK6VOTIx8j2vBj5gK P9iymYzI3CJGR0IZvca1DeLKL61aZ/iU5TiK+NAPfWgw0HnvVAj6q+mcL7d9Vn20 Bv2wHfKmRrP6fBrsTmaVO4uhYBnXoadyhLN9JYIuYSyA0tlUUt/HAvmOjr1YKTj2 7/OsZUOd034SS+3As3PmDMYQJj2qmBVGuGaktS7hjHIReJ7P0QKL0uF7FzwFhPFD DzT8SUxLXw8gd12kn6jsRky2JeXee8+A2G7OQ7RnJUfXh+NjcXdDsk2jrROg== 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=1715595824; x=1715682224; bh=CoKr9B+tfN7alSnQuVPW0COjLhKG VInyuSu+7G4om0A=; b=TVAp6nbzWQMoaSKjqpVyE56L13Hkj2GPHnRZXe7me3x/ yH0mp6HRhU3EXCR5o8mrCfBEe76buk1J1cOvHRabH7R7JINx1xauGIz1wlP37VCV eUsOJDVjPWWQt2I2vcm1YK9KNG2WiIJsKEFxAhOhgGzXkcitNmXjfGxHkgg9b352 jV2q5NOiPxoX4HHzFYIcG7melBMDbBouZhFJsnquWXMffwxoLrqvyw2uSdJqnEYz MdbmIdVkZjIxuucMOVTj4z8d9qb+xsfBmNVacbh3MsJ9MjY8dG3r3EjeAtl4LQ49 TkjGUWzxtwh1vJ1v1MVkOWIi2MRvMmkrV6Irsg3MJw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdeggedgvdejucetufdoteggodetrfdotf 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:23:43 -0400 (EDT) Received: by localhost (OpenSMTPD) with ESMTPSA id 891f46fa (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Mon, 13 May 2024 10:23:24 +0000 (UTC) Date: Mon, 13 May 2024 12:23:40 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , Kyle Lippincott , Eric Sunshine Subject: [PATCH v2 20/21] builtin/config: convert flags to a local variable Message-ID: <294bcd96a4ec1d27e58a5faf22255eb60d303fc6.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: Both the `do_all` and `use_key_regexp` bits essentially act like flags to `get_value()`. Let's convert them to actual flags so that we can get rid of the last two remaining global variables that track options. Signed-off-by: Patrick Steinhardt --- builtin/config.c | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/builtin/config.c b/builtin/config.c index 2ca23a35d2..2ab7d24b62 100644 --- a/builtin/config.c +++ b/builtin/config.c @@ -122,9 +122,6 @@ struct config_display_options { .key_delim = ' ', \ } -static int use_key_regexp; -static int do_all; - #define TYPE_BOOL 1 #define TYPE_INT 2 #define TYPE_BOOL_OR_INT 3 @@ -318,6 +315,9 @@ static int format_config(const struct config_display_options *opts, return 0; } +#define GET_VALUE_ALL (1 << 0) +#define GET_VALUE_KEY_REGEXP (1 << 1) + struct collect_config_data { const struct config_display_options *display_opts; struct strbuf_list *values; @@ -326,6 +326,7 @@ struct collect_config_data { regex_t *regexp; regex_t *key_regexp; int do_not_match; + unsigned get_value_flags; unsigned flags; }; @@ -336,9 +337,11 @@ static int collect_config(const char *key_, const char *value_, struct strbuf_list *values = data->values; const struct key_value_info *kvi = ctx->kvi; - if (!use_key_regexp && strcmp(key_, data->key)) + if (!(data->get_value_flags & GET_VALUE_KEY_REGEXP) && + strcmp(key_, data->key)) return 0; - if (use_key_regexp && regexec(data->key_regexp, key_, 0, NULL, 0)) + if ((data->get_value_flags & GET_VALUE_KEY_REGEXP) && + regexec(data->key_regexp, key_, 0, NULL, 0)) return 0; if ((data->flags & CONFIG_FLAGS_FIXED_VALUE) && strcmp(data->value_pattern, (value_?value_:""))) @@ -356,19 +359,21 @@ static int collect_config(const char *key_, const char *value_, static int get_value(const struct config_location_options *opts, const struct config_display_options *display_opts, - const char *key_, const char *regex_, unsigned flags) + const char *key_, const char *regex_, + unsigned get_value_flags, unsigned flags) { int ret = CONFIG_GENERIC_ERROR; struct strbuf_list values = {NULL}; struct collect_config_data data = { .display_opts = display_opts, .values = &values, + .get_value_flags = get_value_flags, .flags = flags, }; char *key = NULL; int i; - if (use_key_regexp) { + if (get_value_flags & GET_VALUE_KEY_REGEXP) { char *tl; /* @@ -440,7 +445,7 @@ static int get_value(const struct config_location_options *opts, for (i = 0; i < values.nr; i++) { struct strbuf *buf = values.items + i; - if (do_all || i == values.nr - 1) + if ((get_value_flags & GET_VALUE_ALL) || i == values.nr - 1) fwrite(buf->buf, 1, buf->len, stdout); strbuf_release(buf); } @@ -846,11 +851,12 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix) struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT; const char *value_pattern = NULL, *url = NULL; int flags = 0; + unsigned get_value_flags = 0; struct option opts[] = { CONFIG_LOCATION_OPTIONS(location_opts), OPT_GROUP(N_("Filter options")), - OPT_BOOL(0, "all", &do_all, N_("return all values for multi-valued config options")), - OPT_BOOL(0, "regexp", &use_key_regexp, N_("interpret the name as a regular expression")), + OPT_BIT(0, "all", &get_value_flags, N_("return all values for multi-valued config options"), GET_VALUE_ALL), + OPT_BIT(0, "regexp", &get_value_flags, N_("interpret the name as a regular expression"), GET_VALUE_KEY_REGEXP), OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")), OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE), OPT_STRING(0, "url", &url, N_("URL"), N_("show config matching the given URL")), @@ -870,9 +876,12 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix) if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern) die(_("--fixed-value only applies with 'value-pattern'")); - if (display_opts.default_value && (do_all || url)) + if (display_opts.default_value && + ((get_value_flags & GET_VALUE_ALL) || url)) die(_("--default= cannot be used with --all or --url=")); - if (url && (do_all || use_key_regexp || value_pattern)) + if (url && ((get_value_flags & GET_VALUE_ALL) || + (get_value_flags & GET_VALUE_KEY_REGEXP) || + value_pattern)) die(_("--url= cannot be used with --all, --regexp or --value")); location_options_init(&location_opts, prefix); @@ -883,7 +892,8 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix) if (url) ret = get_urlmatch(&location_opts, &display_opts, argv[0], url); else - ret = get_value(&location_opts, &display_opts, argv[0], value_pattern, flags); + ret = get_value(&location_opts, &display_opts, argv[0], value_pattern, + get_value_flags, flags); location_options_release(&location_opts); return ret; @@ -1288,19 +1298,19 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix) } else if (actions == ACTION_GET) { check_argc(argc, 1, 2); - ret = get_value(&location_opts, &display_opts, argv[0], argv[1], flags); + ret = get_value(&location_opts, &display_opts, argv[0], argv[1], + 0, flags); } else if (actions == ACTION_GET_ALL) { - do_all = 1; check_argc(argc, 1, 2); - ret = get_value(&location_opts, &display_opts, argv[0], argv[1], flags); + ret = get_value(&location_opts, &display_opts, argv[0], argv[1], + GET_VALUE_ALL, flags); } else if (actions == ACTION_GET_REGEXP) { display_opts.show_keys = 1; - use_key_regexp = 1; - do_all = 1; check_argc(argc, 1, 2); - ret = get_value(&location_opts, &display_opts, argv[0], argv[1], flags); + ret = get_value(&location_opts, &display_opts, argv[0], argv[1], + GET_VALUE_ALL|GET_VALUE_KEY_REGEXP, flags); } else if (actions == ACTION_GET_URLMATCH) { check_argc(argc, 2, 2);