From patchwork Tue Oct 24 13:10:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13434436 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 6154929404 for ; Tue, 24 Oct 2023 13:10:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="KejSKgq0"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="Yf5bZ1Ah" Received: from wout4-smtp.messagingengine.com (wout4-smtp.messagingengine.com [64.147.123.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C68510F7 for ; Tue, 24 Oct 2023 06:10:45 -0700 (PDT) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.west.internal (Postfix) with ESMTP id 3BF4B3200A27; Tue, 24 Oct 2023 09:10:44 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Tue, 24 Oct 2023 09:10:44 -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=1698153043; x=1698239443; bh=4k 7MKiB3q6wYj5DiWFe3VjKUx/nnZuUeoUeNiE2DEms=; b=KejSKgq0uDk3KRR9yL qxdzhq2VJvPXGDY/LI3mU9ToxU6QHCJsSUU4QB2aTyspJLi9dUV0ep2FmO5BuqVA oDIrYzAps6uTw9nPBPHVHOlYptno68ViLxq9iQ586J4zXuvMzVG1bl/FwZQ9wxWN /MIBRJDwqGyJgWNmV6OHh4xar8tqzCr6XuoGlOqPDk6igv5ehaKMsLz7BTNAUa5T a/W6lR8P1OOw50/ZSgDd3/gWZZhaFWgg47n5MIQ+beHcv+iAjkGrLxlV7rJTjtjM x3A7APUU7D+GAVT9pRLpPFcnC5RCnmyZmkZmlm42OJgQhU9l07gCHzUBrTa6L8jC ZpOA== 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=1698153043; x=1698239443; bh=4k7MKiB3q6wYj 5DiWFe3VjKUx/nnZuUeoUeNiE2DEms=; b=Yf5bZ1AhG+pYixPzDKbX7gQiOWidn Udq3QkSbGV0A8CzjhlAo8B3+Qo0q4z6ExQaTdSRiT2ZZTVNAeHQkw+6s3ThnWzUW OQUaAfHVITL71X7zzIZnFG/OmReOOPI/9U2Qau0xZqsSBiEczBFjoWPt7dZgXDhj IcTTPpXDjUqaelFwZQdEUKDp5mMN2ZCO9hT4zk8wl5NchifVIKkMOdeCvRZeVUwD lzwD+THxWgIXjTOzvPoXXpUfYsfzZ8slp3ibuH9u4CglbuFsVRRg7pl5lvn6A9+0 KSNn4sWuKg+qa8PQ4kj6t7mcWUuj7EAUO+LtiKlBfCZfS3yokmyKJpfOg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrkeekgdeitdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvvefukfhfgggtuggjsehgtderredttddvnecuhfhrohhmpefrrghtrhhi tghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqeenucggtffrrghtthgvrh hnpeeukedtvedtffevleejtefgheehieegkeeluddvfeefgeehgfeltddtheejleffteen ucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehpshesph hkshdrihhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 24 Oct 2023 09:10:42 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id d377ceae (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Tue, 24 Oct 2023 13:10:37 +0000 (UTC) Date: Tue, 24 Oct 2023 15:10:40 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , Eric Sunshine , Han-Wen Nienhuys Subject: [PATCH 01/12] builtin/show-ref: convert pattern to a local variable Message-ID: <78163accbd2c39721249474512dedd8ed2f0fe4e.1698152926.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)