Skip to contentSkip to conversation

Tags are no longer fetched when fetching specific commit

PATCH In review Igor Todorovski 6 participants 61 messages Jan 30, 2025 – Mar 19, 2025 Documentation

lore
Share & tools
lore
#0 lore raw

Hi, we have noticed a change in behaviour with commit 3f763ddf28d28fe63963991513c8db4045eabadc.

Here’s the steps to reproduce:

mkdir git-test-dir
cd git-test-dir
git init --bare
git remote add origin -- https://github.com/golang/go
git -c protocol.version=2 fetch -f --depth=1 origin 16afa6a740fac7442e94dcd2ec5ea4a4853e45dc:refs/dummy git -c log.showsignature=false log --no-decorate -n1 --format="format:%H %ct %D" 16afa6a740fac7442e94dcd2ec5ea4a4853e45dc --

# Expected:
# 16afa6a740fac7442e94dcd2ec5ea4a4853e45dc 1734108730 grafted, tag: go1.24rc1, refs/dummy

# Tags are not fetch when using 2.48.1:
# 16afa6a740fac7442e94dcd2ec5ea4a4853e45dc 1734108730 grafted

---

git bisect revealed 3f763ddf28d28fe63963991513c8db4045eabadc as the culprit:

commit 3f763ddf28d28fe63963991513c8db4045eabadc

Author: Bence Ferdinandy
Date:   Fri Nov 22 13:28:50 2024 +0100

fetch: set remote/HEAD if it does not exist

When cloning a repository remote/HEAD is created, but when the user creates a repository with git init, and later adds a remote, remote/HEAD is only created if the user explicitly runs a variant of "remote set-head". Attempt to set remote/HEAD during fetch, if the user does not have it already set. Silently ignore any errors.

Is this intended?

This was initially reported under https://github.com/zopencommunity/gitport/issues/146.

#1 lore raw
Quoted 27 lines · Igor Todorovski

Hi, we have noticed a change in behaviour with commit 3f763ddf28d28fe63963991513c8db4045eabadc.

Here’s the steps to reproduce:

mkdir git-test-dir
cd git-test-dir
git init --bare
git remote add origin -- https://github.com/golang/go
git -c protocol.version=2 fetch -f --depth=1 origin 16afa6a740fac7442e94dcd2ec5ea4a4853e45dc:refs/dummy git -c log.showsignature=false log --no-decorate -n1 --format="format:%H %ct %D" 16afa6a740fac7442e94dcd2ec5ea4a4853e45dc --

# Expected:
# 16afa6a740fac7442e94dcd2ec5ea4a4853e45dc 1734108730 grafted, tag: go1.24rc1, refs/dummy

# Tags are not fetch when using 2.48.1:
# 16afa6a740fac7442e94dcd2ec5ea4a4853e45dc 1734108730 grafted

---

git bisect revealed 3f763ddf28d28fe63963991513c8db4045eabadc as the culprit:

commit 3f763ddf28d28fe63963991513c8db4045eabadc

Author: Bence Ferdinandy
Date:   Fri Nov 22 13:28:50 2024 +0100

fetch: set remote/HEAD if it does not exist

Quoted 9 lines

When cloning a repository remote/HEAD is created, but when the user creates a repository with git init, and later adds a remote, remote/HEAD is only created if the user explicitly runs a variant of "remote set-head". Attempt to set remote/HEAD during fetch, if the user does not have it already set. Silently ignore any errors.

Thanks for the report and bisection recipe. I was able to bisect the same issue myself, and also found myself at 3f763ddf28 (fetch: set remote/HEAD if it does not exist, 2024-11-22).

Quoted 1 line

Is this intended?

I don't think this was intentional, though the commit's author Bence (CC'd) can confirm.

I suspect what's going on here is that in 3f763ddf28 and onwards we are explicitly adding "HEAD" to the list of ref_prefixes, which causes the server to respond only to the prefixes being asked for. In a pre-3f763ddf28 world, the ref_prefixes list would be empty (if invoked according to your script above), which allowed us to learn about any tags pointing at that commit.

One way to fix it is to move adding the "HEAD" prefix to above where we check

    if (tags == TAGS_SET || tags == TAGS_DEFAULT)

, which would allow us to enter the inner-most conditional which guards us actually adding the refs/tags prefix to our list.

But I don't love that solution, and I think even that is incomplete since as of 6c915c3f85 (fetch: do not ask for HEAD unnecessarily, 2024-12-06) we only ask for "HEAD" if we have a remote in the first place.

I think the real culprit is that we can no longer hold the same assumption from e70a3030e7 (fetch: do not list refs if fetching only hashes, 2018-09-27), which is that we can avoid asking for refs/tags as an explicit prefix if we're (a) fetching literal hashes, (b) tag following wasn't requested, and (c) the fetch is done with protocol v2.

So I think the right fix would really be something like:

--- 8< ---

builtin/fetch.c +2 -3
index fe2b26c74a..0e63621e6c 100644
@@ -1770,9 +1770,8 @@ static int do_fetch(struct transport *transport,
 
 	if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
 		must_list_refs = 1;
-		if (transport_ls_refs_options.ref_prefixes.nr)
-			strvec_push(&transport_ls_refs_options.ref_prefixes,
-				    "refs/tags/");
+		strvec_push(&transport_ls_refs_options.ref_prefixes,
+			    "refs/tags/");
 	}
 
 	if (uses_remote_tracking(transport, rs)) {

--- >8 ---

But I'm unfamiliar enough with this area that I'd appreciate comments from the authors of these various commits, all of whom have been CC'd. Does this seem right to you, or am I totally down the wrong path?

Thanks,
Taylor

#2 lore raw
Quoted 94 lines · Taylor Blau
Quoted 27 lines · Igor Todorovski

Hi, we have noticed a change in behaviour with commit 3f763ddf28d28fe63963991513c8db4045eabadc.

Here’s the steps to reproduce:

mkdir git-test-dir
cd git-test-dir
git init --bare
git remote add origin -- https://github.com/golang/go
git -c protocol.version=2 fetch -f --depth=1 origin 16afa6a740fac7442e94dcd2ec5ea4a4853e45dc:refs/dummy git -c log.showsignature=false log --no-decorate -n1 --format="format:%H %ct %D" 16afa6a740fac7442e94dcd2ec5ea4a4853e45dc --

# Expected:
# 16afa6a740fac7442e94dcd2ec5ea4a4853e45dc 1734108730 grafted, tag: go1.24rc1, refs/dummy

# Tags are not fetch when using 2.48.1:
# 16afa6a740fac7442e94dcd2ec5ea4a4853e45dc 1734108730 grafted

---

git bisect revealed 3f763ddf28d28fe63963991513c8db4045eabadc as the culprit:

commit 3f763ddf28d28fe63963991513c8db4045eabadc

Author: Bence Ferdinandy
Date:   Fri Nov 22 13:28:50 2024 +0100

fetch: set remote/HEAD if it does not exist

Quoted 9 lines

When cloning a repository remote/HEAD is created, but when the user creates a repository with git init, and later adds a remote, remote/HEAD is only created if the user explicitly runs a variant of "remote set-head". Attempt to set remote/HEAD during fetch, if the user does not have it already set. Silently ignore any errors.

Thanks for the report and bisection recipe. I was able to bisect the same issue myself, and also found myself at 3f763ddf28 (fetch: set remote/HEAD if it does not exist, 2024-11-22).

Quoted 1 line

Is this intended?

I don't think this was intentional, though the commit's author Bence (CC'd) can confirm.

I suspect what's going on here is that in 3f763ddf28 and onwards we are explicitly adding "HEAD" to the list of ref_prefixes, which causes the server to respond only to the prefixes being asked for. In a pre-3f763ddf28 world, the ref_prefixes list would be empty (if invoked according to your script above), which allowed us to learn about any tags pointing at that commit.

One way to fix it is to move adding the "HEAD" prefix to above where we check

    if (tags == TAGS_SET || tags == TAGS_DEFAULT)

, which would allow us to enter the inner-most conditional which guards us actually adding the refs/tags prefix to our list.

But I don't love that solution, and I think even that is incomplete since as of 6c915c3f85 (fetch: do not ask for HEAD unnecessarily, 2024-12-06) we only ask for "HEAD" if we have a remote in the first place.

I think the real culprit is that we can no longer hold the same assumption from e70a3030e7 (fetch: do not list refs if fetching only hashes, 2018-09-27), which is that we can avoid asking for refs/tags as an explicit prefix if we're (a) fetching literal hashes, (b) tag following wasn't requested, and (c) the fetch is done with protocol v2.

So I think the right fix would really be something like:

--- 8< ---

builtin/fetch.c +2 -4
index fe2b26c74a..0e63621e6c 100644
@@ -1770,9 +1770,8 @@ static int do_fetch(struct transport *transport,
 
 	if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
 		must_list_refs = 1;
-		if (transport_ls_refs_options.ref_prefixes.nr)
-			strvec_push(&transport_ls_refs_options.ref_prefixes,
-				    "refs/tags/");
+		strvec_push(&transport_ls_refs_options.ref_prefixes,
+			    "refs/tags/");
 	}
 
 	if (uses_remote_tracking(transport, rs)) {
--- >8 ---
Quoted 3 lines

But I'm unfamiliar enough with this area that I'd appreciate comments from the authors of these various commits, all of whom have been CC'd. Does this seem right to you, or am I totally down the wrong path?

This is the same error that came up already in another thread, and I came to a similar conclusion as you did (also about asking previous authors, which is why it is a bit stuck I guess):

https://lore.kernel.org/git/[email protected]/

I'll copy the relevant part of my previous message:

What is not quite clear to me, is that it looks like that the original intention was to pretty much always fetch tags, yet it was not achieved by always pushing refs/tags into ref_prefixes. Deleting the check for ref_prefixes being empty [1] breaks quite a lot of things, but reversing the order [2] does not. That feels a bit strange tbh since it feels like the two should bring about the same state ...

Hopefully someone more knowledgeable knows why things are as they are, but it seems that reversing the order really is a band-aid here.

1: https://github.com/ferdinandyb/git/commit/6074a9b8c88451e589eade4034282dd9b6c86345
2: https://github.com/ferdinandyb/git/commit/31e3f0a6b829d6c7953bf89d015b98e7edabe6b5

So there is some magic going on later on which I don't know enough about.

Best,
Bence

#3 lore raw
In reply to Taylor Blau
Quoted 8 lines · Taylor Blau

So I think the right fix would really be something like:

--- 8< ---

builtin/fetch.c +0 -0
index fe2b26c74a..0e63621e6c 100644

builtin/fetch.c static int do_fetch(struct transport *transport, · line 1775

Quoted 6 lines
builtin/fetch.c +0 -3
@@ -1770,9 +1770,8 @@ static int do_fetch(struct transport *transport,
 
 	if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
 		must_list_refs = 1;
-		if (transport_ls_refs_options.ref_prefixes.nr)
-			strvec_push(&transport_ls_refs_options.ref_prefixes,
-				    "refs/tags/");

I guess the original "if we are giving prefixes already, then make sure we learn about tags" is because the semantics of the ref-prefix is peculiar, in that without _any_, there is no limitation and the remote end would advertise all its refs, but if there is even one, the remote end would advertise only the ones that match any prefix?

builtin/fetch.c static int do_fetch(struct transport *transport, · line 1774

Quoted 2 lines
builtin/fetch.c +2 -0
+		strvec_push(&transport_ls_refs_options.ref_prefixes,
+			    "refs/tags/");

So this change would make sure that, even if somebody else already added a prefix earlier, we would always ask about tags if we come into this block. It also means when nobody has added a prefix before we reach here, we still add prefix to ask about tags. The implication of which may be rather grave. If a later code relied on us _not_ asking for tags here (because nobody is limiting the ls-refs request with prefix), after this change, that code will now see that there is some prefix requested already, and if we wanted to retain what that code does, we would need to adjust that code (and then the need for similar modification cascades X-<).

Quoted 8 lines
builtin/fetch.c +0 -1
 	}
 
 	if (uses_remote_tracking(transport, rs)) {
--- >8 ---

But I'm unfamiliar enough with this area that I'd appreciate comments from the authors of these various commits, all of whom have been CC'd. Does this seem right to you, or am I totally down the wrong path?

I have no idea without doing a deep dive myself, for which I have failed to find time for so far.

I have to wonder if the first thing we should do is to update the logic to decide what ref prefixes to use, so that it does not do such an incremental strvec_push(). There are multiple questions in the form "Do we need to know about X?" the code path need to ask before deciding what prefixes to use, and before Bence's "We need to know about HEAD, because we want to know about its symref value", refs/tags hierarchy was one (and only?) thing we needed to ask about. The bug under discussion smells like the result of how brittle the code's assumption about what prefixes other parts would want to ask. This code did not assume there is anybody else, and that is why it broke when we started asking about HEAD. Now we have two possible X's in that question (refs/tags and HEAD). We may want to make sure the code is robust enough when we add the third one.

Thanks.

#4 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 9 lines · Taylor Blau

So I think the right fix would really be something like:

--- 8< ---

builtin/fetch.c +0 -0
index fe2b26c74a..0e63621e6c 100644
@@ -1770,9 +1770,8 @@ static int do_fetch(struct transport *transport,
 
Quoted 11 lines

builtin/fetch.c static int do_fetch(struct transport *transport, · line 1775

Quoted 5 lines
builtin/fetch.c +0 -3
 	if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
 		must_list_refs = 1;
-		if (transport_ls_refs_options.ref_prefixes.nr)
-			strvec_push(&transport_ls_refs_options.ref_prefixes,
-				    "refs/tags/");

I guess the original "if we are giving prefixes already, then make sure we learn about tags" is because the semantics of the ref-prefix is peculiar, in that without _any_, there is no limitation and the remote end would advertise all its refs, but if there is even one, the remote end would advertise only the ones that match any prefix?

Yes, I think that is accurate. We must either add prefixes for all bits of code interested in seeing the remote refs, or we must add none at all.

And so just removing the conditional like this:

Quoted 5 lines

builtin/fetch.c static int do_fetch(struct transport *transport, · line 1774

Quoted 2 lines
builtin/fetch.c +2 -0
+		strvec_push(&transport_ls_refs_options.ref_prefixes,
+			    "refs/tags/");

So this change would make sure that, even if somebody else already added a prefix earlier, we would always ask about tags if we come

Quoted 8 lines

into this block. It also means when nobody has added a prefix before we reach here, we still add prefix to ask about tags. The implication of which may be rather grave. If a later code relied on us _not_ asking for tags here (because nobody is limiting the ls-refs request with prefix), after this change, that code will now see that there is some prefix requested already, and if we wanted to retain what that code does, we would need to adjust that code (and then the need for similar modification cascades X-<).

is not going to work. I _think_ for the reasons you're implying above, but your mention of "if later code relied on us..." confused me.

It is really the _earlier_ code that we are screwing up here. If the earlier block which conditionally added to ref_prefixes did not add anything, then unconditionally adding "refs/tags/" means that the refs that earlier code expected to find in the advertisement may not be present.

Of course it is the later code which actually starts the fetch and looks at the advertisement that will bear the effects of the missing refs. So maybe that is what you meant by "later code". ;)

Quoted 5 lines

I have to wonder if the first thing we should do is to update the logic to decide what ref prefixes to use, so that it does not do such an incremental strvec_push(). There are multiple questions in the form "Do we need to know about X?" the code path need to ask before deciding what prefixes to use, and before Bence's "We need to

Quoted 8 lines

know about HEAD, because we want to know about its symref value", refs/tags hierarchy was one (and only?) thing we needed to ask about. The bug under discussion smells like the result of how brittle the code's assumption about what prefixes other parts would want to ask. This code did not assume there is anybody else, and that is why it broke when we started asking about HEAD. Now we have two possible X's in that question (refs/tags and HEAD). We may want to make sure the code is robust enough when we add the third one.

There are three spots, really: the refs we are fetching, the tags for following, and now HEAD.

But they are not all equal. The decision of "can we limit the ref advertisement at all" comes from the first one, the set of refs we are fetching[1]. And then the other ancillary data must follow that lead (either adding bits they are interested in, or not touching it at all if we are going to see the whole advertisement).

So I think the bug is in adding the HEAD prefix: it should only happen if we are otherwise limiting the advertisement. I.e., this should fix the problem in this thread:

builtin/fetch.c +2 -1
index 1c740d5aac..c80afc6477 100644
@@ -1777,7 +1777,8 @@ static int do_fetch(struct transport *transport,
 
 	if (uses_remote_tracking(transport, rs)) {
 		must_list_refs = 1;
-		strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+		if (transport_ls_refs_options.ref_prefixes.nr)
+			strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
 	}
 
 	if (must_list_refs) {

as well as other possible ones (where asking for HEAD caused us to suppress the advertisement for the refs we actually wanted to fetch; though see my footnote below).

I do agree this dependency is subtle. I'm not sure how to make it less awkward in the code, though. It's really a property of the protocol, and not the code itself. So this:

Quoted 6 lines

about. The bug under discussion smells like the result of how brittle the code's assumption about what prefixes other parts would want to ask. This code did not assume there is anybody else, and that is why it broke when we started asking about HEAD. Now we have two possible X's in that question (refs/tags and HEAD). We may want to make sure the code is robust enough when we add the third one.

is always going to have that brittle-ness. You can't just have every spot register "I am interested in this prefix", because the _lack_ of registering means something, too.

But it's less bad than one might think, because the primary decision about whether to use prefixes at all happens first, and all three spots are contained in a single function. We could perhaps add a boolean "restrict_ref_prefixes" flag to make it a bit more obvious. But the code would still look like:

  if (...some complicated logic...) {
	restrict_ref_prefixes = 1;
	strvec_push(&ref_prefixes, ...);

}

  if (...we want tags...) {
	if (restrict_ref_prefixes)
		strvec_push(&ref_prefixes, "refs/tags/");

}

  if (...we want HEAD...) {
	if (restrict_ref_prefixes)
		strvec_push(&ref_prefixes, "HEAD");

}

That is perhaps a little more self-documenting, but it introduces a new possible bug: that flag must be set if-and-only-if ref_prefixes is non-empty. In the pseudo-code above that is not too bad, but in the real code there are actually three branching paths which handle it. So it's do-able, but I kind of wonder if a big comment explaining the situation would make more sense.

-Peff

[1] One thing that I didn't dig into is why we would ever not pass ref prefixes at all. Skimming the code, I think we reach that state if we don't have any configure or command-line refspecs, and no branch-merge config. So something like:

git init
git fetch /path/to/some-repo.git

But in that case I think we're just fetching HEAD into FETCH_HEAD? In which case we probably ought to just ask for HEAD, I'd think, to avoid pointless extra advertisement. And that's why the "other possible problems" I mentioned earlier might not exist. If we only hit the no-prefixes state when we're just fetching HEAD, then accidentally adding "HEAD" to the prefix list and limiting ourselves happens to do the right thing. ;)

A patch like this:

builtin/fetch.c +9 -3
index 1c740d5aac..3364546d2a 100644
@@ -1766,13 +1766,19 @@ static int do_fetch(struct transport *transport,
 					    branch->merge[i]->src);
 			}
 		}
+		/*
+		 * If there are no refs specified to fetch, then we just
+		 * fetch HEAD; mention that to narrow the advertisement.
+		 */
+		if (!transport_ls_refs_options.ref_prefixes.nr)
+			strvec_push(&transport_ls_refs_options.ref_prefixes,
+				    "HEAD");
 	}
 
 	if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
 		must_list_refs = 1;
-		if (transport_ls_refs_options.ref_prefixes.nr)
-			strvec_push(&transport_ls_refs_options.ref_prefixes,
-				    "refs/tags/");
+		strvec_push(&transport_ls_refs_options.ref_prefixes,
+			    "refs/tags/");
 	}
 
 	if (uses_remote_tracking(transport, rs)) {

does that limiting in a more explicit way, reducing the advertisement for this kind of HEAD-only fetch. And as a bonus, it means that we'll _always_ have the correct prefixes set, so further code for tags or HEAD can unconditionally append to the prefix list.

It fixes the case that started this thread, and passes all the tests.

But I'm not 100% sure that this is thinking through all of the cases. In particular, the one in this thread is fetching a raw sha1, which is why there's no refspec (and thus no ref prefix set). In that case asking for HEAD is actually pointless (assuming we don't care about tag following or setting a configured $remote/HEAD, we would not need any ref advertisement at all!). Asking for HEAD is not too bad an outcome, but I think the "right" answer is really to suppress the advertisement totally. There's not protocol support for that, though, so asking for HEAD as a default is maybe a not-too-bad workaround?

-Peff

#5 lore raw

PATCH 1/1 fetch: fix following tags when fetching specific OID

In reply to Igor Todorovski

In 3f763ddf28 (fetch: set remote/HEAD if it does not exist, 2024-11-22), unconditionally adds "HEAD" to the list of ref prefixes we send to the server.

This breaks a core assumption that the list of prefixes we send to the server is complete. We must either send all prefixes we care about, or none at all (in the latter case the server then advertises everything).

The tag following code is careful to only add "refs/tags/" to the list of prefixes if there are already entries in the prefix list. But because the new code from 3f763ddf28 runs after the tag code, and because it unconditionally adds to the prefix list, we may end up with a prefix list that _should_ have "refs/tags/" in it, but doesn't.

When that is the case, the server does not advertise any tags, and our auto-following breaks because we never learned about any tags in the first place.

Fix this by only adding "HEAD" to the ref prefixes when we know that we are already limiting the advertisement. In either case we'll learn about HEAD (either through the limited advertisement, or implicitly through a full advertisement).

---
Peff and I talked about this today, and neither of us could find any reasons not to pursue the approach listed in the footnote of

<[email protected]>

, but this is a more conservative approach that should fix the issue and apply cleanly on top of 'maint'. It may be worth picking this into 2.49, even though we are already quite late into the -rc cycle, this is a fairly nasty bug.

 builtin/fetch.c      |  4 +++-
 t/t5503-tagfollow.sh | 15 +++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)
builtin/fetch.c +3 -1
index fe2b26c74ae..a06d1305016 100644
@@ -1777,7 +1777,9 @@ static int do_fetch(struct transport *transport,
 
 	if (uses_remote_tracking(transport, rs)) {
 		must_list_refs = 1;
-		strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+		if (transport_ls_refs_options.ref_prefixes.nr)
+			strvec_push(&transport_ls_refs_options.ref_prefixes,
+				    "HEAD");
 	}
 
 	if (must_list_refs) {
t/t5503-tagfollow.sh +15 -0
index 195fc64dd44..8495730e264 100755
@@ -160,4 +160,19 @@ test_expect_success 'new clone fetch main and tags' '
 	test_cmp expect actual
 '
 
+test_expect_success 'fetch specific OID with tag following' '
+	rm -f $U &&
+	git init --bare clone3.git &&
+	(
+		cd clone3.git &&
+		git remote add origin .. &&
+		git fetch origin $B:refs/heads/main &&
+
+		git -C .. for-each-ref >expect &&
+		git for-each-ref >actual &&
+
+		test_cmp expect actual
+	)
+'
+
 test_done

base-commit: f93ff170b93a1782659637824b25923245ac9dd1 --
2.48.1.1.g965d2fe18fa

#6 lore raw

Re: [PATCH] fetch: fix following tags when fetching specific OID

t/t5503-tagfollow.sh test_expect_success 'new clone fetch main and tags' ' · line 164

Quoted 10 lines · Taylor Blau
t/t5503-tagfollow.sh +2 -0
index 195fc64dd44..8495730e264 100755
@@ -160,4 +160,19 @@ test_expect_success 'new clone fetch main and tags' '
 	test_cmp expect actual
 '
 
+test_expect_success 'fetch specific OID with tag following' '
+	rm -f $U &&

Oops. This line is stray from when I wrote the test with a more conservative approach that matches the above. It's harmless, but could equally be removed while queueing.

Sorry for not catching earlier.

Thanks,
Taylor

#7 lore raw

Re: [PATCH] fetch: fix following tags when fetching specific OID

In reply to Taylor Blau
Quoted 4 lines · Taylor Blau

Fix this by only adding "HEAD" to the ref prefixes when we know that we are already limiting the advertisement. In either case we'll learn about HEAD (either through the limited advertisement, or implicitly through a full advertisement).

Good. "implicitly through a full advertisement" is a good thing to explicitly state here ;-)

Quoted 8 lines

---
Peff and I talked about this today, and neither of us could find any reasons not to pursue the approach listed in the footnote of

Quoted 6 lines

<[email protected]>

, but this is a more conservative approach that should fix the issue and apply cleanly on top of 'maint'. It may be worth picking this into 2.49, even though we are already quite late into the -rc cycle, this is a fairly nasty bug.

Thanks, both.

#8 lore raw

Re: [PATCH] fetch: fix following tags when fetching specific OID

In reply to Taylor Blau

2025. márc. 8. 0:27:43 Taylor Blau <[email protected]>:

Quoted 83 lines

In 3f763ddf28 (fetch: set remote/HEAD if it does not exist, 2024-11-22), unconditionally adds "HEAD" to the list of ref prefixes we send to the server.

This breaks a core assumption that the list of prefixes we send to the server is complete. We must either send all prefixes we care about, or none at all (in the latter case the server then advertises everything).

The tag following code is careful to only add "refs/tags/" to the list of prefixes if there are already entries in the prefix list. But because the new code from 3f763ddf28 runs after the tag code, and because it unconditionally adds to the prefix list, we may end up with a prefix list that _should_ have "refs/tags/" in it, but doesn't.

When that is the case, the server does not advertise any tags, and our auto-following breaks because we never learned about any tags in the first place.

Fix this by only adding "HEAD" to the ref prefixes when we know that we are already limiting the advertisement. In either case we'll learn about HEAD (either through the limited advertisement, or implicitly through a full advertisement).

---
Peff and I talked about this today, and neither of us could find any reasons not to pursue the approach listed in the footnote of

  <[email protected]>

, but this is a more conservative approach that should fix the issue and apply cleanly on top of 'maint'. It may be worth picking this into 2.49, even though we are already quite late into the -rc cycle, this is a fairly nasty bug.

builtin/fetch.c      |  4 +++-
t/t5503-tagfollow.sh | 15 +++++++++++++++

2 files changed, 18 insertions(+), 1 deletion(-)
builtin/fetch.c +3 -1
index fe2b26c74ae..a06d1305016 100644
@@ -1777,7 +1777,9 @@ static int do_fetch(struct transport *transport,
 
     if (uses_remote_tracking(transport, rs)) {
         must_list_refs = 1;
-       strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
+       if (transport_ls_refs_options.ref_prefixes.nr)
+           strvec_push(&transport_ls_refs_options.ref_prefixes,
+                   "HEAD");
     }
 
     if (must_list_refs) {
t/t5503-tagfollow.sh +0 -0
index 195fc64dd44..8495730e264 100755
@@ -160,4 +160,19 @@ test_expect_success 'new clone fetch main and 

tags' '
    test_cmp expect actual
'

t/t5503-tagfollow.sh +15 -0
+test_expect_success 'fetch specific OID with tag following' '
+   rm -f $U &&
+   git init --bare clone3.git &&
+   (
+       cd clone3.git &&
+       git remote add origin .. &&
+       git fetch origin $B:refs/heads/main &&
+
+       git -C .. for-each-ref >expect &&
+       git for-each-ref >actual &&
+
+       test_cmp expect actual
+   )
+'
+

test_done

Quoted 3 lines

base-commit: f93ff170b93a1782659637824b25923245ac9dd1 --
2.48.1.1.g965d2fe18fa

Thanks for cleaning up my mess!

#9 lore raw

cover fetch: further ref-prefix cleanups and optimizations

In reply to Taylor Blau
Quoted 9 lines · Taylor Blau

Peff and I talked about this today, and neither of us could find any reasons not to pursue the approach listed in the footnote of

<[email protected]>

, but this is a more conservative approach that should fix the issue and apply cleanly on top of 'maint'. It may be worth picking this into 2.49, even though we are already quite late into the -rc cycle, this is a fairly nasty bug.

Yeah, I favor doing this simple fix first, and then trying the larger (and slightly riskier) change on top.

I started to write up that larger patch, and found a number of interesting things. ;) So here's a 9-patch series, which would apply on top of tb/fetch-follow-tags-fix (but can very much wait to cook in the next development cycle).

[1/9]: t5702: fix typo in test name
[2/9]: t5516: prefer "oid" to "sha1" in some test titles [3/9]: t5516: drop NEEDSWORK about v2 reachability behavior

Just some cosmetic fixes in nearby areas.

[4/9]: t5516: beef up exact-oid ref prefixes test

Improving test coverage in an area I'm about to touch.

[5/9]: refspec_ref_prefixes(): clean up refspec_item logic

Code clean-up. Not necessary (textually or semantically) for the other patches.

[6/9]: fetch: ask server to advertise HEAD for config-less fetch [7/9]: fetch: stop protecting additions to ref-prefix list [8/9]: fetch: avoid ls-refs only to ask for HEAD symref update

Here we get to the interesting bits. These get rid of the subtle dependency that led to this bug in the first place, and also optimize a few cases where we can narrow the ref advertisement.

The third one is a bit optimization, where we avoid a whole round-trip to the server. It's correct as far as the rest of the code behaves, but I'm not 100% sure that the new origin/HEAD update feature isn't a little buggy. See my comments there.

[9/9]: fetch: use ref prefix list to skip ls-refs

And then this is just a final bit of cleanup enabled by the earlier patches.

 builtin/fetch.c        | 46 +++++++++++++++++-------------------------
 refspec.c              | 22 ++++++++++++++------
 t/t5516-fetch-push.sh  | 12 ++++++-----
 t/t5702-protocol-v2.sh | 44 +++++++++++++++++++++++++++++++++++++++-
 4 files changed, 85 insertions(+), 39 deletions(-)

-Peff

#10 lore raw

PATCH 1/9 t5702: fix typo in test name

---
 t/t5702-protocol-v2.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
t/t5702-protocol-v2.sh +1 -1
index d3df81e785..cea8f92a3d 100755
@@ -665,7 +665,7 @@ test_expect_success 'even with handcrafted request, filter does not work if not
 	test-tool -C server serve-v2 --stateless-rpc <in >/dev/null
 '
 
-test_expect_success 'default refspec is used to filter ref when fetchcing' '
+test_expect_success 'default refspec is used to filter ref when fetching' '
 	test_when_finished "rm -f log" &&
 
 	GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
Signature · 1 line
2.49.0.rc1.381.gc60f5426ff
#11 lore raw

Re: [PATCH 1/9] t5702: fix typo in test name

Quoted 10 lines · Jeff King
---
 t/t5702-protocol-v2.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
t/t5702-protocol-v2.sh +0 -0
index d3df81e785..cea8f92a3d 100755

t/t5702-protocol-v2.sh test_expect_success 'even with handcrafted request, filter does not work if not · line 668

Quoted 5 lines
t/t5702-protocol-v2.sh +1 -1
@@ -665,7 +665,7 @@ test_expect_success 'even with handcrafted request, filter does not work if not
 	test-tool -C server serve-v2 --stateless-rpc <in >/dev/null
 '
 
-test_expect_success 'default refspec is used to filter ref when fetchcing' '
+test_expect_success 'default refspec is used to filter ref when fetching' '

Maybe I am not seeing enough of the context below to know for sure, but

    s/ref/&s

?

Thanks,
Taylor

#12 lore raw

Re: [PATCH 1/9] t5702: fix typo in test name

Quoted 14 lines · Taylor Blau
Quoted 13 lines · Jeff King
---
 t/t5702-protocol-v2.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
t/t5702-protocol-v2.sh +0 -0
index d3df81e785..cea8f92a3d 100755
@@ -665,7 +665,7 @@ test_expect_success 'even with handcrafted request, filter does not work if not
 	test-tool -C server serve-v2 --stateless-rpc <in >/dev/null
 '
 
Quoted 8 lines

t/t5702-protocol-v2.sh test_expect_success 'even with handcrafted request, filter does not work if not · line 668

Quoted 2 lines
t/t5702-protocol-v2.sh +1 -1
-test_expect_success 'default refspec is used to filter ref when fetchcing' '
+test_expect_success 'default refspec is used to filter ref when fetching' '

Maybe I am not seeing enough of the context below to know for sure, but

    s/ref/&s

?

Possibly.

I think you could read it as "it should filter refs" (which it obviously should). But you could also read it as "it should filter some singular ref that we do not expect to find in the result". Of course the test itself seems to just snoop on the protocol conversation, which I guess is more like the first reading.

I didn't really want to get into word-smithing the test titles. I just couldn't handle seeing that typo over and over while working on adjacent tests.

-Peff

#13 lore raw

PATCH 2/9 t5516: prefer "oid" to "sha1" in some test titles

In reply to Jeff King

These old tests refer to object ids as "sha1". These days we prefer the more algorithm-agnostic "oid".

There are a few more tests that mention sha1 in the title and also use it in variables throughout the test. I've left them for now, as changing them is more involved (and they're linked to the allowTipSHA1InWant config, which as a v0-only thing actually is always sha1).

---
 t/t5516-fetch-push.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
t/t5516-fetch-push.sh +3 -3
index 85ed049627..e7629fc536 100755
@@ -495,7 +495,7 @@ test_expect_success 'push tag with non-existent, incomplete dest' '
 
 '
 
-test_expect_success 'push sha1 with non-existent, incomplete dest' '
+test_expect_success 'push oid with non-existent, incomplete dest' '
 
 	mk_test testrepo &&
 	test_must_fail git push testrepo $(git rev-parse main):foo
@@ -1251,7 +1251,7 @@ do
 	'
 done
 
-test_expect_success 'fetch exact SHA1' '
+test_expect_success 'fetch exact oid' '
 	mk_test testrepo heads/main hidden/one &&
 	git push testrepo main:refs/hidden/one &&
 	(
@@ -1297,7 +1297,7 @@ test_expect_success 'fetch exact SHA1' '
 	)
 '
 
-test_expect_success 'fetch exact SHA1 in protocol v2' '
+test_expect_success 'fetch exact oid in protocol v2' '
 	mk_test testrepo heads/main hidden/one &&
 	git push testrepo main:refs/hidden/one &&
 	git -C testrepo config transfer.hiderefs refs/hidden &&
Signature · 1 line
2.49.0.rc1.381.gc60f5426ff
#14 lore raw

PATCH 3/9 t5516: drop NEEDSWORK about v2 reachability behavior

In reply to Jeff King

When this test was added in 6c301adb0a (fetch: do not pass ref-prefixes for fetch by exact SHA1, 2018-05-31), there was still some uncertainty about the v2 protocol's looser behavior with serving objects that are not directly pointed at by a ref.

At this point that behavior is well established, and I do not think we would ever change v2 to match the v0 behavior (and if we did, remembering to update this test is the least of our concerns).

---
 t/t5516-fetch-push.sh | 1 -
 1 file changed, 1 deletion(-)
t/t5516-fetch-push.sh +0 -1
index e7629fc536..e4008f3ca6 100755
@@ -1312,7 +1312,6 @@ test_expect_success 'fetch exact oid in protocol v2' '
 	test_must_fail git -C child cat-file -t $the_commit &&
 
 	# fetching the hidden object succeeds by default
-	# NEEDSWORK: should this match the v0 behavior instead?
 	git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
 '
 
Signature · 1 line
2.49.0.rc1.381.gc60f5426ff
#15 lore raw

Re: [PATCH 3/9] t5516: drop NEEDSWORK about v2 reachability behavior

Quoted 3 lines · Jeff King

At this point that behavior is well established, and I do not think we would ever change v2 to match the v0 behavior (and if we did, remembering to update this test is the least of our concerns).

;-), indeed.

Thanks,
Taylor

#16 lore raw

PATCH 4/9 t5516: beef up exact-oid ref prefixes test

In reply to Jeff King

Commit 6c301adb0a (fetch: do not pass ref-prefixes for fetch by exact SHA1, 2018-05-31) added a test that fetching an exact oid with the v2 protocol works. Originally it failed without the code change from that commit, because fetch failed with "no matching remote head".

That changed in 0177565148 (transport: do not list refs if possible, 2018-09-27), which made fetch more forgiving of this case.

But that now meant the test passes even without its fix! So let's also have it check the packet listing to make sure we did not ask for the bogus prefix (ultimately this is less important than whether the command fails, since it's just an optimization, but we should make sure not to regress it).

---
 t/t5516-fetch-push.sh | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
t/t5516-fetch-push.sh +4 -1
index e4008f3ca6..2904399e97 100755
@@ -1312,7 +1312,10 @@ test_expect_success 'fetch exact oid in protocol v2' '
 	test_must_fail git -C child cat-file -t $the_commit &&
 
 	# fetching the hidden object succeeds by default
-	git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
+	GIT_TRACE_PACKET=$PWD/trace.out \
+	git -C child fetch -v ../testrepo $the_commit:refs/heads/copy &&
+
+	test_grep ! "ref-prefix.*$the_commit" trace.out
 '
 
 for configallowtipsha1inwant in true false
Signature · 1 line
2.49.0.rc1.381.gc60f5426ff
#17 lore raw

PATCH 5/9 refspec_ref_prefixes(): clean up refspec_item logic

In reply to Jeff King

The point of refspec_ref_prefixes() is to look over the set of refspecs and set up an appropriate list of "ref-prefix" strings to send to the server.

The logic for handling individual refspec_items has some confusing bits. The final part of our if/else cascade checks this:

else if (item->src && !item->exact_sha1)

	prefix = item->src;

But we know that "item->exact_sha1" can never be true, because earlier we did:

if (item->exact_sha1 || item->negative)

	continue;

This is due to 6c301adb0a (fetch: do not pass ref-prefixes for fetch by exact SHA1, 2018-05-31), which added the continue. So it is tempting to remove the extra exact_sha1 at the end of the cascade, leaving the one at the top of the loop.

But I don't think that's quite right. The full cascade is:

if (rs->fetch == REFSPEC_FETCH)

	prefix = item->src;
  else if (item->dst)
	prefix = item->dst;
  else if (item->src && !item->exact_sha1)
	prefix = item->src;

which all comes from 6373cb598e (refspec: consolidate ref-prefix generation logic, 2018-05-16). That first "if" is supposed to handle fetches, where we care about the source name, since that is coming from the server. And the rest should be for pushes, where we care about the destination, since that's the name the server will use. And we get that either explicitly from "dst" (for something like "foo:bar") or implicitly from the source (a refspec like "foo" is treated as "foo:foo").

But how should exact_sha1 interact with those? For a fetch, exact_sha1 always means we do not care about sending a name to the server (there is no server refname at all). But pushing an exact sha1 should still care about the destination on the server! It is only if we have to fall back to the implicit source that we need to care if it is a real ref (though arguably such a push does not even make sense; where would the server store it?).

So I think that 6c301adb0a "broke" the push case by always skipping exact_sha1 items, even though a push should only care about the destination.

Of course this is all completely academic. We have still not implemented a v2 push protocol, so even though we do call this function for pushes, we'd never actually send these ref-prefix lines.

However, given the effort I spent to figure out what was going on here, and the overlapping exact_sha1 checks, I'd like to rewrite this to preemptively fix the bug, and hopefully make it less confusing.

This splits the "if" at the top-level into fetch vs push, and then each handles exact_sha1 appropriately itself. The check for negative refspecs remains outside of either (there is no protocol support for them, so we never send them to the server, but rather use them only to reduce the advertisement we receive).

The resulting behavior should be identical for fetches, but hopefully sets us up better for a potential future v2 push.

---
This could be dropped without affecting the rest of the series if it's too churn-y.

 refspec.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
refspec.c +16 -6
index 4cb80b5208..c6ad515f04 100644
@@ -246,14 +246,24 @@ void refspec_ref_prefixes(const struct refspec *rs,
 		const struct refspec_item *item = &rs->items[i];
 		const char *prefix = NULL;
 
-		if (item->exact_sha1 || item->negative)
+		if (item->negative)
 			continue;
-		if (rs->fetch == REFSPEC_FETCH)
-			prefix = item->src;
-		else if (item->dst)
-			prefix = item->dst;
-		else if (item->src && !item->exact_sha1)
+
+		if (rs->fetch == REFSPEC_FETCH) {
+			if (item->exact_sha1)
+				continue;
 			prefix = item->src;
+		} else {
+			/*
+			 * Pushes can have an explicit destination like
+			 * "foo:bar", or can implicitly use the src for both
+			 * ("foo" is the same as "foo:foo").
+			 */
+			if (item->dst)
+				prefix = item->dst;
+			else if (item->src && !item->exact_sha1)
+				prefix = item->src;
+		}
 
 		if (!prefix)
 			continue;
Signature · 1 line
2.49.0.rc1.381.gc60f5426ff
#18 lore raw

Re: [PATCH 5/9] refspec_ref_prefixes(): clean up refspec_item logic

Quoted 3 lines · Jeff King

The point of refspec_ref_prefixes() is to look over the set of refspecs and set up an appropriate list of "ref-prefix" strings to send to the server.

While we're cleaning things up, I wonder if it is worth (slightly) renaming this function to something more descriptive, like:

refspecs_to_ref_prefixes()

, where we pluralize "refspec" and add "to" to make it clear that we're converting from one to the other.

Quoted 7 lines

Of course this is all completely academic. We have still not implemented a v2 push protocol, so even though we do call this function for pushes, we'd never actually send these ref-prefix lines.

However, given the effort I spent to figure out what was going on here, and the overlapping exact_sha1 checks, I'd like to rewrite this to preemptively fix the bug, and hopefully make it less confusing.

All makes sense.

Quoted 25 lines

This splits the "if" at the top-level into fetch vs push, and then each handles exact_sha1 appropriately itself. The check for negative refspecs remains outside of either (there is no protocol support for them, so we never send them to the server, but rather use them only to reduce the advertisement we receive).

The resulting behavior should be identical for fetches, but hopefully sets us up better for a potential future v2 push.

---
This could be dropped without affecting the rest of the series if it's too churn-y.

 refspec.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
refspec.c +0 -0
index 4cb80b5208..c6ad515f04 100644
@@ -246,14 +246,24 @@ void refspec_ref_prefixes(const struct refspec *rs,
 		const struct refspec_item *item = &rs->items[i];
 		const char *prefix = NULL;
 

refspec.c void refspec_ref_prefixes(const struct refspec *rs, · line 252

Quoted 10 lines
refspec.c +3 -6
-		if (item->exact_sha1 || item->negative)
+		if (item->negative)
 			continue;
-		if (rs->fetch == REFSPEC_FETCH)
-			prefix = item->src;
-		else if (item->dst)
-			prefix = item->dst;
-		else if (item->src && !item->exact_sha1)
+
+		if (rs->fetch == REFSPEC_FETCH) {

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 :-).

Quoted 4 lines
refspec.c +3 -0
+			if (item->exact_sha1)
+				continue;
 			prefix = item->src;
+		} else {

refspec.c void refspec_ref_prefixes(const struct refspec *rs, · line 266

Quoted 10 lines
refspec.c +10 -0
+			/*
+			 * Pushes can have an explicit destination like
+			 * "foo:bar", or can implicitly use the src for both
+			 * ("foo" is the same as "foo:foo").
+			 */
+			if (item->dst)
+				prefix = item->dst;
+			else if (item->src && !item->exact_sha1)
+				prefix = item->src;
+		}

All makes sense.

Thanks,
Taylor

#19 lore raw

Re: [PATCH 5/9] refspec_ref_prefixes(): clean up refspec_item logic

Quoted 5 lines · Taylor Blau
Quoted 3 lines · Jeff King

The point of refspec_ref_prefixes() is to look over the set of refspecs and set up an appropriate list of "ref-prefix" strings to send to the server.

Quoted 7 lines

While we're cleaning things up, I wonder if it is worth (slightly) renaming this function to something more descriptive, like:

refspecs_to_ref_prefixes()

, where we pluralize "refspec" and add "to" to make it clear that we're converting from one to the other.

I think a "struct refspec" is plural (despite the name). The singular is "refspec_item".

And...

Quoted 8 lines
Quoted 8 lines
refspec.c +0 -0
index 4cb80b5208..c6ad515f04 100644
@@ -246,14 +246,24 @@ void refspec_ref_prefixes(const struct refspec *rs,
 		const struct refspec_item *item = &rs->items[i];
 		const char *prefix = NULL;
 
Quoted 14 lines

refspec.c void refspec_ref_prefixes(const struct refspec *rs, · line 252

Quoted 10 lines
refspec.c +3 -6
-		if (item->exact_sha1 || item->negative)
+		if (item->negative)
 			continue;
-		if (rs->fetch == REFSPEC_FETCH)
-			prefix = item->src;
-		else if (item->dst)
-			prefix = item->dst;
-		else if (item->src && !item->exact_sha1)
+
+		if (rs->fetch == REFSPEC_FETCH) {

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. ;)

-Peff

#20 lore raw

Re: [PATCH 5/9] refspec_ref_prefixes(): clean up refspec_item logic

Quoted 10 lines · Jeff King
Quoted 5 lines

refspec.c void refspec_ref_prefixes(const struct refspec *rs, · line 252

Quoted 1 line
refspec.c +1 -0
+		if (rs->fetch == REFSPEC_FETCH) {

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?

Sorry, I suspect that it is my fault. I've never been good at naming, and if the allowed values for this member are FETCH and PUSH, then the member itself shouldn't be called fetch or push.

Perhaps 'direction'.

But such a name improvement is clearly outside the scope of this fix.

Thanks.

#21 lore raw

cover refspec: treat 'fetch' as a Boolean value

This series explores treating the 'fetch' field of 'struct refspec' as a Boolean, instead of taking on values called "REFSPEC_FETCH" and "REFSPEC_PUSH".

The refactoring takes place in a couple of steps, but IMHO the end result is pleasing and an improvement in clarity over the existing API.

For reference, in one of the patches above, I had suggested replacing something like:

    if (refspec->fetch == REFSPEC_FETCH) {
      /* something */
    } else {
      /* something else */
    }

with a switch/case-statement, only to realize that 'refspec->fetch' is really a Boolean in disguise.

I am not overly attached to these patches one way or the other. I think they do improve the refspec.h API, but they're also ripe for bike-shedding. So if we agree that they are good, I'm happy to see them move forward. But if not, I'd rather not spend the list's time bike-shedding here, so I'd just as soon drop them.

Thanks in advance for your review!

Taylor Blau (4):
refspec: treat 'fetch' as a Boolean value
refspec: replace refspec_init() with fetch/push variants
refspec: remove refspec_item_init_or_die()
refspec: replace refspec_item_init() with fetch/push variants

 builtin/fetch.c    |   2 +-
 builtin/pull.c     |   3 ++-
 git-diff-pairs     | Bin 0 -> 12166200 bytes
 refspec.c          |  38 +++++++++++++++++++++++++++-----------
 refspec.h          |  18 +++++++-----------
 remote.c           |   4 ++--
 transport-helper.c |   2 +-
 7 files changed, 40 insertions(+), 27 deletions(-)
 create mode 100755 git-diff-pairs

base-commit: c702dd48567cfebca3d4a06b691de97da3f8dc4a

Signature · 1 line
2.49.0.rc2.13.g95783265fd
#22 lore raw

PATCH 1/4 refspec: treat 'fetch' as a Boolean value

Since 6d4c057859 (refspec: introduce struct refspec, 2018-05-16), we have constants called REFSPEC_FETCH and REFSPEC_PUSH. This misleadingly suggests that we might introduce other modes in the future.

But these values are treated as a Boolean, and stored in a struct field called 'fetch'. So the following:

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

, and

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

are equivalent. Let's avoid renaming the Boolean values "true" and "false" here and remove the two REFSPEC_ constants mentioned above.

Since this value is truly a Boolean and will only ever take on a value of 0 or 1, we can declare it as a single bit unsigned field. In practice this won't shrink the size of 'struct refspec', but it more clearly indicates the intent.

Note that this introduces some awkwardness like:

    refspec_item_init_or_die(&spec, refspec, 1);

, where it's unclear what the final "1" does. This will be addressed in the following commits.

---
 builtin/pull.c     |   2 +-
 git-diff-pairs     | Bin 0 -> 12166200 bytes
 refspec.c          |   4 ++--
 refspec.h          |   9 +++------
 remote.c           |   4 ++--
 transport-helper.c |   2 +-
 6 files changed, 9 insertions(+), 12 deletions(-)
 create mode 100755 git-diff-pairs
builtin/pull.c +1 -1
index 9c4a00620a..8bbfcce729 100644
@@ -738,7 +738,7 @@ static const char *get_tracking_branch(const char *remote, const char *refspec)
 	const char *spec_src;
 	const char *merge_branch;
 
-	refspec_item_init_or_die(&spec, refspec, REFSPEC_FETCH);
+	refspec_item_init_or_die(&spec, refspec, 1);
 	spec_src = spec.src;
 	if (!*spec_src || !strcmp(spec_src, "HEAD"))
 		spec_src = "HEAD";
git-diff-pairs new file binary +0 -0
new file mode 100755
index 0000000000000000000000000000000000000000..8eebe3117848710e6c09590e6e5481111801d698
GIT binary patch
refspec.c +2 -2
index c6ad515f04..db5a1c34a5 100644
@@ -233,7 +233,7 @@ void refspec_clear(struct refspec *rs)
 int valid_fetch_refspec(const char *fetch_refspec_str)
 {
 	struct refspec_item refspec;
-	int ret = refspec_item_init(&refspec, fetch_refspec_str, REFSPEC_FETCH);
+	int ret = refspec_item_init(&refspec, fetch_refspec_str, 1);
 	refspec_item_clear(&refspec);
 	return ret;
 }
@@ -249,7 +249,7 @@ void refspec_ref_prefixes(const struct refspec *rs,
 		if (item->negative)
 			continue;
 
-		if (rs->fetch == REFSPEC_FETCH) {
+		if (rs->fetch) {
 			if (item->exact_sha1)
 				continue;
 			prefix = item->src;
refspec.h +3 -6
index e2b5cc54ef..155494cd3a 100644
@@ -32,11 +32,8 @@ struct refspec_item {
 
 struct string_list;
 
-#define REFSPEC_FETCH 1
-#define REFSPEC_PUSH 0
-
-#define REFSPEC_INIT_FETCH { .fetch = REFSPEC_FETCH }
-#define REFSPEC_INIT_PUSH { .fetch = REFSPEC_PUSH }
+#define REFSPEC_INIT_FETCH { .fetch = 1 }
+#define REFSPEC_INIT_PUSH { .fetch = 0 }
 
 /**
  * An array of strings can be parsed into a struct refspec using
@@ -47,7 +44,7 @@ struct refspec {
 	int alloc;
 	int nr;
 
-	int fetch;
+	unsigned fetch : 1;
 };
 
 int refspec_item_init(struct refspec_item *item, const char *refspec,
remote.c +2 -2
index e609cf5c56..addd4a9999 100644
@@ -143,8 +143,8 @@ static struct remote *make_remote(struct remote_state *remote_state,
 	ret->prune = -1;  /* unspecified */
 	ret->prune_tags = -1;  /* unspecified */
 	ret->name = xstrndup(name, len);
-	refspec_init(&ret->push, REFSPEC_PUSH);
-	refspec_init(&ret->fetch, REFSPEC_FETCH);
+	refspec_init(&ret->push, 0);
+	refspec_init(&ret->fetch, 1);
 	string_list_init_dup(&ret->server_options);
 
 	ALLOC_GROW(remote_state->remotes, remote_state->remotes_nr + 1,
transport-helper.c +1 -1
index d457b42550..43cd760119 100644
@@ -162,7 +162,7 @@ static struct child_process *get_helper(struct transport *transport)
 
 	data->helper = helper;
 	data->no_disconnect_req = 0;
-	refspec_init(&data->rs, REFSPEC_FETCH);
+	refspec_init(&data->rs, 1);
 
 	/*
 	 * Open the output as FILE* so strbuf_getline_*() family of
Signature · 1 line
2.49.0.rc2.13.g95783265fd
#23 lore raw

Re: [PATCH 1/4] refspec: treat 'fetch' as a Boolean value

Quoted 3 lines · Taylor Blau

Since 6d4c057859 (refspec: introduce struct refspec, 2018-05-16), we have constants called REFSPEC_FETCH and REFSPEC_PUSH. This misleadingly suggests that we might introduce other modes in the future.

I don't know that I'd call it misleading. We _could_ introduce new modes if we had new operations. But I do agree it's unlikely (even if we had other operations like git-archive, cat-file, etc, they would probably not have refspecs).

So it seems like a reasonable direction to me.

The one thing I don't like is:

builtin/pull.c static const char *get_tracking_branch(const char *remote, const char *refspec) · line 741

Quoted 10 lines
builtin/pull.c +1 -1
index 9c4a00620a..8bbfcce729 100644
@@ -738,7 +738,7 @@ static const char *get_tracking_branch(const char *remote, const char *refspec)
 	const char *spec_src;
 	const char *merge_branch;
 
-	refspec_item_init_or_die(&spec, refspec, REFSPEC_FETCH);
+	refspec_item_init_or_die(&spec, refspec, 1);

The third argument here (and elsewhere) becomes much more mysterious to the reader. Maybe not a big deal, though.

git-diff-pairs

Quoted 1 line
git-diff-pairs +0 -0

Hmm.... :)

-Peff

#24 lore raw

Re: [PATCH 1/4] refspec: treat 'fetch' as a Boolean value

Quoted 5 lines · Jeff King

builtin/pull.c static const char *get_tracking_branch(const char *remote, const char *refspec) · line 741

Quoted 2 lines
builtin/pull.c +1 -1
-	refspec_item_init_or_die(&spec, refspec, REFSPEC_FETCH);
+	refspec_item_init_or_die(&spec, refspec, 1);

The third argument here (and elsewhere) becomes much more mysterious to the reader. Maybe not a big deal, though.

Oh, I should have read the other patches. :)

The end result resolves my complaint nicely.

-Peff

#25 lore raw

Re: [PATCH 1/4] refspec: treat 'fetch' as a Boolean value

In reply to Jeff King
Quoted 6 lines · Jeff King
Quoted 3 lines · Taylor Blau

Since 6d4c057859 (refspec: introduce struct refspec, 2018-05-16), we have constants called REFSPEC_FETCH and REFSPEC_PUSH. This misleadingly suggests that we might introduce other modes in the future.

Quoted 6 lines

I don't know that I'd call it misleading. We _could_ introduce new modes if we had new operations. But I do agree it's unlikely (even if we had other operations like git-archive, cat-file, etc, they would probably not have refspecs).

So it seems like a reasonable direction to me.

Fair, I think a more accurate statement might be to swap "misleadingly" for "confusingly". I'll swap that and amend the paragraph to say "[...], which is possible, but highly unlikely"

Quoted 10 lines

The one thing I don't like is:

Quoted 8 lines
builtin/pull.c +0 -0
index 9c4a00620a..8bbfcce729 100644
@@ -738,7 +738,7 @@ static const char *get_tracking_branch(const char *remote, const char *refspec)
 	const char *spec_src;
 	const char *merge_branch;
 
Quoted 5 lines

builtin/pull.c static const char *get_tracking_branch(const char *remote, const char *refspec) · line 741

Quoted 2 lines
builtin/pull.c +1 -1
-	refspec_item_init_or_die(&spec, refspec, REFSPEC_FETCH);
+	refspec_item_init_or_die(&spec, refspec, 1);

The third argument here (and elsewhere) becomes much more mysterious to the reader. Maybe not a big deal, though.

Hmm. I see later on in the thread that the final patch resolves this awkwardness. I figured that readers might have a similar thought here, which is why I included "Note that this introduces some awkwardness like [...]", so perhaps there is a way to clarify that. But...

Quoted 3 lines

git-diff-pairs

Quoted 1 line
git-diff-pairs +0 -0

Hmm.... :)

...I could forgive you for not noticing given this ugliness ;-).

Thanks,
Taylor

#26 lore raw

PATCH 2/4 refspec: replace `refspec_init()` with fetch/push variants

In reply to Taylor Blau

To avoid having a Boolean argument in the refspec_init() function, replace it with two variants:

- refspec_init_fetch()
- refspec_init_push()

to codify the meaning of that Boolean into the function's name itself.

---
 refspec.c          | 12 +++++++++---
 refspec.h          |  3 ++-
 remote.c           |  4 ++--
 transport-helper.c |  2 +-
 4 files changed, 14 insertions(+), 7 deletions(-)
refspec.c +9 -3
index db5a1c34a5..f6be0c54d7 100644
@@ -178,10 +178,16 @@ void refspec_item_clear(struct refspec_item *item)
 	item->exact_sha1 = 0;
 }
 
-void refspec_init(struct refspec *rs, int fetch)
+void refspec_init_fetch(struct refspec *rs)
 {
-	memset(rs, 0, sizeof(*rs));
-	rs->fetch = fetch;
+	struct refspec blank = REFSPEC_INIT_FETCH;
+	memcpy(rs, &blank, sizeof(*rs));
+}
+
+void refspec_init_push(struct refspec *rs)
+{
+	struct refspec blank = REFSPEC_INIT_PUSH;
+	memcpy(rs, &blank, sizeof(*rs));
 }
 
 void refspec_append(struct refspec *rs, const char *refspec)
refspec.h +2 -1
index 155494cd3a..7db68e56c8 100644
@@ -52,7 +52,8 @@ int refspec_item_init(struct refspec_item *item, const char *refspec,
 void refspec_item_init_or_die(struct refspec_item *item, const char *refspec,
 			      int fetch);
 void refspec_item_clear(struct refspec_item *item);
-void refspec_init(struct refspec *rs, int fetch);
+void refspec_init_fetch(struct refspec *rs);
+void refspec_init_push(struct refspec *rs);
 void refspec_append(struct refspec *rs, const char *refspec);
 __attribute__((format (printf,2,3)))
 void refspec_appendf(struct refspec *rs, const char *fmt, ...);
remote.c +2 -2
index addd4a9999..25af97a44b 100644
@@ -143,8 +143,8 @@ static struct remote *make_remote(struct remote_state *remote_state,
 	ret->prune = -1;  /* unspecified */
 	ret->prune_tags = -1;  /* unspecified */
 	ret->name = xstrndup(name, len);
-	refspec_init(&ret->push, 0);
-	refspec_init(&ret->fetch, 1);
+	refspec_init_push(&ret->push);
+	refspec_init_fetch(&ret->fetch);
 	string_list_init_dup(&ret->server_options);
 
 	ALLOC_GROW(remote_state->remotes, remote_state->remotes_nr + 1,
transport-helper.c +1 -1
index 43cd760119..69391ee7d2 100644
@@ -162,7 +162,7 @@ static struct child_process *get_helper(struct transport *transport)
 
 	data->helper = helper;
 	data->no_disconnect_req = 0;
-	refspec_init(&data->rs, 1);
+	refspec_init_fetch(&data->rs);
 
 	/*
 	 * Open the output as FILE* so strbuf_getline_*() family of
Signature · 1 line
2.49.0.rc2.13.g95783265fd
#27 lore raw

PATCH 3/4 refspec: remove refspec_item_init_or_die()

In reply to Taylor Blau

There are two callers of this function, which ensures that a dispatched call to refspec_item_init() does not fail.

In the following commit, we're going to add fetch/push-specific variants of refspec_item_init(), which will turn one function into two. To avoid introducing yet another pair of new functions (such as refspec_item_init_push_or_die() and refspec_item_init_fetch_or_die()), let's remove the thin wrapper entirely.

This duplicates a single line of code among two callers, but thins the refspec.h API by one function, and prevents introducing two more in the following commit.

Note that we still have a trailing Boolean argument in the function refspec_item_init(). The following commit will address this.

---
 builtin/pull.c |  3 ++-
 refspec.c      | 10 ++--------
 refspec.h      |  2 --
 3 files changed, 4 insertions(+), 11 deletions(-)
builtin/pull.c +2 -1
index 8bbfcce729..a68a9955de 100644
@@ -738,7 +738,8 @@ static const char *get_tracking_branch(const char *remote, const char *refspec)
 	const char *spec_src;
 	const char *merge_branch;
 
-	refspec_item_init_or_die(&spec, refspec, 1);
+	if (!refspec_item_init(&spec, refspec, 1))
+		die(_("invalid refspec '%s'"), refspec);
 	spec_src = spec.src;
 	if (!*spec_src || !strcmp(spec_src, "HEAD"))
 		spec_src = "HEAD";
refspec.c +2 -8
index f6be0c54d7..3aeb697505 100644
@@ -160,13 +160,6 @@ int refspec_item_init(struct refspec_item *item, const char *refspec, int fetch)
 	return parse_refspec(item, refspec, fetch);
 }
 
-void refspec_item_init_or_die(struct refspec_item *item, const char *refspec,
-			      int fetch)
-{
-	if (!refspec_item_init(item, refspec, fetch))
-		die(_("invalid refspec '%s'"), refspec);
-}
-
 void refspec_item_clear(struct refspec_item *item)
 {
 	FREE_AND_NULL(item->src);
@@ -194,7 +187,8 @@ void refspec_append(struct refspec *rs, const char *refspec)
 {
 	struct refspec_item item;
 
-	refspec_item_init_or_die(&item, refspec, rs->fetch);
+	if (!refspec_item_init(&item, refspec, rs->fetch))
+		die(_("invalid refspec '%s'"), refspec);
 
 	ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
 	rs->items[rs->nr] = item;
refspec.h +0 -2
index 7db68e56c8..614f34554e 100644
@@ -49,8 +49,6 @@ struct refspec {
 
 int refspec_item_init(struct refspec_item *item, const char *refspec,
 		      int fetch);
-void refspec_item_init_or_die(struct refspec_item *item, const char *refspec,
-			      int fetch);
 void refspec_item_clear(struct refspec_item *item);
 void refspec_init_fetch(struct refspec *rs);
 void refspec_init_push(struct refspec *rs);
Signature · 1 line
2.49.0.rc2.13.g95783265fd
#28 lore raw

PATCH 4/4 refspec: replace `refspec_item_init()` with fetch/push variants

In reply to Taylor Blau

For similar reasons as in the previous refactoring of refspec_init() into refspec_init_fetch() and refspec_init_push(), apply the same refactoring to refspec_item_init().

---
 builtin/fetch.c |  2 +-
 builtin/pull.c  |  2 +-
 refspec.c       | 22 +++++++++++++++++++---
 refspec.h       |  4 ++--
 4 files changed, 23 insertions(+), 7 deletions(-)
builtin/fetch.c +1 -1
index 02af505469..9830c09011 100644
@@ -586,7 +586,7 @@ static struct ref *get_ref_map(struct remote *remote,
 		struct refspec_item tag_refspec;
 
 		/* also fetch all tags */
-		refspec_item_init(&tag_refspec, TAG_REFSPEC, 0);
+		refspec_item_init_push(&tag_refspec, TAG_REFSPEC);
 		get_fetch_map(remote_refs, &tag_refspec, &tail, 0);
 		refspec_item_clear(&tag_refspec);
 	} else if (tags == TAGS_DEFAULT && *autotags) {
builtin/pull.c +1 -1
index a68a9955de..a1ebc6ad33 100644
@@ -738,7 +738,7 @@ static const char *get_tracking_branch(const char *remote, const char *refspec)
 	const char *spec_src;
 	const char *merge_branch;
 
-	if (!refspec_item_init(&spec, refspec, 1))
+	if (!refspec_item_init_fetch(&spec, refspec))
 		die(_("invalid refspec '%s'"), refspec);
 	spec_src = spec.src;
 	if (!*spec_src || !strcmp(spec_src, "HEAD"))
refspec.c +19 -3
index 3aeb697505..0775358d96 100644
@@ -153,13 +153,24 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet
 	return 1;
 }
 
-int refspec_item_init(struct refspec_item *item, const char *refspec, int fetch)
+static int refspec_item_init(struct refspec_item *item, const char *refspec,
+			     int fetch)
 {
 	memset(item, 0, sizeof(*item));
 	item->raw = xstrdup(refspec);
 	return parse_refspec(item, refspec, fetch);
 }
 
+int refspec_item_init_fetch(struct refspec_item *item, const char *refspec)
+{
+	return refspec_item_init(item, refspec, 1);
+}
+
+int refspec_item_init_push(struct refspec_item *item, const char *refspec)
+{
+	return refspec_item_init(item, refspec, 0);
+}
+
 void refspec_item_clear(struct refspec_item *item)
 {
 	FREE_AND_NULL(item->src);
@@ -186,8 +197,13 @@ void refspec_init_push(struct refspec *rs)
 void refspec_append(struct refspec *rs, const char *refspec)
 {
 	struct refspec_item item;
+	int ret;
 
-	if (!refspec_item_init(&item, refspec, rs->fetch))
+	if (rs->fetch)
+		ret = refspec_item_init_fetch(&item, refspec);
+	else
+		ret = refspec_item_init_push(&item, refspec);
+	if (!ret)
 		die(_("invalid refspec '%s'"), refspec);
 
 	ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
@@ -233,7 +249,7 @@ void refspec_clear(struct refspec *rs)
 int valid_fetch_refspec(const char *fetch_refspec_str)
 {
 	struct refspec_item refspec;
-	int ret = refspec_item_init(&refspec, fetch_refspec_str, 1);
+	int ret = refspec_item_init_fetch(&refspec, fetch_refspec_str);
 	refspec_item_clear(&refspec);
 	return ret;
 }
refspec.h +2 -2
index 614f34554e..8b04f9995e 100644
@@ -47,8 +47,8 @@ struct refspec {
 	unsigned fetch : 1;
 };
 
-int refspec_item_init(struct refspec_item *item, const char *refspec,
-		      int fetch);
+int refspec_item_init_fetch(struct refspec_item *item, const char *refspec);
+int refspec_item_init_push(struct refspec_item *item, const char *refspec);
 void refspec_item_clear(struct refspec_item *item);
 void refspec_init_fetch(struct refspec *rs);
 void refspec_init_push(struct refspec *rs);
Signature · 1 line
2.49.0.rc2.13.g95783265fd
#29 lore raw

Re: [PATCH 0/4] refspec: treat 'fetch' as a Boolean value

In reply to Taylor Blau
Quoted 33 lines · Taylor Blau

This series explores treating the 'fetch' field of 'struct refspec' as a Boolean, instead of taking on values called "REFSPEC_FETCH" and "REFSPEC_PUSH".

The refactoring takes place in a couple of steps, but IMHO the end result is pleasing and an improvement in clarity over the existing API.

For reference, in one of the patches above, I had suggested replacing something like:

    if (refspec->fetch == REFSPEC_FETCH) {
      /* something */
    } else {
      /* something else */
    }

with a switch/case-statement, only to realize that 'refspec->fetch' is really a Boolean in disguise.

I am not overly attached to these patches one way or the other. I think they do improve the refspec.h API, but they're also ripe for bike-shedding. So if we agree that they are good, I'm happy to see them move forward. But if not, I'd rather not spend the list's time bike-shedding here, so I'd just as soon drop them.

Thanks in advance for your review!

Taylor Blau (4):
refspec: treat 'fetch' as a Boolean value
refspec: replace refspec_init() with fetch/push variants
refspec: remove refspec_item_init_or_die()
refspec: replace refspec_item_init() with fetch/push variants

Quoted 9 lines
 builtin/fetch.c    |   2 +-
 builtin/pull.c     |   3 ++-
 git-diff-pairs     | Bin 0 -> 12166200 bytes
 refspec.c          |  38 +++++++++++++++++++++++++++-----------
 refspec.h          |  18 +++++++-----------
 remote.c           |   4 ++--
 transport-helper.c |   2 +-
 7 files changed, 40 insertions(+), 27 deletions(-)
 create mode 100755 git-diff-pairs

Somebody does not proofread their cover letter. Sheesh ;-)

#30 lore raw

Re: [PATCH 0/4] refspec: treat 'fetch' as a Boolean value

Quoted 11 lines · Junio C Hamano
Quoted 9 lines
 builtin/fetch.c    |   2 +-
 builtin/pull.c     |   3 ++-
 git-diff-pairs     | Bin 0 -> 12166200 bytes
 refspec.c          |  38 +++++++++++++++++++++++++++-----------
 refspec.h          |  18 +++++++-----------
 remote.c           |   4 ++--
 transport-helper.c |   2 +-
 7 files changed, 40 insertions(+), 27 deletions(-)
 create mode 100755 git-diff-pairs

Somebody does not proofread their cover letter. Sheesh ;-)

Ugh, for goodness sake. Sorry about that, I'll send another round.

Thanks,
Taylor

#31 lore raw

v2 cover refspec: treat 'fetch' as a Boolean value

In reply to Junio C Hamano

Here's a small reroll that cleans up some wording in the first patch, and drops the accidental inclusion of a stray 'git-diff-pairs' binary that was sitting in my working tree at the time of writing the initial commit.

Otherwise the series is unchanged, but a range-diff is attached nonetheless. Thanks for reviewing :-).

Taylor Blau (4):
refspec: treat 'fetch' as a Boolean value
refspec: replace refspec_init() with fetch/push variants
refspec: remove refspec_item_init_or_die()
refspec: replace refspec_item_init() with fetch/push variants

 builtin/fetch.c    |  2 +-
 builtin/pull.c     |  3 ++-
 refspec.c          | 38 +++++++++++++++++++++++++++-----------
 refspec.h          | 18 +++++++-----------
 remote.c           |  4 ++--
 transport-helper.c |  2 +-
 6 files changed, 40 insertions(+), 27 deletions(-)

Range-diff against v1:
1: 7e662acb5a ! 1: 04e1ab8209 refspec: treat 'fetch' as a Boolean value
@@ Commit message
refspec: treat 'fetch' as a Boolean value

Since 6d4c057859 (refspec: introduce struct refspec, 2018-05-16), we
- have constants called REFSPEC_FETCH and REFSPEC_PUSH. This misleadingly
- suggests that we might introduce other modes in the future.
+ have macros called REFSPEC_FETCH and REFSPEC_PUSH. This confusingly
+ suggests that we might introduce other modes in the future, which, while
+ possible, is highly unlikely.

But these values are treated as a Boolean, and stored in a struct field called 'fetch'. So the following:
@@ Commit message
if (refspec->fetch) { ... }

are equivalent. Let's avoid renaming the Boolean values "true" and
- "false" here and remove the two REFSPEC_ constants mentioned above.
+ "false" here and remove the two REFSPEC_ macros mentioned above.

Since this value is truly a Boolean and will only ever take on a value of 0 or 1, we can declare it as a single bit unsigned field. In
@@ builtin/pull.c: static const char *get_tracking_branch(const char *remote, const
if (!*spec_src || !strcmp(spec_src, "HEAD"))
spec_src = "HEAD";

- ## git-diff-pairs (new) ##
- Binary files /dev/null and git-diff-pairs differ -
## refspec.c ##
@@ refspec.c: void refspec_clear(struct refspec *rs)
int valid_fetch_refspec(const char *fetch_refspec_str)

2:  fd2354dade = 2:  c3021b82ce refspec: replace `refspec_init()` with fetch/push variants
3:  49b470de61 = 3:  88f6a91c46 refspec: remove refspec_item_init_or_die()
4:  95783265fd = 4:  f0c323988f refspec: replace `refspec_item_init()` with fetch/push variants

base-commit: c702dd48567cfebca3d4a06b691de97da3f8dc4a

Signature · 1 line
2.49.0.3.gbb7a4a684c.dirty
#32 lore raw

PATCH v2 1/4 refspec: treat 'fetch' as a Boolean value

Since 6d4c057859 (refspec: introduce struct refspec, 2018-05-16), we have macros called REFSPEC_FETCH and REFSPEC_PUSH. This confusingly suggests that we might introduce other modes in the future, which, while possible, is highly unlikely.

But these values are treated as a Boolean, and stored in a struct field called 'fetch'. So the following:

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

, and

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

are equivalent. Let's avoid renaming the Boolean values "true" and "false" here and remove the two REFSPEC_ macros mentioned above.

Since this value is truly a Boolean and will only ever take on a value of 0 or 1, we can declare it as a single bit unsigned field. In practice this won't shrink the size of 'struct refspec', but it more clearly indicates the intent.

Note that this introduces some awkwardness like:

    refspec_item_init_or_die(&spec, refspec, 1);

, where it's unclear what the final "1" does. This will be addressed in the following commits.

---
 builtin/pull.c     | 2 +-
 refspec.c          | 4 ++--
 refspec.h          | 9 +++------
 remote.c           | 4 ++--
 transport-helper.c | 2 +-
 5 files changed, 9 insertions(+), 12 deletions(-)
builtin/pull.c +1 -1
index 9c4a00620a..8bbfcce729 100644
@@ -738,7 +738,7 @@ static const char *get_tracking_branch(const char *remote, const char *refspec)
 	const char *spec_src;
 	const char *merge_branch;
 
-	refspec_item_init_or_die(&spec, refspec, REFSPEC_FETCH);
+	refspec_item_init_or_die(&spec, refspec, 1);
 	spec_src = spec.src;
 	if (!*spec_src || !strcmp(spec_src, "HEAD"))
 		spec_src = "HEAD";
refspec.c +2 -2
index c6ad515f04..db5a1c34a5 100644
@@ -233,7 +233,7 @@ void refspec_clear(struct refspec *rs)
 int valid_fetch_refspec(const char *fetch_refspec_str)
 {
 	struct refspec_item refspec;
-	int ret = refspec_item_init(&refspec, fetch_refspec_str, REFSPEC_FETCH);
+	int ret = refspec_item_init(&refspec, fetch_refspec_str, 1);
 	refspec_item_clear(&refspec);
 	return ret;
 }
@@ -249,7 +249,7 @@ void refspec_ref_prefixes(const struct refspec *rs,
 		if (item->negative)
 			continue;
 
-		if (rs->fetch == REFSPEC_FETCH) {
+		if (rs->fetch) {
 			if (item->exact_sha1)
 				continue;
 			prefix = item->src;
refspec.h +3 -6
index e2b5cc54ef..155494cd3a 100644
@@ -32,11 +32,8 @@ struct refspec_item {
 
 struct string_list;
 
-#define REFSPEC_FETCH 1
-#define REFSPEC_PUSH 0
-
-#define REFSPEC_INIT_FETCH { .fetch = REFSPEC_FETCH }
-#define REFSPEC_INIT_PUSH { .fetch = REFSPEC_PUSH }
+#define REFSPEC_INIT_FETCH { .fetch = 1 }
+#define REFSPEC_INIT_PUSH { .fetch = 0 }
 
 /**
  * An array of strings can be parsed into a struct refspec using
@@ -47,7 +44,7 @@ struct refspec {
 	int alloc;
 	int nr;
 
-	int fetch;
+	unsigned fetch : 1;
 };
 
 int refspec_item_init(struct refspec_item *item, const char *refspec,
remote.c +2 -2
index e609cf5c56..addd4a9999 100644
@@ -143,8 +143,8 @@ static struct remote *make_remote(struct remote_state *remote_state,
 	ret->prune = -1;  /* unspecified */
 	ret->prune_tags = -1;  /* unspecified */
 	ret->name = xstrndup(name, len);
-	refspec_init(&ret->push, REFSPEC_PUSH);
-	refspec_init(&ret->fetch, REFSPEC_FETCH);
+	refspec_init(&ret->push, 0);
+	refspec_init(&ret->fetch, 1);
 	string_list_init_dup(&ret->server_options);
 
 	ALLOC_GROW(remote_state->remotes, remote_state->remotes_nr + 1,
transport-helper.c +1 -1
index d457b42550..43cd760119 100644
@@ -162,7 +162,7 @@ static struct child_process *get_helper(struct transport *transport)
 
 	data->helper = helper;
 	data->no_disconnect_req = 0;
-	refspec_init(&data->rs, REFSPEC_FETCH);
+	refspec_init(&data->rs, 1);
 
 	/*
 	 * Open the output as FILE* so strbuf_getline_*() family of
Signature · 1 line
2.49.0.3.gbb7a4a684c.dirty
#33 lore raw

PATCH v2 2/4 refspec: replace `refspec_init()` with fetch/push variants

In reply to Taylor Blau

To avoid having a Boolean argument in the refspec_init() function, replace it with two variants:

- refspec_init_fetch()
- refspec_init_push()

to codify the meaning of that Boolean into the function's name itself.

---
 refspec.c          | 12 +++++++++---
 refspec.h          |  3 ++-
 remote.c           |  4 ++--
 transport-helper.c |  2 +-
 4 files changed, 14 insertions(+), 7 deletions(-)
refspec.c +9 -3
index db5a1c34a5..f6be0c54d7 100644
@@ -178,10 +178,16 @@ void refspec_item_clear(struct refspec_item *item)
 	item->exact_sha1 = 0;
 }
 
-void refspec_init(struct refspec *rs, int fetch)
+void refspec_init_fetch(struct refspec *rs)
 {
-	memset(rs, 0, sizeof(*rs));
-	rs->fetch = fetch;
+	struct refspec blank = REFSPEC_INIT_FETCH;
+	memcpy(rs, &blank, sizeof(*rs));
+}
+
+void refspec_init_push(struct refspec *rs)
+{
+	struct refspec blank = REFSPEC_INIT_PUSH;
+	memcpy(rs, &blank, sizeof(*rs));
 }
 
 void refspec_append(struct refspec *rs, const char *refspec)
refspec.h +2 -1
index 155494cd3a..7db68e56c8 100644
@@ -52,7 +52,8 @@ int refspec_item_init(struct refspec_item *item, const char *refspec,
 void refspec_item_init_or_die(struct refspec_item *item, const char *refspec,
 			      int fetch);
 void refspec_item_clear(struct refspec_item *item);
-void refspec_init(struct refspec *rs, int fetch);
+void refspec_init_fetch(struct refspec *rs);
+void refspec_init_push(struct refspec *rs);
 void refspec_append(struct refspec *rs, const char *refspec);
 __attribute__((format (printf,2,3)))
 void refspec_appendf(struct refspec *rs, const char *fmt, ...);
remote.c +2 -2
index addd4a9999..25af97a44b 100644
@@ -143,8 +143,8 @@ static struct remote *make_remote(struct remote_state *remote_state,
 	ret->prune = -1;  /* unspecified */
 	ret->prune_tags = -1;  /* unspecified */
 	ret->name = xstrndup(name, len);
-	refspec_init(&ret->push, 0);
-	refspec_init(&ret->fetch, 1);
+	refspec_init_push(&ret->push);
+	refspec_init_fetch(&ret->fetch);
 	string_list_init_dup(&ret->server_options);
 
 	ALLOC_GROW(remote_state->remotes, remote_state->remotes_nr + 1,
transport-helper.c +1 -1
index 43cd760119..69391ee7d2 100644
@@ -162,7 +162,7 @@ static struct child_process *get_helper(struct transport *transport)
 
 	data->helper = helper;
 	data->no_disconnect_req = 0;
-	refspec_init(&data->rs, 1);
+	refspec_init_fetch(&data->rs);
 
 	/*
 	 * Open the output as FILE* so strbuf_getline_*() family of
Signature · 1 line
2.49.0.3.gbb7a4a684c.dirty
#34 lore raw

PATCH v2 3/4 refspec: remove refspec_item_init_or_die()

In reply to Taylor Blau

There are two callers of this function, which ensures that a dispatched call to refspec_item_init() does not fail.

In the following commit, we're going to add fetch/push-specific variants of refspec_item_init(), which will turn one function into two. To avoid introducing yet another pair of new functions (such as refspec_item_init_push_or_die() and refspec_item_init_fetch_or_die()), let's remove the thin wrapper entirely.

This duplicates a single line of code among two callers, but thins the refspec.h API by one function, and prevents introducing two more in the following commit.

Note that we still have a trailing Boolean argument in the function refspec_item_init(). The following commit will address this.

---
 builtin/pull.c |  3 ++-
 refspec.c      | 10 ++--------
 refspec.h      |  2 --
 3 files changed, 4 insertions(+), 11 deletions(-)
builtin/pull.c +2 -1
index 8bbfcce729..a68a9955de 100644
@@ -738,7 +738,8 @@ static const char *get_tracking_branch(const char *remote, const char *refspec)
 	const char *spec_src;
 	const char *merge_branch;
 
-	refspec_item_init_or_die(&spec, refspec, 1);
+	if (!refspec_item_init(&spec, refspec, 1))
+		die(_("invalid refspec '%s'"), refspec);
 	spec_src = spec.src;
 	if (!*spec_src || !strcmp(spec_src, "HEAD"))
 		spec_src = "HEAD";
refspec.c +2 -8
index f6be0c54d7..3aeb697505 100644
@@ -160,13 +160,6 @@ int refspec_item_init(struct refspec_item *item, const char *refspec, int fetch)
 	return parse_refspec(item, refspec, fetch);
 }
 
-void refspec_item_init_or_die(struct refspec_item *item, const char *refspec,
-			      int fetch)
-{
-	if (!refspec_item_init(item, refspec, fetch))
-		die(_("invalid refspec '%s'"), refspec);
-}
-
 void refspec_item_clear(struct refspec_item *item)
 {
 	FREE_AND_NULL(item->src);
@@ -194,7 +187,8 @@ void refspec_append(struct refspec *rs, const char *refspec)
 {
 	struct refspec_item item;
 
-	refspec_item_init_or_die(&item, refspec, rs->fetch);
+	if (!refspec_item_init(&item, refspec, rs->fetch))
+		die(_("invalid refspec '%s'"), refspec);
 
 	ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
 	rs->items[rs->nr] = item;
refspec.h +0 -2
index 7db68e56c8..614f34554e 100644
@@ -49,8 +49,6 @@ struct refspec {
 
 int refspec_item_init(struct refspec_item *item, const char *refspec,
 		      int fetch);
-void refspec_item_init_or_die(struct refspec_item *item, const char *refspec,
-			      int fetch);
 void refspec_item_clear(struct refspec_item *item);
 void refspec_init_fetch(struct refspec *rs);
 void refspec_init_push(struct refspec *rs);
Signature · 1 line
2.49.0.3.gbb7a4a684c.dirty
#35 lore raw

PATCH v2 4/4 refspec: replace `refspec_item_init()` with fetch/push variants

In reply to Taylor Blau

For similar reasons as in the previous refactoring of refspec_init() into refspec_init_fetch() and refspec_init_push(), apply the same refactoring to refspec_item_init().

---
 builtin/fetch.c |  2 +-
 builtin/pull.c  |  2 +-
 refspec.c       | 22 +++++++++++++++++++---
 refspec.h       |  4 ++--
 4 files changed, 23 insertions(+), 7 deletions(-)
builtin/fetch.c +1 -1
index 02af505469..9830c09011 100644
@@ -586,7 +586,7 @@ static struct ref *get_ref_map(struct remote *remote,
 		struct refspec_item tag_refspec;
 
 		/* also fetch all tags */
-		refspec_item_init(&tag_refspec, TAG_REFSPEC, 0);
+		refspec_item_init_push(&tag_refspec, TAG_REFSPEC);
 		get_fetch_map(remote_refs, &tag_refspec, &tail, 0);
 		refspec_item_clear(&tag_refspec);
 	} else if (tags == TAGS_DEFAULT && *autotags) {
builtin/pull.c +1 -1
index a68a9955de..a1ebc6ad33 100644
@@ -738,7 +738,7 @@ static const char *get_tracking_branch(const char *remote, const char *refspec)
 	const char *spec_src;
 	const char *merge_branch;
 
-	if (!refspec_item_init(&spec, refspec, 1))
+	if (!refspec_item_init_fetch(&spec, refspec))
 		die(_("invalid refspec '%s'"), refspec);
 	spec_src = spec.src;
 	if (!*spec_src || !strcmp(spec_src, "HEAD"))
refspec.c +19 -3
index 3aeb697505..0775358d96 100644
@@ -153,13 +153,24 @@ static int parse_refspec(struct refspec_item *item, const char *refspec, int fet
 	return 1;
 }
 
-int refspec_item_init(struct refspec_item *item, const char *refspec, int fetch)
+static int refspec_item_init(struct refspec_item *item, const char *refspec,
+			     int fetch)
 {
 	memset(item, 0, sizeof(*item));
 	item->raw = xstrdup(refspec);
 	return parse_refspec(item, refspec, fetch);
 }
 
+int refspec_item_init_fetch(struct refspec_item *item, const char *refspec)
+{
+	return refspec_item_init(item, refspec, 1);
+}
+
+int refspec_item_init_push(struct refspec_item *item, const char *refspec)
+{
+	return refspec_item_init(item, refspec, 0);
+}
+
 void refspec_item_clear(struct refspec_item *item)
 {
 	FREE_AND_NULL(item->src);
@@ -186,8 +197,13 @@ void refspec_init_push(struct refspec *rs)
 void refspec_append(struct refspec *rs, const char *refspec)
 {
 	struct refspec_item item;
+	int ret;
 
-	if (!refspec_item_init(&item, refspec, rs->fetch))
+	if (rs->fetch)
+		ret = refspec_item_init_fetch(&item, refspec);
+	else
+		ret = refspec_item_init_push(&item, refspec);
+	if (!ret)
 		die(_("invalid refspec '%s'"), refspec);
 
 	ALLOC_GROW(rs->items, rs->nr + 1, rs->alloc);
@@ -233,7 +249,7 @@ void refspec_clear(struct refspec *rs)
 int valid_fetch_refspec(const char *fetch_refspec_str)
 {
 	struct refspec_item refspec;
-	int ret = refspec_item_init(&refspec, fetch_refspec_str, 1);
+	int ret = refspec_item_init_fetch(&refspec, fetch_refspec_str);
 	refspec_item_clear(&refspec);
 	return ret;
 }
refspec.h +2 -2
index 614f34554e..8b04f9995e 100644
@@ -47,8 +47,8 @@ struct refspec {
 	unsigned fetch : 1;
 };
 
-int refspec_item_init(struct refspec_item *item, const char *refspec,
-		      int fetch);
+int refspec_item_init_fetch(struct refspec_item *item, const char *refspec);
+int refspec_item_init_push(struct refspec_item *item, const char *refspec);
 void refspec_item_clear(struct refspec_item *item);
 void refspec_init_fetch(struct refspec *rs);
 void refspec_init_push(struct refspec *rs);
Signature · 1 line
2.49.0.3.gbb7a4a684c.dirty
#36 lore raw

Re: [PATCH v2 0/4] refspec: treat 'fetch' as a Boolean value

In reply to Taylor Blau
Quoted 56 lines · Taylor Blau

Here's a small reroll that cleans up some wording in the first patch, and drops the accidental inclusion of a stray 'git-diff-pairs' binary that was sitting in my working tree at the time of writing the initial commit.

Otherwise the series is unchanged, but a range-diff is attached nonetheless. Thanks for reviewing :-).

Taylor Blau (4):
refspec: treat 'fetch' as a Boolean value
refspec: replace refspec_init() with fetch/push variants
refspec: remove refspec_item_init_or_die()
refspec: replace refspec_item_init() with fetch/push variants

 builtin/fetch.c    |  2 +-
 builtin/pull.c     |  3 ++-
 refspec.c          | 38 +++++++++++++++++++++++++++-----------
 refspec.h          | 18 +++++++-----------
 remote.c           |  4 ++--
 transport-helper.c |  2 +-
 6 files changed, 40 insertions(+), 27 deletions(-)

Range-diff against v1:
1: 7e662acb5a ! 1: 04e1ab8209 refspec: treat 'fetch' as a Boolean value
@@ Commit message
refspec: treat 'fetch' as a Boolean value

Since 6d4c057859 (refspec: introduce struct refspec, 2018-05-16), we
- have constants called REFSPEC_FETCH and REFSPEC_PUSH. This misleadingly
- suggests that we might introduce other modes in the future.
+ have macros called REFSPEC_FETCH and REFSPEC_PUSH. This confusingly
+ suggests that we might introduce other modes in the future, which, while
+ possible, is highly unlikely.

But these values are treated as a Boolean, and stored in a struct field called 'fetch'. So the following:
@@ Commit message
if (refspec->fetch) { ... }

are equivalent. Let's avoid renaming the Boolean values "true" and
- "false" here and remove the two REFSPEC_ constants mentioned above.
+ "false" here and remove the two REFSPEC_ macros mentioned above.

Since this value is truly a Boolean and will only ever take on a value of 0 or 1, we can declare it as a single bit unsigned field. In
@@ builtin/pull.c: static const char *get_tracking_branch(const char *remote, const
if (!*spec_src || !strcmp(spec_src, "HEAD"))
spec_src = "HEAD";

- ## git-diff-pairs (new) ##
- Binary files /dev/null and git-diff-pairs differ -
## refspec.c ##
@@ refspec.c: void refspec_clear(struct refspec *rs)
int valid_fetch_refspec(const char *fetch_refspec_str)

Quoted 7 lines
2:  fd2354dade = 2:  c3021b82ce refspec: replace `refspec_init()` with fetch/push variants
3:  49b470de61 = 3:  88f6a91c46 refspec: remove refspec_item_init_or_die()
4:  95783265fd = 4:  f0c323988f refspec: replace `refspec_item_init()` with fetch/push variants

base-commit: c702dd48567cfebca3d4a06b691de97da3f8dc4a --
2.49.0.3.gbb7a4a684c.dirty

I read through the four patches, and it seems like a straightforward translation of the previous state to essentially a boolean. I usually would have picked an enum based on avoiding passing a 0 or 1 to functions as you have in the intermediate state, but you didn't stop at that intermediate state and refactored the function names which makes the end result pretty nice. Series looks good to me.

#37 lore raw

Re: [PATCH 5/9] refspec_ref_prefixes(): clean up refspec_item logic

In reply to Jeff King
Quoted 9 lines · Jeff King
Quoted 3 lines

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?

Quoted 6 lines

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< ---

refspec.c +10 -5
index c6ad515f04..07d401bc71 100644
@@ -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)
refspec.h +4 -4
index 382ba2d5c1..a20cf883e4 100644
@@ -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

#38 lore raw

Re: [PATCH 5/9] refspec_ref_prefixes(): clean up refspec_item logic

Quoted 2 lines · Taylor Blau

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

"Type" is so overly generic a word that it is only marginally better than .fetch that is not a Boolean. And in a sense it is worse. At least .fetch hints us that it wants to choose between fetch and something else, most likely push, but .type does not tell us what kind of type it is about.

Do we anticipate that we would acquire a new "type" other than FETCH and PUSH? If not, .fetch = yes/no might be a better choice and if the meaning of the member is so obvious, we may even be able to lose REFSPEC_{FETCH,PUSH} symbols for its value.

On the other hand, if we were to add new kind of refspec used when doing something other than fetch and push (perhaps when bundling? I dunno), then the member specifies how the transfer goes, perhaps, so .transfer = REFSPEC_{FETCH,PUSH,BUNDLE} might be a better choice. But the refspecs are per remote thing (whether the remote is configured or the refspec is used as a one-shot basis fetching from or pushing to a remote) and while you can fetch from a bundle, you cannot push to it, and even if the system is updated to allow pushing into a bundle (which I do not think is such a bad idea), the refspec used in such a case would still be either fetch refspec or push refspec, so perhaps a new, third kind of refspec would not fit well within the design after all.

So, if we can reasonably expect that the choice will stay between fetch and push and we wouldn't be adding a new kind, I think reverting the meaning of .fetch to yes/no and getting rid of REFSPEC_{FETCH,PUSH} may be a better approach. If we stil want to keep the descriptive CPP macro, then perhaps .transfer (or .direction) that lets us choose between fetch or push? I dunno.

Quoted 4 lines

, 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:

Quoted 8 lines
    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'.

Yup, that makes two of us.

#39 lore raw

Re: [PATCH 5/9] refspec_ref_prefixes(): clean up refspec_item logic

Quoted 6 lines · Junio C Hamano

So, if we can reasonably expect that the choice will stay between fetch and push and we wouldn't be adding a new kind, I think reverting the meaning of .fetch to yes/no and getting rid of REFSPEC_{FETCH,PUSH} may be a better approach. If we stil want to keep the descriptive CPP macro, then perhaps .transfer (or .direction) that lets us choose between fetch or push? I dunno.

I suppose adding a new direction/mode/transfer/etc is always possible. But I think that it's unlikely enough to happen any time soon (if at all) in my view that we should simplify the code anyway. If that changes in the future *and* those changes fit will in the design of REFSPEC_FETCH and friends, then we can always resurrect those macros and reinterpret this field.

But I don't think that we should carry this extra baggage around in the meantime if we don't need to.

Quoted 6 lines
Quoted 6 lines

, 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) { ... }
Quoted 8 lines
Quoted 6 lines

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'.

Yup, that makes two of us.

Good :-). I'll send a small reroll of the topic anyway to avoid sending a patch that has a git-diff-pairs binary in it :-<.

Thanks,
Taylor

#40 lore raw

PATCH 6/9 fetch: ask server to advertise HEAD for config-less fetch

In reply to Jeff King

If we're not given any refspecs (either on the command line or via config) and we have no branch merge config, then we fetch the remote HEAD into our local FETCH_HEAD. In that case we do not send any ref-prefix option to the server at all, and we see the full advertisement.

But this is sub-optimal. We only care about HEAD, so we can just ask for that, and ignore all of the other refs.

The new test demonstrates a case where we see fewer refs (in this case only one less, but in theory we could be ignoring millions of them).

This also removes the only case where we care about seeing some refs from the other side, but don't add anything to the ref_prefixes list. Cleaning this up means one less maintenance burden. Before this patch, any code which wanted to add to the list had to make sure the list was not empty, since an empty list meant "ask for everything". Now it really means "we are not interested in any refs".

This should let us optimize a few more cases in subsequent patches.

Note that we'll add "HEAD" to the list of prefixes, and later code for updating "refs/remotes/<remote>/HEAD" may likewise do so. In theory this could cause duplicates in the list, but in practice these can't both trigger. We hit our new case only if there are no refspecs, and the "<remote>/HEAD" feature is enabled only when we are fetching from a remote with configured refspecs. We could be defensive with a flag, but it didn't seem worth it to me (the absolute worse case is a useless redundant ref-prefix line sent to the server).

---
 builtin/fetch.c        |  8 ++++++++
 t/t5702-protocol-v2.sh | 15 +++++++++++++++
 2 files changed, 23 insertions(+)
builtin/fetch.c +8 -0
index 95fd0018b9..f142756441 100644
@@ -1766,6 +1766,14 @@ static int do_fetch(struct transport *transport,
 					    branch->merge[i]->src);
 			}
 		}
+
+		/*
+		 * If there are no refs specified to fetch, then we just
+		 * fetch HEAD; mention that to narrow the advertisement.
+		 */
+		if (!transport_ls_refs_options.ref_prefixes.nr)
+			strvec_push(&transport_ls_refs_options.ref_prefixes,
+				    "HEAD");
 	}
 
 	if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
t/t5702-protocol-v2.sh +15 -0
index cea8f92a3d..2f0a52a72d 100755
@@ -679,6 +679,21 @@ test_expect_success 'default refspec is used to filter ref when fetching' '
 	grep "ref-prefix refs/tags/" log
 '
 
+test_expect_success 'set up parent for prefix tests' '
+	git init prefix-parent &&
+	git -C prefix-parent commit --allow-empty -m foo &&
+	git -C prefix-parent branch unrelated-branch
+'
+
+test_expect_success 'empty refspec filters refs when fetching' '
+	git init configless-child &&
+
+	test_when_finished "rm -f log" &&
+	GIT_TRACE_PACKET="$(pwd)/log" \
+		git -C configless-child fetch ../prefix-parent &&
+	test_grep ! unrelated-branch log
+'
+
 test_expect_success 'fetch supports various ways of have lines' '
 	rm -rf server client trace &&
 	git init server &&
Signature · 1 line
2.49.0.rc1.381.gc60f5426ff
#41 lore raw

Re: [PATCH 6/9] fetch: ask server to advertise HEAD for config-less fetch

Quoted 12 lines · Jeff King

If we're not given any refspecs (either on the command line or via config) and we have no branch merge config, then we fetch the remote HEAD into our local FETCH_HEAD. In that case we do not send any ref-prefix option to the server at all, and we see the full advertisement.

But this is sub-optimal. We only care about HEAD, so we can just ask for that, and ignore all of the other refs.

The new test demonstrates a case where we see fewer refs (in this case only one less, but in theory we could be ignoring millions of them).

Quoted 6 lines

This also removes the only case where we care about seeing some refs from the other side, but don't add anything to the ref_prefixes list. Cleaning this up means one less maintenance burden. Before this patch, any code which wanted to add to the list had to make sure the list was not empty, since an empty list meant "ask for everything". Now it really means "we are not interested in any refs".

Yes. The optimization is nice on its own, but I think this is the real benefit to this patch IMHO.

Quoted 1 line

This should let us optimize a few more cases in subsequent patches.

;-).

Quoted 8 lines

Note that we'll add "HEAD" to the list of prefixes, and later code for updating "refs/remotes/<remote>/HEAD" may likewise do so. In theory this could cause duplicates in the list, but in practice these can't both trigger. We hit our new case only if there are no refspecs, and the "<remote>/HEAD" feature is enabled only when we are fetching from a remote with configured refspecs. We could be defensive with a flag, but it didn't seem worth it to me (the absolute worse case is a useless redundant ref-prefix line sent to the server).

Yeah, I think that we already do this in some instances that you and I talked about off-list, but I can't remember exactly what I did to provoke it. In either case, the server responds correctly, so I don't think it's so urgent to deal with ATM.

Quoted 6 lines
builtin/fetch.c +0 -0
index 95fd0018b9..f142756441 100644
@@ -1766,6 +1766,14 @@ static int do_fetch(struct transport *transport,
 					    branch->merge[i]->src);

builtin/fetch.c static int do_fetch(struct transport *transport, · line 1776

Quoted 10 lines
builtin/fetch.c +8 -0
 			}
 		}
+
+		/*
+		 * If there are no refs specified to fetch, then we just
+		 * fetch HEAD; mention that to narrow the advertisement.
+		 */
+		if (!transport_ls_refs_options.ref_prefixes.nr)
+			strvec_push(&transport_ls_refs_options.ref_prefixes,
+				    "HEAD");

Makes sense. If we know that we just want to fetch HEAD into FETCH_HEAD and we haven't already limited the advertisement, we are free to do so now.

t/t5702-protocol-v2.sh test_expect_success 'default refspec is used to filter ref when fetching' ' · line 684

Quoted 12 lines
builtin/fetch.c +0 -0
 	if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
t/t5702-protocol-v2.sh +3 -0
index cea8f92a3d..2f0a52a72d 100755
@@ -679,6 +679,21 @@ test_expect_success 'default refspec is used to filter ref when fetching' '
 	grep "ref-prefix refs/tags/" log
 '
 
+test_expect_success 'set up parent for prefix tests' '
+	git init prefix-parent &&
+	git -C prefix-parent commit --allow-empty -m foo &&

Any reason to use a bona-fide "commit" here instead of "test_commit"?

Not a big deal either way, of course, I'm just curious.

t/t5702-protocol-v2.sh test_expect_success 'default refspec is used to filter ref when fetching' ' · line 694

Quoted 10 lines
t/t5702-protocol-v2.sh +10 -0
+	git -C prefix-parent branch unrelated-branch
+'
+
+test_expect_success 'empty refspec filters refs when fetching' '
+	git init configless-child &&
+
+	test_when_finished "rm -f log" &&
+	GIT_TRACE_PACKET="$(pwd)/log" \
+		git -C configless-child fetch ../prefix-parent &&
+	test_grep ! unrelated-branch log

Very clean, nice.

Thanks,
Taylor

#42 lore raw

Re: [PATCH 6/9] fetch: ask server to advertise HEAD for config-less fetch

Quoted 9 lines · Taylor Blau
Quoted 8 lines

Note that we'll add "HEAD" to the list of prefixes, and later code for updating "refs/remotes/<remote>/HEAD" may likewise do so. In theory this could cause duplicates in the list, but in practice these can't both trigger. We hit our new case only if there are no refspecs, and the "<remote>/HEAD" feature is enabled only when we are fetching from a remote with configured refspecs. We could be defensive with a flag, but it didn't seem worth it to me (the absolute worse case is a useless redundant ref-prefix line sent to the server).

Quoted 4 lines

Yeah, I think that we already do this in some instances that you and I talked about off-list, but I can't remember exactly what I did to provoke it. In either case, the server responds correctly, so I don't think it's so urgent to deal with ATM.

Yeah, I thought we did, too, but after looking at the code, I doubt it. I think we were always playing with exact-oid refspecs, so we'd always hit the "if (rs->nr)" side of that conditional.

In order to hit the case touched by this patch, you can't have refspecs, but still need to somehow get a true answer out of uses_remote_tracking().

Quoted 7 lines

t/t5702-protocol-v2.sh test_expect_success 'default refspec is used to filter ref when fetching' ' · line 684

Quoted 3 lines
t/t5702-protocol-v2.sh +3 -0
+test_expect_success 'set up parent for prefix tests' '
+	git init prefix-parent &&
+	git -C prefix-parent commit --allow-empty -m foo &&

Any reason to use a bona-fide "commit" here instead of "test_commit"?

Not a big deal either way, of course, I'm just curious.

Nope, I mostly just reach for "git commit" without thinking because that's what I naturally do while debugging or exploring.

But since you asked...;)

I do find test_commit a bit bloated in general. It takes several commands versus one, leaves cruft files in the working tree (that you need to care about not using again, lest your commit fail with "no changes"), and by default makes tags that sometimes cause confusion about fetching, reachability, and so on.

The one thing it does do that git-commit doesn't is increment test_tick. That sometimes is important (if you care about traversal ordering), but usually doesn't.

So I dunno. Maybe I am a bad person for not using test_commit by default and we should have a style suggestion there.

-Peff

#43 lore raw

Re: [PATCH 6/9] fetch: ask server to advertise HEAD for config-less fetch

Quoted 15 lines · Jeff King
Quoted 3 lines

Any reason to use a bona-fide "commit" here instead of "test_commit"?

Not a big deal either way, of course, I'm just curious.

Nope, I mostly just reach for "git commit" without thinking because that's what I naturally do while debugging or exploring.

But since you asked...;)

I do find test_commit a bit bloated in general. It takes several commands versus one, leaves cruft files in the working tree (that you need to care about not using again, lest your commit fail with "no changes"), and by default makes tags that sometimes cause confusion about fetching, reachability, and so on.

Quoted 6 lines

The one thing it does do that git-commit doesn't is increment test_tick. That sometimes is important (if you care about traversal ordering), but usually doesn't.

So I dunno. Maybe I am a bad person for not using test_commit by default and we should have a style suggestion there.

FWIW, my assessment on test_commit exactly matches yours. The cruft files it creates are often not what I want, the tags left by default are even worse, and the only good thing about it is the tick support but even that does not make much of difference in many scenarios.

To its defence, I _suspect_ the automated creation of default changes were useful back when there were no "allow-empty" support. But I think it outlived its usefulness.

#44 lore raw

Re: [PATCH 6/9] fetch: ask server to advertise HEAD for config-less fetch

Quoted 24 lines · Junio C Hamano
Quoted 15 lines · Jeff King
Quoted 3 lines

Any reason to use a bona-fide "commit" here instead of "test_commit"?

Not a big deal either way, of course, I'm just curious.

Nope, I mostly just reach for "git commit" without thinking because that's what I naturally do while debugging or exploring.

But since you asked...;)

I do find test_commit a bit bloated in general. It takes several commands versus one, leaves cruft files in the working tree (that you need to care about not using again, lest your commit fail with "no changes"), and by default makes tags that sometimes cause confusion about fetching, reachability, and so on.

Quoted 6 lines

The one thing it does do that git-commit doesn't is increment test_tick. That sometimes is important (if you care about traversal ordering), but usually doesn't.

So I dunno. Maybe I am a bad person for not using test_commit by default and we should have a style suggestion there.

Quoted 8 lines

FWIW, my assessment on test_commit exactly matches yours. The cruft files it creates are often not what I want, the tags left by default are even worse, and the only good thing about it is the tick support but even that does not make much of difference in many scenarios.

To its defence, I _suspect_ the automated creation of default changes were useful back when there were no "allow-empty" support. But I think it outlived its usefulness.

I don't disagree, but converting the 2,570 instances of 'test_commit' that I have in my tree would be difficult to do without disrupting lots of in-flight work ;-).

Thanks,
Taylor

#45 lore raw

PATCH 7/9 fetch: stop protecting additions to ref-prefix list

In reply to Jeff King

When using the ref-prefix feature of protocol v2, a client which sends no prefixes at all will get the full advertisement. And so the code in git-fetch was historically loose about setting up that list based on our refspecs. There were cases where we needed to know about some refs, so we just didn't add anything to the ref-prefix list.

And hence further code, like that for tag-following and updating origin/HEAD, had to be careful about adding to an empty list. E.g., see the bug fixed by bd52d9a058 (fetch: fix following tags when fetching specific OID, 2025-03-07).

But the previous commit removed the last such case, and now we know an empty ref-prefix list (at least inside git-fetch's do_fetch() function) means that we really don't need to see any refs. So we can drop those extra conditionals.

This simplifies the code a little. But it also means that some cases can now use ref prefixes when they would not otherwise. As the test shows, fetching an exact oid into a local ref can now avoid enumerating all of the refs. The refspec itself doesn't need to know about any remote refs, and the tag auto-following can just ask about refs/tags/.

The same is true for asking about HEAD to update the local origin/HEAD. I didn't add a test for that yet, though, as we can optimize it even further.

---
 builtin/fetch.c        | 10 ++++------
 t/t5702-protocol-v2.sh | 14 ++++++++++++++
 2 files changed, 18 insertions(+), 6 deletions(-)
builtin/fetch.c +4 -6
index f142756441..6ab101fa6d 100644
@@ -1778,16 +1778,14 @@ static int do_fetch(struct transport *transport,
 
 	if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
 		must_list_refs = 1;
-		if (transport_ls_refs_options.ref_prefixes.nr)
-			strvec_push(&transport_ls_refs_options.ref_prefixes,
-				    "refs/tags/");
+		strvec_push(&transport_ls_refs_options.ref_prefixes,
+			    "refs/tags/");
 	}
 
 	if (uses_remote_tracking(transport, rs)) {
 		must_list_refs = 1;
-		if (transport_ls_refs_options.ref_prefixes.nr)
-			strvec_push(&transport_ls_refs_options.ref_prefixes,
-				    "HEAD");
+		strvec_push(&transport_ls_refs_options.ref_prefixes,
+			    "HEAD");
 	}
 
 	if (must_list_refs) {
t/t5702-protocol-v2.sh +14 -0
index 2f0a52a72d..626deb05f0 100755
@@ -682,6 +682,7 @@ test_expect_success 'default refspec is used to filter ref when fetching' '
 test_expect_success 'set up parent for prefix tests' '
 	git init prefix-parent &&
 	git -C prefix-parent commit --allow-empty -m foo &&
+	git -C prefix-parent tag my-tag &&
 	git -C prefix-parent branch unrelated-branch
 '
 
@@ -694,6 +695,19 @@ test_expect_success 'empty refspec filters refs when fetching' '
 	test_grep ! unrelated-branch log
 '
 
+test_expect_success 'exact oid fetch with tag following' '
+	git init exact-oid-tags &&
+
+	commit=$(git -C prefix-parent rev-parse --verify HEAD) &&
+
+	test_when_finished "rm -f log" &&
+	GIT_TRACE_PACKET="$(pwd)/log" \
+		git -C exact-oid-tags fetch ../prefix-parent \
+			$commit:refs/heads/exact &&
+	test_grep ! unrelated-branch log &&
+	git -C exact-oid-tags rev-parse --verify my-tag
+'
+
 test_expect_success 'fetch supports various ways of have lines' '
 	rm -rf server client trace &&
 	git init server &&
Signature · 1 line
2.49.0.rc1.381.gc60f5426ff
#46 lore raw

Re: [PATCH 7/9] fetch: stop protecting additions to ref-prefix list

Quoted 4 lines · Jeff King
---
 builtin/fetch.c        | 10 ++++------
 t/t5702-protocol-v2.sh | 14 ++++++++++++++
 2 files changed, 18 insertions(+), 6 deletions(-)

No comments from me here, since this is very clearly explained and setup within the context of the rest of the series. The end result is very, very nice indeed.

Thanks,
Taylor

#47 lore raw

PATCH 8/9 fetch: avoid ls-refs only to ask for HEAD symref update

In reply to Jeff King

When we fetch from a configured remote, we may try to update the local refs/remotes/<origin>/HEAD, and so we ask the server to advertise its HEAD to us.

But if we aren't otherwise asking about any refs at all, then we know this HEAD update can never happen! To consider a new value for HEAD, the set_head() function uses guess_remote_head(). And even if it sees an explicit symref value for HEAD, it will only report that as a match if we also saw that remote ref advertised, and it mapped to a local tracking ref via get_fetch_map().

In other words, a fetch like this:

git fetch origin $exact_oid:refs/heads/foo

can never update HEAD, because we will never have fetched (nor even see the advertisement for) the ref that HEAD points to.

Currently the command above will still call ls-refs to ask about the HEAD, even though it is pointless. This patch teaches it to skip the ls-refs call entirely in this case, which avoids a round-trip to the server.

---
This describes the current behavior of set_head(). But I do wonder if it should be more aggressive. If we found out that the other side is pointing to refs/heads/foo, should we map that ourselves to find that it would be stored as refs/remotes/origin/foo, and update HEAD anyway?

That would let us actually update HEAD in this case (and also in many other cases where we fetch a specific ref that is not pointed to by the remote HEAD).

OTOH, it would mean we basically always do an ls-refs just to find out about HEAD. Which is working against the optimization from e70a3030e7 (fetch: do not list refs if fetching only hashes, 2018-09-27), and the code before this patch might even be considered a regression.

I also wonder if the uses_remote_tracking() check added by 6c915c3f85 (fetch: do not ask for HEAD unnecessarily, 2024-12-06) is not quite right. It is looking for a configured remote along with at least one refspec with a destination. But wouldn't it need to have both a source ref (not an exact oid) and a destination to work? I suspect we could do the fix there and end up with the same optimized behavior as I have here.

 builtin/fetch.c        |  5 ++---
 t/t5702-protocol-v2.sh | 13 +++++++++++++
 2 files changed, 15 insertions(+), 3 deletions(-)
builtin/fetch.c +2 -3
index 6ab101fa6d..c26866e674 100644
@@ -1782,11 +1782,10 @@ static int do_fetch(struct transport *transport,
 			    "refs/tags/");
 	}
 
-	if (uses_remote_tracking(transport, rs)) {
-		must_list_refs = 1;
+	if (must_list_refs &&
+	    uses_remote_tracking(transport, rs))
 		strvec_push(&transport_ls_refs_options.ref_prefixes,
 			    "HEAD");
-	}
 
 	if (must_list_refs) {
 		trace2_region_enter("fetch", "remote_refs", the_repository);
t/t5702-protocol-v2.sh +13 -0
index 626deb05f0..4d0cbe9872 100755
@@ -708,6 +708,19 @@ test_expect_success 'exact oid fetch with tag following' '
 	git -C exact-oid-tags rev-parse --verify my-tag
 '
 
+test_expect_success 'exact oid fetch avoids pointless HEAD request' '
+	git init exact-oid-head &&
+	git -C exact-oid-head remote add origin ../prefix-parent &&
+
+	commit=$(git -C prefix-parent rev-parse --verify HEAD) &&
+
+	test_when_finished "rm -f log" &&
+	GIT_TRACE_PACKET="$(pwd)/log" \
+		git -C exact-oid-head fetch --no-tags origin \
+			$commit:refs/heads/exact &&
+	test_grep ! command=ls-refs log
+'
+
 test_expect_success 'fetch supports various ways of have lines' '
 	rm -rf server client trace &&
 	git init server &&
Signature · 1 line
2.49.0.rc1.381.gc60f5426ff
#48 lore raw

Re: [PATCH 8/9] fetch: avoid ls-refs only to ask for HEAD symref update

Quoted 3 lines · Jeff King

When we fetch from a configured remote, we may try to update the local refs/remotes/<origin>/HEAD, and so we ask the server to advertise its HEAD to us.

Yes.

I _think_ flipping the remote-tracking HEAD should be tied to the case where we are fetching to update the remote-tracking branches.

Quoted 7 lines

But if we aren't otherwise asking about any refs at all, then we know this HEAD update can never happen! To consider a new value for HEAD, the set_head() function uses guess_remote_head(). And even if it sees an explicit symref value for HEAD, it will only report that as a match if we also saw that remote ref advertised, and it mapped to a local tracking ref via get_fetch_map().

Quoted 6 lines

In other words, a fetch like this:

git fetch origin $exact_oid:refs/heads/foo

can never update HEAD, because we will never have fetched (nor even see the advertisement for) the ref that HEAD points to.

Good observation, and I actually think it would be counter-intuitive if remote HEAD gets updated by a fetch that does not even update the remote-tracking branches.

My preference is to keep the rule simple.

If you fetch using the configured fetch refspec, i.e. "git fetch [<remote>]" without any refspec command line arguments, that is an operation to maintain your remote-tracking branches for that remote up-to-date, hence we also try to flip remote-tracking HEAD (if you told us to) during such a fetch.

If you are doing a single-object fetch (perhaps as a lazy-fill from promisor remote), I do not think I want to see anything else happen than that the single-object comes here.

Thanks.

#49 lore raw

Re: [PATCH 8/9] fetch: avoid ls-refs only to ask for HEAD symref update

Quoted 19 lines · Junio C Hamano
Quoted 6 lines

In other words, a fetch like this:

git fetch origin $exact_oid:refs/heads/foo

can never update HEAD, because we will never have fetched (nor even see the advertisement for) the ref that HEAD points to.

Good observation, and I actually think it would be counter-intuitive if remote HEAD gets updated by a fetch that does not even update the remote-tracking branches.

My preference is to keep the rule simple.

If you fetch using the configured fetch refspec, i.e. "git fetch [<remote>]" without any refspec command line arguments, that is an operation to maintain your remote-tracking branches for that remote up-to-date, hence we also try to flip remote-tracking HEAD (if you told us to) during such a fetch.

Quoted 3 lines

If you are doing a single-object fetch (perhaps as a lazy-fill from promisor remote), I do not think I want to see anything else happen than that the single-object comes here.

That was my intuition, too, but there's a case in between. Doing:

git fetch origin main

will update HEAD, too (with the current code). But if and only if it points to "main"! Is that something we want to keep? It feels like it could sometimes be useful, but it is awfully hard to explain.

So I could see going in two different directions:

1. Only do the HEAD update when we are using the configured refspecs.

2. Do the HEAD update even when we are not fetching its destination, but do not otherwise trigger ls-refs to discover it (so basically, not on object-only fetches). This kicks in for more cases, but is hard to explain.

Both are a user-visible divergence from how the feature behaves now (and so I did not want to touch that in my series), but if we are all in agreement, we can fix it on top. I do think option 1 (i.e., what you are suggesting in your email) is how I would have done it if starting from scratch. And the current rules are weird enough and the feature is new enough that I think it is OK to change.

-Peff

#50 lore raw

Re: [PATCH 8/9] fetch: avoid ls-refs only to ask for HEAD symref update

Quoted 5 lines · Jeff King

That was my intuition, too, but there's a case in between. Doing:

git fetch origin main

will update HEAD, too (with the current code).

Good point, and I do not think it should.

Quoted 3 lines

But if and only if it
points to "main"! Is that something we want to keep? It feels like it could sometimes be useful, but it is awfully hard to explain.

Again, I do not think it should update HEAD. Is this something hard to fix? When I say "please tell me where their main branch is", that is what I mean. I didn't ask about their HEAD, period. Unlike "git fetch" (that default to 'origin') and "git fetch origin", which is "please update our view of 'origin'" and should update HEAD if it is missing (or configured to be updated every time).

Quoted 9 lines

So I could see going in two different directions:

1. Only do the HEAD update when we are using the configured refspecs.

2. Do the HEAD update even when we are not fetching its destination, but do not otherwise trigger ls-refs to discover it (so basically, not on object-only fetches). This kicks in for more cases, but is hard to explain.

Quoted 6 lines

Both are a user-visible divergence from how the feature behaves now (and so I did not want to touch that in my series), but if we are all in agreement, we can fix it on top. I do think option 1 (i.e., what you are suggesting in your email) is how I would have done it if starting from scratch. And the current rules are weird enough and the feature is new enough that I think it is OK to change.

Yup, I do think #1 is the way to go.

Thanks.

#51 lore raw

cover limiting followRemoteHEAD being used

Quoted 9 lines · Junio C Hamano
Quoted 9 lines

So I could see going in two different directions:

1. Only do the HEAD update when we are using the configured refspecs.

2. Do the HEAD update even when we are not fetching its destination, but do not otherwise trigger ls-refs to discover it (so basically, not on object-only fetches). This kicks in for more cases, but is hard to explain.

Quoted 8 lines
Quoted 6 lines

Both are a user-visible divergence from how the feature behaves now (and so I did not want to touch that in my series), but if we are all in agreement, we can fix it on top. I do think option 1 (i.e., what you are suggesting in your email) is how I would have done it if starting from scratch. And the current rules are weird enough and the feature is new enough that I think it is OK to change.

Yup, I do think #1 is the way to go.

It turned out to be pretty easy to do; the code ends up even shorter. These patches apply on top of jk/fetch-ref-prefix-cleanup.

There is one interesting case we haven't discussed. What should:

git fetch origin HEAD

do with respect to refs/remotes/origin/HEAD? Right now it does nothing (at least with the v2 protocol). We ask about HEAD, but since we didn't fetch the matching ref, set_head() won't accept it. And after my patches, we would not even try to call set_head() at all (since we are not using the full refspecs).

But it's also something I could see somebody doing to try to update refs/remotes/origin/HEAD. I've left it alone for now, since my series does not change the behavior either way. But it might be something we could do on top (though it gets funny, because with the code as it is now, we'd have to ask for all of refs/heads/ to see the pointed-to branch advertised).

Anyway, here's what I think we should do now. The first one is the change we discussed, and the second is a related optimization I noticed.

[1/2]: fetch: only respect followRemoteHEAD with configured refspecs [2/2]: fetch: don't ask for remote HEAD if followRemoteHEAD is "never"

 Documentation/config/remote.adoc |  3 ++-
 builtin/fetch.c                  | 29 +++++++----------------------
 t/t5505-remote.sh                |  2 +-
 t/t5510-fetch.sh                 | 19 ++++++++++++++++++-
 4 files changed, 28 insertions(+), 25 deletions(-)
#52 lore raw

PATCH 1/2 fetch: only respect followRemoteHEAD with configured refspecs

The new followRemoteHEAD feature is triggered for almost every fetch, causing us to ask the server about the remote "HEAD" and to consider updating our local tracking HEAD symref. This patch limits the feature only to the case when we are fetching a remote using its configured refspecs (typically into its refs/remotes/ hierarchy). There are two reasons for this.

One is efficiency. E.g., the fixes in 6c915c3f85 (fetch: do not ask for HEAD unnecessarily, 2024-12-06) and 20010b8c20 (fetch: avoid ls-refs only to ask for HEAD symref update, 2025-03-08) were aimed at reducing the work we do when we would not be able to update HEAD anyway. But they do not quite cover all cases. The remaining one is:

git fetch origin refs/heads/foo:refs/remotes/origin/foo

which _sometimes_ can update HEAD, but usually not. And that leads us to the second point, which is being simple and explainable.

The code for updating the tracking HEAD symref requires both that we learned which ref the remote HEAD points at, and that the server advertised that ref to us. But because the v2 protocol narrows the server's advertisement, the command above would not typically update HEAD at all, unless it happened to point to the "foo" branch. Or even weirder, it probably _would_ update if the server is very old and supports only the v0 protocol, which always gives a full advertisement.

This creates confusing behavior for the user: sometimes we may try to update HEAD and sometimes not, depending on vague rules.

One option here would be to loosen the update code to accept the remote HEAD even if the server did not advertise that ref. I think that could work, but it may also lead to interesting corner cases (e.g., creating a dangling symref locally, even though the branch is not unborn on the server, if we happen not to have fetched it).

So let's instead simplify the rules: we'll only consider updating the tracking HEAD symref when we're doing a full fetch of the remote's configured refs. This is easy to implement; we can just set a flag at the moment we realize we're using the configured refspecs. And we can drop the special case code added by 6c915c3f85 and 20010b8c20, since this covers those cases. The existing tests from those commits still pass.

In t5505, an incidental call to "git fetch <remote> <refspec>" updated HEAD, which caused us to adjust the test in 3f763ddf28 (fetch: set remote/HEAD if it does not exist, 2024-11-22). We can now adjust that back to how it was before the feature was added.

Even though t5505 is incidentally testing our new desired behavior, we'll add an explicit test in t5510 to make sure it is covered.

---
The test is weird with the way it calls "cd", but it matches the nearby ones. I think that could all be simplified, but I'll leave that for another day (or never).

 Documentation/config/remote.adoc |  3 ++-
 builtin/fetch.c                  | 25 ++++++-------------------
 t/t5505-remote.sh                |  2 +-
 t/t5510-fetch.sh                 | 14 ++++++++++++++
 4 files changed, 23 insertions(+), 21 deletions(-)
Documentation/config/remote.adoc +2 -1
index 25fe219d10..91e46f66f5 100644
@@ -108,7 +108,8 @@ the values inherited from a lower priority configuration files (e.g.
 `$HOME/.gitconfig`).
 
 remote.<name>.followRemoteHEAD::
-	How linkgit:git-fetch[1] should handle updates to `remotes/<name>/HEAD`.
+	How linkgit:git-fetch[1] should handle updates to `remotes/<name>/HEAD`
+	when fetching using the configured refspecs of a remote.
 	The default value is "create", which will create `remotes/<name>/HEAD`
 	if it exists on the remote, but not locally; this will not touch an
 	already existing local reference. Setting it to "warn" will print
builtin/fetch.c +6 -19
index 02af505469..66f5ae31b6 100644
@@ -1691,21 +1691,6 @@ static int set_head(const struct ref *remote_refs, struct remote *remote)
 	return result;
 }
 
-static int uses_remote_tracking(struct transport *transport, struct refspec *rs)
-{
-	if (!remote_is_configured(transport->remote, 0))
-		return 0;
-
-	if (!rs->nr)
-		rs = &transport->remote->fetch;
-
-	for (int i = 0; i < rs->nr; i++)
-		if (rs->items[i].dst)
-			return 1;
-
-	return 0;
-}
-
 static int do_fetch(struct transport *transport,
 		    struct refspec *rs,
 		    const struct fetch_config *config)
@@ -1720,6 +1705,7 @@ static int do_fetch(struct transport *transport,
 		TRANSPORT_LS_REFS_OPTIONS_INIT;
 	struct fetch_head fetch_head = { 0 };
 	struct strbuf err = STRBUF_INIT;
+	int do_set_head = 0;
 
 	if (tags == TAGS_DEFAULT) {
 		if (transport->remote->fetch_tags == 2)
@@ -1740,9 +1726,11 @@ static int do_fetch(struct transport *transport,
 	} else {
 		struct branch *branch = branch_get(NULL);
 
-		if (transport->remote->fetch.nr)
+		if (transport->remote->fetch.nr) {
 			refspec_ref_prefixes(&transport->remote->fetch,
 					     &transport_ls_refs_options.ref_prefixes);
+			do_set_head = 1;
+		}
 		if (branch_has_merge_config(branch) &&
 		    !strcmp(branch->remote_name, transport->remote->name)) {
 			int i;
@@ -1765,8 +1753,7 @@ static int do_fetch(struct transport *transport,
 		strvec_push(&transport_ls_refs_options.ref_prefixes,
 			    "refs/tags/");
 
-	if (transport_ls_refs_options.ref_prefixes.nr &&
-	    uses_remote_tracking(transport, rs))
+	if (do_set_head)
 		strvec_push(&transport_ls_refs_options.ref_prefixes,
 			    "HEAD");
 
@@ -1918,7 +1905,7 @@ static int do_fetch(struct transport *transport,
 				  "you need to specify exactly one branch with the --set-upstream option"));
 		}
 	}
-	if (set_head(remote_refs, transport->remote))
+	if (do_set_head && set_head(remote_refs, transport->remote))
 		;
 		/*
 		 * Way too many cases where this can go wrong
t/t5505-remote.sh +1 -1
index bb7e0c6879..fed5823e85 100755
@@ -499,7 +499,7 @@ test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
 		cd test &&
 		git fetch two "refs/heads/*:refs/remotes/two/*" &&
 		git remote set-head --auto two >output 2>&1 &&
-		echo "${SQ}two/HEAD${SQ} is unchanged and points to ${SQ}main${SQ}" >expect &&
+		echo "${SQ}two/HEAD${SQ} is now created and points to ${SQ}main${SQ}" >expect &&
 		test_cmp expect output
 	)
 '
t/t5510-fetch.sh +14 -0
index 5f350facf5..ad23dd11ef 100755
@@ -250,6 +250,20 @@ test_expect_success "fetch test followRemoteHEAD always" '
 	)
 '
 
+test_expect_success 'followRemoteHEAD does not kick in with refspecs' '
+	test_when_finished "git config unset remote.origin.followRemoteHEAD" &&
+	(
+		cd "$D" &&
+		cd two &&
+		git remote set-head origin other &&
+		git config set remote.origin.followRemoteHEAD always &&
+		git fetch origin refs/heads/main:refs/remotes/origin/main &&
+		echo refs/remotes/origin/other >expect &&
+		git symbolic-ref refs/remotes/origin/HEAD >actual &&
+		test_cmp expect actual
+	)
+'
+
 test_expect_success 'fetch --prune on its own works as expected' '
 	cd "$D" &&
 	git clone . prune &&
Signature · 1 line
2.49.0.390.gc9996a6a6c
#53 lore raw

Re: [PATCH 1/2] fetch: only respect followRemoteHEAD with configured refspecs

builtin/fetch.c static int do_fetch(struct transport *transport, · line 1911

Quoted 9 lines · Jeff King
builtin/fetch.c +1 -1
@@ -1918,7 +1905,7 @@ static int do_fetch(struct transport *transport,
 				  "you need to specify exactly one branch with the --set-upstream option"));
 		}
 	}
-	if (set_head(remote_refs, transport->remote))
+	if (do_set_head && set_head(remote_refs, transport->remote))
 		;
 		/*
 		 * Way too many cases where this can go wrong

This empty "if" statement is a little odd, though not the fault of your patch. I probably would have rewritten this like:

    if (do_set_head) {
        /*
         * Way too many cases where this can go wrong
         * so let's just fail silently for now.
         */
         set_head(remote, transport->remote);
    }

, but it's obviously NBD here, just something I noticed idly while reading the rest of the patch, which looks great to me.

Thanks,
Taylor

#54 lore raw

PATCH 2/2 fetch: don't ask for remote HEAD if followRemoteHEAD is "never"

In reply to Jeff King

When we are going to consider updating the refs/remotes/*/HEAD symref, we have to ask the remote side where its HEAD points. But if we know that the feature is disabled by config, we don't need to bother!

This saves a little bit of work and network communication for the server. And even a little bit of effort on the client, as our local set_head() function did a bit of work matching the remote HEAD before realizing that we're not going to do anything with it.

---
I guess we could leave the check inside set_head() in case somebody else ever called it without checking the value of follow_remote_head. But it's a static function that is called once, so I'd rather not keep the dead code around.

 builtin/fetch.c  | 6 ++----
 t/t5510-fetch.sh | 5 ++++-
 2 files changed, 6 insertions(+), 5 deletions(-)
builtin/fetch.c +2 -4
index 66f5ae31b6..3658509740 100644
@@ -1643,9 +1643,6 @@ static int set_head(const struct ref *remote_refs, struct remote *remote)
 		string_list_append(&heads, strip_refshead(ref->name));
 	}
 
-	if (follow_remote_head == FOLLOW_REMOTE_NEVER)
-		goto cleanup;
-
 	if (!heads.nr)
 		result = 1;
 	else if (heads.nr > 1)
@@ -1729,7 +1726,8 @@ static int do_fetch(struct transport *transport,
 		if (transport->remote->fetch.nr) {
 			refspec_ref_prefixes(&transport->remote->fetch,
 					     &transport_ls_refs_options.ref_prefixes);
-			do_set_head = 1;
+			if (transport->remote->follow_remote_head != FOLLOW_REMOTE_NEVER)
+				do_set_head = 1;
 		}
 		if (branch_has_merge_config(branch) &&
 		    !strcmp(branch->remote_name, transport->remote->name)) {
t/t5510-fetch.sh +4 -1
index ad23dd11ef..5f0eb5684e 100755
@@ -119,7 +119,10 @@ test_expect_success "fetch test followRemoteHEAD never" '
 		cd two &&
 		git update-ref --no-deref -d refs/remotes/origin/HEAD &&
 		git config set remote.origin.followRemoteHEAD "never" &&
-		git fetch &&
+		GIT_TRACE_PACKET=$PWD/trace.out git fetch &&
+		# Confirm that we do not even ask for HEAD when we are
+		# not going to act on it.
+		test_grep ! "ref-prefix HEAD" trace.out &&
 		test_must_fail git rev-parse --verify refs/remotes/origin/HEAD
 	)
 '
Signature · 1 line
2.49.0.390.gc9996a6a6c
#55 lore raw

Re: [PATCH 0/2] limiting followRemoteHEAD being used

In reply to Jeff King
Quoted 10 lines · Jeff King

There is one interesting case we haven't discussed. What should:

git fetch origin HEAD

do with respect to refs/remotes/origin/HEAD? Right now it does nothing (at least with the v2 protocol). We ask about HEAD, but since we didn't fetch the matching ref, set_head() won't accept it. And after my patches, we would not even try to call set_head() at all (since we are not using the full refspecs).

Quoted 6 lines

But it's also something I could see somebody doing to try to update refs/remotes/origin/HEAD. I've left it alone for now, since my series does not change the behavior either way. But it might be something we could do on top (though it gets funny, because with the code as it is now, we'd have to ask for all of refs/heads/ to see the pointed-to branch advertised).

With v2 protocol, we have direct knowledge of where their HEAD points at (when we ask for it), so we shouldn't even have to know about what they have under "refs/heads/". I do not think it is within our contract that we'd somehow make sure that HEAD in a remote-tracking hierarchy is not dangling, or something.

In any case, I agree that it is sensible to leave it out of these changes for now.

Thanks.

#56 lore raw

Re: [PATCH 0/2] limiting followRemoteHEAD being used

Quoted 19 lines · Junio C Hamano
Quoted 10 lines · Jeff King

There is one interesting case we haven't discussed. What should:

git fetch origin HEAD

do with respect to refs/remotes/origin/HEAD? Right now it does nothing (at least with the v2 protocol). We ask about HEAD, but since we didn't fetch the matching ref, set_head() won't accept it. And after my patches, we would not even try to call set_head() at all (since we are not using the full refspecs).

Quoted 6 lines

But it's also something I could see somebody doing to try to update refs/remotes/origin/HEAD. I've left it alone for now, since my series does not change the behavior either way. But it might be something we could do on top (though it gets funny, because with the code as it is now, we'd have to ask for all of refs/heads/ to see the pointed-to branch advertised).

Quoted 8 lines

With v2 protocol, we have direct knowledge of where their HEAD points at (when we ask for it), so we shouldn't even have to know about what they have under "refs/heads/". I do not think it is within our contract that we'd somehow make sure that HEAD in a remote-tracking hierarchy is not dangling, or something.

In any case, I agree that it is sensible to leave it out of these changes for now.

Ditto, and I am quite happy with the end-state of this series.

Quoted 1 line

Thanks.

Thanks,
Taylor

#57 lore raw

PATCH 9/9 fetch: use ref prefix list to skip ls-refs

In reply to Jeff King

In git-fetch we have an optimization to avoid issuing an ls-refs command to the server if we don't care about the value of any refs (e.g., because we are fetching exact object ids), saving a round-trip to the server. This comes from e70a3030e7 (fetch: do not list refs if fetching only hashes, 2018-09-27).

It uses an explicit flag "must_list_refs" to decide when we need to do so. That was needed back then, because the list of ref-prefixes was not always complete. If it was empty, it did not necessarily mean that we were not interested in any refs). But that is no longer the case; an empty list of prefixes means that we truly do not care about any refs.

And so rather than an explicit flag, we can just check whether we are interested in any ref prefixes. This simplifies the code slightly, as there is now a single source of truth for the decision.

It also fixes a bug in / optimizes a very unlikely case, which is:

git fetch $remote ^foo $oid

I.e., a negative refspec combined with an exact oid fetch. This is somewhat nonsense, in that there are no positive refspecs mentioning refs to countermand with the negative one. But we should be able to do this without issuing an ls-refs command (excluding "foo" from the empty set will obviously still be the empty set).

However, the current code does not do so. The negative refspec is not counted as a noop in un-setting the must_list_refs flag (hardly the fault of e70a3030e7, as negative refspecs did not appear until much later). But by using the prefix list as a source of truth, this naturally just works; the negative refspec does not add a prefix to ask about, and hence does not trigger the ls-refs call.

This is esoteric enough that I didn't bother adding a test. The real value here is in the code simplification.

---
 builtin/fetch.c | 27 +++++++--------------------
 1 file changed, 7 insertions(+), 20 deletions(-)
builtin/fetch.c +7 -20
index c26866e674..02af505469 100644
@@ -1718,7 +1718,6 @@ static int do_fetch(struct transport *transport,
 	const struct ref *remote_refs;
 	struct transport_ls_refs_options transport_ls_refs_options =
 		TRANSPORT_LS_REFS_OPTIONS_INIT;
-	int must_list_refs = 1;
 	struct fetch_head fetch_head = { 0 };
 	struct strbuf err = STRBUF_INIT;
 
@@ -1737,21 +1736,7 @@ static int do_fetch(struct transport *transport,
 	}
 
 	if (rs->nr) {
-		int i;
-
 		refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);
-
-		/*
-		 * We can avoid listing refs if all of them are exact
-		 * OIDs
-		 */
-		must_list_refs = 0;
-		for (i = 0; i < rs->nr; i++) {
-			if (!rs->items[i].exact_sha1) {
-				must_list_refs = 1;
-				break;
-			}
-		}
 	} else {
 		struct branch *branch = branch_get(NULL);
 
@@ -1776,18 +1761,20 @@ static int do_fetch(struct transport *transport,
 				    "HEAD");
 	}
 
-	if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
-		must_list_refs = 1;
+	if (tags == TAGS_SET || tags == TAGS_DEFAULT)
 		strvec_push(&transport_ls_refs_options.ref_prefixes,
 			    "refs/tags/");
-	}
 
-	if (must_list_refs &&
+	if (transport_ls_refs_options.ref_prefixes.nr &&
 	    uses_remote_tracking(transport, rs))
 		strvec_push(&transport_ls_refs_options.ref_prefixes,
 			    "HEAD");
 
-	if (must_list_refs) {
+	/*
+	 * Only initiate ref listing if we have at least one ref we want to
+	 * know about.
+	 */
+	if (transport_ls_refs_options.ref_prefixes.nr) {
 		trace2_region_enter("fetch", "remote_refs", the_repository);
 		remote_refs = transport_get_remote_refs(transport,
 							&transport_ls_refs_options);
Signature · 1 line
2.49.0.rc1.381.gc60f5426ff
#58 lore raw

Re: [PATCH 0/9] fetch: further ref-prefix cleanups and optimizations

In reply to Jeff King
Quoted 7 lines · Jeff King
Quoted 5 lines · Taylor Blau

Peff and I talked about this today, and neither of us could find any reasons not to pursue the approach listed in the footnote of

<[email protected]>

Quoted 7 lines
Quoted 4 lines

, but this is a more conservative approach that should fix the issue and apply cleanly on top of 'maint'. It may be worth picking this into 2.49, even though we are already quite late into the -rc cycle, this is a fairly nasty bug.

Yeah, I favor doing this simple fix first, and then trying the larger (and slightly riskier) change on top.

Yeah, definitely. I think fixing the issue immediately is the most important thing we can do, but cleaning up the code (once the immediate issue has been resolved) is also worth doing.

Quoted 4 lines

I started to write up that larger patch, and found a number of interesting things. ;) So here's a 9-patch series, which would apply on top of tb/fetch-follow-tags-fix (but can very much wait to cook in the next development cycle).

What?! You found other things to clean up along the way? Blasphemy.

Quoted 5 lines
 builtin/fetch.c        | 46 +++++++++++++++++-------------------------
 refspec.c              | 22 ++++++++++++++------
 t/t5516-fetch-push.sh  | 12 ++++++-----
 t/t5702-protocol-v2.sh | 44 +++++++++++++++++++++++++++++++++++++++-
 4 files changed, 85 insertions(+), 39 deletions(-)

Kidding, of course, I am looking forward to reviewing it now.

Thanks,
Taylor

Reviewed-by
#59 lore raw

Re: [PATCH 0/9] fetch: further ref-prefix cleanups and optimizations

Quoted 7 lines · Taylor Blau
Quoted 5 lines
 builtin/fetch.c        | 46 +++++++++++++++++-------------------------
 refspec.c              | 22 ++++++++++++++------
 t/t5516-fetch-push.sh  | 12 ++++++-----
 t/t5702-protocol-v2.sh | 44 +++++++++++++++++++++++++++++++++++++++-
 4 files changed, 85 insertions(+), 39 deletions(-)

Kidding, of course, I am looking forward to reviewing it now.

This looks great to me. I left a few minor comments throughout, but am unsurprisingly on-board with the overall approach as you and I discussed this off-list a week or two ago.

Thanks,
Taylor

#60 lore raw

Re: [PATCH 0/9] fetch: further ref-prefix cleanups and optimizations

Quoted 7 lines · Taylor Blau
Quoted 6 lines · Taylor Blau
Quoted 5 lines
 builtin/fetch.c        | 46 +++++++++++++++++-------------------------
 refspec.c              | 22 ++++++++++++++------
 t/t5516-fetch-push.sh  | 12 ++++++-----
 t/t5702-protocol-v2.sh | 44 +++++++++++++++++++++++++++++++++++++++-
 4 files changed, 85 insertions(+), 39 deletions(-)
Quoted 7 lines
Quoted 1 line

Kidding, of course, I am looking forward to reviewing it now.

This looks great to me. I left a few minor comments throughout, but am unsurprisingly on-board with the overall approach as you and I discussed this off-list a week or two ago.

Thanks. My biggest question is on patch 8, and whether the existing set_head() feature really ought to be more aggressive about pointing at stuff we didn't fetch (and that could even be unborn on our side!).

I don't have a strong opinion either way, but it would mean adjusting the idea of when set_head() kicks in (e.g., should we do it for every fetch, or should exact-oid fetches continue to optimize out the ls-refs call?).

-Peff