git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Taylor Blau <me@ttaylorr.com>
To: Jeff King <peff@peff.net>
Cc: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>,
	Igor Todorovski <itodorov@ca.ibm.com>,
	Bence Ferdinandy <bence@ferdinandy.com>
Subject: Re: [PATCH 5/9] refspec_ref_prefixes(): clean up refspec_item logic
Date: Mon, 17 Mar 2025 18:00:36 -0400	[thread overview]
Message-ID: <Z9ibhJxjlc2DxKdX@nand.local> (raw)
In-Reply-To: <20250313054107.GE94015@coredump.intra.peff.net>

On Thu, Mar 13, 2025 at 01:41:07AM -0400, Jeff King wrote:
> > Do you think it'd be worth handling rs->fetch in a switch/case block? At
> > least that would allow us to catch unknown values more easily, though it
> > seems unlikely we'd ever add any :-).
>
> ...this whole thing is badly named. It is called "fetch", but the only
> two values are true/false. But for some reason we named them
> REFSPEC_FETCH and REFSPEC_PUSH. Surely it should be "type" or
> "operation" or something if we were going to use an enum and switch?
>
> I tried to limit the extent of my changes on opinionated matters like
> this. I almost dropped the patch entirely, but I did enough
> head-scratching to find that latent bug that I didn't want to lose it.
>
> If you want to fix the name and other clarity issues on top, I don't
> mind, though. ;)

OK... I agree that these are at least named confusingly ;-). We could do
something like:

--- 8< ---
diff --git a/refspec.c b/refspec.c
index c6ad515f04..07d401bc71 100644
--- a/refspec.c
+++ b/refspec.c
@@ -181,14 +181,14 @@ void refspec_item_clear(struct refspec_item *item)
 void refspec_init(struct refspec *rs, int fetch)
 {
 	memset(rs, 0, sizeof(*rs));
-	rs->fetch = fetch;
+	rs->type = fetch ? REFSPEC_FETCH : REFSPEC_PUSH;
 }

 void refspec_append(struct refspec *rs, const char *refspec)
 {
 	struct refspec_item item;

-	refspec_item_init_or_die(&item, refspec, rs->fetch);
+	refspec_item_init_or_die(&item, refspec, rs->type);

 	ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
 	rs->items[rs->nr] = item;
@@ -227,7 +227,7 @@ void refspec_clear(struct refspec *rs)
 	rs->alloc = 0;
 	rs->nr = 0;

-	rs->fetch = 0;
+	rs->type = 0;
 }

 int valid_fetch_refspec(const char *fetch_refspec_str)
@@ -249,11 +249,13 @@ void refspec_ref_prefixes(const struct refspec *rs,
 		if (item->negative)
 			continue;

-		if (rs->fetch == REFSPEC_FETCH) {
+		switch (rs->type) {
+		case REFSPEC_FETCH:
 			if (item->exact_sha1)
 				continue;
 			prefix = item->src;
-		} else {
+			break;
+		case REFSPEC_PUSH:
 			/*
 			 * Pushes can have an explicit destination like
 			 * "foo:bar", or can implicitly use the src for both
@@ -263,6 +265,9 @@ void refspec_ref_prefixes(const struct refspec *rs,
 				prefix = item->dst;
 			else if (item->src && !item->exact_sha1)
 				prefix = item->src;
+			break;
+		default:
+			BUG("unexpected refspec type %d", rs->type);
 		}

 		if (!prefix)
diff --git a/refspec.h b/refspec.h
index 382ba2d5c1..a20cf883e4 100644
--- a/refspec.h
+++ b/refspec.h
@@ -32,8 +32,8 @@ struct refspec_item {

 struct string_list;

-#define REFSPEC_INIT_PUSH { .fetch = REFSPEC_PUSH }
-#define REFSPEC_INIT_FETCH { .fetch = REFSPEC_FETCH }
+#define REFSPEC_INIT_PUSH { .type = REFSPEC_PUSH }
+#define REFSPEC_INIT_FETCH { .type = REFSPEC_FETCH }

 /**
  * An array of strings can be parsed into a struct refspec using
@@ -45,9 +45,9 @@ struct refspec {
 	int nr;

 	enum {
-		REFSPEC_PUSH
+		REFSPEC_PUSH,
 		REFSPEC_FETCH,
-	} fetch;
+	} type;
 };

 int refspec_item_init(struct refspec_item *item, const char *refspec,
--- >8 ---

, which gives us the "default" case in the switch statement. But this
really is a boolean. I wonder if we should just use 0/1 constants and
leave the field name alone. That would turn something like:

    if (rs->fetch == REFSPEC_FETCH) { ... }

into:

    if (rs->fetch) { ... }

, which I think is cleaner. There's no reason to rename true/false to
FETCH and PUSH if the field name itself is already 'fetch'.

Thanks,
Taylor

  parent reply	other threads:[~2025-03-17 22:00 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-30  3:49 Tags are no longer fetched when fetching specific commit Igor Todorovski
2025-02-13 22:38 ` Taylor Blau
2025-02-14 13:53   ` Bence Ferdinandy
2025-02-14 18:35   ` Junio C Hamano
2025-02-21  7:25     ` Jeff King
2025-03-07 23:27 ` [PATCH] fetch: fix following tags when fetching specific OID Taylor Blau
2025-03-07 23:32   ` Taylor Blau
2025-03-08  0:10   ` Junio C Hamano
2025-03-08  3:23   ` Bence Ferdinandy
2025-03-09  3:01   ` [PATCH 0/9] fetch: further ref-prefix cleanups and optimizations Jeff King
2025-03-09  3:01     ` [PATCH 1/9] t5702: fix typo in test name Jeff King
2025-03-12 21:30       ` Taylor Blau
2025-03-13  5:37         ` Jeff King
2025-03-09  3:01     ` [PATCH 2/9] t5516: prefer "oid" to "sha1" in some test titles Jeff King
2025-03-09  3:02     ` [PATCH 3/9] t5516: drop NEEDSWORK about v2 reachability behavior Jeff King
2025-03-12 21:30       ` Taylor Blau
2025-03-09  3:02     ` [PATCH 4/9] t5516: beef up exact-oid ref prefixes test Jeff King
2025-03-09  3:07     ` [PATCH 5/9] refspec_ref_prefixes(): clean up refspec_item logic Jeff King
2025-03-12 21:38       ` Taylor Blau
2025-03-13  5:41         ` Jeff King
2025-03-13 13:26           ` Junio C Hamano
2025-03-17 22:24             ` [PATCH 0/4] refspec: treat 'fetch' as a Boolean value Taylor Blau
2025-03-17 22:24               ` [PATCH 1/4] " Taylor Blau
2025-03-18  0:24                 ` Jeff King
2025-03-18  0:26                   ` Jeff King
2025-03-18 22:44                   ` Taylor Blau
2025-03-17 22:24               ` [PATCH 2/4] refspec: replace `refspec_init()` with fetch/push variants Taylor Blau
2025-03-17 22:24               ` [PATCH 3/4] refspec: remove refspec_item_init_or_die() Taylor Blau
2025-03-17 22:24               ` [PATCH 4/4] refspec: replace `refspec_item_init()` with fetch/push variants Taylor Blau
2025-03-17 23:26               ` [PATCH 0/4] refspec: treat 'fetch' as a Boolean value Junio C Hamano
2025-03-18 22:40                 ` Taylor Blau
2025-03-18 22:50             ` [PATCH v2 " Taylor Blau
2025-03-18 22:50               ` [PATCH v2 1/4] " Taylor Blau
2025-03-18 22:50               ` [PATCH v2 2/4] refspec: replace `refspec_init()` with fetch/push variants Taylor Blau
2025-03-18 22:50               ` [PATCH v2 3/4] refspec: remove refspec_item_init_or_die() Taylor Blau
2025-03-18 22:50               ` [PATCH v2 4/4] refspec: replace `refspec_item_init()` with fetch/push variants Taylor Blau
2025-03-19 15:31               ` [PATCH v2 0/4] refspec: treat 'fetch' as a Boolean value Elijah Newren
2025-03-17 22:00           ` Taylor Blau [this message]
2025-03-17 23:25             ` [PATCH 5/9] refspec_ref_prefixes(): clean up refspec_item logic Junio C Hamano
2025-03-18 22:47               ` Taylor Blau
2025-03-09  3:08     ` [PATCH 6/9] fetch: ask server to advertise HEAD for config-less fetch Jeff King
2025-03-12 21:43       ` Taylor Blau
2025-03-13  5:46         ` Jeff King
2025-03-13 12:26           ` Junio C Hamano
2025-03-17 22:23             ` Taylor Blau
2025-03-09  3:10     ` [PATCH 7/9] fetch: stop protecting additions to ref-prefix list Jeff King
2025-03-12 21:45       ` Taylor Blau
2025-03-09  3:20     ` [PATCH 8/9] fetch: avoid ls-refs only to ask for HEAD symref update Jeff King
2025-03-13 15:53       ` Junio C Hamano
2025-03-17 18:06         ` Jeff King
2025-03-17 19:01           ` Junio C Hamano
2025-03-18  5:39             ` [PATCH 0/2] limiting followRemoteHEAD being used Jeff King
2025-03-18  5:40               ` [PATCH 1/2] fetch: only respect followRemoteHEAD with configured refspecs Jeff King
2025-03-18 23:02                 ` Taylor Blau
2025-03-18  5:41               ` [PATCH 2/2] fetch: don't ask for remote HEAD if followRemoteHEAD is "never" Jeff King
2025-03-18 19:18               ` [PATCH 0/2] limiting followRemoteHEAD being used Junio C Hamano
2025-03-18 23:02                 ` Taylor Blau
2025-03-09  3:21     ` [PATCH 9/9] fetch: use ref prefix list to skip ls-refs Jeff King
2025-03-12 21:29     ` [PATCH 0/9] fetch: further ref-prefix cleanups and optimizations Taylor Blau
2025-03-12 21:49       ` Taylor Blau
2025-03-13  5:50         ` Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Z9ibhJxjlc2DxKdX@nand.local \
    --to=me@ttaylorr.com \
    --cc=bence@ferdinandy.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=itodorov@ca.ibm.com \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).