From patchwork Tue Oct 31 08:16:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13441178 Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (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 3FBAE111B3 for ; Tue, 31 Oct 2023 08:16:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="OJaQHHHE"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="ThxDaX+k" Received: from wout3-smtp.messagingengine.com (wout3-smtp.messagingengine.com [64.147.123.19]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AF469C9 for ; Tue, 31 Oct 2023 01:16:16 -0700 (PDT) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id E57633200A6D; Tue, 31 Oct 2023 04:16:15 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Tue, 31 Oct 2023 04:16:16 -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:sender :subject:subject:to:to; s=fm3; t=1698740175; x=1698826575; bh=75 HwvSBQJ9vj6977D+UrNhePVdnw81ZJZlilm+JMNLg=; b=OJaQHHHE+RuIGgZwmj 6/7JKvwu4j4SeuFkDhcE+B0hTLvhnEvXYtqBL6puSS/NQ95bMt3dMgrHPvBSLwNn 1HzMhTKjVOhaG+4m9nGj2NJqD2OSuhwAJ1WNuhh+duWiqjKrCEkPp8yulTcr48Vf tp+OKmGwaaLURkeYxI0Dy+6RoQQyGuFfVxpWfKiRihu6MQuKCoQXqHgQjyn7lamC d3rcB6haOJxM3xnXpuTUDJ2zZZqA0NNq0+U4Wbq9CnWute+bsY94dUmj9q9kLUGw 3riiuQs4gZjbcxweW8gEGYfrd2545O6u1TRT7jC7OwXaMScCA16F2ICaSkDD+9Qp KXKg== 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:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; t=1698740175; x=1698826575; bh=75HwvSBQJ9vj6 977D+UrNhePVdnw81ZJZlilm+JMNLg=; b=ThxDaX+kHZDLabjr1cTKcFMQxyvG9 1I0nWLuI1ms3ada+vzEJKMhN3aigtyP2GgrcdOEcH2Ri278CdQLAZs7Yd/RkI3NW rDYf7NXAUqn4ZUuENxofL/5O3VvVuRDO1jl/I5PoHdQAeEy7TDCKsNkIJMCx8eFB HpauwcT3tZSUxHeUfxVkn1ovXQ33BrUzXptW6rTgNRVdyYo8cGxq8bkCHhATzz2N Z3ipBLguvR4H01m6oXlc72hDH86ipliHUM+/05QlEPuZhhKiMF4QNCyuQzuJ8Iu8 vW4Ac/QP4aDfjRnNa288Gms/etVzYC3jpPlqpBnkXZMJgwtnwG3S2PSKA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedruddtuddgudduhecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfhfgggtuggjsehgtderredttddvnecuhfhrohhmpefrrght rhhitghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqeenucggtffrrghtth gvrhhnpeeukedtvedtffevleejtefgheehieegkeeluddvfeefgeehgfeltddtheejleff teenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehpsh esphhkshdrihhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 31 Oct 2023 04:16:14 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id a02d7f02 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 31 Oct 2023 08:16:07 +0000 (UTC) Date: Tue, 31 Oct 2023 09:16:12 +0100 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Taylor Blau , Junio C Hamano , Eric Sunshine , Han-Wen Nienhuys Subject: [PATCH v3 01/12] builtin/show-ref: convert pattern to a local variable Message-ID: <9570ad639244066802958ea52069ffcad05959ce.1698739941.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: The `pattern` variable is a global variable that tracks either the reference names (not patterns!) for the `--verify` mode or the patterns for the non-verify mode. This is a bit confusing due to the slightly different meanings. Convert the variable to be local. While this does not yet fix the double meaning of the variable, this change allows us to address it in a subsequent patch more easily by explicitly splitting up the different subcommands of git-show-ref(1). Note that we introduce a `struct show_ref_data` to pass the patterns to `show_ref()`. While this is overengineered now, we will extend this structure in a subsequent patch. Signed-off-by: Patrick Steinhardt --- builtin/show-ref.c | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/builtin/show-ref.c b/builtin/show-ref.c index 5110814f796..7efab14b96c 100644 --- a/builtin/show-ref.c +++ b/builtin/show-ref.c @@ -20,7 +20,6 @@ static const char * const show_ref_usage[] = { static int deref_tags, show_head, tags_only, heads_only, found_match, verify, quiet, hash_only, abbrev, exclude_arg; -static const char **pattern; static const char *exclude_existing_arg; static void show_one(const char *refname, const struct object_id *oid) @@ -50,15 +49,21 @@ static void show_one(const char *refname, const struct object_id *oid) } } +struct show_ref_data { + const char **patterns; +}; + static int show_ref(const char *refname, const struct object_id *oid, - int flag UNUSED, void *cbdata UNUSED) + int flag UNUSED, void *cbdata) { + struct show_ref_data *data = cbdata; + if (show_head && !strcmp(refname, "HEAD")) goto match; - if (pattern) { + if (data->patterns) { int reflen = strlen(refname); - const char **p = pattern, *m; + const char **p = data->patterns, *m; while ((m = *p++) != NULL) { int len = strlen(m); if (len > reflen) @@ -180,6 +185,9 @@ static const struct option show_ref_options[] = { int cmd_show_ref(int argc, const char **argv, const char *prefix) { + struct show_ref_data show_ref_data = {0}; + const char **patterns; + git_config(git_default_config, NULL); argc = parse_options(argc, argv, prefix, show_ref_options, @@ -188,38 +196,40 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix) if (exclude_arg) return exclude_existing(exclude_existing_arg); - pattern = argv; - if (!*pattern) - pattern = NULL; + patterns = argv; + if (!*patterns) + patterns = NULL; if (verify) { - if (!pattern) + if (!patterns) die("--verify requires a reference"); - while (*pattern) { + while (*patterns) { struct object_id oid; - if ((starts_with(*pattern, "refs/") || !strcmp(*pattern, "HEAD")) && - !read_ref(*pattern, &oid)) { - show_one(*pattern, &oid); + if ((starts_with(*patterns, "refs/") || !strcmp(*patterns, "HEAD")) && + !read_ref(*patterns, &oid)) { + show_one(*patterns, &oid); } else if (!quiet) - die("'%s' - not a valid ref", *pattern); + die("'%s' - not a valid ref", *patterns); else return 1; - pattern++; + patterns++; } return 0; } + show_ref_data.patterns = patterns; + if (show_head) - head_ref(show_ref, NULL); + head_ref(show_ref, &show_ref_data); if (heads_only || tags_only) { if (heads_only) - for_each_fullref_in("refs/heads/", show_ref, NULL); + for_each_fullref_in("refs/heads/", show_ref, &show_ref_data); if (tags_only) - for_each_fullref_in("refs/tags/", show_ref, NULL); + for_each_fullref_in("refs/tags/", show_ref, &show_ref_data); } else { - for_each_ref(show_ref, NULL); + for_each_ref(show_ref, &show_ref_data); } if (!found_match) { if (verify && !quiet)