From patchwork Thu Oct 26 09:56:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13437421 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 472EE2D033 for ; Thu, 26 Oct 2023 09:56:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=pks.im header.i=@pks.im header.b="CyBPzH5O"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="RSzC9eya" Received: from wout2-smtp.messagingengine.com (wout2-smtp.messagingengine.com [64.147.123.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0ADE118F for ; Thu, 26 Oct 2023 02:56:53 -0700 (PDT) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.west.internal (Postfix) with ESMTP id 35F9F32009EE; Thu, 26 Oct 2023 05:56:52 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Thu, 26 Oct 2023 05:56:52 -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=1698314211; x=1698400611; bh=fu 2FvTtyzOtBibpJyiVHRxHo+Vd+9SBwQ5fZS+M22yw=; b=CyBPzH5OfpoLTelSFi fHNrlukMOLorIbkUKjyEWLnQiA01YefEPvEcRlpoEI7JCXcNoF6ss7AKkVujYvXz 3C6wNEKyUeEchQFkuru/QPUPWEVB1xVDSiRdmAuvBTcpsImXIDmEVoYD5HsaKukb e8m9x+7CKL2N1I+/MQgupAoj88Ikz34SLJMDHZ5lwV50C8cC/VVNWDK4vDLdGQpw u1VLYCthDGhnUr1u+KwPcneAPgshsldDlHQbWccwRQ4O84gfXWU6aAc/NLr7mdur DhQAQlFnRjOZeQKKkotPWnH1lik6Rhy1snrCp/BnTO8rikhWyoTlKBdyUw3eNQ57 1RGw== 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=1698314211; x=1698400611; bh=fu2FvTtyzOtBi bpJyiVHRxHo+Vd+9SBwQ5fZS+M22yw=; b=RSzC9eya97WATvxe8Ou25jE83/3r7 im3MwQOBnafehOcHvpNCETOSpEN1YOkKe7whtC9y99x0GSqZB0DijNMNToaNj2/T M25zc7RQ841YEAp4eWhEa6uM4AmJns0oYACMpLnvkT7iiLQ43r8chRpVnyQ0MKpr YlevkWTftRckOBTBhB0hOrXRALdVlBq96xiJQUcw66TCH5nOcq8YGTtwk8VbgMNm htwVH45hFZpCRou+WywopxgQ8dxnZMexSU/oYtJBInLi8Ufj8auV0bWD3Tgx2d7e 9KG2VPOewC7H4FTDgHU58IhU5Hi1V2PM4z8sb7dLXywEPiqrHfXS6Mrhw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrledvgddvvdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvvefukfhfgggtuggjsehgtderredttddvnecuhfhrohhmpefrrghtrhhi tghkucfuthgvihhnhhgrrhguthcuoehpshesphhkshdrihhmqeenucggtffrrghtthgvrh hnpeeukedtvedtffevleejtefgheehieegkeeluddvfeefgeehgfeltddtheejleffteen ucevlhhushhtvghrufhiiigvpedvnecurfgrrhgrmhepmhgrihhlfhhrohhmpehpshesph hkshdrihhm X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 26 Oct 2023 05:56:50 -0400 (EDT) Received: by vm-mail (OpenSMTPD) with ESMTPSA id 38bf0267 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Thu, 26 Oct 2023 09:56:41 +0000 (UTC) Date: Thu, 26 Oct 2023 11:56:48 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , Eric Sunshine , Han-Wen Nienhuys Subject: [PATCH v2 07/12] builtin/show-ref: stop using global vars for `show_one()` Message-ID: <63f1dadf4c2e58439fde6489ee1ae33819f83462.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 `show_one()` function implicitly receives a bunch of options which are tracked via global variables. This makes it hard to see which subcommands of git-show-ref(1) actually make use of these options. Introduce a `show_one_options` structure that gets passed down to this function. This allows us to get rid of more global state and makes it more explicit which subcommands use those options. Signed-off-by: Patrick Steinhardt --- builtin/show-ref.c | 62 ++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/builtin/show-ref.c b/builtin/show-ref.c index c30c01785cc..3e6ad6b6a84 100644 --- a/builtin/show-ref.c +++ b/builtin/show-ref.c @@ -18,10 +18,17 @@ static const char * const show_ref_usage[] = { NULL }; -static int deref_tags, show_head, tags_only, heads_only, verify, - quiet, hash_only, abbrev; +static int show_head, tags_only, heads_only, verify; -static void show_one(const char *refname, const struct object_id *oid) +struct show_one_options { + int quiet; + int hash_only; + int abbrev; + int deref_tags; +}; + +static void show_one(const struct show_one_options *opts, + const char *refname, const struct object_id *oid) { const char *hex; struct object_id peeled; @@ -30,25 +37,26 @@ static void show_one(const char *refname, const struct object_id *oid) die("git show-ref: bad ref %s (%s)", refname, oid_to_hex(oid)); - if (quiet) + if (opts->quiet) return; - hex = repo_find_unique_abbrev(the_repository, oid, abbrev); - if (hash_only) + hex = repo_find_unique_abbrev(the_repository, oid, opts->abbrev); + if (opts->hash_only) printf("%s\n", hex); else printf("%s %s\n", hex, refname); - if (!deref_tags) + if (!opts->deref_tags) return; if (!peel_iterated_oid(oid, &peeled)) { - hex = repo_find_unique_abbrev(the_repository, &peeled, abbrev); + hex = repo_find_unique_abbrev(the_repository, &peeled, opts->abbrev); printf("%s %s^{}\n", hex, refname); } } struct show_ref_data { + const struct show_one_options *show_one_opts; const char **patterns; int found_match; }; @@ -81,7 +89,7 @@ static int show_ref(const char *refname, const struct object_id *oid, match: data->found_match++; - show_one(refname, oid); + show_one(data->show_one_opts, refname, oid); return 0; } @@ -149,7 +157,8 @@ static int cmd_show_ref__exclude_existing(const struct exclude_existing_options return 0; } -static int cmd_show_ref__verify(const char **refs) +static int cmd_show_ref__verify(const struct show_one_options *show_one_opts, + const char **refs) { if (!refs || !*refs) die("--verify requires a reference"); @@ -159,9 +168,9 @@ static int cmd_show_ref__verify(const char **refs) if ((starts_with(*refs, "refs/") || !strcmp(*refs, "HEAD")) && !read_ref(*refs, &oid)) { - show_one(*refs, &oid); + show_one(show_one_opts, *refs, &oid); } - else if (!quiet) + else if (!show_one_opts->quiet) die("'%s' - not a valid ref", *refs); else return 1; @@ -171,9 +180,12 @@ static int cmd_show_ref__verify(const char **refs) return 0; } -static int cmd_show_ref__patterns(const char **patterns) +static int cmd_show_ref__patterns(const struct show_one_options *show_one_opts, + const char **patterns) { - struct show_ref_data show_ref_data = {0}; + struct show_ref_data show_ref_data = { + .show_one_opts = show_one_opts, + }; if (patterns && *patterns) show_ref_data.patterns = patterns; @@ -196,11 +208,16 @@ static int cmd_show_ref__patterns(const char **patterns) static int hash_callback(const struct option *opt, const char *arg, int unset) { - hash_only = 1; + struct show_one_options *opts = opt->value; + struct option abbrev_opt = *opt; + + opts->hash_only = 1; /* Use full length SHA1 if no argument */ if (!arg) return 0; - return parse_opt_abbrev_cb(opt, arg, unset); + + abbrev_opt.value = &opts->abbrev; + return parse_opt_abbrev_cb(&abbrev_opt, arg, unset); } static int exclude_existing_callback(const struct option *opt, const char *arg, @@ -216,6 +233,7 @@ static int exclude_existing_callback(const struct option *opt, const char *arg, int cmd_show_ref(int argc, const char **argv, const char *prefix) { struct exclude_existing_options exclude_existing_opts = {0}; + struct show_one_options show_one_opts = {0}; const struct option show_ref_options[] = { OPT_BOOL(0, "tags", &tags_only, N_("only show tags (can be combined with heads)")), OPT_BOOL(0, "heads", &heads_only, N_("only show heads (can be combined with tags)")), @@ -225,13 +243,13 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix) N_("show the HEAD reference, even if it would be filtered out")), OPT_BOOL(0, "head", &show_head, N_("show the HEAD reference, even if it would be filtered out")), - OPT_BOOL('d', "dereference", &deref_tags, + OPT_BOOL('d', "dereference", &show_one_opts.deref_tags, N_("dereference tags into object IDs")), - OPT_CALLBACK_F('s', "hash", &abbrev, N_("n"), + OPT_CALLBACK_F('s', "hash", &show_one_opts, N_("n"), N_("only show SHA1 hash using digits"), PARSE_OPT_OPTARG, &hash_callback), - OPT__ABBREV(&abbrev), - OPT__QUIET(&quiet, + OPT__ABBREV(&show_one_opts.abbrev), + OPT__QUIET(&show_one_opts.quiet, N_("do not print results to stdout (useful with --verify)")), OPT_CALLBACK_F(0, "exclude-existing", &exclude_existing_opts, N_("pattern"), N_("show refs from stdin that aren't in local repository"), @@ -247,7 +265,7 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix) if (exclude_existing_opts.enabled) return cmd_show_ref__exclude_existing(&exclude_existing_opts); else if (verify) - return cmd_show_ref__verify(argv); + return cmd_show_ref__verify(&show_one_opts, argv); else - return cmd_show_ref__patterns(argv); + return cmd_show_ref__patterns(&show_one_opts, argv); }