From patchwork Wed May 10 12:34:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Steinhardt X-Patchwork-Id: 13236900 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 184E2C7EE23 for ; Wed, 10 May 2023 12:34:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237042AbjEJMes (ORCPT ); Wed, 10 May 2023 08:34:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36770 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237068AbjEJMek (ORCPT ); Wed, 10 May 2023 08:34:40 -0400 Received: from wout3-smtp.messagingengine.com (wout3-smtp.messagingengine.com [64.147.123.19]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 172FF83F1 for ; Wed, 10 May 2023 05:34:33 -0700 (PDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 34C973200915; Wed, 10 May 2023 08:34:32 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Wed, 10 May 2023 08:34:32 -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=1683722071; x=1683808471; bh=Lq vKaXXSjS+TBnj8gSPxfC+VLjGy811a+k9Lf2eHT7Y=; b=kMM5qVRu7pWQpUYjZW 5cF/Dzip0thX4qTOUvbNjhKY04mp9oOsmiqhFr4+xeww88hO7UQ7u2xaFnbdu0f7 yxflVsnMKMZtbwd6jkmvSoa9KmYiU80jX7PF73POMTMGR0UFXjssGbRDhb4cwbZZ EWduxl7zDKJeI6ZV525fBks9GtTrUL6WIQRmvC/9jXX5BcU5+Vv2+D0ibOIyf2ND WftmB86+jsCLTEP7Y2N281QwKRo/sh4IbGrj/eIA6D8xgyu+SJlRZWHU2KsMFmAZ JypiPtjrd+8nos/DOpU3ydzqPOWsBu24Qqpgn2cr/NvFTQSlYmQIIHuQP37jWtVS Gh7A== 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=1683722071; x=1683808471; bh=LqvKaXXSjS+TB nj8gSPxfC+VLjGy811a+k9Lf2eHT7Y=; b=ZeDtmxhpa/MPW29fxQ0sWXlvrdw5+ VngQ54DhyUmfCC04FeWRITFXFMUsjRa4cm1DCiXrJ77tPovjF+QPle1iAI46RXeb uZJ6KhOFPSNkvY98neOdezCjvmOXx08cwVrFZnLCKtsLdce5W28ZiJAd+tnJ9QC1 pKtDmqC7wlOMCtXgr/tWieA0LS13OAECTOHUIDDgjrtupnbSIlThV5UKUoOqm5V5 bzwYMxy7yM7o4MImUNekZaKre6VFow86D/2GylN9n2uqHCfX++Cq2cw3f3WaOhGj RWUHeNZNXyAruMKLm3RU5V6dUoAqw4Fdxxq9Nex6Xy0GTVgQZwXGAA/7Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrfeegiedgfedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvfevuffkfhggtggujgesghdtreertddtvdenucfhrhhomheprfgrthhr ihgtkhcuufhtvghinhhhrghrughtuceophhssehpkhhsrdhimheqnecuggftrfgrthhtvg hrnhepueektdevtdffveeljeetgfehheeigeekleduvdeffeeghefgledttdehjeelffet necuvehluhhsthgvrhfuihiivgepudenucfrrghrrghmpehmrghilhhfrhhomhepphhsse hpkhhsrdhimh X-ME-Proxy: Feedback-ID: i197146af:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 10 May 2023 08:34:30 -0400 (EDT) Received: by pks.im (OpenSMTPD) with ESMTPSA id 6686fe17 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Wed, 10 May 2023 12:34:20 +0000 (UTC) Date: Wed, 10 May 2023 14:34:28 +0200 From: Patrick Steinhardt To: git@vger.kernel.org Cc: Junio C Hamano , Felipe Contreras , Glen Choo , Jonathan Tan , Jacob Keller Subject: [PATCH v5 7/9] fetch: lift up parsing of "fetch.output" config variable Message-ID: <0c3dbcd09fb5e064601dc3fd9ac650cb496371e5.1683721293.git.ps@pks.im> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Parsing the display format happens inside of `display_state_init()`. As we only need to check for a simple config entry, this is a natural location to put this code as it means that display-state logic is neatly contained in a single location. We're about to introduce a new "porcelain" output format though that is intended to be parseable by machines, for example inside of a script. This format can be enabled by passing the `--porcelain` switch to git-fetch(1). As a consequence, we'll have to add a second callsite that influences the output format, which will become awkward to handle. Refactor the code such that callers are expected to pass the display format that is to be used into `display_state_init()`. This allows us to lift up the code into the main function, where we can then hook it into command line options parser in a follow-up commit. Signed-off-by: Patrick Steinhardt --- builtin/fetch.c | 50 +++++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/builtin/fetch.c b/builtin/fetch.c index 4c58ee503a..439d3c667b 100644 --- a/builtin/fetch.c +++ b/builtin/fetch.c @@ -104,8 +104,14 @@ static int fetch_write_commit_graph = -1; static int stdin_refspecs = 0; static int negotiate_only; +struct fetch_config { + enum display_format display_format; +}; + static int git_fetch_config(const char *k, const char *v, void *cb) { + struct fetch_config *fetch_config = cb; + if (!strcmp(k, "fetch.prune")) { fetch_prune_config = git_config_bool(k, v); return 0; @@ -144,6 +150,18 @@ static int git_fetch_config(const char *k, const char *v, void *cb) return 0; } + if (!strcmp(k, "fetch.output")) { + if (!v) + return config_error_nonbool(k); + else if (!strcasecmp(v, "full")) + fetch_config->display_format = DISPLAY_FORMAT_FULL; + else if (!strcasecmp(v, "compact")) + fetch_config->display_format = DISPLAY_FORMAT_COMPACT; + else + die(_("invalid value for '%s': '%s'"), + "fetch.output", v); + } + return git_default_config(k, v, cb); } @@ -800,14 +818,13 @@ static int refcol_width(const struct ref *ref_map, int compact_format) } static void display_state_init(struct display_state *display_state, struct ref *ref_map, - const char *raw_url) + const char *raw_url, enum display_format format) { - const char *format = "full"; int i; memset(display_state, 0, sizeof(*display_state)); - strbuf_init(&display_state->buf, 0); + display_state->format = format; if (raw_url) display_state->url = transport_anonymize_url(raw_url); @@ -824,15 +841,6 @@ static void display_state_init(struct display_state *display_state, struct ref * if (verbosity < 0) return; - git_config_get_string_tmp("fetch.output", &format); - if (!strcasecmp(format, "full")) - display_state->format = DISPLAY_FORMAT_FULL; - else if (!strcasecmp(format, "compact")) - display_state->format = DISPLAY_FORMAT_COMPACT; - else - die(_("invalid value for '%s': '%s'"), - "fetch.output", format); - switch (display_state->format) { case DISPLAY_FORMAT_FULL: case DISPLAY_FORMAT_COMPACT: @@ -1605,7 +1613,8 @@ static int backfill_tags(struct display_state *display_state, } static int do_fetch(struct transport *transport, - struct refspec *rs) + struct refspec *rs, + enum display_format display_format) { struct ref_transaction *transaction = NULL; struct ref *ref_map = NULL; @@ -1691,7 +1700,7 @@ static int do_fetch(struct transport *transport, if (retcode) goto cleanup; - display_state_init(&display_state, ref_map, transport->url); + display_state_init(&display_state, ref_map, transport->url, display_format); if (atomic_fetch) { transaction = ref_transaction_begin(&err); @@ -2069,7 +2078,8 @@ static inline void fetch_one_setup_partial(struct remote *remote) } static int fetch_one(struct remote *remote, int argc, const char **argv, - int prune_tags_ok, int use_stdin_refspecs) + int prune_tags_ok, int use_stdin_refspecs, + enum display_format display_format) { struct refspec rs = REFSPEC_INIT_FETCH; int i; @@ -2136,7 +2146,7 @@ static int fetch_one(struct remote *remote, int argc, const char **argv, sigchain_push_common(unlock_pack_on_signal); atexit(unlock_pack_atexit); sigchain_push(SIGPIPE, SIG_IGN); - exit_code = do_fetch(gtransport, &rs); + exit_code = do_fetch(gtransport, &rs, display_format); sigchain_pop(SIGPIPE); refspec_clear(&rs); transport_disconnect(gtransport); @@ -2146,6 +2156,9 @@ static int fetch_one(struct remote *remote, int argc, const char **argv, int cmd_fetch(int argc, const char **argv, const char *prefix) { + struct fetch_config config = { + .display_format = DISPLAY_FORMAT_FULL, + }; int i; const char *bundle_uri; struct string_list list = STRING_LIST_INIT_DUP; @@ -2165,7 +2178,7 @@ int cmd_fetch(int argc, const char **argv, const char *prefix) free(anon); } - git_config(git_fetch_config, NULL); + git_config(git_fetch_config, &config); if (the_repository->gitdir) { prepare_repo_settings(the_repository); the_repository->settings.command_requires_full_index = 0; @@ -2302,7 +2315,8 @@ int cmd_fetch(int argc, const char **argv, const char *prefix) } else if (remote) { if (filter_options.choice || has_promisor_remote()) fetch_one_setup_partial(remote); - result = fetch_one(remote, argc, argv, prune_tags_ok, stdin_refspecs); + result = fetch_one(remote, argc, argv, prune_tags_ok, stdin_refspecs, + config.display_format); } else { int max_children = max_jobs;