From patchwork Thu Oct 26 09:56:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13437415 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 5859E2B75D for ; Thu, 26 Oct 2023 09:56:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="b8AkkUeE"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="rjlXycbu" Received: from wout2-smtp.messagingengine.com (wout2-smtp.messagingengine.com [64.147.123.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CF8DA1A7 for ; Thu, 26 Oct 2023 02:56:25 -0700 (PDT) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id E5B543200928; Thu, 26 Oct 2023 05:56:24 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Thu, 26 Oct 2023 05:56:25 -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=1698314184; x=1698400584; bh=yL uvweuH0oMfdASyQV703Khqqv4zEuGn5mmt7rB7+Sw=; b=b8AkkUeEmb8q3WRMNE mmDnW3shM2V8W3TW7E/qbZ9qtbmbZ+kgGdjE3q6effgL4p1JHw7DqFsPWJxF0lSD RW96ne2x42+EYF2rKP9XGmh1eTth1jaVZgT03jMmcjqrCura1zD614ohipcuD2gJ NeniwJeK1WkdT7+SUiYwT4TB2YFDgAU21u/rWVoz0HfOQ4gA64V9fW/0WwpEk8WN EqJ3PNY5WKZQqlY7fqOMD6NdFJEbMwje4cmkPIXprzdAYcV67sVZvTvu3F+n740o tKqII9+oJHm+Mw8XZaeoz/fYnz1qvispIUNoCbHjmxYzsCDdXqfZ/4txnZVXfAHP UzeQ== 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=1698314184; x=1698400584; bh=yLuvweuH0oMfd ASyQV703Khqqv4zEuGn5mmt7rB7+Sw=; b=rjlXycbuOT1DqGO2MFVK5p46nQ/tw mb7+Q/3BadrYXwW1a5wJIgIPlyHFqYFRtFbI0BlQnkF7nGxMgPP+fCdh96z39Gqv K7WJNHnXH2V3QeiOhXp/UNSU1KDXO78zzcT5Kks7GQWteKtZPJtP/8bdPtgKZufv z05UvKH3cOUDr42aIxK2z1yrZIi7yueGKYTqQkMu6KsU0Sw/NVDWB9UIcQKWoHOf ZFklgGHNBMUsZb/9kZy9HGIyUcufL6a6jXRD7xyG4TRwIFpPHoljRqSgH2/gTb9b OVmknJPQxCFfxegOVq+Rz40oWt2aU9WlRkNKB32tlBiTV446oLClzA0sg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrledvgddvvdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvvefukfhfgggtuggjsehgtderredttddvnecuhfhrohhmpefrrghtrhhi tghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqeenucggtffrrghtthgvrh hnpeeukedtvedtffevleejtefgheehieegkeeluddvfeefgeehgfeltddtheejleffteen ucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehpshesph hkshdrihhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 26 Oct 2023 05:56:23 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 084f9a37 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 26 Oct 2023 09:56:14 +0000 (UTC) Date: Thu, 26 Oct 2023 11:56:21 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , Eric Sunshine , Han-Wen Nienhuys Subject: [PATCH v2 01/12] builtin/show-ref: convert pattern to a local variable Message-ID: <78163accbd2c39721249474512dedd8ed2f0fe4e.1698314128.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)