Skip to contentSkip to conversation

fetch: add config to avoid fetching every branch in shallow repo

PATCH In review Harald Nordgren via GitGitGadget 7 participants 177 messages Sep 19 – Nov 26 Page 2 of 2

Subsystems (2)
lore
Share & tools
lore
#124 lore raw

PATCH v12 5/5 remote: default to --limited-fetch in a shallow repository

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

Adding a second remote to a shallow clone used to still fetch every branch that remote has, since "git remote add" always set up a wildcard remote.<name>.fetch refspec regardless of how shallow the repository already was. That defeats the purpose of having cloned shallow in the first place (for example with --single-branch), and can make a plain "git fetch" on that remote hang or take a very long time on a repository with many branches.

Turn --limited-fetch on by default when the repository is already shallow and neither -t/--track nor --mirror was given, so that adding a remote there does not by itself commit to following every branch it has. --no-limited-fetch keeps the previous behavior for whoever wants it.

---
 Documentation/git-remote.adoc |  8 ++++-
 builtin/remote.c              |  5 ++-
 t/t5505-remote.sh             | 60 +++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+), 2 deletions(-)
Documentation/git-remote.adoc +7 -1
index 1f490819ba..245c0c4558 100644
@@ -57,6 +57,11 @@ Add a remote named _<name>_ for the repository at
 _<URL>_.  The command `git fetch <name>` can then be used to create and
 update remote-tracking branches `<name>/<branch>`.
 +
+If the repository is already a shallow repository (see linkgit:git-clone[1]
+`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,
+`--limited-fetch` is turned on by default, so that `git fetch <name>` does
+not need to negotiate history for every branch the remote has.
++
 With `-f` option, `git fetch <name>` is run immediately after
 the remote information is set up.
 +
@@ -79,7 +84,8 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec
 that tracks all branches, `remote.<name>.fetchRefmap` is set up so that a
 refspec-less `git fetch <name>` only fetches branches our local branches
 are built on. See the `--refmap` entry in linkgit:git-fetch[1] for
-details.
+details. `--no-limited-fetch` explicitly disables this, overriding the
+shallow-repository default described above.
 +
 With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set
 up to point at remote's _<master>_ branch. See also the set-head command.
builtin/remote.c +4 -1
index 5f8a8a2f71..5d2388a26c 100644
@@ -16,6 +16,7 @@
 #include "rebase.h"
 #include "refs.h"
 #include "refspec.h"
+#include "shallow.h"
 #include "odb.h"
 #include "strvec.h"
 #include "commit-reach.h"
@@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,
 
 	if (!mirror || mirror & MIRROR_FETCH) {
 		bool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&
-			limited_fetch == 1;
+			(limited_fetch == 1 ||
+			 (limited_fetch == -1 &&
+			  is_repository_shallow(the_repository)));
 
 		strbuf_reset(&buf);
 		if (use_limited_fetch) {
t/t5505-remote.sh +60 -0

66 lines not on this page · raw message

Signature · 1 line
gitgitgadget
#79 lore raw

Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap

In reply to Junio C Hamano
Quoted 10 lines · Junio C Hamano

I actually do not think we should have master next seen on the same remote.cabal.fetch configuration variable. It is a multi-valued variable, so it should read more like

[remote "cabal"]
fetch = refs/heads/master
fetch = refs/heads/next
fetch = refs/heads/seen
refmap = +refs/heads/*:refs/remotes/cabal/*

Doesn't that cause older versions of git to try and update the local branch when fetching, or is it only when pushing that we'll fill in the right hand side of the refspec with a copy of the left hand side? If we use a different config setting for "what to fetch" then users that want the new behavior can unset remote.<name>.fetch and older versions of git will fail rather than overwriting local branches when there is no refspec on the commandline.

Thanks

Phillip

Quoted 15 lines

And without these remote.cabal.fetch configured, but with remote.cabal.refmap configured, this command line

	$ git fetch cabal next

should work just like

	$ git fetch cabal \
		--refmap="+refs/heads/*:refs/remotes/cabal/*" \
		next

I think.

Thanks.

#112 lore raw

Re: [PATCH v10 0/5] fetch: avoid fetching every branch of a new remote in a shallow repo

In reply to Junio C Hamano
Quoted 9 lines · Junio C Hamano

Phillip Wood <[email protected]> writes:

When I wrote the refmap feature (without configuration variable) long time ago, one of the goals was to allow me to say

     $ git fetch cabal \
         --refmap="+refs/heads/*:refs/remotes/cabal/*" \
	maint master next
Quoted 8 lines

That is, "here is the place I grab from there are stored" and "maint master next are the things I grab from there" are specified separately. With the configuration variable added in this series,

     $ git config set remote.cabal.refmap "+refs/heads/*:refs/remotes/cabal/*"
     $ git fetch cabal master next seen

should become possible with the same behaviour.

That makes sense

Quoted 8 lines
Quoted 7 lines

That
setting could accept something like "*@{upstream}" to "mean fetch the upstream branches of my local branches that live on this remote" which would cover what this series wants. I think it would be useful to support "+" and "^" prefixes on the refs to fetch as well, so that the refmap config controls the mapping and the "what to fetch" config handles negation and forced updates on a per-ref basis.

Quoted 6 lines

The reasoning here is slightly wrong. "^" (not) may be good thing to consider as it is to decide what to fetch, but "+" (overwrite) should be on the refmap side, i.e., where to store. If you are not storing, then "overwrite what I have with non-fast-forward" becomes a nonsense request, and that is why it does not belong to "what to fetch".

I see what you're saying. It is unfortunate though that we then need separate refspecs if want to reject non fast-forward updates of some branches but map the refs in the same way for the fast-forward and non-fast-forward cases.

Thanks

Phillip

#80 lore raw

Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap

In reply to Phillip Wood
Quoted 12 lines · Phillip Wood
Quoted 10 lines · Junio C Hamano

I actually do not think we should have master next seen on the same remote.cabal.fetch configuration variable. It is a multi-valued variable, so it should read more like

[remote "cabal"]
fetch = refs/heads/master
fetch = refs/heads/next
fetch = refs/heads/seen
refmap = +refs/heads/*:refs/remotes/cabal/*

Quoted 3 lines

Doesn't that cause older versions of git to try and update the local branch when fetching, or is it only when pushing that we'll fill in the right hand side of the refspec with a copy of the left hand side?

"git fetch --help" is fairly clear on what should happen in its description on <refspec> command line arguments, and

I meant in the above example that remote.cabal.fetch should be used as if these three refnames are written on the fetch command line when nothing is given from the command line. Now, <refspec> that does not have :dst side for fetch stores the result nowhere but FETCH_HEAD, so without remote.cabal.refmap (i.e., older versions of git), you will get them in FETCH_HEAD, so your "git pull" will still work to create an octopus merge ;-)

I was hoping that with refmap, the users can tell the system where to store them. As specifying a shorthand also works,

[remote "cabal"]
fetch = master
fetch = next
fetch = seen
refmap = +refs/heads/*:refs/remotes/cabal/*

would be an easy layout to allow adding different branches than having to say

[remote "cabal"]
fetch = +refs/heads/master:refs/remotes/cabal/master fetch = +refs/heads/next:refs/remotes/cabal/next fetch = +refs/heads/seen:refs/remotes/cabal/seen

Thanks.

#98 lore raw

Re: [PATCH v10 2/5] fetch: extract collect_upstream_from_remote() helper

In reply to Harald Nordgren
Quoted 32 lines · Harald Nordgren
Quoted 32 lines
Quoted 5 lines

The latter is independent of the former on purpose: a narrow refspec (for example from "git remote add -t") can leave the current branch's own upstream uncovered, and we still need to be able to fetch it.

I don't really understand the meaning here. I can guess that you mean something like "A remote with a narrow refspec, like what you get from 'git remote add -t', can omit a branch's upstream from the remote's fetch refspec, and we still want to fetch that upstream." But it's taken me a while to put that together (after puzzling out the above) after first not knowing what "uncovered" meant.

I think the whole thing would be clearer with an example:

"""
Imagine a remote with a narrow refspec:

git remote add -t main colleague <remote>

and a branch whose upstream points at that remote, but isn't part of the remote's refspec:

git fetch colleague work:refs/remotes/colleague/work git switch -c work colleague/work

When we "git fetch colleague" without a refspec, we decide what to fetch by combining the remote's refspec (main -> remote-tracking branch, in this case) and the current branch's upstream, if it was based on that remote (work -> remote-tracking branch, in this case). """

Quoted 8 lines
Quoted 1 line

Note: we aren't "guessing" what to fetch!

Thanks! Maybe this should be the example instead?

git clone --single-branch <url>
git switch -c work
git push -u origin work

I'm not sure if that captures the same "narrow refspec" remote or not, to be honest, though it might be simpler? I'm not sure what the push has to do with it, though, and I don't know what you think about the rest of the proposed wording ;)

[later]

Reading the new version, I think I see what you're after. I personally would love to see an example adding a remote, since I tend to want "big" fetches from my primary remotes, but that's fine.

#81 lore raw

Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap

In reply to Junio C Hamano
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Phillip Wood

Doesn't that cause older versions of git to try and update the local branch when fetching, or is it only when pushing that we'll fill in the right hand side of the refspec with a copy of the left hand side?

Quoted 7 lines

I meant in the above example that remote.cabal.fetch should be used as if these three refnames are written on the fetch command line when nothing is given from the command line. Now, <refspec> that does not have :dst side for fetch stores the result nowhere but FETCH_HEAD, so without remote.cabal.refmap (i.e., older versions of git), you will get them in FETCH_HEAD, so your "git pull" will still work to create an octopus merge ;-)

Thanks for clarifying that

Quoted 9 lines

I was hoping that with refmap, the users can tell the system where to store them. As specifying a shorthand also works,

[remote "cabal"]
fetch = master
fetch = next
fetch = seen
refmap = +refs/heads/*:refs/remotes/cabal/*

Quoted 7 lines

would be an easy layout to allow adding different branches than having to say

[remote "cabal"]
fetch = +refs/heads/master:refs/remotes/cabal/master fetch = +refs/heads/next:refs/remotes/cabal/next fetch = +refs/heads/seen:refs/remotes/cabal/seen

That makes sense. What should we do if "remote.cabal.refmap" exists and "remote.cabal.fetch" contains a colon? We could just ignore that entry but it might be better to error out.

Thanks

Phillip

#82 lore raw

Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap

Quoted 12 lines · Phillip Wood
Quoted 12 lines

I was hoping that with refmap, the users can tell the system where to store them. As specifying a shorthand also works,

[remote "cabal"]
fetch = master
fetch = next
fetch = seen
refmap = +refs/heads/*:refs/remotes/cabal/*

would be an easy layout to allow adding different branches than having to say

Quoted 8 lines
Quoted 4 lines

[remote "cabal"]
fetch = +refs/heads/master:refs/remotes/cabal/master fetch = +refs/heads/next:refs/remotes/cabal/next fetch = +refs/heads/seen:refs/remotes/cabal/seen

That makes sense. What should we do if "remote.cabal.refmap" exists and "remote.cabal.fetch" contains a colon? We could just ignore that entry but it might be better to error out.

Possible alternatives include

- giving fetch with dst take precedence over refmap
- storing the result in two locations
- error out not knowing what is the right thing to do

I think the command line version, i.e.

    $ git fetch $URL --refmap='refs/heads/*:refs/remotes/alt/*' \
		refs/heads/master:refs/remotes/origin/master \
		next

stores 'master' as alt/master and origin/master, and 'next' as alt/next, i.e., takes the second option.

Thanks.

#83 lore raw

Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap

Quoted 24 lines · Junio C Hamano
Quoted 12 lines · Phillip Wood
Quoted 12 lines

I was hoping that with refmap, the users can tell the system where to store them. As specifying a shorthand also works,

[remote "cabal"]
fetch = master
fetch = next
fetch = seen
refmap = +refs/heads/*:refs/remotes/cabal/*

would be an easy layout to allow adding different branches than having to say

Quoted 8 lines
Quoted 4 lines

[remote "cabal"]
fetch = +refs/heads/master:refs/remotes/cabal/master fetch = +refs/heads/next:refs/remotes/cabal/next fetch = +refs/heads/seen:refs/remotes/cabal/seen

That makes sense. What should we do if "remote.cabal.refmap" exists and "remote.cabal.fetch" contains a colon? We could just ignore that entry but it might be better to error out.

Quoted 5 lines

Possible alternatives include

- giving fetch with dst take precedence over refmap
- storing the result in two locations
- error out not knowing what is the right thing to do

The answer seems to be the second one, with my v12, and I wrote a test now to confirm it, I can include it in a v13 when it's time:

```
+test_expect_success 'remote.<name>.fetchRefmap stores an explicit-dst
remote.<name>.fetch entry in both places' '
+       test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
+       git init -b master fetch-refmap-upstream &&
+       test_commit -C fetch-refmap-upstream base &&
+       git init downstream &&
+       (
+               cd downstream &&
+               git remote add cabal ../fetch-refmap-upstream &&
+               git config --unset-all remote.cabal.fetch &&
+               git config --add remote.cabal.fetch \
+                       "+refs/heads/master:refs/remotes/custom/master" &&
+               git config remote.cabal.fetchRefmap
"+refs/heads/*:refs/remotes/cabal/*" &&
+               git fetch cabal &&
+               check_fetched_refs <<-\EOF
+               refs/remotes/cabal/HEAD
+               refs/remotes/cabal/master
+               refs/remotes/custom/master
+               EOF
+       )
+'
```

Harald

#89 lore raw

Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap

In reply to Phillip Wood
Quoted 12 lines · Phillip Wood

Hi Harald

Quoted 5 lines · Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>
Add a per-remote config variable, remote.<name>.refmap, that provides the default value for --refmap the same way remote.<name>.fetch already provides the default refspecs to fetch.

The second half of this sentence seems redundant to me. Can we please call the new config setting "remote.<name>.fetchRefmap" so that it is clear it applies to fetches. That would also allow us to add a matching "remote.<name>.pushRefmap" in the future to map the refnames of refs that are pushed without setting up a default set of refs to push.

Yes, I would love to add pushRefmap atop later! It would ease my working with notes, I think.

I’m on mobile now, but if I remember I’ll try to spell out what I mean more concretely. Thanks for the reminder!

#90 lore raw

Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap

Quoted 12 lines · Ben Knoble
Quoted 12 lines · Phillip Wood

Hi Harald

Quoted 5 lines · Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>
Add a per-remote config variable, remote.<name>.refmap, that provides the default value for --refmap the same way remote.<name>.fetch already provides the default refspecs to fetch.

Quoted 7 lines
Quoted 1 line

The second half of this sentence seems redundant to me. Can we please call the new config setting "remote.<name>.fetchRefmap" so that it is clear it applies to fetches. That would also allow us to add a matching "remote.<name>.pushRefmap" in the future to map the refnames of refs that are pushed without setting up a default set of refs to push.

Yes, I would love to add pushRefmap atop later! It would ease my working with notes, I think.

I’m on mobile now, but if I remember I’ll try to spell out what I mean more concretely. Thanks for the reminder!

It has already been renamed to fetchRefmap in v12.

Harald

#84 lore raw

Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap

In reply to Junio C Hamano
Quoted 13 lines · Junio C Hamano
Quoted 4 lines · Phillip Wood

That makes sense. What should we do if "remote.cabal.refmap" exists and "remote.cabal.fetch" contains a colon? We could just ignore that entry but it might be better to error out.

Possible alternatives include

- giving fetch with dst take precedence over refmap
- storing the result in two locations
- error out not knowing what is the right thing to do

Quoted 8 lines

I think the command line version, i.e.

     $ git fetch $URL --refmap='refs/heads/*:refs/remotes/alt/*' \
		refs/heads/master:refs/remotes/origin/master \
		next

stores 'master' as alt/master and origin/master, and 'next' as alt/next, i.e., takes the second option.

That's surprising, I'd naively assumed a refspec on the command line would override the refmap. It feels problematic if we cannot override the fetchRefmap config with a refspec on the command line, but need to use "--refmap" instead.

I think there are at least a couple of ways of thinking about the interaction of "remote.<name>.fetchRefmap" and "remote.<name>.fetch". The first is to consider that the "fetchRefmap" config is for "where to store the fetched refs" and the "fetch" config is for "what to fetch". In that case we'd reject values containing colons in the "fetch" config as any mapping should be done by the "fetchRefmap" config. The other way is to consider that the "fetchRefmap" config holds the default argument for "--refmap" and "fetch" holds the default command line refspecs to use when the user doesn't specify any. In that case we should accept "fetch" values containing colons and behave as if those values had been given as command line arguments. I'm leaning towards the latter, but if we do decide to do that, I think we should make sure

  git -c remote.cabal.fetchRefmap='refs/heads/*:refs/remotes/cabal/*' \
    fetch cabal refs/heads/master:refs/remotes/origin/master

only updates origin/master and not cabal/master.

Thanks

Phillip

#85 lore raw

Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap

Quoted 6 lines · Phillip Wood
Quoted 6 lines

I think the command line version, i.e.

     $ git fetch $URL --refmap='refs/heads/*:refs/remotes/alt/*' \
		refs/heads/master:refs/remotes/origin/master \
		next
Quoted 7 lines
Quoted 2 lines

stores 'master' as alt/master and origin/master, and 'next' as alt/next, i.e., takes the second option.

That's surprising, I'd naively assumed a refspec on the command line would override the refmap. It feels problematic if we cannot override the fetchRefmap config with a refspec on the command line, but need to use "--refmap" instead.

Maybe, maybe not. The command line refmap was meant to primarily help use case where the "what to fetch" was to be specified without colon, so when fetching multiple branches, you do not have to repeat the refs/remotes/origin/ all over again. So if you are giving a refspec with a colon (like 'master' in the above example) to specify where to store, it is an unusual request to do something different.

Something different could be "do more than what the refmap alone would do", which led to the behaviour implemented when refmap was introduced. But it could be "refrain from what the refmap alone would do and do something else", and if you take that position, you would get the "overriding" behaviour.

#91 lore raw

Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap

In reply to Ben Knoble
Quoted 5 lines · Ben Knoble

Yes, I would love to add pushRefmap atop later! It would ease my working with notes, I think.

Yes, I would love to add pushRefmap atop later! It would ease my working with notes, I think.

Tell us more when you are off mobile ;-).

I do not think I planned to add '--refmap' support on the push side, even from the command line, but I can imagine there would be reasonable workflows that benefit from having one.

#92 lore raw

Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap

Quoted 6 lines · Junio C Hamano
Quoted 2 lines · Ben Knoble

Yes, I would love to add pushRefmap atop later! It would ease my working with notes, I think.

Quoted 8 lines
Quoted 2 lines

Yes, I would love to add pushRefmap atop later! It would ease my working with notes, I think.

Tell us more when you are off mobile ;-).

I do not think I planned to add '--refmap' support on the push side, even from the command line, but I can imagine there would be reasonable workflows that benefit from having one.

Not sure if it's related to this specific conversation, but I realized that @{push} is not being resolved here, so I plan on adding a small fix in a v13:

```
--- a/remote.c
+++ b/remote.c
@@ -2018,6 +2018,8 @@ static char *tracking_for_push_dest(struct
repository *repo,

        remote = repo_remote_for_push_tracking(repo, remote);
        ret = apply_refspecs(&remote->fetch, refname);
+       if (!ret)
+               ret = apply_refspecs(&remote->refmap, refname);
        if (!ret)
                return error_buf(err,
                                 _("push destination '%s' on remote
'%s' has no local tracking branch"),
```

Harald

#125 lore raw

v13 cover fetch: avoid fetching every branch of a new remote in a shallow repo

In reply to Harald Nordgren via GitGitGadget

Avoid fetching every branch of a new remote in a shallow repo.

Changes in v13:

* Added a new commit so @{push} resolves correctly for a --limited-fetch remote, falling back to remote.<name>.fetchRefmap the same way fetch already does when remote.<name>.fetch doesn't cover the branch being pushed.
* Added a regression test to fetch: add remote.<name>.fetchRefmap covering a remote.<name>.fetch entry with an explicit destination, confirming it's stored in both that destination and the refmap's.

Changes in v12:

* Renamed remote.<name>.refmap to remote.<name>.fetchRefmap throughout code, docs, and tests, so it reads unambiguously as fetch-only.
* Added the missing code path in get_ref_map() so a branch fetched by name via remote.<name>.fetch is also stored through remote.<name>.fetchRefmap, with a new test (the "cabal" example: fetch = master/next/seen plus a refmap storing them all under one namespace).
* Reworded the fetchRefmap commit message and doc entry to drop a redundant sentence, fix a confusing paragraph, and spell out that remote.<name>.fetch, when configured, still decides what's fetched while the refmap only remaps it.
* Added a prefetch coverage test to t5586-fetch-refmap.sh.

Changes in v11:

* remote_find_tracking() now falls back to remote.<name>.refmap when remote.<name>.fetch doesn't match. Negative refspecs in fetch still exclude refs, and both cases have tests
* The helpers write straight into a strvec, take the output parameter last and the remote as const
* The followRemoteHEAD lookup is done once, not duplicated in two branches
* Clearer commit messages and docs: a working example plus a reference to 49ca2fba39, the goal stated first, an incorrect "used to die" claim dropped, and an explanation of why a refmap turns on the inference
* use_limited_fetch is a bool

Changes in v10:

* Resolve merge conflict after master integrated
ch/fetch-followremotehead-lazy-validation.

Changes in v9:

* Moved the do_fetch() if/else-if/else split out of the preliminary helper-extraction commit and into the refmap-inference commit that needs it. Preliminary commit is now a simpler refactor.

Changes in v8:

* Split into a preliminary collect_upstream_from_remote() helper commit plus a simplified refmap-inference commit.
* Fixed a regression that could drop the current branch's @{upstream} when remote.<name>.fetch was narrowed.
* Threaded struct repository * through instead of hardcoding the_repository.
* Minor naming and comment cleanups.

Changes in v7:

* Fixed fatal: --refmap option is only meaningful with command-line refspec(s) firing just from configuring remote.<name>.refmap alone, with nothing else to fetch.
* When both remote.<name>.fetch and remote.<name>.refmap are configured, a refspec-less fetch follows .fetch as usual, .refmap only remaps something already being fetched by name. Added tests.

Changes in v6:

* Remove leftover reference to deleted default-branch logic in commit message.

Changes in v5:

* Renumber test file from t5585 to t5586.

Changes in v4:

* Removed the automatic default-branch fetch. A fresh remote fetches nothing until you track a branch explicitly.
* Fixed fetch report showing "new ref HEAD" instead of "new branch "
* Reworded remote..refmap docs and commit message.

Changes in v3:

* Replace the special ":"/"+:" fetch refspec with remote.<name>.refmap, reusing git's existing --refmap mechanism instead of inventing new refspec syntax.
* Split the change into 4 commits.

Changes in v2:

* Replaced the opt-in fetch.shallow config entirely with a new special fetch refspec (+:) that git remote add now defaults new remotes to in a shallow repository. The new refspec fetches whichever branches any local branch tracks at that remote, plus the remote's default branch.

Harald Nordgren (6):
fetch: add remote.<name>.fetchRefmap
fetch: extract collect_upstream_from_remote() helper
fetch: infer branches to fetch from a refmap-only remote
remote: add "git remote add --limited-fetch"
remote: default to --limited-fetch in a shallow repository
remote: resolve @{push} through remote.<name>.fetchRefmap

 Documentation/config/remote.adoc |   9 ++
 Documentation/fetch-options.adoc |  10 ++
 Documentation/git-remote.adoc    |  14 ++-
 builtin/fetch.c                  | 100 +++++++++++++++-----
 builtin/remote.c                 |  31 +++++--
 remote.c                         |  62 ++++++++++++-
 remote.h                         |  20 ++++
 t/meson.build                    |   1 +
 t/t1514-rev-parse-push.sh        |  19 ++++
 t/t5505-remote.sh                |  76 ++++++++++++++++
 t/t5510-fetch.sh                 | 151 +++++++++++++++++++++++++++++++
 t/t5586-fetch-refmap.sh          | 133 +++++++++++++++++++++++++++
 12 files changed, 593 insertions(+), 33 deletions(-)
 create mode 100755 t/t5586-fetch-refmap.sh

base-commit: b9e5cff1803ab24582201ef059516eb16014b9d9
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v13
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v13
Pull-Request: https://github.com/git/git/pull/2412

Range-diff vs v12:

1: a1f5e81fdc ! 1: 07081828d1 fetch: add remote.<name>.fetchRefmap

     @@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote
      +	)
      +'
      +
     ++test_expect_success 'remote.<name>.fetchRefmap stores an explicit-dst remote.<name>.fetch entry in both places' '
     ++	test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
     ++	git init -b master fetch-refmap-upstream &&
     ++	test_commit -C fetch-refmap-upstream base &&
     ++	git init downstream &&
     ++	(
     ++		cd downstream &&
     ++		git remote add cabal ../fetch-refmap-upstream &&
     ++		git config --unset-all remote.cabal.fetch &&
     ++		git config --add remote.cabal.fetch \
     ++			"+refs/heads/master:refs/remotes/custom/master" &&
     ++		git config remote.cabal.fetchRefmap "+refs/heads/*:refs/remotes/cabal/*" &&
     ++		git fetch cabal &&
     ++		check_fetched_refs <<-\EOF
     ++		refs/remotes/cabal/HEAD
     ++		refs/remotes/cabal/master
     ++		refs/remotes/custom/master
     ++		EOF
     ++	)
     ++'
     ++
      +test_expect_success 'remote.<name>.fetchRefmap tracks a branch outside remote.<name>.fetch' '
      +	test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
      +	git init -b main fetch-refmap-upstream &&
 2:  e8af5bc76b = 2:  3f5808cbf3 fetch: extract collect_upstream_from_remote() helper
 3:  aad6e5f66f = 3:  b02e0a5397 fetch: infer branches to fetch from a refmap-only remote
 4:  97650da870 = 4:  3ee427de87 remote: add "git remote add --limited-fetch"
 5:  a802c9f82f = 5:  2cba4623ee remote: default to --limited-fetch in a shallow repository
 -:  ---------- > 6:  25eec8d967 remote: resolve @{push} through remote.<name>.fetchRefmap
Signature · 1 line
gitgitgadget
#126 lore raw

PATCH v13 1/6 fetch: add remote.<name>.fetchRefmap

From: Harald Nordgren <[email protected]>

Add a per-remote config variable, remote.<name>.fetchRefmap, that provides the default value for --refmap the same way remote.<name>.fetch already provides the default refspecs to fetch.

Unlike passing --refmap on the command line, which requires an explicit refspec to go with it, configuring remote.<name>.fetchRefmap on its own does not change what a refspec-less fetch does: it falls back to the same defaults as a remote with no fetchRefmap at all.

When remote.<name>.fetch is also configured, it still decides what to fetch, and the refmap maps what it fetches. This lets a remote name only a few branches and store them all through one refmap:

[remote "cabal"]
fetch = master
fetch = next
fetch = seen
fetchRefmap = +refs/heads/*:refs/remotes/cabal/*

Looking up the remote-tracking branch for a remote branch, as @{upstream} and "git branch --track" do, falls back to the refmap when remote.<name>.fetch has no match, so that these work for such branches too. A branch excluded by a negative refspec in remote.<name>.fetch stays excluded.

---
 Documentation/config/remote.adoc |   7 ++
 Documentation/fetch-options.adoc |   3 +
 builtin/fetch.c                  |  16 +++-
 remote.c                         |  20 +++-
 remote.h                         |   6 ++
 t/t5510-fetch.sh                 | 151 +++++++++++++++++++++++++++++++
 6 files changed, 200 insertions(+), 3 deletions(-)
Documentation/config/remote.adoc +7 -0
index 3a20d0f752..236ccca93e 100644
@@ -33,6 +33,13 @@ remote.<name>.fetch::
 	The default set of "refspec" for linkgit:git-fetch[1]. See
 	linkgit:git-fetch[1].
 
+remote.<name>.fetchRefmap::
+	The default value of the `--refmap` option, used to map refs being
+	fetched to remote-tracking refs to store, the same way
+	`remote.<name>.fetch` does for an explicit refspec.
+	`remote.<name>.fetch`, if also configured, still decides what is
+	fetched. See the `--refmap` entry in linkgit:git-fetch[1].
+
 remote.<name>.push::
 	The default set of "refspec" for linkgit:git-push[1]. See
 	linkgit:git-push[1].
Documentation/fetch-options.adoc +3 -0
index 47dea1de8e..c2101a7b39 100644
@@ -244,6 +244,9 @@ endif::git-pull[]
 	refspecs and rely entirely on the refspecs supplied as
 	command-line arguments. See section on "Configured Remote-tracking
 	Branches" for details.
++
+`remote.<name>.refmap` provides the default value for this option, the
+same way `remote.<name>.fetch` provides the default refspecs to fetch.
 
 `-t`::
 `--tags`::
builtin/fetch.c +14 -2
index 1f76ae6242..305934f5f4 100644
@@ -518,6 +518,12 @@ static struct ref *get_ref_map(struct remote *remote,
 	struct ref *rm;
 	struct ref *ref_map = NULL;
 	struct ref **tail = &ref_map;
+	/*
+	 * The --refmap command line option, if given, takes precedence
+	 * over remote.<name>.fetchRefmap.
+	 */
+	struct refspec *effective_refmap =
+		refmap.nr ? &refmap : remote ? &remote->refmap : NULL;
 
 	/* opportunistically-updated references: */
 	struct ref *orefs = NULL, **oref_tail = &orefs;
@@ -528,6 +534,8 @@ static struct ref *get_ref_map(struct remote *remote,
 	filter_prefetch_refspec(rs);
 	if (remote)
 		filter_prefetch_refspec(&remote->fetch);
+	if (effective_refmap)
+		filter_prefetch_refspec(effective_refmap);
 
 	if (rs->nr) {
 		struct refspec *fetch_refspec;
@@ -561,8 +569,8 @@ static struct ref *get_ref_map(struct remote *remote,
 		 * by ref_remove_duplicates() in favor of one of these
 		 * opportunistic entries with FETCH_HEAD_IGNORE.
 		 */
-		if (refmap.nr)
-			fetch_refspec = &refmap;
+		if (effective_refmap && effective_refmap->nr)
+			fetch_refspec = effective_refmap;
 		else
 			fetch_refspec = &remote->fetch;
 
@@ -597,6 +605,10 @@ static struct ref *get_ref_map(struct remote *remote,
 			if (has_merge &&
 			    !strcmp(branch->remote_name, remote->name))
 				add_merge_config(&ref_map, remote_refs, branch, &tail);
+
+			for (i = 0; i < remote->refmap.nr; i++)
+				get_fetch_map(ref_map, &remote->refmap.items[i],
+					      &oref_tail, 1);
 		} else if (!prefetch) {
 			ref_map = get_remote_ref(remote_refs, "HEAD");
 			if (!ref_map)
remote.c +19 -1
index 49ee2ea318..9acab2e01a 100644
@@ -152,6 +152,7 @@ static struct remote *make_remote(struct remote_state *remote_state,
 	ret->name = xstrndup(name, len);
 	refspec_init_push(&ret->push, the_hash_algo);
 	refspec_init_fetch(&ret->fetch, the_hash_algo);
+	refspec_init_fetch(&ret->refmap, the_hash_algo);
 	string_list_init_dup(&ret->server_options);
 	string_list_init_dup(&ret->negotiation_restrict);
 	string_list_init_dup(&ret->negotiation_include);
@@ -176,6 +177,7 @@ static void remote_clear(struct remote *remote)
 
 	refspec_clear(&remote->push);
 	refspec_clear(&remote->fetch);
+	refspec_clear(&remote->refmap);
 
 	free((char *)remote->receivepack);
 	free((char *)remote->uploadpack);
@@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,
 			return -1;
 		refspec_append(&remote->fetch, v);
 		free(v);
+	} else if (!strcmp(subkey, "fetchrefmap")) {
+		char *v;
+		if (git_config_string(&v, key, value))
+			return -1;
+		refspec_append(&remote->refmap, v);
+		free(v);
 	} else if (!strcmp(subkey, "receivepack")) {
 		char *v;
 		if (git_config_string(&v, key, value))
@@ -995,7 +1003,17 @@ void ref_push_report_free(struct ref_push_report *report)
 
 int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
 {
-	return refspec_find_match(&remote->fetch, refspec);
+	char **result = refspec->src ? &refspec->dst : &refspec->src;
+
+	if (remote->fetch.nr && !refspec_find_match(&remote->fetch, refspec))
+		return 0;
+	if (refspec_find_match(&remote->refmap, refspec))
+		return -1;
+	if (refname_matches_negative_refspec_item(refspec->src, &remote->fetch)) {
+		FREE_AND_NULL(*result);
+		return -1;
+	}
+	return 0;
 }
 
 static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
remote.h +6 -0
index cca02033b9..ac485a584d 100644
@@ -90,6 +90,12 @@ struct remote {
 
 	struct refspec fetch;
 
+	/*
+	 * How to map refs fetched without an explicit destination into our
+	 * own namespace, the same as the --refmap command line option.
+	 */
+	struct refspec refmap;
+
 	/*
 	 * The setting for whether to fetch tags (as a separate rule from the
 	 * configured refspecs);
t/t5510-fetch.sh +151 -0

157 lines not on this page · raw message

Signature · 1 line
gitgitgadget
#130 lore raw

PATCH v13 2/6 fetch: extract collect_upstream_from_remote() helper

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

When a fetch has no refspec to work from, we decide what to fetch from two sources: the remote's configured fetch refspec, and the current branch's upstream, if it lives on that remote. For example, after

git clone --single-branch <url>
git switch -c work
git push -u origin work

the remote's refspec only covers "main", but a refspec-less "git fetch origin" still fetches "work" into FETCH_HEAD, so that a plain "git pull" finds the branch to merge. See 49ca2fba39 (fetch: add branch.*.merge to default ref-prefix extension, 2022-09-08).

That "does this branch build on this remote" check was inlined for just the current branch. Move it into its own function, collect_upstream_from_remote(), so the same logic can be reused for branches other than just the current one.

---
 builtin/fetch.c | 18 ++++++++----------
 remote.c        | 15 +++++++++++++++
 remote.h        |  9 +++++++++
 3 files changed, 32 insertions(+), 10 deletions(-)
builtin/fetch.c +8 -10
index 305934f5f4..31ec963db7 100644
@@ -1985,8 +1985,6 @@ static int do_fetch(struct transport *transport,
 	if (rs->nr) {
 		refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);
 	} else {
-		struct branch *branch = branch_get(NULL);
-
 		if (transport->remote->fetch.nr) {
 			refspec_ref_prefixes(&transport->remote->fetch,
 					     &transport_ls_refs_options.ref_prefixes);
@@ -2001,14 +1999,14 @@ static int do_fetch(struct transport *transport,
 			else
 				follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;
 		}
-		if (branch && branch_has_merge_config(branch) &&
-		    !strcmp(branch->remote_name, transport->remote->name)) {
-			int i;
-			for (i = 0; i < branch->merge_nr; i++) {
-				strvec_push(&transport_ls_refs_options.ref_prefixes,
-					    branch->merge[i]->src);
-			}
-		}
+
+		/*
+		 * The configured refspec may not cover the current
+		 * branch's upstream (e.g. a narrowed -t refspec), so
+		 * make sure we can still fetch it regardless.
+		 */
+		collect_upstream_from_remote(the_repository, transport->remote,
+					      NULL, &transport_ls_refs_options.ref_prefixes);
 
 		/*
 		 * If there are no refs specified to fetch, then we just
remote.c +15 -0
index 9acab2e01a..d4259fe7f3 100644
@@ -1899,6 +1899,21 @@ int branch_merge_matches(struct branch *branch,
 	return refname_match(branch->merge[i]->src, refname);
 }
 
+void collect_upstream_from_remote(struct repository *repo,
+				   const struct remote *remote,
+				   const char *refname,
+				   struct strvec *tracked)
+{
+	struct branch *branch = repo_branch_get(repo, refname);
+
+	if (!branch_has_merge_config(branch) ||
+	    strcmp(branch->remote_name, remote->name))
+		return;
+
+	for (int i = 0; i < branch->merge_nr; i++)
+		strvec_push(tracked, branch->merge[i]->src);
+}
+
 __attribute__((format (printf,2,3)))
 static char *error_buf(struct strbuf *err, const char *fmt, ...)
 {
remote.h +9 -0
index ac485a584d..003c8fd180 100644
@@ -359,6 +359,15 @@ int branch_has_merge_config(struct branch *branch);
 
 int branch_merge_matches(struct branch *, int n, const char *);
 
+/*
+ * If refname's branch builds on remote, add its upstream on remote to
+ * tracked. A NULL refname means the current branch.
+ */
+void collect_upstream_from_remote(struct repository *repo,
+				   const struct remote *remote,
+				   const char *refname,
+				   struct strvec *tracked);
+
 /* list of the remote in a group as configured */
 struct remote_group_data {
 	const char *name;
Signature · 1 line
gitgitgadget
#131 lore raw

PATCH v13 3/6 fetch: infer branches to fetch from a refmap-only remote

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

We want remote.<name>.fetchRefmap to work on its own, without also setting remote.<name>.fetch, so a remote can be limited to the branches already in use locally, without listing them by hand or touching the command line every time.

Infer what to fetch from the local branches whose @{upstream} is already on that remote. The refmap itself still only controls where those fetched refs land, not which branches are selected.

---
 Documentation/config/remote.adoc |   4 +-
 Documentation/fetch-options.adoc |  11 ++-
 builtin/fetch.c                  |  72 +++++++++++++----
 remote.c                         |  25 ++++++
 remote.h                         |   5 ++
 t/meson.build                    |   1 +
 t/t5586-fetch-refmap.sh          | 133 +++++++++++++++++++++++++++++++
 7 files changed, 232 insertions(+), 19 deletions(-)
 create mode 100755 t/t5586-fetch-refmap.sh
Documentation/config/remote.adoc +3 -1
index 236ccca93e..0d24abfcbd 100644
@@ -38,7 +38,9 @@ remote.<name>.fetchRefmap::
 	fetched to remote-tracking refs to store, the same way
 	`remote.<name>.fetch` does for an explicit refspec.
 	`remote.<name>.fetch`, if also configured, still decides what is
-	fetched. See the `--refmap` entry in linkgit:git-fetch[1].
+	fetched. If it is not set either, a refspec-less fetch instead
+	infers what to fetch from local branches built on this remote.
+	See the `--refmap` entry in linkgit:git-fetch[1].
 
 remote.<name>.push::
 	The default set of "refspec" for linkgit:git-push[1]. See
Documentation/fetch-options.adoc +9 -2
index c2101a7b39..be27c03de7 100644
@@ -245,8 +245,15 @@ endif::git-pull[]
 	command-line arguments. See section on "Configured Remote-tracking
 	Branches" for details.
 +
-`remote.<name>.refmap` provides the default value for this option, the
-same way `remote.<name>.fetch` provides the default refspecs to fetch.
+When a refmap is active (from `--refmap` or `remote.<name>.fetchRefmap`) but
+nothing to fetch is specified on the command line, nor is there a
+`remote.<name>.fetch`, infer what to fetch from local branches built on
+this remote: remote-tracking branches from this remote that are also
+configured as the `@{upstream}` of a local branch are fetched. The
+refmap only decides where those fetched refs land, the same as it
+would for an explicit refspec, and does not narrow which branches are
+selected. It is what turns this on, since without it the fetched
+branches would have nowhere to land.
 
 `-t`::
 `--tags`::
builtin/fetch.c +56 -16

130 lines not on this page · raw message

remote.c +25 -0
index d4259fe7f3..15cd96aa8e 100644
@@ -1914,6 +1914,31 @@ void collect_upstream_from_remote(struct repository *repo,
 		strvec_push(tracked, branch->merge[i]->src);
 }
 
+struct branches_tracking_remote_cb_data {
+	struct repository *repo;
+	const struct remote *remote;
+	struct strvec *tracked;
+};
+
+static int add_if_tracking_remote(const struct reference *ref, void *cb_data)
+{
+	struct branches_tracking_remote_cb_data *data = cb_data;
+
+	collect_upstream_from_remote(data->repo, data->remote, ref->name,
+				      data->tracked);
+	return 0;
+}
+
+void branches_tracking_remote(struct repository *repo,
+			       const struct remote *remote,
+			       struct strvec *tracked)
+{
+	struct branches_tracking_remote_cb_data data = { repo, remote, tracked };
+
+	refs_for_each_branch_ref(get_main_ref_store(repo),
+				  add_if_tracking_remote, &data);
+}
+
 __attribute__((format (printf,2,3)))
 static char *error_buf(struct strbuf *err, const char *fmt, ...)
 {
remote.h +5 -0
index 003c8fd180..6fd9b05a79 100644
@@ -368,6 +368,11 @@ void collect_upstream_from_remote(struct repository *repo,
 				   const char *refname,
 				   struct strvec *tracked);
 
+/* Same as collect_upstream_from_remote(), but checked for every local branch. */
+void branches_tracking_remote(struct repository *repo,
+			       const struct remote *remote,
+			       struct strvec *tracked);
+
 /* list of the remote in a group as configured */
 struct remote_group_data {
 	const char *name;
t/meson.build +1 -0
index 11b3752f00..0953a4112f 100644
@@ -736,6 +736,7 @@ integration_tests = [
   't5582-fetch-negative-refspec.sh',
   't5583-push-branches.sh',
   't5584-http-429-retry.sh',
+  't5586-fetch-refmap.sh',
   't5600-clone-fail-cleanup.sh',
   't5601-clone.sh',
   't5602-clone-remote-exec.sh',
t/t5586-fetch-refmap.sh new file +133 -0

133 lines not on this page · raw message

Signature · 1 line
gitgitgadget
#132 lore raw

PATCH v13 4/6 remote: add "git remote add --limited-fetch"

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

A remote added the ordinary way tracks every branch it has, via a wildcard remote.<name>.fetch refspec. That is wasteful for a remote whose history is only worth following for the branches actually in use locally, and it can make "git fetch" negotiate history for branches nobody asked for.

Give "git remote add" a --limited-fetch option that sets up remote.<name>.fetchRefmap instead of remote.<name>.fetch, so that "git fetch <name>" only fetches the branches already tracked, as described in the previous commit. It is rejected together with -t/--track or --mirror, since those already say explicitly what to fetch.

---
 Documentation/git-remote.adoc |  8 +++++++-
 builtin/remote.c              | 28 ++++++++++++++++++++++------
 t/t5505-remote.sh             | 16 ++++++++++++++++
 3 files changed, 45 insertions(+), 7 deletions(-)
Documentation/git-remote.adoc +7 -1
index 315100409d..1f490819ba 100644
@@ -10,7 +10,7 @@ SYNOPSIS
 --------
 [synopsis]
 git remote [-v | --verbose]
-git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>
+git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] [--[no-]limited-fetch] <name> <URL>
 git remote rename [--[no-]progress] <old> <new>
 git remote remove <name>
 git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
@@ -75,6 +75,12 @@ the `refs/remotes/<name>/` namespace, a refspec to track only _<branch>_
 is created.  You can give more than one `-t <branch>` to track
 multiple branches without grabbing all branches.
 +
+With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec
+that tracks all branches, `remote.<name>.fetchRefmap` is set up so that a
+refspec-less `git fetch <name>` only fetches branches our local branches
+are built on. See the `--refmap` entry in linkgit:git-fetch[1] for
+details.
++
 With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set
 up to point at remote's _<master>_ branch. See also the set-head command.
 +
builtin/remote.c +22 -6
index fb7e5b114f..5f8a8a2f71 100644
@@ -179,6 +179,7 @@ static int add(int argc, const char **argv, const char *prefix,
 {
 	int fetch = 0, fetch_tags = TAGS_DEFAULT;
 	unsigned mirror = MIRROR_NONE;
+	int limited_fetch = -1; /* unspecified */
 	struct string_list track = STRING_LIST_INIT_NODUP;
 	const char *master = NULL;
 	struct remote *remote;
@@ -198,6 +199,8 @@ static int add(int argc, const char **argv, const char *prefix,
 		OPT_CALLBACK_F(0, "mirror", &mirror, "(push|fetch)",
 			N_("set up remote as a mirror to push to or fetch from"),
 			PARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),
+		OPT_BOOL(0, "limited-fetch", &limited_fetch,
+			N_("fetch only the branches we build on, instead of every branch")),
 		OPT_END()
 	};
 
@@ -211,6 +214,10 @@ static int add(int argc, const char **argv, const char *prefix,
 		die(_("specifying a master branch makes no sense with --mirror"));
 	if (mirror && !(mirror & MIRROR_FETCH) && track.nr)
 		die(_("specifying branches to track makes sense only with fetch mirrors"));
+	if (limited_fetch == 1 && track.nr)
+		die(_("--limited-fetch does not make sense with -t/--track"));
+	if (limited_fetch == 1 && mirror)
+		die(_("--limited-fetch does not make sense with --mirror"));
 
 	name = argv[0];
 	url = argv[1];
@@ -230,13 +237,22 @@ static int add(int argc, const char **argv, const char *prefix,
 	repo_config_set(the_repository, buf.buf, url);
 
 	if (!mirror || mirror & MIRROR_FETCH) {
+		bool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&
+			limited_fetch == 1;
+
 		strbuf_reset(&buf);
-		strbuf_addf(&buf, "remote.%s.fetch", name);
-		if (track.nr == 0)
-			string_list_append(&track, "*");
-		for (size_t i = 0; i < track.nr; i++) {
-			add_branch(buf.buf, track.items[i].string,
-				   name, mirror, &buf2);
+		if (use_limited_fetch) {
+			strbuf_addf(&buf, "remote.%s.fetchRefmap", name);
+			strbuf_reset(&buf2);
+			strbuf_addf(&buf2, "+refs/heads/*:refs/remotes/%s/*", name);
+			repo_config_set(the_repository, buf.buf, buf2.buf);
+		} else {
+			strbuf_addf(&buf, "remote.%s.fetch", name);
+			if (track.nr == 0)
+				string_list_append(&track, "*");
+			for (size_t i = 0; i < track.nr; i++)
+				add_branch(buf.buf, track.items[i].string,
+					   name, mirror, &buf2);
 		}
 	}
 
t/t5505-remote.sh +16 -0
index 5fbfcb0848..90cba75f93 100755
@@ -137,6 +137,22 @@ test_expect_success 'filters are listed by git remote -v only' '
 	test_grep ! "\[blob:none\]" out
 '
 
+test_expect_success '--limited-fetch works in a full repository too' '
+	test_when_finished "rm -rf full-add" &&
+	git clone --no-local one full-add &&
+	(
+		cd full-add &&
+		git remote add --limited-fetch upstream ../two &&
+		test_cmp_config "+refs/heads/*:refs/remotes/upstream/*" \
+			remote.upstream.fetchRefmap &&
+		test_must_fail git config get remote.upstream.fetch
+	)
+'
+
+test_expect_success '--limited-fetch conflicts with -t' '
+	test_must_fail git remote add --limited-fetch -t main upstream ../two
+'
+
 test_expect_success 'check remote-tracking' '
 	(
 		cd test &&
Signature · 1 line
gitgitgadget
#133 lore raw

PATCH v13 5/6 remote: default to --limited-fetch in a shallow repository

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

Adding a second remote to a shallow clone used to still fetch every branch that remote has, since "git remote add" always set up a wildcard remote.<name>.fetch refspec regardless of how shallow the repository already was. That defeats the purpose of having cloned shallow in the first place (for example with --single-branch), and can make a plain "git fetch" on that remote hang or take a very long time on a repository with many branches.

Turn --limited-fetch on by default when the repository is already shallow and neither -t/--track nor --mirror was given, so that adding a remote there does not by itself commit to following every branch it has. --no-limited-fetch keeps the previous behavior for whoever wants it.

---
 Documentation/git-remote.adoc |  8 ++++-
 builtin/remote.c              |  5 ++-
 t/t5505-remote.sh             | 60 +++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+), 2 deletions(-)
Documentation/git-remote.adoc +7 -1
index 1f490819ba..245c0c4558 100644
@@ -57,6 +57,11 @@ Add a remote named _<name>_ for the repository at
 _<URL>_.  The command `git fetch <name>` can then be used to create and
 update remote-tracking branches `<name>/<branch>`.
 +
+If the repository is already a shallow repository (see linkgit:git-clone[1]
+`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,
+`--limited-fetch` is turned on by default, so that `git fetch <name>` does
+not need to negotiate history for every branch the remote has.
++
 With `-f` option, `git fetch <name>` is run immediately after
 the remote information is set up.
 +
@@ -79,7 +84,8 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec
 that tracks all branches, `remote.<name>.fetchRefmap` is set up so that a
 refspec-less `git fetch <name>` only fetches branches our local branches
 are built on. See the `--refmap` entry in linkgit:git-fetch[1] for
-details.
+details. `--no-limited-fetch` explicitly disables this, overriding the
+shallow-repository default described above.
 +
 With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set
 up to point at remote's _<master>_ branch. See also the set-head command.
builtin/remote.c +4 -1
index 5f8a8a2f71..5d2388a26c 100644
@@ -16,6 +16,7 @@
 #include "rebase.h"
 #include "refs.h"
 #include "refspec.h"
+#include "shallow.h"
 #include "odb.h"
 #include "strvec.h"
 #include "commit-reach.h"
@@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,
 
 	if (!mirror || mirror & MIRROR_FETCH) {
 		bool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&
-			limited_fetch == 1;
+			(limited_fetch == 1 ||
+			 (limited_fetch == -1 &&
+			  is_repository_shallow(the_repository)));
 
 		strbuf_reset(&buf);
 		if (use_limited_fetch) {
t/t5505-remote.sh +60 -0

66 lines not on this page · raw message

Signature · 1 line
gitgitgadget
#134 lore raw

PATCH v13 6/6 remote: resolve @{push} through remote.<name>.fetchRefmap

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

A branch pushed to a remote set up with "git remote add --limited-fetch" has no remote.<name>.fetch entry to map it through, so @{push} cannot tell where it landed and fails with "has no local tracking branch".

Give tracking_for_push_dest() the same fallback to remote.<name>.fetchRefmap that get_ref_map() uses on the fetch side, so a --limited-fetch remote supports @{push} resolution the same way any other remote does.

---
 remote.c                  |  2 ++
 t/t1514-rev-parse-push.sh | 19 +++++++++++++++++++
 2 files changed, 21 insertions(+)
remote.c +2 -0
index 15cd96aa8e..ecd104766c 100644
@@ -2018,6 +2018,8 @@ static char *tracking_for_push_dest(struct repository *repo,
 
 	remote = repo_remote_for_push_tracking(repo, remote);
 	ret = apply_refspecs(&remote->fetch, refname);
+	if (!ret)
+		ret = apply_refspecs(&remote->refmap, refname);
 	if (!ret)
 		return error_buf(err,
 				 _("push destination '%s' on remote '%s' has no local tracking branch"),
t/t1514-rev-parse-push.sh +19 -0
index 5a4f16867a..bcf00107f5 100755
@@ -80,4 +80,23 @@ test_expect_success 'resolving @{push} fails with a detached HEAD' '
 	test_must_fail git rev-parse @{push}
 '
 
+test_expect_success '@{push} resolves after pushing to a --limited-fetch remote' '
+	test_when_finished "rm -fr upstream fork downstream" &&
+	git init -b main upstream &&
+	test_commit -C upstream base &&
+	git init --bare fork &&
+	git clone --sparse upstream downstream &&
+	(
+		cd downstream &&
+		git remote add --limited-fetch fork ../fork &&
+		git config push.default current &&
+		git config remote.pushDefault fork &&
+		git checkout -b topic main &&
+		test_commit on-topic &&
+		git push &&
+		resolve topic@{push} refs/remotes/fork/topic &&
+		test_cmp_rev topic topic@{push}
+	)
+'
+
 test_done
Signature · 1 line
gitgitgadget
#93 lore raw

Notes, refs, etc. (was: Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap)

In reply to Junio C Hamano

+Cc Kristoffer Haugsbakk as a fellow notes user ;)

Quoted 6 lines · Junio C Hamano
Quoted 2 lines · Ben Knoble

Yes, I would love to add pushRefmap atop later! It would ease my working with notes, I think.

Quoted 8 lines
Quoted 2 lines

Yes, I would love to add pushRefmap atop later! It would ease my working with notes, I think.

Tell us more when you are off mobile ;-).

I do not think I planned to add '--refmap' support on the push side, even from the command line, but I can imagine there would be reasonable workflows that benefit from having one.

In my local copy of git.git, I want to have both Junio's amlog for spelunking and my own notes for patches. I only want to push out my notes + topics to my remote, though (I work on Git from different machines and it's nice to have the notes sync'd on both). Here's the hack I ended up with, and it has some downsides.

In particular, "git push" in this configuration does too much, so I usually have to think about whether that's ok. A pushRefmap would let me separate what to push by default (push.default!) from how to map pushing when I want to push, say, notes or other things. I have to be especially careful with a "git pf" (push, force-with-lease, force-if-includes), since I usually do *not* want to forcibly overwrite the remote notes---instead I should pull those down and merge them†.

So, here's the relevant config (I have a remote "benknoble" and a remote "origin", among others):

[core]
notesRef = refs/notes/benknoble/commits
[notes]
rewriteRef = refs/notes/benknoble/commits displayRef = refs/notes/origin/amlog
[remote "origin"]
fetch = +refs/notes/amlog:refs/notes/origin/amlog

The "usual" stuff to make my notes important, but still show amlog notes when browsing history. (The origin remote section is in addition to the normal remote section with it's url and heads->origin/ mapping.)

[remote]
pushDefault = benknoble

Push to my remote.

[remote "benknoble"]
fetch = refs/notes/*:refs/notes/benknoble/* push = refs/heads/*:refs/heads/* push = refs/notes/benknoble/*:refs/notes/* push = ^refs/notes/origin/amlog

In addition to the usual config added by "git remote add", fetch remote notes into a local hierarchy, and push them out, but ignore amlog, and don't forget to also be able to push heads. Without the heads map, "git push" will not push branches!

Now, maybe I'm holding something in a way it wasn't intended and that's made my life harder, I dunno.

†: There isn't a great story for that, either :/ When "git fetch" fails to fetch notes because of divergence with local, I end up saying

git fetch benknoble refs/notes/commits
git range-diff refs/notes/benknoble/commits...FETCH_HEAD git notes merge FETCH_HEAD

or something.

It's *almost* like we need remote-tracking branches for notes, ahaha… at that point, Scott's git-meta work becomes possibly more appealing to me, even if a part of me wants to "fix" notes so everyone can use them easily.

Signature · 1 line
D. Ben Knoble
#139 lore raw

Re: [PATCH v13 0/6] fetch: avoid fetching every branch of a new remote in a shallow repo

Hi Harald

Quoted 1 line · Harald Nordgren

Hi, could I get some (hopefully final) reviews here?

The diffstat for this series is

12 files changed, 593 insertions(+), 33 deletions(-)

which will take a considerable amount of time to review properly. Yet the cover letter which should explain the problem being solved, how it is solved (i.e. the design and any trade-offs that entails, alternatives that were discarded etc.) and anything notable in the implementation is just

Avoid fetching every branch of a new remote in a shallow repo.

I've no idea from that how these patches relate to the discussion on separating "which refs to fetch" from "where to store those refs" or anything else. If you want people to volunteer their time to review your patches you need to make a effort to explain what those patches do.

Thanks

Phillip

#140 lore raw

Re: [PATCH v13 0/6] fetch: avoid fetching every branch of a new remote in a shallow repo

Quoted 14 lines · Phillip Wood

Hi Harald

Quoted 1 line · Harald Nordgren

Hi, could I get some (hopefully final) reviews here?

The diffstat for this series is

12 files changed, 593 insertions(+), 33 deletions(-)

which will take a considerable amount of time to review properly. Yet the cover letter which should explain the problem being solved, how it is solved (i.e. the design and any trade-offs that entails, alternatives that were discarded etc.) and anything notable in the implementation is just

Quoted 6 lines

Avoid fetching every branch of a new remote in a shallow repo.

I've no idea from that how these patches relate to the discussion on separating "which refs to fetch" from "where to store those refs" or anything else. If you want people to volunteer their time to review your patches you need to make a effort to explain what those patches do.

I would say that it is a fair comment.

I understand what the first three wants to do.

1. We have had for a long time "git fetch --refmap=from:to remote refs..." given on the command line to decide how refs fetched are stored separately from what refs are fetched. This allows a configuration to allow you to pretend that you give it all the time.

2. Preliminary refactoring of a helper that will gain second callsite in the next step.

3. This allows "git fetch --refmap=from:to remote" that does not specify what to fetch (contrast it with 1. above) to default to fetch refs that our local branches build on. The net effect with configured remote.<remote>.fetchRefmap is that "git fetch remote" would only fetch and store selected refs from them, only the ones that we "care about" (aka "build on").

I do not have strong opinion on the other 3 patches. 4. and 5. may help ergonomics by allowing to set up refmap from the command line option, but from my point of view it looks more like icing on the cake.

When we are working with only a subset of their branches, the first thing that comes to my mind is a "matching push" that does not need any fancy @{push} support. Because of that, I am not interested in
6. very much.

In any case, this topic one is keeping a topic by another author waiting to settle, so we have stronger incentive to see it move past the finish line.

Thanks.

#135 lore raw

Re: [PATCH v13 6/6] remote: resolve @{push} through remote.<name>.fetchRefmap

In reply to Harald Nordgren via GitGitGadget

Hi Harald,

Quoted 7 lines · Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

A branch pushed to a remote set up with "git remote add --limited-fetch" has no remote.<name>.fetch entry to map it through, so @{push} cannot tell where it landed and fails with "has no local tracking branch".

I don't have the rest of the series paged in right now (esp. patches 4 & 5 which deal with --limited-fetch), so forgive me if this comes from ignorance.

Wouldn't the same problem happen if we applied the first 3 patches, then setup a repository's remote with a fetchRefmap and limited (or none) default fetch? If so, that almost makes this seem like a bugfix for an earlier patch, and perhaps it belongs together? It also would then be better justified in those terms, I think.

Such a reorganization would maybe let the early part of the series be mostly about fetchRefmap (which I'm keen on) and let the end be about --limited-fetch, which seems nice but I don't have a strong motivation to review right now (personally---it's just limited time to spend!).

Quoted 16 lines

Give tracking_for_push_dest() the same fallback to remote.<name>.fetchRefmap that get_ref_map() uses on the fetch side, so a --limited-fetch remote supports @{push} resolution the same way any other remote does.

---
 remote.c                  |  2 ++
 t/t1514-rev-parse-push.sh | 19 +++++++++++++++++++
 2 files changed, 21 insertions(+)
remote.c +0 -0
index 15cd96aa8e..ecd104766c 100644

remote.c static char *tracking_for_push_dest(struct repository *repo, · line 2022

Quoted 5 lines
remote.c +2 -0
@@ -2018,6 +2018,8 @@ static char *tracking_for_push_dest(struct repository *repo,
 
        remote = repo_remote_for_push_tracking(repo, remote);
        ret = apply_refspecs(&remote->fetch, refname);
+       if (!ret)
+               ret = apply_refspecs(&remote->refmap, refname);

Ok, if we don't find the information in the fetch spec, we fallback to the refmap. That also means the former overrides the latter in case both are configured.

Does that match what a fetch does if both are configured and overlap? (I'm not sure, as I can't recall what we decided the fetching behavior is!)

Quoted 20 lines
remote.c +0 -0
        if (!ret)
                return error_buf(err,
                                 _("push destination '%s' on remote '%s' has no local tracking branch"),
t/t1514-rev-parse-push.sh +9 -0
index 5a4f16867a..bcf00107f5 100755
@@ -80,4 +80,23 @@ test_expect_success 'resolving @{push} fails with a detached HEAD' '
        test_must_fail git rev-parse @{push}
 '
 
+test_expect_success '@{push} resolves after pushing to a --limited-fetch remote' '
+       test_when_finished "rm -fr upstream fork downstream" &&
+       git init -b main upstream &&
+       test_commit -C upstream base &&
+       git init --bare fork &&
+       git clone --sparse upstream downstream &&
+       (
+               cd downstream &&
+               git remote add --limited-fetch fork ../fork &&

t/t1514-rev-parse-push.sh test_expect_success 'resolving @{push} fails with a detached HEAD' ' · line 101

Quoted 10 lines
t/t1514-rev-parse-push.sh +10 -0
+               git config push.default current &&
+               git config remote.pushDefault fork &&
+               git checkout -b topic main &&
+               test_commit on-topic &&
+               git push &&
+               resolve topic@{push} refs/remotes/fork/topic &&
+               test_cmp_rev topic topic@{push}
+       )
+'
+

If you take my suggestion to make this change with the early parts of the series, the test would probably need to be in terms of fetchRefmap instead of --limited-fetch, as well. That might be one reason to consider leaving it at the end, but… really the "problem" is the interaction of the earlier feature more than --limited-fetch, right?

HTH,

Signature · 1 line
D. Ben Knoble
#141 lore raw

Re: [PATCH v13 0/6] fetch: avoid fetching every branch of a new remote in a shallow repo

In reply to Junio C Hamano
Quoted 17 lines · Junio C Hamano
Quoted 14 lines · Phillip Wood

Hi Harald

Quoted 1 line · Harald Nordgren

Hi, could I get some (hopefully final) reviews here?

The diffstat for this series is

12 files changed, 593 insertions(+), 33 deletions(-)

which will take a considerable amount of time to review properly. Yet the cover letter which should explain the problem being solved, how it is solved (i.e. the design and any trade-offs that entails, alternatives that were discarded etc.) and anything notable in the implementation is just

Quoted 8 lines
Quoted 6 lines

Avoid fetching every branch of a new remote in a shallow repo.

I've no idea from that how these patches relate to the discussion on separating "which refs to fetch" from "where to store those refs" or anything else. If you want people to volunteer their time to review your patches you need to make a effort to explain what those patches do.

I would say that it is a fair comment.

Agreed. Sometimes the cover letter itself should evolve (beyond "Changes since…") as a series does.

Quoted 10 lines

I understand what the first three wants to do.

1. We have had for a long time "git fetch --refmap=from:to remote refs..." given on the command line to decide how refs fetched are stored separately from what refs are fetched. This allows a configuration to allow you to pretend that you give it all the time.

2. Preliminary refactoring of a helper that will gain second callsite in the next step.

Quoted 6 lines

3. This allows "git fetch --refmap=from:to remote" that does not specify what to fetch (contrast it with 1. above) to default to fetch refs that our local branches build on. The net effect with configured remote.<remote>.fetchRefmap is that "git fetch remote" would only fetch and store selected refs from them, only the ones that we "care about" (aka "build on").

I like these 3 as well, at least in terms of direction. I suspect we're approaching diminishing returns there?

Quoted 4 lines

I do not have strong opinion on the other 3 patches. 4. and 5. may help ergonomics by allowing to set up refmap from the command line option, but from my point of view it looks more like icing on the cake.

I think these are a reasonable idea, but I do not have time + motivation to review them further.

Quoted 4 lines

When we are working with only a subset of their branches, the first thing that comes to my mind is a "matching push" that does not need any fancy @{push} support. Because of that, I am not interested in
6. very much.

As [6] was new to me, I've just given it a glance. It reminded me that I'm not sure how an overlapping fetch + fetchRefmap configuration behaves (I think previously we said the command line version does both mappings). It's probably a good idea to make @{push} line up, but I'm not sure how to manage the overlapping case.

Quoted 3 lines

In any case, this topic one is keeping a topic by another author waiting to settle, so we have stronger incentive to see it move past the finish line.

I'm a bit out of sorts and don't recall what topic depends on this one (and "git branch --remotes --contains …/hn/fetch-refmap" didn't show any); is the dependent topic waiting on --limited-fetch or just the fetchRefmap parts? If the latter, after wrangling patch [6] we could maybe merge those parts and give --limited-fetch time for more review? idk.

Signature · 1 line
D. Ben Knoble
#136 lore raw

Re: [PATCH v13 6/6] remote: resolve @{push} through remote.<name>.fetchRefmap

In reply to D. Ben Knoble
Quoted 5 lines · D. Ben Knoble
Quoted 4 lines

A branch pushed to a remote set up with "git remote add --limited-fetch" has no remote.<name>.fetch entry to map it through, so @{push} cannot tell where it landed and fails with "has no local tracking branch".

Quoted 7 lines

I don't have the rest of the series paged in right now (esp. patches 4 & 5 which deal with --limited-fetch), so forgive me if this comes from ignorance.

Wouldn't the same problem happen if we applied the first 3 patches, then setup a repository's remote with a fetchRefmap and limited (or none) default fetch?

I think [4/6] and [5/6] would not change the picture, as they are about making it easier to set remote.*.fetchRefmap configuration from a command. You can do exactly the same with "git config" to set up the configuration and you will see the same thing.

Quoted 2 lines

If so, that almost makes this seem like a bugfix for an earlier patch, and perhaps it belongs together?

Hmph, you may have a point.

I am not a heavy user of @{push} to begin with, but if the chain of configuration through which we map the current branch to where it is pushed to always assume remote.<remote>.fetch to exist, and does not consult remote.<remote>.fetchRefmap, then yes, you could claim it as a bug that is introduced by [3/6], I think.

Quoted 1 line

It also would then be better justified in those terms, I think.

It is unclear what you exactly mean by "in those terms", but I agree that tweaking @{push} to take the new remote.<remote>.fetchRefmap into account is a logical part of that.

#127 lore raw

Re: [PATCH v13 1/6] fetch: add remote.<name>.fetchRefmap

In reply to Harald Nordgren via GitGitGadget

Hi Harald

Quoted 15 lines · Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

Add a per-remote config variable, remote.<name>.fetchRefmap, that provides the default value for --refmap the same way remote.<name>.fetch already provides the default refspecs to fetch.

Unlike passing --refmap on the command line, which requires an explicit refspec to go with it, configuring remote.<name>.fetchRefmap on its own does not change what a refspec-less fetch does: it falls back to the same defaults as a remote with no fetchRefmap at all.

When remote.<name>.fetch is also configured, it still decides what to fetch, and the refmap maps what it fetches. This lets a remote name only a few branches and store them all through one refmap:

Quoted 5 lines

[remote "cabal"]
fetch = master
fetch = next
fetch = seen
fetchRefmap = +refs/heads/*:refs/remotes/cabal/*

What happens if remote.cabal.fetch is a refspec and remote.cabal.fetchRefmap is set?

Thanks

Phillip

Quoted 327 lines

Looking up the remote-tracking branch for a remote branch, as @{upstream} and "git branch --track" do, falls back to the refmap when remote.<name>.fetch has no match, so that these work for such branches too. A branch excluded by a negative refspec in remote.<name>.fetch stays excluded.

---
  Documentation/config/remote.adoc |   7 ++
  Documentation/fetch-options.adoc |   3 +
  builtin/fetch.c                  |  16 +++-
  remote.c                         |  20 +++-
  remote.h                         |   6 ++
  t/t5510-fetch.sh                 | 151 +++++++++++++++++++++++++++++++
  6 files changed, 200 insertions(+), 3 deletions(-)
Documentation/config/remote.adoc +7 -0
index 3a20d0f752..236ccca93e 100644
@@ -33,6 +33,13 @@ remote.<name>.fetch::
  	The default set of "refspec" for linkgit:git-fetch[1]. See
  	linkgit:git-fetch[1].
 
+remote.<name>.fetchRefmap::
+	The default value of the `--refmap` option, used to map refs being
+	fetched to remote-tracking refs to store, the same way
+	`remote.<name>.fetch` does for an explicit refspec.
+	`remote.<name>.fetch`, if also configured, still decides what is
+	fetched. See the `--refmap` entry in linkgit:git-fetch[1].
+
  remote.<name>.push::
  	The default set of "refspec" for linkgit:git-push[1]. See
  	linkgit:git-push[1].
Documentation/fetch-options.adoc +3 -0
index 47dea1de8e..c2101a7b39 100644
@@ -244,6 +244,9 @@ endif::git-pull[]
  	refspecs and rely entirely on the refspecs supplied as
  	command-line arguments. See section on "Configured Remote-tracking
  	Branches" for details.
++
+`remote.<name>.refmap` provides the default value for this option, the
+same way `remote.<name>.fetch` provides the default refspecs to fetch.
 
  `-t`::
  `--tags`::
builtin/fetch.c +14 -2
index 1f76ae6242..305934f5f4 100644
@@ -518,6 +518,12 @@ static struct ref *get_ref_map(struct remote *remote,
  	struct ref *rm;
  	struct ref *ref_map = NULL;
  	struct ref **tail = &ref_map;
+	/*
+	 * The --refmap command line option, if given, takes precedence
+	 * over remote.<name>.fetchRefmap.
+	 */
+	struct refspec *effective_refmap =
+		refmap.nr ? &refmap : remote ? &remote->refmap : NULL;
 
  	/* opportunistically-updated references: */
  	struct ref *orefs = NULL, **oref_tail = &orefs;
@@ -528,6 +534,8 @@ static struct ref *get_ref_map(struct remote *remote,
  	filter_prefetch_refspec(rs);
  	if (remote)
  		filter_prefetch_refspec(&remote->fetch);
+	if (effective_refmap)
+		filter_prefetch_refspec(effective_refmap);
 
  	if (rs->nr) {
  		struct refspec *fetch_refspec;
@@ -561,8 +569,8 @@ static struct ref *get_ref_map(struct remote *remote,
  		 * by ref_remove_duplicates() in favor of one of these
  		 * opportunistic entries with FETCH_HEAD_IGNORE.
  		 */
-		if (refmap.nr)
-			fetch_refspec = &refmap;
+		if (effective_refmap && effective_refmap->nr)
+			fetch_refspec = effective_refmap;
  		else
  			fetch_refspec = &remote->fetch;
 
@@ -597,6 +605,10 @@ static struct ref *get_ref_map(struct remote *remote,
  			if (has_merge &&
  			    !strcmp(branch->remote_name, remote->name))
  				add_merge_config(&ref_map, remote_refs, branch, &tail);
+
+			for (i = 0; i < remote->refmap.nr; i++)
+				get_fetch_map(ref_map, &remote->refmap.items[i],
+					      &oref_tail, 1);
  		} else if (!prefetch) {
  			ref_map = get_remote_ref(remote_refs, "HEAD");
  			if (!ref_map)
remote.c +19 -1
index 49ee2ea318..9acab2e01a 100644
@@ -152,6 +152,7 @@ static struct remote *make_remote(struct remote_state *remote_state,
  	ret->name = xstrndup(name, len);
  	refspec_init_push(&ret->push, the_hash_algo);
  	refspec_init_fetch(&ret->fetch, the_hash_algo);
+	refspec_init_fetch(&ret->refmap, the_hash_algo);
  	string_list_init_dup(&ret->server_options);
  	string_list_init_dup(&ret->negotiation_restrict);
  	string_list_init_dup(&ret->negotiation_include);
@@ -176,6 +177,7 @@ static void remote_clear(struct remote *remote)
 
  	refspec_clear(&remote->push);
  	refspec_clear(&remote->fetch);
+	refspec_clear(&remote->refmap);
 
  	free((char *)remote->receivepack);
  	free((char *)remote->uploadpack);
@@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,
  			return -1;
  		refspec_append(&remote->fetch, v);
  		free(v);
+	} else if (!strcmp(subkey, "fetchrefmap")) {
+		char *v;
+		if (git_config_string(&v, key, value))
+			return -1;
+		refspec_append(&remote->refmap, v);
+		free(v);
  	} else if (!strcmp(subkey, "receivepack")) {
  		char *v;
  		if (git_config_string(&v, key, value))
@@ -995,7 +1003,17 @@ void ref_push_report_free(struct ref_push_report *report)
 
  int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
  {
-	return refspec_find_match(&remote->fetch, refspec);
+	char **result = refspec->src ? &refspec->dst : &refspec->src;
+
+	if (remote->fetch.nr && !refspec_find_match(&remote->fetch, refspec))
+		return 0;
+	if (refspec_find_match(&remote->refmap, refspec))
+		return -1;
+	if (refname_matches_negative_refspec_item(refspec->src, &remote->fetch)) {
+		FREE_AND_NULL(*result);
+		return -1;
+	}
+	return 0;
  }
 
  static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
remote.h +6 -0
index cca02033b9..ac485a584d 100644
@@ -90,6 +90,12 @@ struct remote {
 
  	struct refspec fetch;
 
+	/*
+	 * How to map refs fetched without an explicit destination into our
+	 * own namespace, the same as the --refmap command line option.
+	 */
+	struct refspec refmap;
+
  	/*
  	 * The setting for whether to fetch tags (as a separate rule from the
  	 * configured refspecs);
t/t5510-fetch.sh +151 -0
index 17c0be499c..b5eba020ad 100755
@@ -927,6 +927,157 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '
  	)
  '
 
+test_expect_success 'remote.*.fetchRefmap acts like --refmap on the command line' '
+	test_when_finished "git -C three config --unset remote.origin.fetchRefmap" &&
+	git branch -f side &&
+	git -C three config remote.origin.fetchRefmap \
+		"refs/heads/*:refs/remotes/other/*" &&
+	(
+		cd three &&
+		git update-ref refs/remotes/origin/main base-origin-main &&
+		o=$(git rev-parse --verify refs/remotes/origin/main) &&
+		git fetch origin main &&
+		n=$(git rev-parse --verify refs/remotes/origin/main) &&
+		test "$o" = "$n" &&
+		test_must_fail git rev-parse --verify refs/remotes/origin/side &&
+		git rev-parse --verify refs/remotes/other/main
+	)
+'
+
+check_fetched_refs () {
+	git for-each-ref --format="%(refname)" refs/remotes/ >actual &&
+	cat >expect &&
+	test_cmp expect actual
+}
+
+test_expect_success 'remote.<name>.fetchRefmap without tracking (baseline)' '
+	test_when_finished "rm -fr fetch-refmap-baseline fetch-refmap-upstream" &&
+	git init -b main fetch-refmap-upstream &&
+	test_commit -C fetch-refmap-upstream base &&
+	git -C fetch-refmap-upstream branch other &&
+	git init fetch-refmap-baseline &&
+	(
+		cd fetch-refmap-baseline &&
+		git remote add origin ../fetch-refmap-upstream &&
+
+		# Without fetch refspec, but with fetch refmap.
+		git config --unset-all remote.origin.fetch &&
+		git config remote.origin.fetchRefmap "+refs/heads/*:refs/remotes/origin/*" &&
+
+		# Nothing tracked, nothing fetched, no error.
+		git fetch origin &&
+		check_fetched_refs <<-\EOF &&
+		EOF
+
+		# Nothing tracked, explicit ref on the command line.
+		git fetch origin main &&
+		check_fetched_refs <<-\EOF &&
+		refs/remotes/origin/main
+		EOF
+
+		# With both refmap and fetch configured, remote.<name>.fetch
+		# wins: a refspec-less fetch follows it as usual, and
+		# remote.<name>.fetchRefmap plays no part in deciding what to
+		# fetch, only in mapping what is fetched.
+		git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
+		git fetch origin &&
+		check_fetched_refs <<-\EOF
+		refs/remotes/origin/HEAD
+		refs/remotes/origin/main
+		refs/remotes/origin/other
+		EOF
+	)
+'
+
+test_expect_success 'remote.<name>.fetchRefmap stores branches named in remote.<name>.fetch' '
+	test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
+	git init -b master fetch-refmap-upstream &&
+	test_commit -C fetch-refmap-upstream base &&
+	git -C fetch-refmap-upstream branch next &&
+	git -C fetch-refmap-upstream branch seen &&
+	git -C fetch-refmap-upstream branch other &&
+	git init downstream &&
+	(
+		cd downstream &&
+		git remote add cabal ../fetch-refmap-upstream &&
+		git config --unset-all remote.cabal.fetch &&
+		git config --add remote.cabal.fetch master &&
+		git config --add remote.cabal.fetch next &&
+		git config --add remote.cabal.fetch seen &&
+		git config remote.cabal.fetchRefmap "+refs/heads/*:refs/remotes/cabal/*" &&
+		git fetch cabal &&
+		check_fetched_refs <<-\EOF &&
+		refs/remotes/cabal/HEAD
+		refs/remotes/cabal/master
+		refs/remotes/cabal/next
+		refs/remotes/cabal/seen
+		EOF
+		git branch --track topic cabal/next &&
+		echo refs/remotes/cabal/next >expect &&
+		git rev-parse --symbolic-full-name topic@{upstream} >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'remote.<name>.fetchRefmap stores an explicit-dst remote.<name>.fetch entry in both places' '
+	test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
+	git init -b master fetch-refmap-upstream &&
+	test_commit -C fetch-refmap-upstream base &&
+	git init downstream &&
+	(
+		cd downstream &&
+		git remote add cabal ../fetch-refmap-upstream &&
+		git config --unset-all remote.cabal.fetch &&
+		git config --add remote.cabal.fetch \
+			"+refs/heads/master:refs/remotes/custom/master" &&
+		git config remote.cabal.fetchRefmap "+refs/heads/*:refs/remotes/cabal/*" &&
+		git fetch cabal &&
+		check_fetched_refs <<-\EOF
+		refs/remotes/cabal/HEAD
+		refs/remotes/cabal/master
+		refs/remotes/custom/master
+		EOF
+	)
+'
+
+test_expect_success 'remote.<name>.fetchRefmap tracks a branch outside remote.<name>.fetch' '
+	test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
+	git init -b main fetch-refmap-upstream &&
+	test_commit -C fetch-refmap-upstream base &&
+	git -C fetch-refmap-upstream branch other &&
+	git init downstream &&
+	(
+		cd downstream &&
+		git remote add -t main origin ../fetch-refmap-upstream &&
+		git config remote.origin.fetchRefmap "+refs/heads/*:refs/remotes/origin/*" &&
+		git fetch origin other &&
+		git branch --track topic origin/other &&
+		test_cmp_config origin branch.topic.remote &&
+		test_cmp_config refs/heads/other branch.topic.merge &&
+		echo refs/remotes/origin/other >expect &&
+		git rev-parse --symbolic-full-name topic@{upstream} >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'remote.<name>.fetchRefmap does not track a branch excluded by remote.<name>.fetch' '
+	test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
+	git init -b main fetch-refmap-upstream &&
+	test_commit -C fetch-refmap-upstream base &&
+	git -C fetch-refmap-upstream branch secret &&
+	git init downstream &&
+	(
+		cd downstream &&
+		git remote add origin ../fetch-refmap-upstream &&
+		git config --add remote.origin.fetch "^refs/heads/secret" &&
+		git config remote.origin.fetchRefmap "+refs/heads/*:refs/remotes/origin/*" &&
+		git fetch origin secret &&
+		git rev-parse --verify refs/remotes/origin/secret &&
+		test_must_fail git branch --track topic origin/secret 2>err &&
+		test_grep "not a branch" err
+	)
+'
+
  test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '
  	git branch -f side &&
  	(
#142 lore raw

Re: [PATCH v13 0/6] fetch: avoid fetching every branch of a new remote in a shallow repo

In reply to Junio C Hamano
Quoted 32 lines · Junio C Hamano
Quoted 12 lines · Phillip Wood
Quoted 1 line · Harald Nordgren

Hi, could I get some (hopefully final) reviews here?

The diffstat for this series is

12 files changed, 593 insertions(+), 33 deletions(-)

which will take a considerable amount of time to review properly. Yet the cover letter which should explain the problem being solved, how it is solved (i.e. the design and any trade-offs that entails, alternatives that were discarded etc.) and anything notable in the implementation is just

Quoted 6 lines

Avoid fetching every branch of a new remote in a shallow repo.

I've no idea from that how these patches relate to the discussion on separating "which refs to fetch" from "where to store those refs" or anything else. If you want people to volunteer their time to review your patches you need to make a effort to explain what those patches do.

I would say that it is a fair comment.

I understand what the first three wants to do.

1. We have had for a long time "git fetch --refmap=from:to remote refs..." given on the command line to decide how refs fetched are stored separately from what refs are fetched. This allows a configuration to allow you to pretend that you give it all the time.

2. Preliminary refactoring of a helper that will gain second callsite in the next step.

Quoted 6 lines

3. This allows "git fetch --refmap=from:to remote" that does not specify what to fetch (contrast it with 1. above) to default to fetch refs that our local branches build on. The net effect with configured remote.<remote>.fetchRefmap is that "git fetch remote" would only fetch and store selected refs from them, only the ones that we "care about" (aka "build on").

Thanks for the summary. I'm still not keen on patch 3 as I said in [1] I think it is a mistake to use the same variable for both "what to fetch" and "where to store it". After patch 1 we have the remote.<name>.fetchRefmap for "where to store it" and can use use remote.<name>.fetch for "what to fetch" (though I am concerned about the doubling up that happens when "fetchRefmap" is set and "fetch" is a refspec). We don't need a special case for when there are no refspecs on the command line - we can use the default values of those config settings instead and the user can configure the behavior that they want.

Quoted 4 lines

I do not have strong opinion on the other 3 patches. 4. and 5. may help ergonomics by allowing to set up refmap from the command line option, but from my point of view it looks more like icing on the cake.

I think having a way to set up fetching a limited set of branches is worthwhile, but it does not need to be part of the same series.

Quoted 4 lines

When we are working with only a subset of their branches, the first thing that comes to my mind is a "matching push" that does not need any fancy @{push} support. Because of that, I am not interested in
6. very much.

I've not looked at what that patch does

Quoted 3 lines

In any case, this topic one is keeping a topic by another author waiting to settle, so we have stronger incentive to see it move past the finish line.

What topic is that? - I can't see anything marked as depending on this in the latest "what's cooking" email. Would merging the first part of this series unblock the other one?

Thanks

Phillip

#143 lore raw

Re: [PATCH v13 0/6] fetch: avoid fetching every branch of a new remote in a shallow repo

Quoted 9 lines · Phillip Wood
Quoted 3 lines · Junio C Hamano

In any case, this topic one is keeping a topic by another author waiting to settle, so we have stronger incentive to see it move past the finish line.

What topic is that? - I can't see anything marked as depending on this in the latest "what's cooking" email. Would merging the first part of this series unblock the other one?

I believe that is ch/remote-defer-follow-remote-head-validation, which doesn't strictly depend on the changes from this topic, but the two have textual conflicts that the other author is deferring to resolve when this one lands.

<CAHeTm9M5maWM=yrJPNzCibv_rewH3-yBK8J5-MDyDvPzfihe4A@mail.gmail.com>

It looks like if this topic were merged up to patch 3/6 (fetch: infer branches to fetch from a refmap-only remote), the situation could resolve.

#128 lore raw

Re: [PATCH v13 1/6] fetch: add remote.<name>.fetchRefmap

In reply to Phillip Wood
Quoted 4 lines · Phillip Wood
Quoted 4 lines

When remote.<name>.fetch is also configured, it still decides what to fetch, and the refmap maps what it fetches. This lets a remote name only a few branches and store them all through one refmap:

Quoted 8 lines
Quoted 5 lines

[remote "cabal"]
fetch = master
fetch = next
fetch = seen
fetchRefmap = +refs/heads/*:refs/remotes/cabal/*

What happens if remote.cabal.fetch is a refspec and remote.cabal.fetchRefmap is set?

Good question. But "fetch = master" is a refspec already ;-), so the question needs to be clarified a bit.

If .fetch is a refspec with a right hand side that stores the result somewhere, and that somewhere is different from what the .fetchRefmap suggests should happen, what happens?

I think we tried earlier and saw that we store them twice [*], even without this series (i.e., specifying both refspec and refmap from the commmand line). As it is a long-standing behavior since the --refmap feature was introduced, and I do not see huge problem with that behavior, I am not sure if it is worth tweaking. But if it is wroth tweaking, we probably should do so as a preliminary clean-up before all of this, since .fetchRefmap is merely a way to allow you to avoid having to specify --refmap from the command line all the time, and this series does not change its semantics until step [3/6].

[Reference]

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

#129 lore raw

Re: [PATCH v13 1/6] fetch: add remote.<name>.fetchRefmap

Quoted 17 lines · Junio C Hamano
Quoted 10 lines · Phillip Wood
Quoted 9 lines

When remote.<name>.fetch is also configured, it still decides what to fetch, and the refmap maps what it fetches. This lets a remote name only a few branches and store them all through one refmap:

[remote "cabal"]
fetch = master
fetch = next
fetch = seen
fetchRefmap = +refs/heads/*:refs/remotes/cabal/*

If .fetch is a refspec with a right hand side that stores the result somewhere, and that somewhere is different from what the .fetchRefmap suggests should happen, what happens?

Quoted 5 lines

I think we tried earlier and saw that we store them twice [*], even without this series (i.e., specifying both refspec and refmap from the commmand line). As it is a long-standing behavior since the --refmap feature was introduced, and I do not see huge problem with that behavior, I am not sure if it is worth tweaking.

If we want to be able to use "remote.<name>.fetchRefmap" to specify how to map the names of fetched refs without specifying the default set of refs to fetch then I don't think we want

git -c 'remote.cabal.fetchRefmap=refs/heads/*:refs/remotes/cabal/*'
master feature:wip/feature

to update "refs/remotes/cabal/feature" when we gave a destination for "feature" on the command line. The existing behavior is long-standing but I wonder how often people specify a destination in the commandline refspec when they pass --refmap, so would anyone really notice if we changed it?

Quoted 6 lines

But if it is
wroth tweaking, we probably should do so as a preliminary clean-up before all of this, since .fetchRefmap is merely a way to allow you to avoid having to specify --refmap from the command line all the time, and this series does not change its semantics until step [3/6].

I agree with having a separate patch to handle the cleanup, though as I said elsewhere I think we should drop patch 3 and use the combination of ".fetchRefmap" and ".fetch" to set the default behavior when there are no refspecs on the commandline.

Thanks

Phillip

Quoted 3 lines

[Reference]

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

#137 lore raw

Re: [PATCH v13 6/6] remote: resolve @{push} through remote.<name>.fetchRefmap

In reply to D. Ben Knoble
Quoted 5 lines

Wouldn't the same problem happen if we applied the first 3 patches, then setup a repository's remote with a fetchRefmap and limited (or none) default fetch? If so, that almost makes this seem like a bugfix for an earlier patch, and perhaps it belongs together? It also would then be better justified in those terms, I think.

That's an important point, and I will take a look at fixing this. Good to have your keen eye here.

Harald

#145 lore raw

v14 cover fetch: avoid fetching every branch of a new remote in a shallow repo

In reply to Harald Nordgren via GitGitGadget

In a shallow single-branch clone of a large repository, git remote add upstream <url> sets up the default +refs/heads/*:refs/remotes/upstream/* refspec. The next git fetch upstream then downloads every branch of that remote, which defeats the point of a shallow clone.

This series lets a remote fetch only the branches that local branches track:

git remote add --limited-fetch upstream <url>
git fetch upstream main
git branch --set-upstream-to=upstream/main
git fetch upstream # fetches only main from now on

* Patches 1 and 2 add remote.<name>.fetchRefmap, a configured default for --refmap, and a small helper refactor.
* Patch 3 makes a remote with a fetchRefmap and no fetch refspec work on its own. A plain git fetch fetches the branches whose @{upstream} is on that remote, and @{push} resolves through the refmap.
* Patch 4 adds git remote add --limited-fetch to set up such a remote.
* Patch 5 makes --limited-fetch the default for git remote add in a shallow repository.

Changes in v14:

* Squashed the @{push} fix into "fetch: infer branches to fetch from a refmap-only remote", so the series no longer adds the bug in one patch and fixes it in a later one
* Moved its test to t5586 using the refmap-only remote, so it no longer depends on --limited-fetch
* Dropped the separate "remote: resolve @{push} through remote..fetchRefmap" commit

Changes in v13:

* Added a new commit so @{push} resolves correctly for a --limited-fetch remote, falling back to remote.<name>.fetchRefmap the same way fetch already does when remote.<name>.fetch doesn't cover the branch being pushed.
* Added a regression test to fetch: add remote.<name>.fetchRefmap covering a remote.<name>.fetch entry with an explicit destination, confirming it's stored in both that destination and the refmap's.

Changes in v12:

* Renamed remote.<name>.refmap to remote.<name>.fetchRefmap throughout code, docs, and tests, so it reads unambiguously as fetch-only.
* Added the missing code path in get_ref_map() so a branch fetched by name via remote.<name>.fetch is also stored through remote.<name>.fetchRefmap, with a new test (the "cabal" example: fetch = master/next/seen plus a refmap storing them all under one namespace).
* Reworded the fetchRefmap commit message and doc entry to drop a redundant sentence, fix a confusing paragraph, and spell out that remote.<name>.fetch, when configured, still decides what's fetched while the refmap only remaps it.
* Added a prefetch coverage test to t5586-fetch-refmap.sh.

Changes in v11:

* remote_find_tracking() now falls back to remote.<name>.refmap when remote.<name>.fetch doesn't match. Negative refspecs in fetch still exclude refs, and both cases have tests
* The helpers write straight into a strvec, take the output parameter last and the remote as const
* The followRemoteHEAD lookup is done once, not duplicated in two branches
* Clearer commit messages and docs: a working example plus a reference to 49ca2fba39, the goal stated first, an incorrect "used to die" claim dropped, and an explanation of why a refmap turns on the inference
* use_limited_fetch is a bool

Changes in v10:

* Resolve merge conflict after master integrated
ch/fetch-followremotehead-lazy-validation.

Changes in v9:

* Moved the do_fetch() if/else-if/else split out of the preliminary helper-extraction commit and into the refmap-inference commit that needs it. Preliminary commit is now a simpler refactor.

Changes in v8:

* Split into a preliminary collect_upstream_from_remote() helper commit plus a simplified refmap-inference commit.
* Fixed a regression that could drop the current branch's @{upstream} when remote.<name>.fetch was narrowed.
* Threaded struct repository * through instead of hardcoding the_repository.
* Minor naming and comment cleanups.

Changes in v7:

* Fixed fatal: --refmap option is only meaningful with command-line refspec(s) firing just from configuring remote.<name>.refmap alone, with nothing else to fetch.
* When both remote.<name>.fetch and remote.<name>.refmap are configured, a refspec-less fetch follows .fetch as usual, .refmap only remaps something already being fetched by name. Added tests.

Changes in v6:

* Remove leftover reference to deleted default-branch logic in commit message.

Changes in v5:

* Renumber test file from t5585 to t5586.

Changes in v4:

* Removed the automatic default-branch fetch. A fresh remote fetches nothing until you track a branch explicitly.
* Fixed fetch report showing "new ref HEAD" instead of "new branch "
* Reworded remote..refmap docs and commit message.

Changes in v3:

* Replace the special ":"/"+:" fetch refspec with remote.<name>.refmap, reusing git's existing --refmap mechanism instead of inventing new refspec syntax.
* Split the change into 4 commits.

Changes in v2:

* Replaced the opt-in fetch.shallow config entirely with a new special fetch refspec (+:) that git remote add now defaults new remotes to in a shallow repository. The new refspec fetches whichever branches any local branch tracks at that remote, plus the remote's default branch.

Harald Nordgren (5):
fetch: add remote.<name>.fetchRefmap
fetch: extract collect_upstream_from_remote() helper
fetch: infer branches to fetch from a refmap-only remote
remote: add "git remote add --limited-fetch"
remote: default to --limited-fetch in a shallow repository

 Documentation/config/remote.adoc |   9 ++
 Documentation/fetch-options.adoc |  10 ++
 Documentation/git-remote.adoc    |  14 ++-
 builtin/fetch.c                  | 100 +++++++++++++++-----
 builtin/remote.c                 |  31 +++++--
 remote.c                         |  62 ++++++++++++-
 remote.h                         |  20 ++++
 t/meson.build                    |   1 +
 t/t5505-remote.sh                |  76 ++++++++++++++++
 t/t5510-fetch.sh                 | 151 +++++++++++++++++++++++++++++++
 t/t5586-fetch-refmap.sh          | 147 ++++++++++++++++++++++++++++++
 11 files changed, 588 insertions(+), 33 deletions(-)
 create mode 100755 t/t5586-fetch-refmap.sh

base-commit: 7fb6055e678f800320d8ab8cd8f201d9493d1ba0
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v14
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v14
Pull-Request: https://github.com/git/git/pull/2412

Range-diff vs v13:

 1:  07081828d1 = 1:  c63699b402 fetch: add remote.<name>.fetchRefmap
 2:  3f5808cbf3 = 2:  4ad94eefe9 fetch: extract collect_upstream_from_remote() helper
 3:  b02e0a5397 ! 3:  6724152e69 fetch: infer branches to fetch from a refmap-only remote
     @@ Commit message
          already on that remote. The refmap itself still only controls where
          those fetched refs land, not which branches are selected.
      
     +    Let @{push} use the refmap too, so a branch pushed to such a remote
     +    resolves to the remote-tracking branch the push updated, instead of
     +    failing with "has no local tracking branch".
     +
          Signed-off-by: Harald Nordgren <[email protected]>
      
       ## Documentation/config/remote.adoc ##
     @@ remote.c: void collect_upstream_from_remote(struct repository *repo,
       __attribute__((format (printf,2,3)))
       static char *error_buf(struct strbuf *err, const char *fmt, ...)
       {
     +@@ remote.c: static char *tracking_for_push_dest(struct repository *repo,
     + 
     + 	remote = repo_remote_for_push_tracking(repo, remote);
     + 	ret = apply_refspecs(&remote->fetch, refname);
     ++	if (!ret)
     ++		ret = apply_refspecs(&remote->refmap, refname);
     + 	if (!ret)
     + 		return error_buf(err,
     + 				 _("push destination '%s' on remote '%s' has no local tracking branch"),
      
       ## remote.h ##
      @@ remote.h: void collect_upstream_from_remote(struct repository *repo,
     @@ t/t5586-fetch-refmap.sh (new)
      +	)
      +'
      +
     ++test_expect_success '@{push} resolves for a branch pushed to a refmap-only remote' '
     ++	(
     ++		cd client &&
     ++		git config push.default current &&
     ++		git checkout -b topic upstream/main &&
     ++		test_commit topic-1 &&
     ++		git push &&
     ++		echo refs/remotes/upstream/topic >expect &&
     ++		git rev-parse --symbolic-full-name topic@{push} >actual &&
     ++		test_cmp expect actual &&
     ++		test_cmp_rev topic topic@{push}
     ++	)
     ++'
     ++
      +test_done
 4:  3ee427de87 = 4:  6cff6786ba remote: add "git remote add --limited-fetch"
 5:  2cba4623ee = 5:  00f9d44a88 remote: default to --limited-fetch in a shallow repository
 6:  25eec8d967 < -:  ---------- remote: resolve @{push} through remote.<name>.fetchRefmap
Signature · 1 line
gitgitgadget
#146 lore raw

PATCH v14 1/5 fetch: add remote.<name>.fetchRefmap

From: Harald Nordgren <[email protected]>

Add a per-remote config variable, remote.<name>.fetchRefmap, that provides the default value for --refmap the same way remote.<name>.fetch already provides the default refspecs to fetch.

Unlike passing --refmap on the command line, which requires an explicit refspec to go with it, configuring remote.<name>.fetchRefmap on its own does not change what a refspec-less fetch does: it falls back to the same defaults as a remote with no fetchRefmap at all.

When remote.<name>.fetch is also configured, it still decides what to fetch, and the refmap maps what it fetches. This lets a remote name only a few branches and store them all through one refmap:

[remote "cabal"]
fetch = master
fetch = next
fetch = seen
fetchRefmap = +refs/heads/*:refs/remotes/cabal/*

Looking up the remote-tracking branch for a remote branch, as @{upstream} and "git branch --track" do, falls back to the refmap when remote.<name>.fetch has no match, so that these work for such branches too. A branch excluded by a negative refspec in remote.<name>.fetch stays excluded.

---
 Documentation/config/remote.adoc |   7 ++
 Documentation/fetch-options.adoc |   3 +
 builtin/fetch.c                  |  16 +++-
 remote.c                         |  20 +++-
 remote.h                         |   6 ++
 t/t5510-fetch.sh                 | 151 +++++++++++++++++++++++++++++++
 6 files changed, 200 insertions(+), 3 deletions(-)
Documentation/config/remote.adoc +7 -0
index 3a20d0f752..236ccca93e 100644
@@ -33,6 +33,13 @@ remote.<name>.fetch::
 	The default set of "refspec" for linkgit:git-fetch[1]. See
 	linkgit:git-fetch[1].
 
+remote.<name>.fetchRefmap::
+	The default value of the `--refmap` option, used to map refs being
+	fetched to remote-tracking refs to store, the same way
+	`remote.<name>.fetch` does for an explicit refspec.
+	`remote.<name>.fetch`, if also configured, still decides what is
+	fetched. See the `--refmap` entry in linkgit:git-fetch[1].
+
 remote.<name>.push::
 	The default set of "refspec" for linkgit:git-push[1]. See
 	linkgit:git-push[1].
Documentation/fetch-options.adoc +3 -0
index 47dea1de8e..c2101a7b39 100644
@@ -244,6 +244,9 @@ endif::git-pull[]
 	refspecs and rely entirely on the refspecs supplied as
 	command-line arguments. See section on "Configured Remote-tracking
 	Branches" for details.
++
+`remote.<name>.refmap` provides the default value for this option, the
+same way `remote.<name>.fetch` provides the default refspecs to fetch.
 
 `-t`::
 `--tags`::
builtin/fetch.c +14 -2
index f588dba2f0..e54bf52d86 100644
@@ -518,6 +518,12 @@ static struct ref *get_ref_map(struct remote *remote,
 	struct ref *rm;
 	struct ref *ref_map = NULL;
 	struct ref **tail = &ref_map;
+	/*
+	 * The --refmap command line option, if given, takes precedence
+	 * over remote.<name>.fetchRefmap.
+	 */
+	struct refspec *effective_refmap =
+		refmap.nr ? &refmap : remote ? &remote->refmap : NULL;
 
 	/* opportunistically-updated references: */
 	struct ref *orefs = NULL, **oref_tail = &orefs;
@@ -528,6 +534,8 @@ static struct ref *get_ref_map(struct remote *remote,
 	filter_prefetch_refspec(rs);
 	if (remote)
 		filter_prefetch_refspec(&remote->fetch);
+	if (effective_refmap)
+		filter_prefetch_refspec(effective_refmap);
 
 	if (rs->nr) {
 		struct refspec *fetch_refspec;
@@ -561,8 +569,8 @@ static struct ref *get_ref_map(struct remote *remote,
 		 * by ref_remove_duplicates() in favor of one of these
 		 * opportunistic entries with FETCH_HEAD_IGNORE.
 		 */
-		if (refmap.nr)
-			fetch_refspec = &refmap;
+		if (effective_refmap && effective_refmap->nr)
+			fetch_refspec = effective_refmap;
 		else
 			fetch_refspec = &remote->fetch;
 
@@ -597,6 +605,10 @@ static struct ref *get_ref_map(struct remote *remote,
 			if (has_merge &&
 			    !strcmp(branch->remote_name, remote->name))
 				add_merge_config(&ref_map, remote_refs, branch, &tail);
+
+			for (i = 0; i < remote->refmap.nr; i++)
+				get_fetch_map(ref_map, &remote->refmap.items[i],
+					      &oref_tail, 1);
 		} else if (!prefetch) {
 			ref_map = get_remote_ref(remote_refs, "HEAD");
 			if (!ref_map)
remote.c +19 -1
index 98665a3085..26f42fb6d1 100644
@@ -152,6 +152,7 @@ static struct remote *make_remote(struct remote_state *remote_state,
 	ret->name = xstrndup(name, len);
 	refspec_init_push(&ret->push, the_hash_algo);
 	refspec_init_fetch(&ret->fetch, the_hash_algo);
+	refspec_init_fetch(&ret->refmap, the_hash_algo);
 	string_list_init_dup(&ret->server_options);
 	string_list_init_dup(&ret->negotiation_restrict);
 	string_list_init_dup(&ret->negotiation_include);
@@ -176,6 +177,7 @@ static void remote_clear(struct remote *remote)
 
 	refspec_clear(&remote->push);
 	refspec_clear(&remote->fetch);
+	refspec_clear(&remote->refmap);
 
 	free((char *)remote->receivepack);
 	free((char *)remote->uploadpack);
@@ -539,6 +541,12 @@ static int handle_config(const char *key, const char *value,
 			return -1;
 		refspec_append(&remote->fetch, v);
 		free(v);
+	} else if (!strcmp(subkey, "fetchrefmap")) {
+		char *v;
+		if (git_config_string(&v, key, value))
+			return -1;
+		refspec_append(&remote->refmap, v);
+		free(v);
 	} else if (!strcmp(subkey, "receivepack")) {
 		char *v;
 		if (git_config_string(&v, key, value))
@@ -995,7 +1003,17 @@ void ref_push_report_free(struct ref_push_report *report)
 
 int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
 {
-	return refspec_find_match(&remote->fetch, refspec);
+	char **result = refspec->src ? &refspec->dst : &refspec->src;
+
+	if (remote->fetch.nr && !refspec_find_match(&remote->fetch, refspec))
+		return 0;
+	if (refspec_find_match(&remote->refmap, refspec))
+		return -1;
+	if (refname_matches_negative_refspec_item(refspec->src, &remote->fetch)) {
+		FREE_AND_NULL(*result);
+		return -1;
+	}
+	return 0;
 }
 
 static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
remote.h +6 -0
index cca02033b9..ac485a584d 100644
@@ -90,6 +90,12 @@ struct remote {
 
 	struct refspec fetch;
 
+	/*
+	 * How to map refs fetched without an explicit destination into our
+	 * own namespace, the same as the --refmap command line option.
+	 */
+	struct refspec refmap;
+
 	/*
 	 * The setting for whether to fetch tags (as a separate rule from the
 	 * configured refspecs);
t/t5510-fetch.sh +151 -0

157 lines not on this page · raw message

Signature · 1 line
gitgitgadget
#153 lore raw

PATCH v14 2/5 fetch: extract collect_upstream_from_remote() helper

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

When a fetch has no refspec to work from, we decide what to fetch from two sources: the remote's configured fetch refspec, and the current branch's upstream, if it lives on that remote. For example, after

git clone --single-branch <url>
git switch -c work
git push -u origin work

the remote's refspec only covers "main", but a refspec-less "git fetch origin" still fetches "work" into FETCH_HEAD, so that a plain "git pull" finds the branch to merge. See 49ca2fba39 (fetch: add branch.*.merge to default ref-prefix extension, 2022-09-08).

That "does this branch build on this remote" check was inlined for just the current branch. Move it into its own function, collect_upstream_from_remote(), so the same logic can be reused for branches other than just the current one.

---
 builtin/fetch.c | 18 ++++++++----------
 remote.c        | 15 +++++++++++++++
 remote.h        |  9 +++++++++
 3 files changed, 32 insertions(+), 10 deletions(-)
builtin/fetch.c +8 -10
index e54bf52d86..3614d576dc 100644
@@ -2002,8 +2002,6 @@ static int do_fetch(struct transport *transport,
 	if (rs->nr) {
 		refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);
 	} else {
-		struct branch *branch = branch_get(NULL);
-
 		if (transport->remote->fetch.nr) {
 			refspec_ref_prefixes(&transport->remote->fetch,
 					     &transport_ls_refs_options.ref_prefixes);
@@ -2018,14 +2016,14 @@ static int do_fetch(struct transport *transport,
 			else
 				follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;
 		}
-		if (branch && branch_has_merge_config(branch) &&
-		    !strcmp(branch->remote_name, transport->remote->name)) {
-			int i;
-			for (i = 0; i < branch->merge_nr; i++) {
-				strvec_push(&transport_ls_refs_options.ref_prefixes,
-					    branch->merge[i]->src);
-			}
-		}
+
+		/*
+		 * The configured refspec may not cover the current
+		 * branch's upstream (e.g. a narrowed -t refspec), so
+		 * make sure we can still fetch it regardless.
+		 */
+		collect_upstream_from_remote(the_repository, transport->remote,
+					      NULL, &transport_ls_refs_options.ref_prefixes);
 
 		/*
 		 * If there are no refs specified to fetch, then we just
remote.c +15 -0
index 26f42fb6d1..4e250ba393 100644
@@ -1899,6 +1899,21 @@ int branch_merge_matches(struct branch *branch,
 	return refname_match(branch->merge[i]->src, refname);
 }
 
+void collect_upstream_from_remote(struct repository *repo,
+				   const struct remote *remote,
+				   const char *refname,
+				   struct strvec *tracked)
+{
+	struct branch *branch = repo_branch_get(repo, refname);
+
+	if (!branch_has_merge_config(branch) ||
+	    strcmp(branch->remote_name, remote->name))
+		return;
+
+	for (int i = 0; i < branch->merge_nr; i++)
+		strvec_push(tracked, branch->merge[i]->src);
+}
+
 __attribute__((format (printf,2,3)))
 static char *error_buf(struct strbuf *err, const char *fmt, ...)
 {
remote.h +9 -0
index ac485a584d..003c8fd180 100644
@@ -359,6 +359,15 @@ int branch_has_merge_config(struct branch *branch);
 
 int branch_merge_matches(struct branch *, int n, const char *);
 
+/*
+ * If refname's branch builds on remote, add its upstream on remote to
+ * tracked. A NULL refname means the current branch.
+ */
+void collect_upstream_from_remote(struct repository *repo,
+				   const struct remote *remote,
+				   const char *refname,
+				   struct strvec *tracked);
+
 /* list of the remote in a group as configured */
 struct remote_group_data {
 	const char *name;
Signature · 1 line
gitgitgadget
#154 lore raw

PATCH v14 3/5 fetch: infer branches to fetch from a refmap-only remote

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

We want remote.<name>.fetchRefmap to work on its own, without also setting remote.<name>.fetch, so a remote can be limited to the branches already in use locally, without listing them by hand or touching the command line every time.

Infer what to fetch from the local branches whose @{upstream} is already on that remote. The refmap itself still only controls where those fetched refs land, not which branches are selected.

Let @{push} use the refmap too, so a branch pushed to such a remote resolves to the remote-tracking branch the push updated, instead of failing with "has no local tracking branch".

---
 Documentation/config/remote.adoc |   4 +-
 Documentation/fetch-options.adoc |  11 ++-
 builtin/fetch.c                  |  72 +++++++++++----
 remote.c                         |  27 ++++++
 remote.h                         |   5 ++
 t/meson.build                    |   1 +
 t/t5586-fetch-refmap.sh          | 147 +++++++++++++++++++++++++++++++
 7 files changed, 248 insertions(+), 19 deletions(-)
 create mode 100755 t/t5586-fetch-refmap.sh
Documentation/config/remote.adoc +3 -1
index 236ccca93e..0d24abfcbd 100644
@@ -38,7 +38,9 @@ remote.<name>.fetchRefmap::
 	fetched to remote-tracking refs to store, the same way
 	`remote.<name>.fetch` does for an explicit refspec.
 	`remote.<name>.fetch`, if also configured, still decides what is
-	fetched. See the `--refmap` entry in linkgit:git-fetch[1].
+	fetched. If it is not set either, a refspec-less fetch instead
+	infers what to fetch from local branches built on this remote.
+	See the `--refmap` entry in linkgit:git-fetch[1].
 
 remote.<name>.push::
 	The default set of "refspec" for linkgit:git-push[1]. See
Documentation/fetch-options.adoc +9 -2
index c2101a7b39..be27c03de7 100644
@@ -245,8 +245,15 @@ endif::git-pull[]
 	command-line arguments. See section on "Configured Remote-tracking
 	Branches" for details.
 +
-`remote.<name>.refmap` provides the default value for this option, the
-same way `remote.<name>.fetch` provides the default refspecs to fetch.
+When a refmap is active (from `--refmap` or `remote.<name>.fetchRefmap`) but
+nothing to fetch is specified on the command line, nor is there a
+`remote.<name>.fetch`, infer what to fetch from local branches built on
+this remote: remote-tracking branches from this remote that are also
+configured as the `@{upstream}` of a local branch are fetched. The
+refmap only decides where those fetched refs land, the same as it
+would for an explicit refspec, and does not narrow which branches are
+selected. It is what turns this on, since without it the fetched
+branches would have nowhere to land.
 
 `-t`::
 `--tags`::
builtin/fetch.c +56 -16

130 lines not on this page · raw message

remote.c +27 -0
index 4e250ba393..989986d2a2 100644
@@ -1914,6 +1914,31 @@ void collect_upstream_from_remote(struct repository *repo,
 		strvec_push(tracked, branch->merge[i]->src);
 }
 
+struct branches_tracking_remote_cb_data {
+	struct repository *repo;
+	const struct remote *remote;
+	struct strvec *tracked;
+};
+
+static int add_if_tracking_remote(const struct reference *ref, void *cb_data)
+{
+	struct branches_tracking_remote_cb_data *data = cb_data;
+
+	collect_upstream_from_remote(data->repo, data->remote, ref->name,
+				      data->tracked);
+	return 0;
+}
+
+void branches_tracking_remote(struct repository *repo,
+			       const struct remote *remote,
+			       struct strvec *tracked)
+{
+	struct branches_tracking_remote_cb_data data = { repo, remote, tracked };
+
+	refs_for_each_branch_ref(get_main_ref_store(repo),
+				  add_if_tracking_remote, &data);
+}
+
 __attribute__((format (printf,2,3)))
 static char *error_buf(struct strbuf *err, const char *fmt, ...)
 {
@@ -1993,6 +2018,8 @@ static char *tracking_for_push_dest(struct repository *repo,
 
 	remote = repo_remote_for_push_tracking(repo, remote);
 	ret = apply_refspecs(&remote->fetch, refname);
+	if (!ret)
+		ret = apply_refspecs(&remote->refmap, refname);
 	if (!ret)
 		return error_buf(err,
 				 _("push destination '%s' on remote '%s' has no local tracking branch"),
remote.h +5 -0
index 003c8fd180..6fd9b05a79 100644
@@ -368,6 +368,11 @@ void collect_upstream_from_remote(struct repository *repo,
 				   const char *refname,
 				   struct strvec *tracked);
 
+/* Same as collect_upstream_from_remote(), but checked for every local branch. */
+void branches_tracking_remote(struct repository *repo,
+			       const struct remote *remote,
+			       struct strvec *tracked);
+
 /* list of the remote in a group as configured */
 struct remote_group_data {
 	const char *name;
t/meson.build +1 -0
index 1cf1d95f70..92f877da1c 100644
@@ -737,6 +737,7 @@ integration_tests = [
   't5582-fetch-negative-refspec.sh',
   't5583-push-branches.sh',
   't5584-http-429-retry.sh',
+  't5586-fetch-refmap.sh',
   't5600-clone-fail-cleanup.sh',
   't5601-clone.sh',
   't5602-clone-remote-exec.sh',
t/t5586-fetch-refmap.sh new file +147 -0

147 lines not on this page · raw message

Signature · 1 line
gitgitgadget
#155 lore raw

PATCH v14 4/5 remote: add "git remote add --limited-fetch"

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

A remote added the ordinary way tracks every branch it has, via a wildcard remote.<name>.fetch refspec. That is wasteful for a remote whose history is only worth following for the branches actually in use locally, and it can make "git fetch" negotiate history for branches nobody asked for.

Give "git remote add" a --limited-fetch option that sets up remote.<name>.fetchRefmap instead of remote.<name>.fetch, so that "git fetch <name>" only fetches the branches already tracked, as described in the previous commit. It is rejected together with -t/--track or --mirror, since those already say explicitly what to fetch.

---
 Documentation/git-remote.adoc |  8 +++++++-
 builtin/remote.c              | 28 ++++++++++++++++++++++------
 t/t5505-remote.sh             | 16 ++++++++++++++++
 3 files changed, 45 insertions(+), 7 deletions(-)
Documentation/git-remote.adoc +7 -1
index 315100409d..1f490819ba 100644
@@ -10,7 +10,7 @@ SYNOPSIS
 --------
 [synopsis]
 git remote [-v | --verbose]
-git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>
+git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] [--[no-]limited-fetch] <name> <URL>
 git remote rename [--[no-]progress] <old> <new>
 git remote remove <name>
 git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
@@ -75,6 +75,12 @@ the `refs/remotes/<name>/` namespace, a refspec to track only _<branch>_
 is created.  You can give more than one `-t <branch>` to track
 multiple branches without grabbing all branches.
 +
+With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec
+that tracks all branches, `remote.<name>.fetchRefmap` is set up so that a
+refspec-less `git fetch <name>` only fetches branches our local branches
+are built on. See the `--refmap` entry in linkgit:git-fetch[1] for
+details.
++
 With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set
 up to point at remote's _<master>_ branch. See also the set-head command.
 +
builtin/remote.c +22 -6
index fb7e5b114f..5f8a8a2f71 100644
@@ -179,6 +179,7 @@ static int add(int argc, const char **argv, const char *prefix,
 {
 	int fetch = 0, fetch_tags = TAGS_DEFAULT;
 	unsigned mirror = MIRROR_NONE;
+	int limited_fetch = -1; /* unspecified */
 	struct string_list track = STRING_LIST_INIT_NODUP;
 	const char *master = NULL;
 	struct remote *remote;
@@ -198,6 +199,8 @@ static int add(int argc, const char **argv, const char *prefix,
 		OPT_CALLBACK_F(0, "mirror", &mirror, "(push|fetch)",
 			N_("set up remote as a mirror to push to or fetch from"),
 			PARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),
+		OPT_BOOL(0, "limited-fetch", &limited_fetch,
+			N_("fetch only the branches we build on, instead of every branch")),
 		OPT_END()
 	};
 
@@ -211,6 +214,10 @@ static int add(int argc, const char **argv, const char *prefix,
 		die(_("specifying a master branch makes no sense with --mirror"));
 	if (mirror && !(mirror & MIRROR_FETCH) && track.nr)
 		die(_("specifying branches to track makes sense only with fetch mirrors"));
+	if (limited_fetch == 1 && track.nr)
+		die(_("--limited-fetch does not make sense with -t/--track"));
+	if (limited_fetch == 1 && mirror)
+		die(_("--limited-fetch does not make sense with --mirror"));
 
 	name = argv[0];
 	url = argv[1];
@@ -230,13 +237,22 @@ static int add(int argc, const char **argv, const char *prefix,
 	repo_config_set(the_repository, buf.buf, url);
 
 	if (!mirror || mirror & MIRROR_FETCH) {
+		bool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&
+			limited_fetch == 1;
+
 		strbuf_reset(&buf);
-		strbuf_addf(&buf, "remote.%s.fetch", name);
-		if (track.nr == 0)
-			string_list_append(&track, "*");
-		for (size_t i = 0; i < track.nr; i++) {
-			add_branch(buf.buf, track.items[i].string,
-				   name, mirror, &buf2);
+		if (use_limited_fetch) {
+			strbuf_addf(&buf, "remote.%s.fetchRefmap", name);
+			strbuf_reset(&buf2);
+			strbuf_addf(&buf2, "+refs/heads/*:refs/remotes/%s/*", name);
+			repo_config_set(the_repository, buf.buf, buf2.buf);
+		} else {
+			strbuf_addf(&buf, "remote.%s.fetch", name);
+			if (track.nr == 0)
+				string_list_append(&track, "*");
+			for (size_t i = 0; i < track.nr; i++)
+				add_branch(buf.buf, track.items[i].string,
+					   name, mirror, &buf2);
 		}
 	}
 
t/t5505-remote.sh +16 -0
index 5fbfcb0848..90cba75f93 100755
@@ -137,6 +137,22 @@ test_expect_success 'filters are listed by git remote -v only' '
 	test_grep ! "\[blob:none\]" out
 '
 
+test_expect_success '--limited-fetch works in a full repository too' '
+	test_when_finished "rm -rf full-add" &&
+	git clone --no-local one full-add &&
+	(
+		cd full-add &&
+		git remote add --limited-fetch upstream ../two &&
+		test_cmp_config "+refs/heads/*:refs/remotes/upstream/*" \
+			remote.upstream.fetchRefmap &&
+		test_must_fail git config get remote.upstream.fetch
+	)
+'
+
+test_expect_success '--limited-fetch conflicts with -t' '
+	test_must_fail git remote add --limited-fetch -t main upstream ../two
+'
+
 test_expect_success 'check remote-tracking' '
 	(
 		cd test &&
Signature · 1 line
gitgitgadget
#156 lore raw

PATCH v14 5/5 remote: default to --limited-fetch in a shallow repository

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

Adding a second remote to a shallow clone used to still fetch every branch that remote has, since "git remote add" always set up a wildcard remote.<name>.fetch refspec regardless of how shallow the repository already was. That defeats the purpose of having cloned shallow in the first place (for example with --single-branch), and can make a plain "git fetch" on that remote hang or take a very long time on a repository with many branches.

Turn --limited-fetch on by default when the repository is already shallow and neither -t/--track nor --mirror was given, so that adding a remote there does not by itself commit to following every branch it has. --no-limited-fetch keeps the previous behavior for whoever wants it.

---
 Documentation/git-remote.adoc |  8 ++++-
 builtin/remote.c              |  5 ++-
 t/t5505-remote.sh             | 60 +++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+), 2 deletions(-)
Documentation/git-remote.adoc +7 -1
index 1f490819ba..245c0c4558 100644
@@ -57,6 +57,11 @@ Add a remote named _<name>_ for the repository at
 _<URL>_.  The command `git fetch <name>` can then be used to create and
 update remote-tracking branches `<name>/<branch>`.
 +
+If the repository is already a shallow repository (see linkgit:git-clone[1]
+`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,
+`--limited-fetch` is turned on by default, so that `git fetch <name>` does
+not need to negotiate history for every branch the remote has.
++
 With `-f` option, `git fetch <name>` is run immediately after
 the remote information is set up.
 +
@@ -79,7 +84,8 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec
 that tracks all branches, `remote.<name>.fetchRefmap` is set up so that a
 refspec-less `git fetch <name>` only fetches branches our local branches
 are built on. See the `--refmap` entry in linkgit:git-fetch[1] for
-details.
+details. `--no-limited-fetch` explicitly disables this, overriding the
+shallow-repository default described above.
 +
 With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set
 up to point at remote's _<master>_ branch. See also the set-head command.
builtin/remote.c +4 -1
index 5f8a8a2f71..5d2388a26c 100644
@@ -16,6 +16,7 @@
 #include "rebase.h"
 #include "refs.h"
 #include "refspec.h"
+#include "shallow.h"
 #include "odb.h"
 #include "strvec.h"
 #include "commit-reach.h"
@@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,
 
 	if (!mirror || mirror & MIRROR_FETCH) {
 		bool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&
-			limited_fetch == 1;
+			(limited_fetch == 1 ||
+			 (limited_fetch == -1 &&
+			  is_repository_shallow(the_repository)));
 
 		strbuf_reset(&buf);
 		if (use_limited_fetch) {
t/t5505-remote.sh +60 -0

66 lines not on this page · raw message

Signature · 1 line
gitgitgadget
#144 lore raw

Re: [PATCH v13 0/6] fetch: avoid fetching every branch of a new remote in a shallow repo

In reply to Matt Hunter

Hi Matt

Quoted 16 lines · Matt Hunter
Quoted 9 lines · Phillip Wood
Quoted 3 lines · Junio C Hamano

In any case, this topic one is keeping a topic by another author waiting to settle, so we have stronger incentive to see it move past the finish line.

What topic is that? - I can't see anything marked as depending on this in the latest "what's cooking" email. Would merging the first part of this series unblock the other one?

I believe that is ch/remote-defer-follow-remote-head-validation, which doesn't strictly depend on the changes from this topic, but the two have textual conflicts that the other author is deferring to resolve when this one lands.

Quoted 5 lines

<CAHeTm9M5maWM=yrJPNzCibv_rewH3-yBK8J5-MDyDvPzfihe4A@mail.gmail.com>

It looks like if this topic were merged up to patch 3/6 (fetch: infer branches to fetch from a refmap-only remote), the situation could resolve.

Thanks for the link. For this topic we're still waiting for a response to this part of [1] from three weeks ago

Quoted 7 lines

For now, as this "what is tracked by the local branches" is the only kind of smart that was designed to decide what to fetch, it may be OK to use "you gave me nothing to specify what to fetch, so I'll guess using the smart". But when we introduce more kinds of smart later, this becomes a problem. So I do agree with you that we should have something to specifically say "when I do not explicitly give you what to fetch, decide based on what local branches track".

so perhaps we should give that other topic priority while we wait.

Thanks

Phillip

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

#147 lore raw

Re: [PATCH v14 1/5] fetch: add remote.<name>.fetchRefmap

In reply to Harald Nordgren via GitGitGadget

Hi Harald

Quoted 11 lines · Harald Nordgren via GitGitGadget
t/t5510-fetch.sh +10 -0
+test_expect_success 'remote.<name>.fetchRefmap stores an explicit-dst remote.<name>.fetch entry in both places' '
+	test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
+	git init -b master fetch-refmap-upstream &&
+	test_commit -C fetch-refmap-upstream base &&
+	git init downstream &&
+	(
+		cd downstream &&
+		git remote add cabal ../fetch-refmap-upstream &&
+		git config --unset-all remote.cabal.fetch &&
+		git config --add remote.cabal.fetch \

t/t5510-fetch.sh test_expect_success 'explicit --refmap option overrides remote.*.fetch' ' · line 1041

Quoted 10 lines
t/t5510-fetch.sh +10 -0
+			"+refs/heads/master:refs/remotes/custom/master" &&
+		git config remote.cabal.fetchRefmap "+refs/heads/*:refs/remotes/cabal/*" &&
+		git fetch cabal &&
+		check_fetched_refs <<-\EOF
+		refs/remotes/cabal/HEAD
+		refs/remotes/cabal/master
+		refs/remotes/custom/master
+		EOF
+	)
+'

This behavior arguably isn't too bad when "--refmap" is given on the command line because the user can simply adjust the refmap argument or the commandline refspec if they don't want to ref to be stored in two places. The same is true for when the refmap comes from the ".fetchRefmap" config and the refspec comes form the ".fetch" config. Both those cases could probably be filed under "if it hurts don't do it". However, I think it becomes confusing if we store the ref in two places when the user configures ".fetchRefmap" and then passes "src:dst" on the commandline because users are used to a commandline "src:dst" overriding the ".fetch" config, not adding to it. It is going to be very awkward for users if they cannot override ".fetchRefmap" by passing "src:dst" on the commandline. I think we should change the behavior so that "--refmap" and the ".fetchRefmap" config are ignored when the user includes an explicit destination on the commandline or the in ".fetch" config.

Thanks

Phillip

#148 lore raw

Re: [PATCH v14 1/5] fetch: add remote.<name>.fetchRefmap

Quoted 4 lines · Phillip Wood

However, I think it becomes confusing if we store the ref in two places when the user configures ".fetchRefmap" and then passes "src:dst" on the commandline because users are used to a commandline "src:dst" overriding the ".fetch" config, not adding to it.

True, but the src:dst on the command line in your example is still overriding the .fetch configuration. Wouldn't it make it confusing if it overrode .fetchRefmap configuration, since src:dst on the command line is what .fetch configuration gives the defaults for (and .fetchRefmap configuration gives the defaults for --refmap, not the refspec like src:dst). It may be just as confusing as if, say, a command line --refmap suddenly started overriding .fetch configuration, I am afraid.

I can buy it if the suggestion was to allow passing --no-refmap from the command line (or would --refmap="" work?) to ignore .fetchRefmap configuration, though.

Thanks.

#149 lore raw

Re: [PATCH v14 1/5] fetch: add remote.<name>.fetchRefmap

Quoted 7 lines · Junio C Hamano
Quoted 4 lines · Phillip Wood

However, I think it becomes confusing if we store the ref in two places when the user configures ".fetchRefmap" and then passes "src:dst" on the commandline because users are used to a commandline "src:dst" overriding the ".fetch" config, not adding to it.

Quoted 8 lines

True, but the src:dst on the command line in your example is still overriding the .fetch configuration. Wouldn't it make it confusing if it overrode .fetchRefmap configuration, since src:dst on the command line is what .fetch configuration gives the defaults for (and .fetchRefmap configuration gives the defaults for --refmap, not the refspec like src:dst). It may be just as confusing as if, say, a command line --refmap suddenly started overriding .fetch configuration, I am afraid.

I think it depends on whether users already know how --refmap works. My contention is that they don't and are used to src:dst on the commandline overriding their config and will therefore be confused when it does not override .fetchRefmap. If one took the view that users are already familiar with what happens when they pass --refmap and src:dst then I agree changing the behavior would be confusing, but I don't think most users even know --refmap exists let alone what it does in this particular corner case. I had a look to see if the current behavior of --refmap combined with src:dst is documented, all I could see was

When git fetch is run with explicit branches and/or tags to fetch on the command line, e.g. git fetch origin master, the <refspec>s given on the command line determine what are to be fetched (e.g. master in the example, which is a short-hand for master:, which in turn means "fetch the master branch but I do not explicitly say what remote-tracking branch to update with it from the command line"), and the example command will fetch only the master branch. The remote.<repository>.fetch values determine which remote-tracking branch, if any, is updated. When used in this way, the remote.<repository>.fetch values do not have any effect in deciding what gets fetched (i.e. the values are not used as refspecs when the command-line lists refspecs); they are only used to decide where the refs that are fetched are stored by acting as a mapping.

The latter use of the remote.<repository>.fetch values can be overridden by giving the --refmap=<refspec> parameter(s) on the command line.

Which reads to me that --refmap overrides remote.<repository>.fetch when remote.<repository>.fetch is used as a mapping. As remote.<repository>.fetch is not used as a mapping when there is a explicit destination on the commandline, it is not clear to me that the current behavior of --refmap combined with src:dst on the commandline matches the documentation.

Quoted 3 lines

I can buy it if the suggestion was to allow passing --no-refmap from the command line (or would --refmap="" work?) to ignore .fetchRefmap configuration, though.

I think --refmap="" might work as --refmap overrides the config and --refmap="" is the documented way to ignore the ".fetch" config.

Thanks

Phillip

#150 lore raw

Re: [PATCH v14 1/5] fetch: add remote.<name>.fetchRefmap

Quoted 6 lines · Phillip Wood
Quoted 3 lines

I can buy it if the suggestion was to allow passing --no-refmap from the command line (or would --refmap="" work?) to ignore .fetchRefmap configuration, though.

I think --refmap="" might work as --refmap overrides the config and --refmap="" is the documented way to ignore the ".fetch" config.

Do you mean ... ignore the .fetchRefmap config (not ".fetch", which is used as the default for refspecs and not value to a dashed option)?

All that said, it feels much more flexible if a refspec with a destination (whether on the command line or in the configuration) simply ignores the refmap. In other words, the refmap would only apply to refspec entries that lack a destination, leaving elements with their own destination untouched.

This way, we won't have to worry about what --refmap="" means.

So I'm completely fine with going in that direction. The only thing I care about is keeping a clear one-to-one relationship between the configuration and the command-line option: the configuration sets the default, and the command-line option overrides it.

Thanks.

#151 lore raw

Re: [PATCH v14 1/5] fetch: add remote.<name>.fetchRefmap

Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Phillip Wood
Quoted 3 lines

I can buy it if the suggestion was to allow passing --no-refmap from the command line (or would --refmap="" work?) to ignore .fetchRefmap configuration, though.

Quoted 6 lines
Quoted 2 lines

I think --refmap="" might work as --refmap overrides the config and --refmap="" is the documented way to ignore the ".fetch" config.

Do you mean ... ignore the .fetchRefmap config (not ".fetch", which is used as the default for refspecs and not value to a dashed option)?

Yes I meant --refmap overrides .fetchRefmap

Quoted 5 lines

All that said, it feels much more flexible if a refspec with a destination (whether on the command line or in the configuration) simply ignores the refmap. In other words, the refmap would only apply to refspec entries that lack a destination, leaving elements with their own destination untouched.

I agree

Quoted 6 lines

This way, we won't have to worry about what --refmap="" means.

So I'm completely fine with going in that direction. The only thing I care about is keeping a clear one-to-one relationship between the configuration and the command-line option: the configuration sets the default, and the command-line option overrides it.

Great. So I think we'd want a preliminary patch to ignore --refmap when there is a destination on the commandline and this patch to be tweaked so that

git config remote.cabal.fetch master
git fetch --refmap=refs/heads/*:refs/test/* cabal

and

git config remote.cabal.fetch master
git config remote.cabal.fetchRefmap refs/heads/*:refs/test/* git fetch

both fetch "master" and store it under "refs/test/master"

Thanks

Phillip

#152 lore raw

Re: [PATCH v14 1/5] fetch: add remote.<name>.fetchRefmap

Quoted 7 lines · Phillip Wood

Great. So I think we'd want a preliminary patch to ignore --refmap when there is a destination on the commandline and this patch to be tweaked so that

git config remote.cabal.fetch master
git fetch --refmap=refs/heads/*:refs/test/* cabal

Quoted 7 lines

and

git config remote.cabal.fetch master
git config remote.cabal.fetchRefmap refs/heads/*:refs/test/* git fetch

both fetch "master" and store it under "refs/test/master"

I thought "master" as a fetch refspec element is a short-hand of "master:" that tells the machinery not to store the fetched result anywhere (other than FETCH_HEAD that is). So the first one of the above should be how the current code (without any of Harald's code for configuration) behave, and the second one should be how the code we have been reviewing should already behave, no?

With the first line in each sample replaced with (with or without the leading '+')

git config remote.cabal.fetch +refs/heads/master:refs/heads/main

the first one should store what it grabbed in two places with the current code, and the proposed update will stop touching refs/heads/main because refmap will take precedence.

Do I understand what we agreed correctly?

Thanks.

#94 lore raw

Re: Notes, refs, etc. (was: Re: [PATCH v10 1/5] fetch: add remote.<name>.refmap)

In reply to D. Ben Knoble
Quoted 33 lines · D. Ben Knoble

[snip]

Quoted 5 lines
Quoted 2 lines · Ben Knoble

Yes, I would love to add pushRefmap atop later! It would ease my working with notes, I think.

Quoted 8 lines
Quoted 2 lines

Yes, I would love to add pushRefmap atop later! It would ease my working with notes, I think.

Tell us more when you are off mobile ;-).

I do not think I planned to add '--refmap' support on the push side, even from the command line, but I can imagine there would be reasonable workflows that benefit from having one.

In my local copy of git.git, I want to have both Junio's amlog for spelunking and my own notes for patches. I only want to push out my notes + topics to my remote, though (I work on Git from different machines and it's nice to have the notes sync'd on both). Here's the hack I ended up with, and it has some downsides.

In particular, "git push" in this configuration does too much, so I usually have to think about whether that's ok. A pushRefmap would let me separate what to push by default (push.default!) from how to map pushing when I want to push, say, notes or other things. I have to be especially careful with a "git pf" (push, force-with-lease, force-if-includes), since I usually do *not* want to forcibly overwrite the remote notes---instead I should pull those down and merge them†.

So, here's the relevant config (I have a remote "benknoble" and a remote "origin", among others):

Quoted 7 lines

[core]
notesRef = refs/notes/benknoble/commits
[notes]
rewriteRef = refs/notes/benknoble/commits displayRef = refs/notes/origin/amlog
[remote "origin"]
fetch = +refs/notes/amlog:refs/notes/origin/amlog

I don’t use multiple computers right now. But I do have both private and public Git notes. In the work repository I have this

[remote "o"]
[...]
# NOTE “tracking” idea came from the Git mailing list
# NOTE We cannot use a negative refspec to exclude branch things here
# (what I wanted) because neg. refspecs cannot have
# the destination part
fetch = +refs/*:refs/tracking/o/refs/*

This is from some email thread(s) about using a namespace for tracking all remote refs instead of just branches. Now I have the state of the remote notes in case there is a divergence.

(Realistically this would only check my own mistakes since no one else seems to use these Notes.)

There’s even a comment there about negative refspecs. I think something like that got mentioned here in this thread or some months ago? I didn’t have the opportunity to engage then.

Quoted 17 lines

The "usual" stuff to make my notes important, but still show amlog notes when browsing history. (The origin remote section is in addition to the normal remote section with it's url and heads->origin/ mapping.)

[remote]
pushDefault = benknoble

Push to my remote.

[remote "benknoble"]
fetch = refs/notes/*:refs/notes/benknoble/* push = refs/heads/*:refs/heads/* push = refs/notes/benknoble/*:refs/notes/* push = ^refs/notes/origin/amlog

Quoted 7 lines

In addition to the usual config added by "git remote add", fetch remote notes into a local hierarchy, and push them out, but ignore amlog, and don't forget to also be able to push heads. Without the heads map, "git push" will not push branches!

Now, maybe I'm holding something in a way it wasn't intended and that's made my life harder, I dunno.

I haven’t tried the above but it doesn’t look like you have made it harder for yourself than you need to, considering your needs. ;)

Quoted 8 lines

†: There isn't a great story for that, either :/ When "git fetch" fails to fetch notes because of divergence with local, I end up saying

git fetch benknoble refs/notes/commits
git range-diff refs/notes/benknoble/commits...FETCH_HEAD git notes merge FETCH_HEAD

Quoted 6 lines

or something.

It's *almost* like we need remote-tracking branches for notes, ahaha… at that point, Scott's git-meta work becomes possibly more appealing to me, even if a part of me wants to "fix" notes so everyone can use them easily.

I don’t know what git-meta is. But in any case, I haven’t seen a forcing-use case for making Notes simpler to use. Right now they are okay-enough for the simple needs that I have seen, which is single-author (program or person) or automatic-merge (git-appraise(1) uses json-lines so that cat-sort-uniq merge strategy works).

But imagine some kind of collaborative Notes usage, like multiple people posting commentaries on commits. In that case one would need better tooling, for sure. But collaborative metadata on commits is a bit like the ideas like “history of history”.[1] It’s probably not going to happen.

[1] https://lwn.net/Articles/914041/

#157 lore raw

v15 cover fetch: avoid fetching every branch of a new remote in a shallow repo

In reply to Harald Nordgren via GitGitGadget

In a shallow single-branch clone of a large repository, git remote add upstream <url> sets up the default +refs/heads/*:refs/remotes/upstream/* refspec. The next git fetch upstream then downloads every branch of that remote, which defeats the point of a shallow clone.

This series lets a remote fetch only the branches that local branches track:

git remote add --limited-fetch upstream <url>
git fetch upstream main
git branch --set-upstream-to=upstream/main
git fetch upstream # fetches only main from now on

What to fetch and where to store it are separate settings. remote.<name>.fetchUpstreams says what to fetch, and remote.<name>.fetchRefmap says where it is stored. --limited-fetch sets both:

[remote "upstream"]
url = <url>
fetchRefmap = +refs/heads/*:refs/remotes/upstream/* fetchUpstreams = true

remote.<name>.fetchRefmap is the configured default for --refmap, the same way remote.<name>.fetch is the default for the refspecs on the command line. The command line overrides the configuration, and at the same level a refspec's own destination wins over the refmap:

* src:dst on the command line is stored at dst only, with --refmap or with fetchRefmap.
* A destination in remote.<name>.fetch is stored only there when the refmap comes from fetchRefmap.
* --refmap on the command line replaces the destinations in remote.<name>.fetch. It now also works without refspecs on the command line, where it used to die.

A refspec without a destination is stored through the refmap. So this stores three branches under refs/remotes/cabal/:

[remote "cabal"]
url = <url>
fetch = master
fetch = next
fetch = seen
fetchRefmap = +refs/heads/*:refs/remotes/cabal/*

In a shallow repository, git remote add turns --limited-fetch on by default.

Design notes:

* A *@{upstream} entry in remote.<name>.fetch was considered for saying what to fetch. Released versions of Git die with "invalid refspec" on it, so a separate setting is used.
* The opportunistic update through remote.<name>.fetch is unchanged. On an ordinary remote git fetch origin side:refs/heads/tmp still updates origin/side. On a remote that only has a fetchRefmap it stores only refs/heads/tmp.

Changes in v15:

* Added remote.<name>.fetchUpstreams to say explicitly that the upstreams of local branches are fetched, in addition to remote.<name>.fetch. A fetchRefmap alone no longer changes what is fetched.
* A refspec with its own destination on the command line is no longer mapped through --refmap or fetchRefmap. This is a new first patch. A destination in remote.<name>.fetch is no longer mapped through fetchRefmap either.
* --refmap now works without refspecs on the command line. This is a new second patch. It stores what remote.<name>.fetch fetches through the given refmap, and it overrides fetchRefmap.
* Fixed git pull merging every tracked branch into the current one.
* git remote add --limited-fetch now sets both fetchRefmap and fetchUpstreams.
* Fixed a leftover remote.<name>.refmap in the --refmap docs.
* Tests share helpers for repeated setup and checks, drop two steps that other tests cover, and the "does not fetch it twice" test now checks FETCH_HEAD.

Changes in v14:

* Squashed the @{push} fix into "fetch: infer branches to fetch from a refmap-only remote", so the series no longer adds the bug in one patch and fixes it in a later one
* Moved its test to t5586 using the refmap-only remote, so it no longer depends on --limited-fetch
* Dropped the separate "remote: resolve @{push} through remote..fetchRefmap" commit

Changes in v13:

* Added a new commit so @{push} resolves correctly for a --limited-fetch remote, falling back to remote.<name>.fetchRefmap the same way fetch already does when remote.<name>.fetch doesn't cover the branch being pushed.
* Added a regression test to fetch: add remote.<name>.fetchRefmap covering a remote.<name>.fetch entry with an explicit destination, confirming it's stored in both that destination and the refmap's.

Changes in v12:

* Renamed remote.<name>.refmap to remote.<name>.fetchRefmap throughout code, docs, and tests, so it reads unambiguously as fetch-only.
* Added the missing code path in get_ref_map() so a branch fetched by name via remote.<name>.fetch is also stored through remote.<name>.fetchRefmap, with a new test (the "cabal" example: fetch = master/next/seen plus a refmap storing them all under one namespace).
* Reworded the fetchRefmap commit message and doc entry to drop a redundant sentence, fix a confusing paragraph, and spell out that remote.<name>.fetch, when configured, still decides what's fetched while the refmap only remaps it.
* Added a prefetch coverage test to t5586-fetch-refmap.sh.

Changes in v11:

* remote_find_tracking() now falls back to remote.<name>.refmap when remote.<name>.fetch doesn't match. Negative refspecs in fetch still exclude refs, and both cases have tests
* The helpers write straight into a strvec, take the output parameter last and the remote as const
* The followRemoteHEAD lookup is done once, not duplicated in two branches
* Clearer commit messages and docs: a working example plus a reference to 49ca2fba39, the goal stated first, an incorrect "used to die" claim dropped, and an explanation of why a refmap turns on the inference
* use_limited_fetch is a bool

Changes in v10:

* Resolve merge conflict after master integrated
ch/fetch-followremotehead-lazy-validation.

Changes in v9:

* Moved the do_fetch() if/else-if/else split out of the preliminary helper-extraction commit and into the refmap-inference commit that needs it. Preliminary commit is now a simpler refactor.

Changes in v8:

* Split into a preliminary collect_upstream_from_remote() helper commit plus a simplified refmap-inference commit.
* Fixed a regression that could drop the current branch's @{upstream} when remote.<name>.fetch was narrowed.
* Threaded struct repository * through instead of hardcoding the_repository.
* Minor naming and comment cleanups.

Changes in v7:

* Fixed fatal: --refmap option is only meaningful with command-line refspec(s) firing just from configuring remote.<name>.refmap alone, with nothing else to fetch.
* When both remote.<name>.fetch and remote.<name>.refmap are configured, a refspec-less fetch follows .fetch as usual, .refmap only remaps something already being fetched by name. Added tests.

Changes in v6:

* Remove leftover reference to deleted default-branch logic in commit message.

Changes in v5:

* Renumber test file from t5585 to t5586.

Changes in v4:

* Removed the automatic default-branch fetch. A fresh remote fetches nothing until you track a branch explicitly.
* Fixed fetch report showing "new ref HEAD" instead of "new branch "
* Reworded remote..refmap docs and commit message.

Changes in v3:

* Replace the special ":"/"+:" fetch refspec with remote.<name>.refmap, reusing git's existing --refmap mechanism instead of inventing new refspec syntax.
* Split the change into 4 commits.

Changes in v2:

* Replaced the opt-in fetch.shallow config entirely with a new special fetch refspec (+:) that git remote add now defaults new remotes to in a shallow repository. The new refspec fetches whichever branches any local branch tracks at that remote, plus the remote's default branch.

Harald Nordgren (7):
fetch: do not apply --refmap to a refspec with a destination
fetch: let --refmap apply to configured refspecs
fetch: add remote.<name>.fetchRefmap
fetch: extract collect_upstream_from_remote() helper
fetch: add remote.<name>.fetchUpstreams
remote: add "git remote add --limited-fetch"
remote: default to --limited-fetch in a shallow repository

 Documentation/config/remote.adoc |  17 +++
 Documentation/fetch-options.adoc |  11 +-
 Documentation/git-remote.adoc    |  15 ++-
 builtin/fetch.c                  | 109 ++++++++++++++----
 builtin/remote.c                 |  34 +++++-
 remote.c                         |  64 ++++++++++-
 remote.h                         |  26 +++++
 t/meson.build                    |   1 +
 t/t5505-remote.sh                |  77 +++++++++++++
 t/t5510-fetch.sh                 | 175 ++++++++++++++++++++++++++++-
 t/t5586-fetch-refmap.sh          | 182 +++++++++++++++++++++++++++++++
 11 files changed, 678 insertions(+), 33 deletions(-)
 create mode 100755 t/t5586-fetch-refmap.sh

base-commit: f7de81456940dd6f3274e461149bba12965de609
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v15
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v15
Pull-Request: https://github.com/git/git/pull/2412

Range-diff vs v14:

 -:  ---------- > 1:  02bffb6b35 fetch: do not apply --refmap to a refspec with a destination
 -:  ---------- > 2:  f848c9b0a8 fetch: let --refmap apply to configured refspecs
 1:  c63699b402 ! 3:  1169013f2e fetch: add remote.<name>.fetchRefmap
     @@ Commit message
          Add a per-remote config variable, remote.<name>.fetchRefmap, that
          provides the default value for --refmap the same way
          remote.<name>.fetch already provides the default refspecs to fetch.
     +    --refmap on the command line overrides it.
      
     -    Unlike passing --refmap on the command line, which requires an
     -    explicit refspec to go with it, configuring remote.<name>.fetchRefmap
     -    on its own does not change what a refspec-less fetch does: it falls
     -    back to the same defaults as a remote with no fetchRefmap at all.
     +    The refmap only says where fetched refs are stored. Configuring it
     +    does not change what a fetch without refspecs fetches.
      
          When remote.<name>.fetch is also configured, it still decides what to
     -    fetch, and the refmap maps what it fetches. This lets a remote name
     -    only a few branches and store them all through one refmap:
     +    fetch. Its entries without a destination are stored through the
     +    refmap, and an entry with its own destination is stored only there.
     +    This lets a remote name only a few branches and store them all
     +    through one refmap:
      
              [remote "cabal"]
                  fetch = master
     @@ Documentation/config/remote.adoc: remote.<name>.fetch::
      +	fetched to remote-tracking refs to store, the same way
      +	`remote.<name>.fetch` does for an explicit refspec.
      +	`remote.<name>.fetch`, if also configured, still decides what is
     -+	fetched. See the `--refmap` entry in linkgit:git-fetch[1].
     ++	fetched, and an entry there with its own destination is stored
     ++	only at that destination. See the `--refmap` entry in
     ++	linkgit:git-fetch[1].
      +
       remote.<name>.push::
       	The default set of "refspec" for linkgit:git-push[1]. See
     @@ Documentation/fetch-options.adoc: endif::git-pull[]
       	command-line arguments. See section on "Configured Remote-tracking
       	Branches" for details.
      ++
     -+`remote.<name>.refmap` provides the default value for this option, the
     ++`remote.<name>.fetchRefmap` provides the default value for this option, the
      +same way `remote.<name>.fetch` provides the default refspecs to fetch.
       
       `-t`::
     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
      +		filter_prefetch_refspec(effective_refmap);
       
       	if (rs->nr) {
     - 		struct refspec *fetch_refspec;
     + 		for (i = 0; i < rs->nr; i++) {
      @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
     - 		 * by ref_remove_duplicates() in favor of one of these
       		 * opportunistic entries with FETCH_HEAD_IGNORE.
     + 		 *
     + 		 * A refspec that names its own destination is not
     +-		 * mapped through --refmap.
     ++		 * mapped through the refmap.
       		 */
      -		if (refmap.nr)
     --			fetch_refspec = &refmap;
     +-			map_refs_without_dst(ref_map, &refmap, &oref_tail);
      +		if (effective_refmap && effective_refmap->nr)
     -+			fetch_refspec = effective_refmap;
     ++			map_refs_without_dst(ref_map, effective_refmap,
     ++					     &oref_tail);
       		else
     - 			fetch_refspec = &remote->fetch;
     - 
     + 			for (i = 0; i < remote->fetch.nr; i++)
     + 				get_fetch_map(ref_map, &remote->fetch.items[i],
      @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
     - 			if (has_merge &&
     - 			    !strcmp(branch->remote_name, remote->name))
     - 				add_merge_config(&ref_map, remote_refs, branch, &tail);
     -+
     -+			for (i = 0; i < remote->refmap.nr; i++)
     -+				get_fetch_map(ref_map, &remote->refmap.items[i],
     -+					      &oref_tail, 1);
     - 		} else if (!prefetch) {
     - 			ref_map = get_remote_ref(remote_refs, "HEAD");
     - 			if (!ref_map)
     + 			tail = &ref_map->next;
     + 		}
     + 
     +-		if (refmap.nr) {
     ++		if (refmap.nr)
     + 			drop_destinations(ref_map);
     +-			map_refs_without_dst(ref_map, &refmap, &oref_tail);
     +-		}
     ++		if (effective_refmap)
     ++			map_refs_without_dst(ref_map, effective_refmap,
     ++					     &oref_tail);
     + 	}
     + 
     + 	if (tags == TAGS_SET) {
      
       ## remote.c ##
      @@ remote.c: static struct remote *make_remote(struct remote_state *remote_state,
     @@ remote.h: struct remote {
       	 * configured refspecs);
      
       ## t/t5510-fetch.sh ##
     -@@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote.*.fetch' '
     +@@ t/t5510-fetch.sh: test_expect_success '--refmap is not applied to a refspec with a destination' '
       	)
       '
       
     @@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote
      +	test_cmp expect actual
      +}
      +
     -+test_expect_success 'remote.<name>.fetchRefmap without tracking (baseline)' '
     -+	test_when_finished "rm -fr fetch-refmap-baseline fetch-refmap-upstream" &&
     -+	git init -b main fetch-refmap-upstream &&
     ++setup_refmap_repos () {
     ++	test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
     ++	git init -b "$1" fetch-refmap-upstream &&
      +	test_commit -C fetch-refmap-upstream base &&
     -+	git -C fetch-refmap-upstream branch other &&
     -+	git init fetch-refmap-baseline &&
     ++	shift &&
     ++	for branch
     ++	do
     ++		git -C fetch-refmap-upstream branch "$branch" || return 1
     ++	done &&
     ++	git init downstream &&
      +	(
     -+		cd fetch-refmap-baseline &&
     -+		git remote add origin ../fetch-refmap-upstream &&
     ++		cd downstream &&
     ++		git remote add cabal ../fetch-refmap-upstream &&
     ++		git config --unset-all remote.cabal.fetch &&
     ++		git config remote.cabal.fetchRefmap \
     ++			"+refs/heads/*:refs/remotes/cabal/*"
     ++	)
     ++}
      +
     -+		# Without fetch refspec, but with fetch refmap.
     -+		git config --unset-all remote.origin.fetch &&
     -+		git config remote.origin.fetchRefmap "+refs/heads/*:refs/remotes/origin/*" &&
     ++test_expect_success 'remote.<name>.fetchRefmap without tracking (baseline)' '
     ++	setup_refmap_repos main other &&
     ++	(
     ++		cd downstream &&
      +
      +		# Nothing tracked, nothing fetched, no error.
     -+		git fetch origin &&
     -+		check_fetched_refs <<-\EOF &&
     -+		EOF
     -+
     -+		# Nothing tracked, explicit ref on the command line.
     -+		git fetch origin main &&
     ++		git fetch cabal &&
      +		check_fetched_refs <<-\EOF &&
     -+		refs/remotes/origin/main
      +		EOF
      +
     -+		# With both refmap and fetch configured, remote.<name>.fetch
     -+		# wins: a refspec-less fetch follows it as usual, and
     -+		# remote.<name>.fetchRefmap plays no part in deciding what to
     -+		# fetch, only in mapping what is fetched.
     -+		git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
     -+		git fetch origin &&
     ++		# An ordinary remote.<name>.fetch still decides what is
     ++		# fetched and where it is stored.
     ++		git config remote.cabal.fetch "+refs/heads/*:refs/remotes/cabal/*" &&
     ++		git fetch cabal &&
      +		check_fetched_refs <<-\EOF
     -+		refs/remotes/origin/HEAD
     -+		refs/remotes/origin/main
     -+		refs/remotes/origin/other
     ++		refs/remotes/cabal/HEAD
     ++		refs/remotes/cabal/main
     ++		refs/remotes/cabal/other
      +		EOF
      +	)
      +'
      +
      +test_expect_success 'remote.<name>.fetchRefmap stores branches named in remote.<name>.fetch' '
     -+	test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
     -+	git init -b master fetch-refmap-upstream &&
     -+	test_commit -C fetch-refmap-upstream base &&
     -+	git -C fetch-refmap-upstream branch next &&
     -+	git -C fetch-refmap-upstream branch seen &&
     -+	git -C fetch-refmap-upstream branch other &&
     -+	git init downstream &&
     ++	setup_refmap_repos master next seen other &&
      +	(
      +		cd downstream &&
     -+		git remote add cabal ../fetch-refmap-upstream &&
     -+		git config --unset-all remote.cabal.fetch &&
      +		git config --add remote.cabal.fetch master &&
      +		git config --add remote.cabal.fetch next &&
      +		git config --add remote.cabal.fetch seen &&
     -+		git config remote.cabal.fetchRefmap "+refs/heads/*:refs/remotes/cabal/*" &&
      +		git fetch cabal &&
      +		check_fetched_refs <<-\EOF &&
      +		refs/remotes/cabal/HEAD
     @@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote
      +	)
      +'
      +
     -+test_expect_success 'remote.<name>.fetchRefmap stores an explicit-dst remote.<name>.fetch entry in both places' '
     -+	test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
     -+	git init -b master fetch-refmap-upstream &&
     -+	test_commit -C fetch-refmap-upstream base &&
     -+	git init downstream &&
     ++test_expect_success '--refmap on the command line overrides remote.<name>.fetchRefmap' '
     ++	setup_refmap_repos master next &&
     ++	(
     ++		cd downstream &&
     ++		git config --add remote.cabal.fetch master &&
     ++		git fetch --refmap="refs/heads/*:refs/remotes/other/*" cabal &&
     ++		check_fetched_refs <<-\EOF
     ++		refs/remotes/other/master
     ++		EOF
     ++	)
     ++'
     ++
     ++test_expect_success 'remote.<name>.fetchRefmap is not applied to a remote.<name>.fetch entry with a destination' '
     ++	setup_refmap_repos master next &&
      +	(
      +		cd downstream &&
     -+		git remote add cabal ../fetch-refmap-upstream &&
     -+		git config --unset-all remote.cabal.fetch &&
      +		git config --add remote.cabal.fetch \
      +			"+refs/heads/master:refs/remotes/custom/master" &&
     -+		git config remote.cabal.fetchRefmap "+refs/heads/*:refs/remotes/cabal/*" &&
     ++		git config --add remote.cabal.fetch next &&
      +		git fetch cabal &&
      +		check_fetched_refs <<-\EOF
     -+		refs/remotes/cabal/HEAD
     -+		refs/remotes/cabal/master
     ++		refs/remotes/cabal/next
     ++		refs/remotes/custom/master
     ++		EOF
     ++	)
     ++'
     ++
     ++test_expect_success 'remote.<name>.fetchRefmap is not applied to a command-line refspec with a destination' '
     ++	setup_refmap_repos master next &&
     ++	(
     ++		cd downstream &&
     ++		git fetch cabal master:refs/remotes/custom/master next &&
     ++		check_fetched_refs <<-\EOF
     ++		refs/remotes/cabal/next
      +		refs/remotes/custom/master
      +		EOF
      +	)
      +'
      +
      +test_expect_success 'remote.<name>.fetchRefmap tracks a branch outside remote.<name>.fetch' '
     -+	test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
     -+	git init -b main fetch-refmap-upstream &&
     -+	test_commit -C fetch-refmap-upstream base &&
     -+	git -C fetch-refmap-upstream branch other &&
     -+	git init downstream &&
     ++	setup_refmap_repos main other &&
      +	(
      +		cd downstream &&
     -+		git remote add -t main origin ../fetch-refmap-upstream &&
     -+		git config remote.origin.fetchRefmap "+refs/heads/*:refs/remotes/origin/*" &&
     -+		git fetch origin other &&
     -+		git branch --track topic origin/other &&
     -+		test_cmp_config origin branch.topic.remote &&
     ++		git config --add remote.cabal.fetch \
     ++			"+refs/heads/main:refs/remotes/cabal/main" &&
     ++		git fetch cabal other &&
     ++		git branch --track topic cabal/other &&
     ++		test_cmp_config cabal branch.topic.remote &&
      +		test_cmp_config refs/heads/other branch.topic.merge &&
     -+		echo refs/remotes/origin/other >expect &&
     ++		echo refs/remotes/cabal/other >expect &&
      +		git rev-parse --symbolic-full-name topic@{upstream} >actual &&
      +		test_cmp expect actual
      +	)
      +'
      +
      +test_expect_success 'remote.<name>.fetchRefmap does not track a branch excluded by remote.<name>.fetch' '
     -+	test_when_finished "rm -fr fetch-refmap-upstream downstream" &&
     -+	git init -b main fetch-refmap-upstream &&
     -+	test_commit -C fetch-refmap-upstream base &&
     -+	git -C fetch-refmap-upstream branch secret &&
     -+	git init downstream &&
     ++	setup_refmap_repos main secret &&
      +	(
      +		cd downstream &&
     -+		git remote add origin ../fetch-refmap-upstream &&
     -+		git config --add remote.origin.fetch "^refs/heads/secret" &&
     -+		git config remote.origin.fetchRefmap "+refs/heads/*:refs/remotes/origin/*" &&
     -+		git fetch origin secret &&
     -+		git rev-parse --verify refs/remotes/origin/secret &&
     -+		test_must_fail git branch --track topic origin/secret 2>err &&
     ++		git config --add remote.cabal.fetch \
     ++			"+refs/heads/*:refs/remotes/cabal/*" &&
     ++		git config --add remote.cabal.fetch "^refs/heads/secret" &&
     ++		git fetch cabal secret &&
     ++		git rev-parse --verify refs/remotes/cabal/secret &&
     ++		test_must_fail git branch --track topic cabal/secret 2>err &&
      +		test_grep "not a branch" err
      +	)
      +'
 2:  4ad94eefe9 = 4:  2e2bc76f79 fetch: extract collect_upstream_from_remote() helper
 3:  6724152e69 ! 5:  ece6bd824b fetch: infer branches to fetch from a refmap-only remote
     @@ Metadata
      Author: Harald Nordgren <[email protected]>
      
       ## Commit message ##
     -    fetch: infer branches to fetch from a refmap-only remote
     +    fetch: add remote.<name>.fetchUpstreams
      
     -    We want remote.<name>.fetchRefmap to work on its own, without also
     -    setting remote.<name>.fetch, so a remote can be limited to the
     -    branches already in use locally, without listing them by hand or
     -    touching the command line every time.
     +    In a repository that only works with a few branches of a large
     +    remote, the default refspec makes every "git fetch" download all of
     +    that remote's branches. Narrowing remote.<name>.fetch by hand works,
     +    but the list has to be kept up to date whenever a branch is started
     +    or dropped.
      
     -    Infer what to fetch from the local branches whose @{upstream} is
     -    already on that remote. The refmap itself still only controls where
     -    those fetched refs land, not which branches are selected.
     +    Add remote.<name>.fetchUpstreams. When it is set, "git fetch <name>"
     +    without refspecs on the command line also fetches the upstream
     +    branches that local branches have on that remote, in addition to
     +    what remote.<name>.fetch asks for.
     +
     +    The setting only decides what to fetch. Where the result is stored
     +    is still up to remote.<name>.fetch and remote.<name>.fetchRefmap, so
     +    a remote with fetchUpstreams, a fetchRefmap and no fetch refspec
     +    follows exactly the branches in use locally. Only the upstream of
     +    the current branch is marked for merge, so "git pull" keeps
     +    integrating that branch alone.
      
          Let @{push} use the refmap too, so a branch pushed to such a remote
          resolves to the remote-tracking branch the push updated, instead of
     @@ Commit message
      
       ## Documentation/config/remote.adoc ##
      @@ Documentation/config/remote.adoc: remote.<name>.fetchRefmap::
     - 	fetched to remote-tracking refs to store, the same way
     - 	`remote.<name>.fetch` does for an explicit refspec.
     - 	`remote.<name>.fetch`, if also configured, still decides what is
     --	fetched. See the `--refmap` entry in linkgit:git-fetch[1].
     -+	fetched. If it is not set either, a refspec-less fetch instead
     -+	infers what to fetch from local branches built on this remote.
     -+	See the `--refmap` entry in linkgit:git-fetch[1].
     + 	only at that destination. See the `--refmap` entry in
     + 	linkgit:git-fetch[1].
       
     ++remote.<name>.fetchUpstreams::
     ++	If true, a `git fetch <name>` without refspecs on the command
     ++	line also fetches the upstream branches that local branches
     ++	have on this remote, in addition to what `remote.<name>.fetch`
     ++	asks for. Combined with `remote.<name>.fetchRefmap` and no
     ++	`remote.<name>.fetch`, the remote is limited to the branches in
     ++	use locally. Defaults to false.
     ++
       remote.<name>.push::
       	The default set of "refspec" for linkgit:git-push[1]. See
     -
     - ## Documentation/fetch-options.adoc ##
     -@@ Documentation/fetch-options.adoc: endif::git-pull[]
     - 	command-line arguments. See section on "Configured Remote-tracking
     - 	Branches" for details.
     - +
     --`remote.<name>.refmap` provides the default value for this option, the
     --same way `remote.<name>.fetch` provides the default refspecs to fetch.
     -+When a refmap is active (from `--refmap` or `remote.<name>.fetchRefmap`) but
     -+nothing to fetch is specified on the command line, nor is there a
     -+`remote.<name>.fetch`, infer what to fetch from local branches built on
     -+this remote: remote-tracking branches from this remote that are also
     -+configured as the `@{upstream}` of a local branch are fetched. The
     -+refmap only decides where those fetched refs land, the same as it
     -+would for an explicit refspec, and does not narrow which branches are
     -+selected. It is what turns this on, since without it the fetched
     -+branches would have nowhere to land.
     - 
     - `-t`::
     - `--tags`::
     + 	linkgit:git-push[1].
      
       ## builtin/fetch.c ##
     -@@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
     - 	 */
     - 	struct refspec *effective_refmap =
     - 		refmap.nr ? &refmap : remote ? &remote->refmap : NULL;
     -+	struct refspec inferred_rs;
     -+	int infer_from_refmap = 0;
     - 
     - 	/* opportunistically-updated references: */
     - 	struct ref *orefs = NULL, **oref_tail = &orefs;
     -@@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
     - 	struct hashmap existing_refs;
     - 	int existing_refs_populated = 0;
     - 
     -+	refspec_init_fetch(&inferred_rs, the_hash_algo);
     -+
     - 	filter_prefetch_refspec(rs);
     - 	if (remote)
     - 		filter_prefetch_refspec(&remote->fetch);
     - 	if (effective_refmap)
     - 		filter_prefetch_refspec(effective_refmap);
     +@@ builtin/fetch.c: static void add_merge_config(struct ref **head,
     + 	}
     + }
       
     -+	if (!rs->nr && remote && !remote->fetch.nr &&
     -+	    effective_refmap && effective_refmap->nr) {
     -+		struct strvec tracked = STRVEC_INIT;
     ++static void add_branch_upstreams(struct ref **head,
     ++				 const struct ref *remote_refs,
     ++				 struct remote *remote,
     ++				 struct ref ***tail)
     ++{
     ++	struct strvec upstreams = STRVEC_INIT;
      +
     -+		branches_tracking_remote(the_repository, remote, &tracked);
     -+		for (size_t i = 0; i < tracked.nr; i++)
     -+			refspec_append(&inferred_rs, tracked.v[i]);
     -+		strvec_clear(&tracked);
     ++	branches_tracking_remote(the_repository, remote, &upstreams);
     ++	for (size_t i = 0; i < upstreams.nr; i++) {
     ++		struct refspec_item refspec = { .src = (char *)upstreams.v[i] };
     ++		struct ref *rm;
      +
     -+		rs = &inferred_rs;
     -+		infer_from_refmap = 1;
     ++		for (rm = *head; rm; rm = rm->next)
     ++			if (refname_match(upstreams.v[i], rm->name))
     ++				break;
     ++		if (!rm)
     ++			get_fetch_map(remote_refs, &refspec, tail, 1);
      +	}
     ++	strvec_clear(&upstreams);
     ++}
      +
     - 	if (rs->nr) {
     - 		struct refspec *fetch_refspec;
     - 
     - 		for (i = 0; i < rs->nr; i++) {
     --			get_fetch_map(remote_refs, &rs->items[i], &tail, 0);
     -+			get_fetch_map(remote_refs, &rs->items[i], &tail,
     -+				      infer_from_refmap);
     - 			if (rs->items[i].dst && rs->items[i].dst[0])
     - 				*autotags = 1;
     - 		}
     + static void map_refs_without_dst(const struct ref *ref_map,
     + 				 const struct refspec *refmap,
     + 				 struct ref ***tail)
      @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
     - 
     - 		for (i = 0; i < fetch_refspec->nr; i++)
     - 			get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);
     -+	} else if (infer_from_refmap) {
     -+		/* Already fully handled above. */
     - 	} else if (refmap.nr) {
     - 		die("--refmap option is only meaningful with command-line refspec(s)");
     - 	} else {
     + 		struct branch *branch = branch_get(NULL);
     + 		int has_merge = branch_has_merge_config(branch);
     + 		if (remote &&
     +-		    (remote->fetch.nr ||
     ++		    (remote->fetch.nr || remote->fetch_upstreams ||
     + 		     /* Note: has_merge implies non-NULL branch->remote_name */
     + 		     (has_merge && !strcmp(branch->remote_name, remote->name)))) {
     + 			for (i = 0; i < remote->fetch.nr; i++) {
      @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
     - 	if (existing_refs_populated)
     - 		hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);
     - 
     -+	refspec_clear(&inferred_rs);
     - 	return ref_map;
     - }
     - 
     -@@ builtin/fetch.c: static int do_fetch(struct transport *transport,
     - 	struct strmap rejected_refs = STRMAP_INIT;
     - 	int summary_width = 0;
     - 	int follow_remote_head = FOLLOW_REMOTE_NEVER;
     -+	bool apply_follow_remote_head_config = false;
     - 
     - 	if (tags == TAGS_DEFAULT) {
     - 		if (transport->remote->fetch_tags == 2)
     + 				    !remote->fetch.items[0].pattern)
     + 					ref_map->fetch_head_status = FETCH_HEAD_MERGE;
     + 			}
     ++			if (remote->fetch_upstreams)
     ++				add_branch_upstreams(&ref_map, remote_refs,
     ++						     remote, &tail);
     + 			/*
     + 			 * if the remote we're fetching from is the same
     + 			 * as given in branch.<name>.remote, we add the
      @@ builtin/fetch.c: static int do_fetch(struct transport *transport,
     - 
       	if (rs->nr) {
       		refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);
     --	} else {
     + 	} else {
      -		if (transport->remote->fetch.nr) {
     --			refspec_ref_prefixes(&transport->remote->fetch,
     --					     &transport_ls_refs_options.ref_prefixes);
     --			/*
     --			 * See remote.c's handling of remote.<name>.followRemoteHEAD
     --			 * for the analogous, still-unresolved case.
     --			 */
     --			if (transport->remote->follow_remote_head)
     --				follow_remote_head = transport->remote->follow_remote_head;
     --			else if (config->follow_remote_head_seen)
     --				follow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);
     --			else
     --				follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;
     --		}
     -+	} else if (transport->remote->fetch.nr) {
     -+		refspec_ref_prefixes(&transport->remote->fetch,
     -+				     &transport_ls_refs_options.ref_prefixes);
     -+		apply_follow_remote_head_config = true;
     - 
     - 		/*
     - 		 * The configured refspec may not cover the current
     ++		if (transport->remote->fetch.nr ||
     ++		    transport->remote->fetch_upstreams) {
     + 			refspec_ref_prefixes(&transport->remote->fetch,
     + 					     &transport_ls_refs_options.ref_prefixes);
     + 			/*
      @@ builtin/fetch.c: static int do_fetch(struct transport *transport,
     + 		 * branch's upstream (e.g. a narrowed -t refspec), so
     + 		 * make sure we can still fetch it regardless.
       		 */
     - 		collect_upstream_from_remote(the_repository, transport->remote,
     - 					      NULL, &transport_ls_refs_options.ref_prefixes);
     -+	} else {
     -+		/*
     -+		 * The --refmap command line option, if given, takes
     -+		 * precedence over remote.<name>.fetchRefmap.
     -+		 */
     -+		struct refspec *effective_refmap = refmap.nr ? &refmap :
     -+			&transport->remote->refmap;
     -+
     -+		if (effective_refmap->nr) {
     +-		collect_upstream_from_remote(the_repository, transport->remote,
     +-					      NULL, &transport_ls_refs_options.ref_prefixes);
     ++		if (transport->remote->fetch_upstreams)
      +			branches_tracking_remote(the_repository, transport->remote,
      +						  &transport_ls_refs_options.ref_prefixes);
     -+			apply_follow_remote_head_config = true;
     -+		} else {
     ++		else
      +			collect_upstream_from_remote(the_repository, transport->remote,
      +						      NULL, &transport_ls_refs_options.ref_prefixes);
     -+		}
       
       		/*
       		 * If there are no refs specified to fetch, then we just
     -@@ builtin/fetch.c: static int do_fetch(struct transport *transport,
     - 				    "HEAD");
     - 	}
     - 
     -+	if (apply_follow_remote_head_config) {
     -+		/*
     -+		 * See remote.c's handling of remote.<name>.followRemoteHEAD
     -+		 * for the analogous, still-unresolved case.
     -+		 */
     -+		if (transport->remote->follow_remote_head)
     -+			follow_remote_head = transport->remote->follow_remote_head;
     -+		else if (config->follow_remote_head_seen)
     -+			follow_remote_head = get_follow_remote_head(config->follow_remote_head_raw);
     -+		else
     -+			follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;
     -+	}
     -+
     - 	if (tags == TAGS_SET || tags == TAGS_DEFAULT)
     - 		strvec_push(&transport_ls_refs_options.ref_prefixes,
     - 			    "refs/tags/");
      
       ## remote.c ##
     +@@ remote.c: static int handle_config(const char *key, const char *value,
     + 			return -1;
     + 		refspec_append(&remote->refmap, v);
     + 		free(v);
     ++	} else if (!strcmp(subkey, "fetchupstreams")) {
     ++		remote->fetch_upstreams = git_config_bool(key, value);
     + 	} else if (!strcmp(subkey, "receivepack")) {
     + 		char *v;
     + 		if (git_config_string(&v, key, value))
      @@ remote.c: void collect_upstream_from_remote(struct repository *repo,
       		strvec_push(tracked, branch->merge[i]->src);
       }
     @@ remote.c: static char *tracking_for_push_dest(struct repository *repo,
       				 _("push destination '%s' on remote '%s' has no local tracking branch"),
      
       ## remote.h ##
     +@@ remote.h: struct remote {
     + 	 */
     + 	struct refspec refmap;
     + 
     ++	/*
     ++	 * Whether a fetch without refspecs on the command line also
     ++	 * fetches the upstreams that local branches have on this remote.
     ++	 */
     ++	bool fetch_upstreams;
     ++
     + 	/*
     + 	 * The setting for whether to fetch tags (as a separate rule from the
     + 	 * configured refspecs);
      @@ remote.h: void collect_upstream_from_remote(struct repository *repo,
       				   const char *refname,
       				   struct strvec *tracked);
     @@ t/t5586-fetch-refmap.sh (new)
      @@
      +#!/bin/sh
      +
     -+test_description='"git fetch" with a remote.<name>.fetchRefmap but no remote.<name>.fetch
     ++test_description='"git fetch" with remote.<name>.fetchUpstreams
      +
     -+When a remote has a refmap configured but no fetch refspec, a
     -+refspec-less fetch infers what to fetch from the local branches whose
     -+@{upstream} is on that remote.
     ++With remote.<name>.fetchUpstreams set, a refspec-less fetch also
     ++fetches the upstreams that local branches have on that remote, and
     ++remote.<name>.fetchRefmap decides where they are stored.
      +'
      +
      +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
     @@ t/t5586-fetch-refmap.sh (new)
      +
      +. ./test-lib.sh
      +
     ++check_upstream_refs () {
     ++	git for-each-ref --format="%(refname)" refs/remotes/upstream >actual &&
     ++	cat >expect &&
     ++	test_cmp expect actual
     ++}
     ++
     ++check_upstream_tip () {
     ++	git -C .. rev-parse "$2" >expect &&
     ++	git rev-parse "$1" >actual &&
     ++	test_cmp expect actual
     ++}
     ++
      +test_expect_success 'setup' '
      +	test_commit main-1 &&
      +	test_commit main-2 &&
     @@ t/t5586-fetch-refmap.sh (new)
      +		git remote add upstream .. &&
      +		test_might_fail git config unset remote.upstream.fetch &&
      +		git config remote.upstream.fetchRefmap \
     -+			"+refs/heads/*:refs/remotes/upstream/*"
     ++			"+refs/heads/*:refs/remotes/upstream/*" &&
     ++		git config remote.upstream.fetchUpstreams true
      +	)
      +'
      +
     @@ t/t5586-fetch-refmap.sh (new)
      +	(
      +		cd client &&
      +		git fetch upstream &&
     -+		git for-each-ref --format="%(refname)" refs/remotes/upstream >actual &&
     -+		test_must_be_empty actual
     ++		check_upstream_refs <<-\EOF
     ++		EOF
      +	)
      +'
      +
     @@ t/t5586-fetch-refmap.sh (new)
      +	(
      +		cd client &&
      +		git fetch upstream &&
     -+		git for-each-ref --format="%(refname)" refs/remotes/upstream >actual &&
     -+		cat >expect <<-\EOF &&
     ++		check_upstream_refs <<-\EOF &&
      +		refs/remotes/upstream/HEAD
      +		refs/remotes/upstream/main
      +		refs/remotes/upstream/side
      +		EOF
     -+		test_cmp expect actual &&
     -+		git rev-parse refs/remotes/upstream/side >actual-oid &&
     -+		git -C .. rev-parse side >expect-oid &&
     -+		test_cmp expect-oid actual-oid
     ++		check_upstream_tip refs/remotes/upstream/side side
      +	)
      +'
      +
     @@ t/t5586-fetch-refmap.sh (new)
      +		cd client &&
      +		git branch side-topic-2 upstream/side &&
      +		git fetch upstream &&
     -+		git for-each-ref --format="%(refname)" refs/remotes/upstream >actual &&
     -+		cat >expect <<-\EOF &&
     -+		refs/remotes/upstream/HEAD
     -+		refs/remotes/upstream/main
     -+		refs/remotes/upstream/side
     -+		EOF
     -+		test_cmp expect actual
     ++		grep side .git/FETCH_HEAD >fetched &&
     ++		test_line_count = 1 fetched
      +	)
      +'
      +
     @@ t/t5586-fetch-refmap.sh (new)
      +		git fetch other next:refs/remotes/other/next &&
      +		git branch next-topic other/next &&
      +		git fetch upstream &&
     -+		git for-each-ref --format="%(refname)" refs/remotes/upstream >actual &&
     -+		cat >expect <<-\EOF &&
     ++		check_upstream_refs <<-\EOF
      +		refs/remotes/upstream/HEAD
      +		refs/remotes/upstream/main
      +		refs/remotes/upstream/side
      +		EOF
     ++	)
     ++'
     ++
     ++test_expect_success 'upstreams are not fetched without remote.<name>.fetchUpstreams' '
     ++	git checkout side &&
     ++	test_commit side-3 &&
     ++	git checkout main &&
     ++	(
     ++		cd client &&
     ++		git rev-parse refs/remotes/upstream/side >expect &&
     ++		git -c remote.upstream.fetchUpstreams=false fetch upstream &&
     ++		git rev-parse refs/remotes/upstream/side >actual &&
      +		test_cmp expect actual
      +	)
      +'
      +
     ++test_expect_success 'remote.<name>.fetch adds to the upstreams being fetched' '
     ++	(
     ++		cd client &&
     ++		git -c remote.upstream.fetch=next fetch upstream &&
     ++		check_upstream_refs <<-\EOF &&
     ++		refs/remotes/upstream/HEAD
     ++		refs/remotes/upstream/main
     ++		refs/remotes/upstream/next
     ++		refs/remotes/upstream/side
     ++		EOF
     ++		check_upstream_tip refs/remotes/upstream/side side
     ++	)
     ++'
     ++
      +test_expect_success 'prefetch stores tracked branches under refs/prefetch/' '
      +	test_commit main-3 &&
      +	(
     @@ t/t5586-fetch-refmap.sh (new)
      +		git fetch --prefetch upstream &&
      +		git rev-parse refs/remotes/upstream/main >actual &&
      +		test_cmp expect actual &&
     -+		git -C .. rev-parse main >expect &&
     -+		git rev-parse refs/prefetch/remotes/upstream/main >actual &&
     -+		test_cmp expect actual
     ++		check_upstream_tip refs/prefetch/remotes/upstream/main main
     ++	)
     ++'
     ++
     ++test_expect_success 'git pull merges only the upstream of the current branch' '
     ++	(
     ++		cd client &&
     ++		git pull &&
     ++		check_upstream_tip HEAD main
      +	)
      +'
      +

4: 6cff6786ba ! 6: e60b61e117 remote: add "git remote add --limited-fetch"
@@ Commit message
branches nobody asked for.

Give "git remote add" a --limited-fetch option that sets up
- remote.<name>.fetchRefmap instead of remote.<name>.fetch, so that
- "git fetch <name>" only fetches the branches already tracked, as
- described in the previous commit. It is rejected together with
- -t/--track or --mirror, since those already say explicitly what to
- fetch.
+ remote.<name>.fetchRefmap and remote.<name>.fetchUpstreams instead
+ of remote.<name>.fetch, so that "git fetch <name>" only fetches the
+ branches already tracked, as described in the previous commit. It
+ is rejected together with -t/--track or --mirror, since those
+ already say explicitly what to fetch.

@@ Documentation/git-remote.adoc: the refs/remotes/<name>/ namespace, a refspec t
multiple branches without grabbing all branches.
+
+With --limited-fetch option, instead of a remote.<name>.fetch refspec
-+that tracks all branches, remote.<name>.fetchRefmap is set up so that a -+refspec-less git fetch <name> only fetches branches our local branches -+are built on. See the --refmap entry in linkgit:git-fetch[1] for -+details.
++that tracks all branches, remote.<name>.fetchRefmap and ++remote.<name>.fetchUpstreams are set up so that a refspec-less ++git fetch <name> only fetches branches our local branches are built ++on. See linkgit:git-config[1] for details.
++
With -m <master> option, a symbolic-ref refs/remotes/<name>/HEAD is set up to point at remote's _<master>_ branch. See also the set-head command.
@@ builtin/remote.c: static int add(int argc, const char **argv, const char *prefix
+ strbuf_reset(&buf2);
+ strbuf_addf(&buf2, "+refs/heads/*:refs/remotes/%s/*", name);
+ repo_config_set(the_repository, buf.buf, buf2.buf);
++ strbuf_reset(&buf);
++ strbuf_addf(&buf, "remote.%s.fetchUpstreams", name);
++ repo_config_set(the_repository, buf.buf, "true");
+ } else {
+ strbuf_addf(&buf, "remote.%s.fetch", name);
+ if (track.nr == 0)
@@ t/t5505-remote.sh: test_expect_success 'filters are listed by git remote -v only
+ git remote add --limited-fetch upstream ../two &&
+ test_cmp_config "+refs/heads/*:refs/remotes/upstream/*" \
+ remote.upstream.fetchRefmap &&
++ test_cmp_config true remote.upstream.fetchUpstreams &&
+ test_must_fail git config get remote.upstream.fetch
+ )
+'
5: 00f9d44a88 ! 7: 3f06f7f9e2 remote: default to --limited-fetch in a shallow repository

     @@ Documentation/git-remote.adoc: Add a remote named _<name>_ for the repository at
       the remote information is set up.
       +
      @@ Documentation/git-remote.adoc: With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec
     - that tracks all branches, `remote.<name>.fetchRefmap` is set up so that a
     - refspec-less `git fetch <name>` only fetches branches our local branches
     - are built on. See the `--refmap` entry in linkgit:git-fetch[1] for
     --details.
     -+details. `--no-limited-fetch` explicitly disables this, overriding the
     -+shallow-repository default described above.
     + that tracks all branches, `remote.<name>.fetchRefmap` and
     + `remote.<name>.fetchUpstreams` are set up so that a refspec-less
     + `git fetch <name>` only fetches branches our local branches are built
     +-on. See linkgit:git-config[1] for details.
     ++on. See linkgit:git-config[1] for details. `--no-limited-fetch`
     ++explicitly disables this, overriding the shallow-repository default
     ++described above.
       +
       With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set
       up to point at remote's _<master>_ branch. See also the set-head command.
     @@ t/t5505-remote.sh: test_expect_success 'filters are listed by git remote -v only
      +		git remote add upstream ../two &&
      +		test_cmp_config "+refs/heads/*:refs/remotes/upstream/*" \
      +			remote.upstream.fetchRefmap &&
     ++		test_cmp_config true remote.upstream.fetchUpstreams &&
      +		test_must_fail git config get remote.upstream.fetch &&
      +		git fetch upstream main &&
      +		git branch --set-upstream-to=upstream/main &&
     -+		test_cmp_config upstream branch.main.remote &&
     -+		test_cmp_config refs/heads/main branch.main.merge &&
      +		git fetch upstream &&
      +		git for-each-ref --format="%(refname)" refs/remotes/upstream >actual &&
      +		cat >expect <<-\EOF &&
     @@ t/t5505-remote.sh: test_expect_success 'filters are listed by git remote -v only
      +		git remote add --no-limited-fetch upstream ../two &&
      +		test_cmp_config "+refs/heads/*:refs/remotes/upstream/*" \
      +			remote.upstream.fetch &&
     -+		test_must_fail git config get remote.upstream.fetchRefmap
     ++		test_must_fail git config get remote.upstream.fetchRefmap &&
     ++		test_must_fail git config get remote.upstream.fetchUpstreams
      +	)
      +'
      +
Signature · 1 line
gitgitgadget
#158 lore raw

PATCH v15 1/7 fetch: do not apply --refmap to a refspec with a destination

From: Harald Nordgren <[email protected]>

"git fetch --refmap=<map> <remote> <src>:<dst>" stores the fetched ref twice, once at <dst> and once where <map> sends <src>. That is harmless while --refmap has to be typed on the same command line, but the next commit lets a remote configure a default for --refmap, and then a refspec that names its own destination would have no way to say "store it only here".

Apply --refmap only to refspecs that have no destination of their own. A refspec with a destination is stored there and nowhere else.

The opportunistic update of remote-tracking branches through remote.<name>.fetch is left alone, so "git fetch origin main:tmp" still updates origin/main.

---
 Documentation/fetch-options.adoc |  4 +++-
 builtin/fetch.c                  | 31 ++++++++++++++++++++++++-------
 t/t5510-fetch.sh                 | 12 ++++++++++++
 3 files changed, 39 insertions(+), 8 deletions(-)
Documentation/fetch-options.adoc +3 -1
index 47dea1de8e..327169cee7 100644
@@ -239,7 +239,9 @@ endif::git-pull[]
 	specified refspec (can be given more than once) to map the
 	refs to remote-tracking branches, instead of the values of
 	`remote.<name>.fetch` configuration variables for the remote
-	repository.  Providing an empty _<refspec>_ to the
+	repository.  A ref fetched with a destination of its own
+	(`<src>:<dst>`) is stored only there and is not mapped.
+	Providing an empty _<refspec>_ to the
 	`--refmap` option causes Git to ignore the configured
 	refspecs and rely entirely on the refspecs supplied as
 	command-line arguments. See section on "Configured Remote-tracking
builtin/fetch.c +24 -7
index c646bc6698..0ee252e353 100644
@@ -274,6 +274,23 @@ static void add_merge_config(struct ref **head,
 	}
 }
 
+static void map_refs_without_dst(const struct ref *ref_map,
+				 const struct refspec *refmap,
+				 struct ref ***tail)
+{
+	struct ref *unmapped = NULL, **unmapped_tail = &unmapped;
+
+	for (const struct ref *rm = ref_map; rm; rm = rm->next) {
+		if (rm->peer_ref)
+			continue;
+		*unmapped_tail = copy_ref(rm);
+		unmapped_tail = &(*unmapped_tail)->next;
+	}
+	for (int i = 0; i < refmap->nr; i++)
+		get_fetch_map(unmapped, &refmap->items[i], tail, 1);
+	free_refs(unmapped);
+}
+
 static void create_fetch_oidset(struct ref **head, struct oidset *out)
 {
 	struct ref *rm = *head;
@@ -530,8 +547,6 @@ static struct ref *get_ref_map(struct remote *remote,
 		filter_prefetch_refspec(&remote->fetch);
 
 	if (rs->nr) {
-		struct refspec *fetch_refspec;
-
 		for (i = 0; i < rs->nr; i++) {
 			get_fetch_map(remote_refs, &rs->items[i], &tail, 0);
 			if (rs->items[i].dst && rs->items[i].dst[0])
@@ -560,14 +575,16 @@ static struct ref *get_ref_map(struct remote *remote,
 		 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
 		 * by ref_remove_duplicates() in favor of one of these
 		 * opportunistic entries with FETCH_HEAD_IGNORE.
+		 *
+		 * A refspec that names its own destination is not
+		 * mapped through --refmap.
 		 */
 		if (refmap.nr)
-			fetch_refspec = &refmap;
+			map_refs_without_dst(ref_map, &refmap, &oref_tail);
 		else
-			fetch_refspec = &remote->fetch;
-
-		for (i = 0; i < fetch_refspec->nr; i++)
-			get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);
+			for (i = 0; i < remote->fetch.nr; i++)
+				get_fetch_map(ref_map, &remote->fetch.items[i],
+					      &oref_tail, 1);
 	} else if (refmap.nr) {
 		die("--refmap option is only meaningful with command-line refspec(s)");
 	} else {
t/t5510-fetch.sh +12 -0
index 5b27f1d5bb..4b40ec965a 100755
@@ -927,6 +927,18 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '
 	)
 '
 
+test_expect_success '--refmap is not applied to a refspec with a destination' '
+	git branch -f side &&
+	(
+		cd three &&
+		git fetch --refmap="refs/heads/*:refs/remotes/mapped/*" origin \
+			main:refs/remotes/custom/main side &&
+		git rev-parse --verify refs/remotes/custom/main &&
+		test_must_fail git rev-parse --verify refs/remotes/mapped/main &&
+		git rev-parse --verify refs/remotes/mapped/side
+	)
+'
+
 test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '
 	git branch -f side &&
 	(
Signature · 1 line
gitgitgadget
#160 lore raw

PATCH v15 2/7 fetch: let --refmap apply to configured refspecs

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

"git fetch --refmap=<map> <remote>" without refspecs on the command line dies with "--refmap option is only meaningful with command-line refspec(s)". So there is no way to fetch what remote.<name>.fetch normally fetches and store it somewhere else for one invocation.

Let the option apply there as well. The configured refspecs still decide what is fetched, and the refs are stored through <map> instead of at the destinations configured in remote.<name>.fetch. The command line overrides the configuration, the same way refspecs given on the command line override remote.<name>.fetch.

---
 Documentation/fetch-options.adoc |  8 ++++++--
 builtin/fetch.c                  | 15 +++++++++++++--
 t/t5510-fetch.sh                 |  9 +++++++--
 3 files changed, 26 insertions(+), 6 deletions(-)
Documentation/fetch-options.adoc +6 -2
index 327169cee7..27f2ac6dc7 100644
@@ -239,8 +239,12 @@ endif::git-pull[]
 	specified refspec (can be given more than once) to map the
 	refs to remote-tracking branches, instead of the values of
 	`remote.<name>.fetch` configuration variables for the remote
-	repository.  A ref fetched with a destination of its own
-	(`<src>:<dst>`) is stored only there and is not mapped.
+	repository.  A refspec on the command line with a destination
+	of its own (`<src>:<dst>`) is stored only there and is not
+	mapped.  Without refspecs on the command line, the
+	`remote.<name>.fetch` values still decide what is fetched, and
+	the refs are stored through the specified refspec instead of
+	at the destinations configured there.
 	Providing an empty _<refspec>_ to the
 	`--refmap` option causes Git to ignore the configured
 	refspecs and rely entirely on the refspecs supplied as
builtin/fetch.c +13 -2
index 0ee252e353..ccff0075c9 100644
@@ -291,6 +291,14 @@ static void map_refs_without_dst(const struct ref *ref_map,
 	free_refs(unmapped);
 }
 
+static void drop_destinations(struct ref *ref_map)
+{
+	for (struct ref *rm = ref_map; rm; rm = rm->next) {
+		free_one_ref(rm->peer_ref);
+		rm->peer_ref = NULL;
+	}
+}
+
 static void create_fetch_oidset(struct ref **head, struct oidset *out)
 {
 	struct ref *rm = *head;
@@ -585,8 +593,6 @@ static struct ref *get_ref_map(struct remote *remote,
 			for (i = 0; i < remote->fetch.nr; i++)
 				get_fetch_map(ref_map, &remote->fetch.items[i],
 					      &oref_tail, 1);
-	} else if (refmap.nr) {
-		die("--refmap option is only meaningful with command-line refspec(s)");
 	} else {
 		/* Use the defaults */
 		struct branch *branch = branch_get(NULL);
@@ -621,6 +627,11 @@ static struct ref *get_ref_map(struct remote *remote,
 			ref_map->fetch_head_status = FETCH_HEAD_MERGE;
 			tail = &ref_map->next;
 		}
+
+		if (refmap.nr) {
+			drop_destinations(ref_map);
+			map_refs_without_dst(ref_map, &refmap, &oref_tail);
+		}
 	}
 
 	if (tags == TAGS_SET) {
t/t5510-fetch.sh +7 -2
index 4b40ec965a..412a5401a8 100755
@@ -906,10 +906,15 @@ test_expect_success 'explicit pull should update tracking' '
 	)
 '
 
-test_expect_success 'explicit --refmap is allowed only with command-line refspec' '
+test_expect_success '--refmap without command-line refspec replaces where remote.*.fetch stores' '
 	(
 		cd three &&
-		test_must_fail git fetch --refmap="*:refs/remotes/none/*"
+		git update-ref refs/remotes/origin/main base-origin-main &&
+		o=$(git rev-parse --verify refs/remotes/origin/main) &&
+		git fetch --refmap="refs/heads/*:refs/remotes/none/*" origin &&
+		n=$(git rev-parse --verify refs/remotes/origin/main) &&
+		test "$o" = "$n" &&
+		git rev-parse --verify refs/remotes/none/main
 	)
 '
 
Signature · 1 line
gitgitgadget
#162 lore raw

PATCH v15 3/7 fetch: add remote.<name>.fetchRefmap

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

Add a per-remote config variable, remote.<name>.fetchRefmap, that provides the default value for --refmap the same way remote.<name>.fetch already provides the default refspecs to fetch. --refmap on the command line overrides it.

The refmap only says where fetched refs are stored. Configuring it does not change what a fetch without refspecs fetches.

When remote.<name>.fetch is also configured, it still decides what to fetch. Its entries without a destination are stored through the refmap, and an entry with its own destination is stored only there. This lets a remote name only a few branches and store them all through one refmap:

[remote "cabal"]
fetch = master
fetch = next
fetch = seen
fetchRefmap = +refs/heads/*:refs/remotes/cabal/*

Looking up the remote-tracking branch for a remote branch, as @{upstream} and "git branch --track" do, falls back to the refmap when remote.<name>.fetch has no match, so that these work for such branches too. A branch excluded by a negative refspec in remote.<name>.fetch stays excluded.

---
 Documentation/config/remote.adoc |   9 ++
 Documentation/fetch-options.adoc |   3 +
 builtin/fetch.c                  |  22 +++--
 remote.c                         |  20 +++-
 remote.h                         |   6 ++
 t/t5510-fetch.sh                 | 154 +++++++++++++++++++++++++++++++
 6 files changed, 207 insertions(+), 7 deletions(-)
Documentation/config/remote.adoc +9 -0
index 3a20d0f752..c088d6257d 100644
@@ -33,6 +33,15 @@ remote.<name>.fetch::
 	The default set of "refspec" for linkgit:git-fetch[1]. See
 	linkgit:git-fetch[1].
 
+remote.<name>.fetchRefmap::
+	The default value of the `--refmap` option, used to map refs being
+	fetched to remote-tracking refs to store, the same way
+	`remote.<name>.fetch` does for an explicit refspec.
+	`remote.<name>.fetch`, if also configured, still decides what is
+	fetched, and an entry there with its own destination is stored
+	only at that destination. See the `--refmap` entry in
+	linkgit:git-fetch[1].
+
 remote.<name>.push::
 	The default set of "refspec" for linkgit:git-push[1]. See
 	linkgit:git-push[1].
Documentation/fetch-options.adoc +3 -0
index 27f2ac6dc7..842bcc6fe5 100644
@@ -250,6 +250,9 @@ endif::git-pull[]
 	refspecs and rely entirely on the refspecs supplied as
 	command-line arguments. See section on "Configured Remote-tracking
 	Branches" for details.
++
+`remote.<name>.fetchRefmap` provides the default value for this option, the
+same way `remote.<name>.fetch` provides the default refspecs to fetch.
 
 `-t`::
 `--tags`::
builtin/fetch.c +16 -6
index ccff0075c9..b83f9e5cc9 100644
@@ -543,6 +543,12 @@ static struct ref *get_ref_map(struct remote *remote,
 	struct ref *rm;
 	struct ref *ref_map = NULL;
 	struct ref **tail = &ref_map;
+	/*
+	 * The --refmap command line option, if given, takes precedence
+	 * over remote.<name>.fetchRefmap.
+	 */
+	struct refspec *effective_refmap =
+		refmap.nr ? &refmap : remote ? &remote->refmap : NULL;
 
 	/* opportunistically-updated references: */
 	struct ref *orefs = NULL, **oref_tail = &orefs;
@@ -553,6 +559,8 @@ static struct ref *get_ref_map(struct remote *remote,
 	filter_prefetch_refspec(rs);
 	if (remote)
 		filter_prefetch_refspec(&remote->fetch);
+	if (effective_refmap)
+		filter_prefetch_refspec(effective_refmap);
 
 	if (rs->nr) {
 		for (i = 0; i < rs->nr; i++) {
@@ -585,10 +593,11 @@ static struct ref *get_ref_map(struct remote *remote,
 		 * opportunistic entries with FETCH_HEAD_IGNORE.
 		 *
 		 * A refspec that names its own destination is not
-		 * mapped through --refmap.
+		 * mapped through the refmap.
 		 */
-		if (refmap.nr)
-			map_refs_without_dst(ref_map, &refmap, &oref_tail);
+		if (effective_refmap && effective_refmap->nr)
+			map_refs_without_dst(ref_map, effective_refmap,
+					     &oref_tail);
 		else
 			for (i = 0; i < remote->fetch.nr; i++)
 				get_fetch_map(ref_map, &remote->fetch.items[i],
@@ -628,10 +637,11 @@ static struct ref *get_ref_map(struct remote *remote,
 			tail = &ref_map->next;
 		}
 
-		if (refmap.nr) {
+		if (refmap.nr)
 			drop_destinations(ref_map);
-			map_refs_without_dst(ref_map, &refmap, &oref_tail);
-		}
+		if (effective_refmap)
+			map_refs_without_dst(ref_map, effective_refmap,
+					     &oref_tail);
 	}
 
 	if (tags == TAGS_SET) {
remote.c +19 -1
index df069e802f..6a6e2717c1 100644
@@ -153,6 +153,7 @@ static struct remote *make_remote(struct remote_state *remote_state,
 	ret->name = xstrndup(name, len);
 	refspec_init_push(&ret->push, the_hash_algo);
 	refspec_init_fetch(&ret->fetch, the_hash_algo);
+	refspec_init_fetch(&ret->refmap, the_hash_algo);
 	string_list_init_dup(&ret->server_options);
 	string_list_init_dup(&ret->negotiation_restrict);
 	string_list_init_dup(&ret->negotiation_include);
@@ -177,6 +178,7 @@ static void remote_clear(struct remote *remote)
 
 	refspec_clear(&remote->push);
 	refspec_clear(&remote->fetch);
+	refspec_clear(&remote->refmap);
 
 	free((char *)remote->receivepack);
 	free((char *)remote->uploadpack);
@@ -540,6 +542,12 @@ static int handle_config(const char *key, const char *value,
 			return -1;
 		refspec_append(&remote->fetch, v);
 		free(v);
+	} else if (!strcmp(subkey, "fetchrefmap")) {
+		char *v;
+		if (git_config_string(&v, key, value))
+			return -1;
+		refspec_append(&remote->refmap, v);
+		free(v);
 	} else if (!strcmp(subkey, "receivepack")) {
 		char *v;
 		if (git_config_string(&v, key, value))
@@ -996,7 +1004,17 @@ void ref_push_report_free(struct ref_push_report *report)
 
 int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
 {
-	return refspec_find_match(&remote->fetch, refspec);
+	char **result = refspec->src ? &refspec->dst : &refspec->src;
+
+	if (remote->fetch.nr && !refspec_find_match(&remote->fetch, refspec))
+		return 0;
+	if (refspec_find_match(&remote->refmap, refspec))
+		return -1;
+	if (refname_matches_negative_refspec_item(refspec->src, &remote->fetch)) {
+		FREE_AND_NULL(*result);
+		return -1;
+	}
+	return 0;
 }
 
 static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
remote.h +6 -0
index cca02033b9..ac485a584d 100644
@@ -90,6 +90,12 @@ struct remote {
 
 	struct refspec fetch;
 
+	/*
+	 * How to map refs fetched without an explicit destination into our
+	 * own namespace, the same as the --refmap command line option.
+	 */
+	struct refspec refmap;
+
 	/*
 	 * The setting for whether to fetch tags (as a separate rule from the
 	 * configured refspecs);
t/t5510-fetch.sh +154 -0

160 lines not on this page · raw message

Signature · 1 line
gitgitgadget
#163 lore raw

PATCH v15 4/7 fetch: extract collect_upstream_from_remote() helper

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

When a fetch has no refspec to work from, we decide what to fetch from two sources: the remote's configured fetch refspec, and the current branch's upstream, if it lives on that remote. For example, after

git clone --single-branch <url>
git switch -c work
git push -u origin work

the remote's refspec only covers "main", but a refspec-less "git fetch origin" still fetches "work" into FETCH_HEAD, so that a plain "git pull" finds the branch to merge. See 49ca2fba39 (fetch: add branch.*.merge to default ref-prefix extension, 2022-09-08).

That "does this branch build on this remote" check was inlined for just the current branch. Move it into its own function, collect_upstream_from_remote(), so the same logic can be reused for branches other than just the current one.

---
 builtin/fetch.c | 18 ++++++++----------
 remote.c        | 15 +++++++++++++++
 remote.h        |  9 +++++++++
 3 files changed, 32 insertions(+), 10 deletions(-)
builtin/fetch.c +8 -10
index b83f9e5cc9..a9b7305f5f 100644
@@ -2028,8 +2028,6 @@ static int do_fetch(struct transport *transport,
 	if (rs->nr) {
 		refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);
 	} else {
-		struct branch *branch = branch_get(NULL);
-
 		if (transport->remote->fetch.nr) {
 			refspec_ref_prefixes(&transport->remote->fetch,
 					     &transport_ls_refs_options.ref_prefixes);
@@ -2044,14 +2042,14 @@ static int do_fetch(struct transport *transport,
 			else
 				follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;
 		}
-		if (branch && branch_has_merge_config(branch) &&
-		    !strcmp(branch->remote_name, transport->remote->name)) {
-			int i;
-			for (i = 0; i < branch->merge_nr; i++) {
-				strvec_push(&transport_ls_refs_options.ref_prefixes,
-					    branch->merge[i]->src);
-			}
-		}
+
+		/*
+		 * The configured refspec may not cover the current
+		 * branch's upstream (e.g. a narrowed -t refspec), so
+		 * make sure we can still fetch it regardless.
+		 */
+		collect_upstream_from_remote(the_repository, transport->remote,
+					      NULL, &transport_ls_refs_options.ref_prefixes);
 
 		/*
 		 * If there are no refs specified to fetch, then we just
remote.c +15 -0
index 6a6e2717c1..ffe892a326 100644
@@ -1967,6 +1967,21 @@ int branch_merge_matches(struct branch *branch,
 	return refname_match(branch->merge[i]->src, refname);
 }
 
+void collect_upstream_from_remote(struct repository *repo,
+				   const struct remote *remote,
+				   const char *refname,
+				   struct strvec *tracked)
+{
+	struct branch *branch = repo_branch_get(repo, refname);
+
+	if (!branch_has_merge_config(branch) ||
+	    strcmp(branch->remote_name, remote->name))
+		return;
+
+	for (int i = 0; i < branch->merge_nr; i++)
+		strvec_push(tracked, branch->merge[i]->src);
+}
+
 __attribute__((format (printf,2,3)))
 static char *error_buf(struct strbuf *err, const char *fmt, ...)
 {
remote.h +9 -0
index ac485a584d..003c8fd180 100644
@@ -359,6 +359,15 @@ int branch_has_merge_config(struct branch *branch);
 
 int branch_merge_matches(struct branch *, int n, const char *);
 
+/*
+ * If refname's branch builds on remote, add its upstream on remote to
+ * tracked. A NULL refname means the current branch.
+ */
+void collect_upstream_from_remote(struct repository *repo,
+				   const struct remote *remote,
+				   const char *refname,
+				   struct strvec *tracked);
+
 /* list of the remote in a group as configured */
 struct remote_group_data {
 	const char *name;
Signature · 1 line
gitgitgadget
#164 lore raw

PATCH v15 5/7 fetch: add remote.<name>.fetchUpstreams

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

In a repository that only works with a few branches of a large remote, the default refspec makes every "git fetch" download all of that remote's branches. Narrowing remote.<name>.fetch by hand works, but the list has to be kept up to date whenever a branch is started or dropped.

Add remote.<name>.fetchUpstreams. When it is set, "git fetch <name>" without refspecs on the command line also fetches the upstream branches that local branches have on that remote, in addition to what remote.<name>.fetch asks for.

The setting only decides what to fetch. Where the result is stored is still up to remote.<name>.fetch and remote.<name>.fetchRefmap, so a remote with fetchUpstreams, a fetchRefmap and no fetch refspec follows exactly the branches in use locally. Only the upstream of the current branch is marked for merge, so "git pull" keeps integrating that branch alone.

Let @{push} use the refmap too, so a branch pushed to such a remote resolves to the remote-tracking branch the push updated, instead of failing with "has no local tracking branch".

---
 Documentation/config/remote.adoc |   8 ++
 builtin/fetch.c                  |  37 ++++++-
 remote.c                         |  29 +++++
 remote.h                         |  11 ++
 t/meson.build                    |   1 +
 t/t5586-fetch-refmap.sh          | 182 +++++++++++++++++++++++++++++++
 6 files changed, 264 insertions(+), 4 deletions(-)
 create mode 100755 t/t5586-fetch-refmap.sh
Documentation/config/remote.adoc +8 -0
index c088d6257d..cd9a86c8bb 100644
@@ -42,6 +42,14 @@ remote.<name>.fetchRefmap::
 	only at that destination. See the `--refmap` entry in
 	linkgit:git-fetch[1].
 
+remote.<name>.fetchUpstreams::
+	If true, a `git fetch <name>` without refspecs on the command
+	line also fetches the upstream branches that local branches
+	have on this remote, in addition to what `remote.<name>.fetch`
+	asks for. Combined with `remote.<name>.fetchRefmap` and no
+	`remote.<name>.fetch`, the remote is limited to the branches in
+	use locally. Defaults to false.
+
 remote.<name>.push::
 	The default set of "refspec" for linkgit:git-push[1]. See
 	linkgit:git-push[1].
builtin/fetch.c +33 -4
index a9b7305f5f..0cfcd5bdd1 100644
@@ -274,6 +274,27 @@ static void add_merge_config(struct ref **head,
 	}
 }
 
+static void add_branch_upstreams(struct ref **head,
+				 const struct ref *remote_refs,
+				 struct remote *remote,
+				 struct ref ***tail)
+{
+	struct strvec upstreams = STRVEC_INIT;
+
+	branches_tracking_remote(the_repository, remote, &upstreams);
+	for (size_t i = 0; i < upstreams.nr; i++) {
+		struct refspec_item refspec = { .src = (char *)upstreams.v[i] };
+		struct ref *rm;
+
+		for (rm = *head; rm; rm = rm->next)
+			if (refname_match(upstreams.v[i], rm->name))
+				break;
+		if (!rm)
+			get_fetch_map(remote_refs, &refspec, tail, 1);
+	}
+	strvec_clear(&upstreams);
+}
+
 static void map_refs_without_dst(const struct ref *ref_map,
 				 const struct refspec *refmap,
 				 struct ref ***tail)
@@ -607,7 +628,7 @@ static struct ref *get_ref_map(struct remote *remote,
 		struct branch *branch = branch_get(NULL);
 		int has_merge = branch_has_merge_config(branch);
 		if (remote &&
-		    (remote->fetch.nr ||
+		    (remote->fetch.nr || remote->fetch_upstreams ||
 		     /* Note: has_merge implies non-NULL branch->remote_name */
 		     (has_merge && !strcmp(branch->remote_name, remote->name)))) {
 			for (i = 0; i < remote->fetch.nr; i++) {
@@ -619,6 +640,9 @@ static struct ref *get_ref_map(struct remote *remote,
 				    !remote->fetch.items[0].pattern)
 					ref_map->fetch_head_status = FETCH_HEAD_MERGE;
 			}
+			if (remote->fetch_upstreams)
+				add_branch_upstreams(&ref_map, remote_refs,
+						     remote, &tail);
 			/*
 			 * if the remote we're fetching from is the same
 			 * as given in branch.<name>.remote, we add the
@@ -2028,7 +2052,8 @@ static int do_fetch(struct transport *transport,
 	if (rs->nr) {
 		refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);
 	} else {
-		if (transport->remote->fetch.nr) {
+		if (transport->remote->fetch.nr ||
+		    transport->remote->fetch_upstreams) {
 			refspec_ref_prefixes(&transport->remote->fetch,
 					     &transport_ls_refs_options.ref_prefixes);
 			/*
@@ -2048,8 +2073,12 @@ static int do_fetch(struct transport *transport,
 		 * branch's upstream (e.g. a narrowed -t refspec), so
 		 * make sure we can still fetch it regardless.
 		 */
-		collect_upstream_from_remote(the_repository, transport->remote,
-					      NULL, &transport_ls_refs_options.ref_prefixes);
+		if (transport->remote->fetch_upstreams)
+			branches_tracking_remote(the_repository, transport->remote,
+						  &transport_ls_refs_options.ref_prefixes);
+		else
+			collect_upstream_from_remote(the_repository, transport->remote,
+						      NULL, &transport_ls_refs_options.ref_prefixes);
 
 		/*
 		 * If there are no refs specified to fetch, then we just
remote.c +29 -0
index ffe892a326..72b4c15a04 100644
@@ -548,6 +548,8 @@ static int handle_config(const char *key, const char *value,
 			return -1;
 		refspec_append(&remote->refmap, v);
 		free(v);
+	} else if (!strcmp(subkey, "fetchupstreams")) {
+		remote->fetch_upstreams = git_config_bool(key, value);
 	} else if (!strcmp(subkey, "receivepack")) {
 		char *v;
 		if (git_config_string(&v, key, value))
@@ -1982,6 +1984,31 @@ void collect_upstream_from_remote(struct repository *repo,
 		strvec_push(tracked, branch->merge[i]->src);
 }
 
+struct branches_tracking_remote_cb_data {
+	struct repository *repo;
+	const struct remote *remote;
+	struct strvec *tracked;
+};
+
+static int add_if_tracking_remote(const struct reference *ref, void *cb_data)
+{
+	struct branches_tracking_remote_cb_data *data = cb_data;
+
+	collect_upstream_from_remote(data->repo, data->remote, ref->name,
+				      data->tracked);
+	return 0;
+}
+
+void branches_tracking_remote(struct repository *repo,
+			       const struct remote *remote,
+			       struct strvec *tracked)
+{
+	struct branches_tracking_remote_cb_data data = { repo, remote, tracked };
+
+	refs_for_each_branch_ref(get_main_ref_store(repo),
+				  add_if_tracking_remote, &data);
+}
+
 __attribute__((format (printf,2,3)))
 static char *error_buf(struct strbuf *err, const char *fmt, ...)
 {
@@ -2061,6 +2088,8 @@ static char *tracking_for_push_dest(struct repository *repo,
 
 	remote = repo_remote_for_push_tracking(repo, remote);
 	ret = apply_refspecs(&remote->fetch, refname);
+	if (!ret)
+		ret = apply_refspecs(&remote->refmap, refname);
 	if (!ret)
 		return error_buf(err,
 				 _("push destination '%s' on remote '%s' has no local tracking branch"),
remote.h +11 -0
index 003c8fd180..73bbe729f5 100644
@@ -96,6 +96,12 @@ struct remote {
 	 */
 	struct refspec refmap;
 
+	/*
+	 * Whether a fetch without refspecs on the command line also
+	 * fetches the upstreams that local branches have on this remote.
+	 */
+	bool fetch_upstreams;
+
 	/*
 	 * The setting for whether to fetch tags (as a separate rule from the
 	 * configured refspecs);
@@ -368,6 +374,11 @@ void collect_upstream_from_remote(struct repository *repo,
 				   const char *refname,
 				   struct strvec *tracked);
 
+/* Same as collect_upstream_from_remote(), but checked for every local branch. */
+void branches_tracking_remote(struct repository *repo,
+			       const struct remote *remote,
+			       struct strvec *tracked);
+
 /* list of the remote in a group as configured */
 struct remote_group_data {
 	const char *name;
t/meson.build +1 -0
index 1cf1d95f70..92f877da1c 100644
@@ -737,6 +737,7 @@ integration_tests = [
   't5582-fetch-negative-refspec.sh',
   't5583-push-branches.sh',
   't5584-http-429-retry.sh',
+  't5586-fetch-refmap.sh',
   't5600-clone-fail-cleanup.sh',
   't5601-clone.sh',
   't5602-clone-remote-exec.sh',
t/t5586-fetch-refmap.sh new file +182 -0

182 lines not on this page · raw message

Signature · 1 line
gitgitgadget
#165 lore raw

PATCH v15 6/7 remote: add "git remote add --limited-fetch"

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

A remote added the ordinary way tracks every branch it has, via a wildcard remote.<name>.fetch refspec. That is wasteful for a remote whose history is only worth following for the branches actually in use locally, and it can make "git fetch" negotiate history for branches nobody asked for.

Give "git remote add" a --limited-fetch option that sets up remote.<name>.fetchRefmap and remote.<name>.fetchUpstreams instead of remote.<name>.fetch, so that "git fetch <name>" only fetches the branches already tracked, as described in the previous commit. It is rejected together with -t/--track or --mirror, since those already say explicitly what to fetch.

---
 Documentation/git-remote.adoc |  8 +++++++-
 builtin/remote.c              | 31 +++++++++++++++++++++++++------
 t/t5505-remote.sh             | 17 +++++++++++++++++
 3 files changed, 49 insertions(+), 7 deletions(-)
Documentation/git-remote.adoc +7 -1
index 315100409d..39b9281293 100644
@@ -10,7 +10,7 @@ SYNOPSIS
 --------
 [synopsis]
 git remote [-v | --verbose]
-git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>
+git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] [--[no-]limited-fetch] <name> <URL>
 git remote rename [--[no-]progress] <old> <new>
 git remote remove <name>
 git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
@@ -75,6 +75,12 @@ the `refs/remotes/<name>/` namespace, a refspec to track only _<branch>_
 is created.  You can give more than one `-t <branch>` to track
 multiple branches without grabbing all branches.
 +
+With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec
+that tracks all branches, `remote.<name>.fetchRefmap` and
+`remote.<name>.fetchUpstreams` are set up so that a refspec-less
+`git fetch <name>` only fetches branches our local branches are built
+on. See linkgit:git-config[1] for details.
++
 With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set
 up to point at remote's _<master>_ branch. See also the set-head command.
 +
builtin/remote.c +25 -6
index fb7e5b114f..18fe6f697c 100644
@@ -179,6 +179,7 @@ static int add(int argc, const char **argv, const char *prefix,
 {
 	int fetch = 0, fetch_tags = TAGS_DEFAULT;
 	unsigned mirror = MIRROR_NONE;
+	int limited_fetch = -1; /* unspecified */
 	struct string_list track = STRING_LIST_INIT_NODUP;
 	const char *master = NULL;
 	struct remote *remote;
@@ -198,6 +199,8 @@ static int add(int argc, const char **argv, const char *prefix,
 		OPT_CALLBACK_F(0, "mirror", &mirror, "(push|fetch)",
 			N_("set up remote as a mirror to push to or fetch from"),
 			PARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),
+		OPT_BOOL(0, "limited-fetch", &limited_fetch,
+			N_("fetch only the branches we build on, instead of every branch")),
 		OPT_END()
 	};
 
@@ -211,6 +214,10 @@ static int add(int argc, const char **argv, const char *prefix,
 		die(_("specifying a master branch makes no sense with --mirror"));
 	if (mirror && !(mirror & MIRROR_FETCH) && track.nr)
 		die(_("specifying branches to track makes sense only with fetch mirrors"));
+	if (limited_fetch == 1 && track.nr)
+		die(_("--limited-fetch does not make sense with -t/--track"));
+	if (limited_fetch == 1 && mirror)
+		die(_("--limited-fetch does not make sense with --mirror"));
 
 	name = argv[0];
 	url = argv[1];
@@ -230,13 +237,25 @@ static int add(int argc, const char **argv, const char *prefix,
 	repo_config_set(the_repository, buf.buf, url);
 
 	if (!mirror || mirror & MIRROR_FETCH) {
+		bool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&
+			limited_fetch == 1;
+
 		strbuf_reset(&buf);
-		strbuf_addf(&buf, "remote.%s.fetch", name);
-		if (track.nr == 0)
-			string_list_append(&track, "*");
-		for (size_t i = 0; i < track.nr; i++) {
-			add_branch(buf.buf, track.items[i].string,
-				   name, mirror, &buf2);
+		if (use_limited_fetch) {
+			strbuf_addf(&buf, "remote.%s.fetchRefmap", name);
+			strbuf_reset(&buf2);
+			strbuf_addf(&buf2, "+refs/heads/*:refs/remotes/%s/*", name);
+			repo_config_set(the_repository, buf.buf, buf2.buf);
+			strbuf_reset(&buf);
+			strbuf_addf(&buf, "remote.%s.fetchUpstreams", name);
+			repo_config_set(the_repository, buf.buf, "true");
+		} else {
+			strbuf_addf(&buf, "remote.%s.fetch", name);
+			if (track.nr == 0)
+				string_list_append(&track, "*");
+			for (size_t i = 0; i < track.nr; i++)
+				add_branch(buf.buf, track.items[i].string,
+					   name, mirror, &buf2);
 		}
 	}
 
t/t5505-remote.sh +17 -0
index 5fbfcb0848..73f0125907 100755
@@ -137,6 +137,23 @@ test_expect_success 'filters are listed by git remote -v only' '
 	test_grep ! "\[blob:none\]" out
 '
 
+test_expect_success '--limited-fetch works in a full repository too' '
+	test_when_finished "rm -rf full-add" &&
+	git clone --no-local one full-add &&
+	(
+		cd full-add &&
+		git remote add --limited-fetch upstream ../two &&
+		test_cmp_config "+refs/heads/*:refs/remotes/upstream/*" \
+			remote.upstream.fetchRefmap &&
+		test_cmp_config true remote.upstream.fetchUpstreams &&
+		test_must_fail git config get remote.upstream.fetch
+	)
+'
+
+test_expect_success '--limited-fetch conflicts with -t' '
+	test_must_fail git remote add --limited-fetch -t main upstream ../two
+'
+
 test_expect_success 'check remote-tracking' '
 	(
 		cd test &&
Signature · 1 line
gitgitgadget
#166 lore raw

PATCH v15 7/7 remote: default to --limited-fetch in a shallow repository

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

Adding a second remote to a shallow clone used to still fetch every branch that remote has, since "git remote add" always set up a wildcard remote.<name>.fetch refspec regardless of how shallow the repository already was. That defeats the purpose of having cloned shallow in the first place (for example with --single-branch), and can make a plain "git fetch" on that remote hang or take a very long time on a repository with many branches.

Turn --limited-fetch on by default when the repository is already shallow and neither -t/--track nor --mirror was given, so that adding a remote there does not by itself commit to following every branch it has. --no-limited-fetch keeps the previous behavior for whoever wants it.

---
 Documentation/git-remote.adoc |  9 +++++-
 builtin/remote.c              |  5 ++-
 t/t5505-remote.sh             | 60 +++++++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 2 deletions(-)
Documentation/git-remote.adoc +8 -1
index 39b9281293..2216a2a752 100644
@@ -57,6 +57,11 @@ Add a remote named _<name>_ for the repository at
 _<URL>_.  The command `git fetch <name>` can then be used to create and
 update remote-tracking branches `<name>/<branch>`.
 +
+If the repository is already a shallow repository (see linkgit:git-clone[1]
+`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,
+`--limited-fetch` is turned on by default, so that `git fetch <name>` does
+not need to negotiate history for every branch the remote has.
++
 With `-f` option, `git fetch <name>` is run immediately after
 the remote information is set up.
 +
@@ -79,7 +84,9 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec
 that tracks all branches, `remote.<name>.fetchRefmap` and
 `remote.<name>.fetchUpstreams` are set up so that a refspec-less
 `git fetch <name>` only fetches branches our local branches are built
-on. See linkgit:git-config[1] for details.
+on. See linkgit:git-config[1] for details. `--no-limited-fetch`
+explicitly disables this, overriding the shallow-repository default
+described above.
 +
 With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set
 up to point at remote's _<master>_ branch. See also the set-head command.
builtin/remote.c +4 -1
index 18fe6f697c..8163fcc618 100644
@@ -16,6 +16,7 @@
 #include "rebase.h"
 #include "refs.h"
 #include "refspec.h"
+#include "shallow.h"
 #include "odb.h"
 #include "strvec.h"
 #include "commit-reach.h"
@@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,
 
 	if (!mirror || mirror & MIRROR_FETCH) {
 		bool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&
-			limited_fetch == 1;
+			(limited_fetch == 1 ||
+			 (limited_fetch == -1 &&
+			  is_repository_shallow(the_repository)));
 
 		strbuf_reset(&buf);
 		if (use_limited_fetch) {
t/t5505-remote.sh +60 -0

66 lines not on this page · raw message

Signature · 1 line
gitgitgadget
#159 lore raw

Re: [PATCH v15 1/7] fetch: do not apply --refmap to a refspec with a destination

In reply to Harald Nordgren via GitGitGadget
Quoted 6 lines · Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

"git fetch --refmap=<map> <remote> <src>:<dst>" stores the fetched ref twice, once at <dst> and once where <map> sends <src>. That is harmless while --refmap has to be typed on the same command line, but the next commit lets a remote configure a default for --refmap,

Minor nit. The configuration is added in 3/7, not 2/7. Rephrasing "the next commit" to a looser "a later commit" would be a sufficient fix.

Documentation/fetch-options.adoc endif::git-pull[] · line 243

Quoted 11 lines
Documentation/fetch-options.adoc +2 -1
index 47dea1de8e..327169cee7 100644
@@ -239,7 +239,9 @@ endif::git-pull[]
 	specified refspec (can be given more than once) to map the
 	refs to remote-tracking branches, instead of the values of
 	`remote.<name>.fetch` configuration variables for the remote
-	repository.  Providing an empty _<refspec>_ to the
+	repository.  A ref fetched with a destination of its own
+	(`<src>:<dst>`) is stored only there and is not mapped.

Tighten the wording to "A ref fetched with a refspec "<src>:<dst>" with non-empty <dst> is stored only there and is not mapped".

With

 $ git fetch '--refmap=refs/heads/*:refs/mapped/*' origin \
	refs/heads/main \
	refs/heads/next: \
	refs/heads/seen:refs/remotes/origin/seen

- "refs/heads/main" is a shorthand for "refs/heads/main:" so the first two are equivalent, the first one merely uses a shorter syntax. In general, any refspec element always has <src>:<dst>, even though sometimes <dst> may be empty for fetches, and <src> may be empty in pushes. That is why "if you have <src>:<dst>" is a bad condition to describe the new behaviour, as you would ALWAYS have <src>:<dst>.

- "refs/heads/next:" does have <dst> but it is an empty <dst>. 'refs/heads/main' and 'refs/heads/next' will be mapped according to the refmap.

- "refs/heads/seen:refs/remotes/origin/seen" has nonempty <src> and <dst>. This is stored only the specified <dst> and refmap exempts it.

Quoted 6 lines
Documentation/fetch-options.adoc +6 -0
+static void map_refs_without_dst(const struct ref *ref_map,
+				 const struct refspec *refmap,
+				 struct ref ***tail)
+{
+	struct ref *unmapped = NULL, **unmapped_tail = &unmapped;
+

Documentation/fetch-options.adoc endif::git-pull[]

Quoted 10 lines
Documentation/fetch-options.adoc +10 -0
+	for (const struct ref *rm = ref_map; rm; rm = rm->next) {
+		if (rm->peer_ref)
+			continue;
+		*unmapped_tail = copy_ref(rm);
+		unmapped_tail = &(*unmapped_tail)->next;
+	}
+	for (int i = 0; i < refmap->nr; i++)
+		get_fetch_map(unmapped, &refmap->items[i], tail, 1);
+	free_refs(unmapped);
+}

Because rm->peer_ref is NULL for every copied element, copy_ref() does not recurse into peer_ref, and get_fetch_map() allocates fresh copies into *tail before free_refs(unmapped) releases the temporary list.

In parse_refspec() and get_local_ref(), a command-line refspec with an explicit empty destination like 'main:' also leaves rm->peer_ref as NULL, so map_refs_without_dst() still maps it through --refmap (same as before). It is exactly what we want to happen, if the semantics is "a non-empty <dst> bypasses refmap, but an empty <dst> triggers refmap".

Thanks.

#161 lore raw

Re: [PATCH v15 2/7] fetch: let --refmap apply to configured refspecs

In reply to Harald Nordgren via GitGitGadget

builtin/fetch.c static struct ref *get_ref_map(struct remote *remote, · line 598

Quoted 9 lines · Harald Nordgren via GitGitGadget
builtin/fetch.c +0 -2
@@ -585,8 +593,6 @@ static struct ref *get_ref_map(struct remote *remote,
 			for (i = 0; i < remote->fetch.nr; i++)
 				get_fetch_map(ref_map, &remote->fetch.items[i],
 					      &oref_tail, 1);
-	} else if (refmap.nr) {
-		die("--refmap option is only meaningful with command-line refspec(s)");
 	} else {
 		/* Use the defaults */
 		struct branch *branch = branch_get(NULL);

With no configured remote.<name>.fetch,

  $ git fetch --refmap=... <URL>

would no longer error out due to this removal. Instead, it would fetch HEAD into FETCH_HEAD while silently ignoring the given refmap.

I am not sure if we still want to reject --refmap when neither rs->nr nor remote->fetch.nr (nor fetchUpstreams later) gives it anything to map, or if allowing it is harmless.

builtin/fetch.c static struct ref *get_ref_map(struct remote *remote, · line 635

Quoted 10 lines
builtin/fetch.c +5 -0
@@ -621,6 +627,11 @@ static struct ref *get_ref_map(struct remote *remote,
 			ref_map->fetch_head_status = FETCH_HEAD_MERGE;
 			tail = &ref_map->next;
 		}
+
+		if (refmap.nr) {
+			drop_destinations(ref_map);
+			map_refs_without_dst(ref_map, &refmap, &oref_tail);
+		}
 	}

This is a bit hard to read without context, but doesn't this break automatic tag following? Earlier in the loop over remote->fetch, *autotags is set to 1 whenever remote->fetch.items[i].dst is non-empty.

If a user passes --refmap="" from the command line to decline storage of remote-tracking branches, drop_destinations() clears all '.peer_ref' pointers here, yet *autotags has already been computed and stays 1, so tag auto-following still runs.

Conversely, if '.fetch' has destinationless "just store in FETCH_HEAD" entries and --refmap supplies destinations, *autotags stays 0.

In addition, in do_fetch(), when rs->nr is 0, prune_refs() is given &transport->remote->fetch and not &refmap. So

  $ git fetch --prune --refmap="refs/heads/*:refs/remotes/none/*" origin

would still prune based on remote.origin.fetch, which would most likely be set to store in refs/remotes/origin/, and the refs/remotes/none/ hierarchy would not be pruned. I am not sure if this is actually a good thing.

Thanks.

#167 lore raw

v16 cover fetch: avoid fetching every branch of a new remote in a shallow repo

In reply to Harald Nordgren via GitGitGadget

In a shallow single-branch clone of a large repository, git remote add upstream <url> sets up the default +refs/heads/*:refs/remotes/upstream/* refspec. The next git fetch upstream then downloads every branch of that remote, which defeats the point of a shallow clone.

This series lets a remote fetch only the branches that local branches track:

git remote add --limited-fetch upstream <url>
git fetch upstream main
git branch --set-upstream-to=upstream/main
git fetch upstream # fetches only main from now on

What to fetch and where to store it are separate settings. remote.<name>.fetchUpstreams says what to fetch, and remote.<name>.fetchRefmap says where it is stored. --limited-fetch sets both:

[remote "upstream"]
url = <url>
fetchRefmap = +refs/heads/*:refs/remotes/upstream/* fetchUpstreams = true

remote.<name>.fetchRefmap is the configured default for --refmap, the same way remote.<name>.fetch is the default for the refspecs on the command line. The command line overrides the configuration, and at the same level a refspec's own destination wins over the refmap:

* A refspec <src>:<dst> with non-empty <dst> on the command line is stored at <dst> only, with --refmap or with fetchRefmap.
* A non-empty destination in remote.<name>.fetch is stored only there when the refmap comes from fetchRefmap.
* --refmap on the command line replaces the destinations in remote.<name>.fetch. It now also works without refspecs on the command line, as long as the remote has something configured to fetch.

A refspec with an empty <dst>, which is what a bare <src> means, is stored through the refmap. So this stores three branches under refs/remotes/cabal/:

[remote "cabal"]
url = <url>
fetch = master
fetch = next
fetch = seen
fetchRefmap = +refs/heads/*:refs/remotes/cabal/*

In a shallow repository, git remote add turns --limited-fetch on by default.

Design notes:

* A *@{upstream} entry in remote.<name>.fetch was considered for saying what to fetch. Released versions of Git die with "invalid refspec" on it, so a separate setting is used.
* The opportunistic update through remote.<name>.fetch is unchanged. On an ordinary remote git fetch origin side:refs/heads/tmp still updates origin/side. On a remote that only has a fetchRefmap it stores only refs/heads/tmp.
* Tags are followed when a fetch without command-line refspecs ends up storing refs, through a destination or through a refmap. --refmap="" stores nothing and follows no tags.
* --prune only acts on destinations written in refspecs. With --refmap and no command-line refspecs it leaves the destinations in remote.<name>.fetch alone, and destinations that come from a refmap are not pruned.

Changes in v16:

* Tags are now followed when a fetch without command-line refspecs stores refs through --refmap or fetchRefmap. --refmap="" stores nothing and no longer follows tags.
* --refmap without command-line refspecs is rejected again when the remote has nothing configured to fetch, as with a bare URL.
* --prune with --refmap and no command-line refspecs no longer prunes under the destinations in remote.<name>.fetch.
* The --refmap docs and the first commit message now say that a refspec with non-empty <dst> is not mapped, and that commit message says "a later commit".

Changes in v15:

* Added remote.<name>.fetchUpstreams to say explicitly that the upstreams of local branches are fetched, in addition to remote.<name>.fetch. A fetchRefmap alone no longer changes what is fetched.
* A refspec with its own destination on the command line is no longer mapped through --refmap or fetchRefmap. This is a new first patch. A destination in remote.<name>.fetch is no longer mapped through fetchRefmap either.
* --refmap now works without refspecs on the command line. This is a new second patch. It stores what remote.<name>.fetch fetches through the given refmap, and it overrides fetchRefmap.
* Fixed git pull merging every tracked branch into the current one.
* git remote add --limited-fetch now sets both fetchRefmap and fetchUpstreams.
* Fixed a leftover remote.<name>.refmap in the --refmap docs.
* Tests share helpers for repeated setup and checks, drop two steps that other tests cover, and the "does not fetch it twice" test now checks FETCH_HEAD.

Changes in v14:

* Squashed the @{push} fix into "fetch: infer branches to fetch from a refmap-only remote", so the series no longer adds the bug in one patch and fixes it in a later one
* Moved its test to t5586 using the refmap-only remote, so it no longer depends on --limited-fetch
* Dropped the separate "remote: resolve @{push} through remote..fetchRefmap" commit

Changes in v13:

* Added a new commit so @{push} resolves correctly for a --limited-fetch remote, falling back to remote.<name>.fetchRefmap the same way fetch already does when remote.<name>.fetch doesn't cover the branch being pushed.
* Added a regression test to fetch: add remote.<name>.fetchRefmap covering a remote.<name>.fetch entry with an explicit destination, confirming it's stored in both that destination and the refmap's.

Changes in v12:

* Renamed remote.<name>.refmap to remote.<name>.fetchRefmap throughout code, docs, and tests, so it reads unambiguously as fetch-only.
* Added the missing code path in get_ref_map() so a branch fetched by name via remote.<name>.fetch is also stored through remote.<name>.fetchRefmap, with a new test (the "cabal" example: fetch = master/next/seen plus a refmap storing them all under one namespace).
* Reworded the fetchRefmap commit message and doc entry to drop a redundant sentence, fix a confusing paragraph, and spell out that remote.<name>.fetch, when configured, still decides what's fetched while the refmap only remaps it.
* Added a prefetch coverage test to t5586-fetch-refmap.sh.

Changes in v11:

* remote_find_tracking() now falls back to remote.<name>.refmap when remote.<name>.fetch doesn't match. Negative refspecs in fetch still exclude refs, and both cases have tests
* The helpers write straight into a strvec, take the output parameter last and the remote as const
* The followRemoteHEAD lookup is done once, not duplicated in two branches
* Clearer commit messages and docs: a working example plus a reference to 49ca2fba39, the goal stated first, an incorrect "used to die" claim dropped, and an explanation of why a refmap turns on the inference
* use_limited_fetch is a bool

Changes in v10:

* Resolve merge conflict after master integrated
ch/fetch-followremotehead-lazy-validation.

Changes in v9:

* Moved the do_fetch() if/else-if/else split out of the preliminary helper-extraction commit and into the refmap-inference commit that needs it. Preliminary commit is now a simpler refactor.

Changes in v8:

* Split into a preliminary collect_upstream_from_remote() helper commit plus a simplified refmap-inference commit.
* Fixed a regression that could drop the current branch's @{upstream} when remote.<name>.fetch was narrowed.
* Threaded struct repository * through instead of hardcoding the_repository.
* Minor naming and comment cleanups.

Changes in v7:

* Fixed fatal: --refmap option is only meaningful with command-line refspec(s) firing just from configuring remote.<name>.refmap alone, with nothing else to fetch.
* When both remote.<name>.fetch and remote.<name>.refmap are configured, a refspec-less fetch follows .fetch as usual, .refmap only remaps something already being fetched by name. Added tests.

Changes in v6:

* Remove leftover reference to deleted default-branch logic in commit message.

Changes in v5:

* Renumber test file from t5585 to t5586.

Changes in v4:

* Removed the automatic default-branch fetch. A fresh remote fetches nothing until you track a branch explicitly.
* Fixed fetch report showing "new ref HEAD" instead of "new branch "
* Reworded remote..refmap docs and commit message.

Changes in v3:

* Replace the special ":"/"+:" fetch refspec with remote.<name>.refmap, reusing git's existing --refmap mechanism instead of inventing new refspec syntax.
* Split the change into 4 commits.

Changes in v2:

* Replaced the opt-in fetch.shallow config entirely with a new special fetch refspec (+:) that git remote add now defaults new remotes to in a shallow repository. The new refspec fetches whichever branches any local branch tracks at that remote, plus the remote's default branch.

Harald Nordgren (7):
fetch: do not apply --refmap to a refspec with a destination
fetch: let --refmap apply to configured refspecs
fetch: add remote.<name>.fetchRefmap
fetch: extract collect_upstream_from_remote() helper
fetch: add remote.<name>.fetchUpstreams
remote: add "git remote add --limited-fetch"
remote: default to --limited-fetch in a shallow repository

 Documentation/config/remote.adoc |  17 +++
 Documentation/fetch-options.adoc |  12 +-
 Documentation/git-remote.adoc    |  15 ++-
 builtin/fetch.c                  | 121 +++++++++++++----
 builtin/remote.c                 |  34 ++++-
 remote.c                         |  64 ++++++++-
 remote.h                         |  26 ++++
 t/meson.build                    |   1 +
 t/t5505-remote.sh                |  77 +++++++++++
 t/t5510-fetch.sh                 | 220 ++++++++++++++++++++++++++++++-
 t/t5586-fetch-refmap.sh          | 183 +++++++++++++++++++++++++
 11 files changed, 735 insertions(+), 35 deletions(-)
 create mode 100755 t/t5586-fetch-refmap.sh

base-commit: 77904e546de3bd940621092ecec47a88d0cede5d
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2412%2FHaraldNordgren%2Ffetch-shallow-narrow-refspec-v16
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v16
Pull-Request: https://github.com/git/git/pull/2412

Range-diff vs v15:

1: 02bffb6b35 ! 1: 9c7036a75d fetch: do not apply --refmap to a refspec with a destination
@@ Commit message
"git fetch --refmap=<map> <remote> <src>:<dst>" stores the fetched ref twice, once at <dst> and once where <map> sends <src>. That is harmless while --refmap has to be typed on the same command line,
- but the next commit lets a remote configure a default for --refmap,
+ but a later commit lets a remote configure a default for --refmap,
and then a refspec that names its own destination would have no way to say "store it only here".

- Apply --refmap only to refspecs that have no destination of their
- own. A refspec with a destination is stored there and nowhere else.
+ Apply --refmap only to refspecs with an empty <dst>, which is what a
+ bare <src> is shorthand for. A refspec with a non-empty <dst> is
+ stored there and nowhere else.

The opportunistic update of remote-tracking branches through remote.<name>.fetch is left alone, so "git fetch origin main:tmp"
@@ Documentation/fetch-options.adoc: endif::git-pull[]
refs to remote-tracking branches, instead of the values of remote.<name>.fetch configuration variables for the remote
- repository. Providing an empty _<refspec>_ to the
-+ repository. A ref fetched with a destination of its own
-+ (<src>:<dst>) is stored only there and is not mapped.
++ repository. A ref fetched with a refspec <src>:<dst> with
++ non-empty <dst> is stored only there and is not mapped.
+ Providing an empty _<refspec>_ to the
--refmap option causes Git to ignore the configured
refspecs and rely entirely on the refspecs supplied as
@@ t/t5510-fetch.sh: test_expect_success 'explicit --refmap option overrides remote
+ (
+ cd three &&
+ git fetch --refmap="refs/heads/*:refs/remotes/mapped/*" origin \
-+ main:refs/remotes/custom/main side &&
++ main:refs/remotes/custom/main side: &&
+ git rev-parse --verify refs/remotes/custom/main &&
+ test_must_fail git rev-parse --verify refs/remotes/mapped/main &&
+ git rev-parse --verify refs/remotes/mapped/side
2: f848c9b0a8 ! 2: 72c288a81e fetch: let --refmap apply to configured refspecs
@@ Commit message
line overrides the configuration, the same way refspecs given on the command line override remote.<name>.fetch.

+ Tags are followed when the fetched refs end up stored somewhere, so
+ --refmap="" follows none. --prune leaves the configured destinations
+ alone, since this fetch does not store there. Destinations that come
+ from the refmap are not pruned either, the same as with refspecs on
+ the command line.
+
+ The option is still rejected when it has nothing to map, as in "git
+ fetch --refmap=<map> <url>" for a URL with no refspecs configured.
+

## Documentation/fetch-options.adoc ##
@@ Documentation/fetch-options.adoc: endif::git-pull[]
specified refspec (can be given more than once) to map the refs to remote-tracking branches, instead of the values of remote.<name>.fetch configuration variables for the remote
-- repository. A ref fetched with a destination of its own
-- (<src>:<dst>) is stored only there and is not mapped.
-+ repository. A refspec on the command line with a destination
-+ of its own (<src>:<dst>) is stored only there and is not
-+ mapped. Without refspecs on the command line, the
+- repository. A ref fetched with a refspec <src>:<dst> with
+- non-empty <dst> is stored only there and is not mapped.
++ repository. A ref fetched with a refspec <src>:<dst> on the
++ command line with non-empty <dst> is stored only there and is
++ not mapped. Without refspecs on the command line, the
+ remote.<name>.fetch values still decide what is fetched, and
+ the refs are stored through the specified refspec instead of
-+ at the destinations configured there.
++ at the destinations configured there. --prune then leaves
++ the refs at those configured destinations alone.
Providing an empty _<refspec>_ to the
--refmap option causes Git to ignore the configured refspecs and rely entirely on the refspecs supplied as
@@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
} else {
/* Use the defaults */
struct branch *branch = branch_get(NULL);
+@@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
+ (has_merge && !strcmp(branch->remote_name, remote->name)))) {
+ for (i = 0; i < remote->fetch.nr; i++) {
+ get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);
+- if (remote->fetch.items[i].dst &&
++ if (!refmap.nr && remote->fetch.items[i].dst &&
+ remote->fetch.items[i].dst[0])
+ *autotags = 1;
+ if (!i && !has_merge && ref_map &&
+@@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
+ if (has_merge &&
+ !strcmp(branch->remote_name, remote->name))
+ add_merge_config(&ref_map, remote_refs, branch, &tail);
++ } else if (refmap.nr) {
++ die("--refmap option is only meaningful with refspec(s) to fetch");
+ } else if (!prefetch) {
+ ref_map = get_remote_ref(remote_refs, "HEAD");
+ if (!ref_map)
@@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
ref_map->fetch_head_status = FETCH_HEAD_MERGE; tail = &ref_map->next;
@@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
+ if (refmap.nr) {
+ drop_destinations(ref_map);
+ map_refs_without_dst(ref_map, &refmap, &oref_tail);
++ if (orefs)
++ *autotags = 1;
+ }
}

if (tags == TAGS_SET) {
+@@ builtin/fetch.c: static int do_fetch(struct transport *transport,
+ * We only prune based on refspecs specified
+ * explicitly (via command line or configuration); we
+ * don't care whether --tags was specified.
++ *
++ * With --refmap and no command-line refspecs, the
++ * configured destinations are not where we store, so
++ * they are left alone.
+ */
+ if (rs->nr) {
+ retcode = prune_refs(&display_state, rs, transaction, ref_map);
+- } else {
++ } else if (!refmap.nr) {
+ retcode = prune_refs(&display_state, &transport->remote->fetch,
+ transaction, ref_map);
+ }

## t/t5510-fetch.sh ##
@@ t/t5510-fetch.sh: test_expect_success 'explicit pull should update tracking' '
@@ t/t5510-fetch.sh: test_expect_success 'explicit pull should update tracking' '
+ n=$(git rev-parse --verify refs/remotes/origin/main) &&
+ test "$o" = "$n" &&
+ git rev-parse --verify refs/remotes/none/main
++ )
++'
++
++test_expect_success '--refmap stores remote.*.fetch entries without a destination and follows tags' '
++ test_when_finished "git tag -d refmap-followed" &&
++ test_when_finished "git -C three tag -d refmap-followed" &&
++ test_when_finished "git -C three remote remove names" &&
++ git tag refmap-followed &&
++ (
++ cd three &&
++ git remote add names .. &&
++ git config --unset-all remote.names.fetch &&
++ git config remote.names.fetch main &&
++ git fetch --refmap="refs/heads/*:refs/remotes/names/*" names &&
++ git rev-parse --verify refs/remotes/names/main &&
++ git rev-parse --verify refs/tags/refmap-followed
++ )
++'
++
++test_expect_success '--refmap="" without command-line refspec does not follow tags' '
++ test_when_finished "git tag -d refmap-unfollowed" &&
++ git tag refmap-unfollowed &&
++ (
++ cd three &&
++ git fetch --refmap= origin &&
++ test_must_fail git rev-parse --verify refs/tags/refmap-unfollowed
++ )
++'
++
++test_expect_success '--prune with --refmap leaves the configured destinations alone' '
++ test_when_finished "git -C three update-ref -d refs/remotes/origin/stale" &&
++ (
++ cd three &&
++ git update-ref refs/remotes/origin/stale base-origin-main &&
++ git fetch --prune --refmap="refs/heads/*:refs/remotes/none/*" origin &&
++ git rev-parse --verify refs/remotes/origin/stale
++ )
++'
++
++test_expect_success '--refmap is rejected when there are no refspecs to fetch' '
++ (
++ cd three &&
++ test_must_fail git fetch --refmap="refs/heads/*:refs/remotes/none/*" .. 2>err &&
++ test_grep "only meaningful with refspec" err
)
'

3: 1169013f2e ! 3: f94dd72443 fetch: add remote.<name>.fetchRefmap

     @@ Commit message
                  fetch = seen
                  fetchRefmap = +refs/heads/*:refs/remotes/cabal/*
      
     +    Tags are followed for refs stored through the refmap, the same as
     +    for refs stored through remote.<name>.fetch.
     +
          Looking up the remote-tracking branch for a remote branch, as
          @{upstream} and "git branch --track" do, falls back to the refmap when
          remote.<name>.fetch has no match, so that these work for such
     @@ builtin/fetch.c: static struct ref *get_ref_map(struct remote *remote,
      +		if (refmap.nr)
       			drop_destinations(ref_map);
      -			map_refs_without_dst(ref_map, &refmap, &oref_tail);
     +-			if (orefs)
     +-				*autotags = 1;
      -		}
      +		if (effective_refmap)
      +			map_refs_without_dst(ref_map, effective_refmap,
      +					     &oref_tail);
     ++		if (orefs)
     ++			*autotags = 1;
       	}
       
       	if (tags == TAGS_SET) {
     @@ t/t5510-fetch.sh: test_expect_success '--refmap is not applied to a refspec with
      +		refs/remotes/cabal/next
      +		refs/remotes/cabal/seen
      +		EOF
     ++		git rev-parse --verify refs/tags/base &&
      +		git branch --track topic cabal/next &&
      +		echo refs/remotes/cabal/next >expect &&
      +		git rev-parse --symbolic-full-name topic@{upstream} >actual &&
 4:  2e2bc76f79 = 4:  f019dd4529 fetch: extract collect_upstream_from_remote() helper
 5:  ece6bd824b ! 5:  8b43d460fe fetch: add remote.<name>.fetchUpstreams
     @@ t/t5586-fetch-refmap.sh (new)
      +		refs/remotes/upstream/main
      +		refs/remotes/upstream/side
      +		EOF
     -+		check_upstream_tip refs/remotes/upstream/side side
     ++		check_upstream_tip refs/remotes/upstream/side side &&
     ++		git rev-parse --verify refs/tags/side-2
      +	)
      +'
      +
     @@ t/t5586-fetch-refmap.sh (new)
      +		cd client &&
      +		git branch side-topic-2 upstream/side &&
      +		git fetch upstream &&
     -+		grep side .git/FETCH_HEAD >fetched &&
     ++		grep "branch .side. of" .git/FETCH_HEAD >fetched &&
      +		test_line_count = 1 fetched
      +	)
      +'
 6:  e60b61e117 = 6:  1f7845b39a remote: add "git remote add --limited-fetch"
 7:  3f06f7f9e2 = 7:  639a7a5737 remote: default to --limited-fetch in a shallow repository
Signature · 1 line
gitgitgadget
#168 lore raw

PATCH v16 1/7 fetch: do not apply --refmap to a refspec with a destination

From: Harald Nordgren <[email protected]>

"git fetch --refmap=<map> <remote> <src>:<dst>" stores the fetched ref twice, once at <dst> and once where <map> sends <src>. That is harmless while --refmap has to be typed on the same command line, but a later commit lets a remote configure a default for --refmap, and then a refspec that names its own destination would have no way to say "store it only here".

Apply --refmap only to refspecs with an empty <dst>, which is what a bare <src> is shorthand for. A refspec with a non-empty <dst> is stored there and nowhere else.

The opportunistic update of remote-tracking branches through remote.<name>.fetch is left alone, so "git fetch origin main:tmp" still updates origin/main.

---
 Documentation/fetch-options.adoc |  4 +++-
 builtin/fetch.c                  | 31 ++++++++++++++++++++++++-------
 t/t5510-fetch.sh                 | 12 ++++++++++++
 3 files changed, 39 insertions(+), 8 deletions(-)
Documentation/fetch-options.adoc +3 -1
index 47dea1de8e..54c5c8f671 100644
@@ -239,7 +239,9 @@ endif::git-pull[]
 	specified refspec (can be given more than once) to map the
 	refs to remote-tracking branches, instead of the values of
 	`remote.<name>.fetch` configuration variables for the remote
-	repository.  Providing an empty _<refspec>_ to the
+	repository.  A ref fetched with a refspec `<src>:<dst>` with
+	non-empty `<dst>` is stored only there and is not mapped.
+	Providing an empty _<refspec>_ to the
 	`--refmap` option causes Git to ignore the configured
 	refspecs and rely entirely on the refspecs supplied as
 	command-line arguments. See section on "Configured Remote-tracking
builtin/fetch.c +24 -7
index c646bc6698..0ee252e353 100644
@@ -274,6 +274,23 @@ static void add_merge_config(struct ref **head,
 	}
 }
 
+static void map_refs_without_dst(const struct ref *ref_map,
+				 const struct refspec *refmap,
+				 struct ref ***tail)
+{
+	struct ref *unmapped = NULL, **unmapped_tail = &unmapped;
+
+	for (const struct ref *rm = ref_map; rm; rm = rm->next) {
+		if (rm->peer_ref)
+			continue;
+		*unmapped_tail = copy_ref(rm);
+		unmapped_tail = &(*unmapped_tail)->next;
+	}
+	for (int i = 0; i < refmap->nr; i++)
+		get_fetch_map(unmapped, &refmap->items[i], tail, 1);
+	free_refs(unmapped);
+}
+
 static void create_fetch_oidset(struct ref **head, struct oidset *out)
 {
 	struct ref *rm = *head;
@@ -530,8 +547,6 @@ static struct ref *get_ref_map(struct remote *remote,
 		filter_prefetch_refspec(&remote->fetch);
 
 	if (rs->nr) {
-		struct refspec *fetch_refspec;
-
 		for (i = 0; i < rs->nr; i++) {
 			get_fetch_map(remote_refs, &rs->items[i], &tail, 0);
 			if (rs->items[i].dst && rs->items[i].dst[0])
@@ -560,14 +575,16 @@ static struct ref *get_ref_map(struct remote *remote,
 		 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
 		 * by ref_remove_duplicates() in favor of one of these
 		 * opportunistic entries with FETCH_HEAD_IGNORE.
+		 *
+		 * A refspec that names its own destination is not
+		 * mapped through --refmap.
 		 */
 		if (refmap.nr)
-			fetch_refspec = &refmap;
+			map_refs_without_dst(ref_map, &refmap, &oref_tail);
 		else
-			fetch_refspec = &remote->fetch;
-
-		for (i = 0; i < fetch_refspec->nr; i++)
-			get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);
+			for (i = 0; i < remote->fetch.nr; i++)
+				get_fetch_map(ref_map, &remote->fetch.items[i],
+					      &oref_tail, 1);
 	} else if (refmap.nr) {
 		die("--refmap option is only meaningful with command-line refspec(s)");
 	} else {
t/t5510-fetch.sh +12 -0
index 5b27f1d5bb..38185e192d 100755
@@ -927,6 +927,18 @@ test_expect_success 'explicit --refmap option overrides remote.*.fetch' '
 	)
 '
 
+test_expect_success '--refmap is not applied to a refspec with a destination' '
+	git branch -f side &&
+	(
+		cd three &&
+		git fetch --refmap="refs/heads/*:refs/remotes/mapped/*" origin \
+			main:refs/remotes/custom/main side: &&
+		git rev-parse --verify refs/remotes/custom/main &&
+		test_must_fail git rev-parse --verify refs/remotes/mapped/main &&
+		git rev-parse --verify refs/remotes/mapped/side
+	)
+'
+
 test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '
 	git branch -f side &&
 	(
Signature · 1 line
gitgitgadget
#169 lore raw

PATCH v16 2/7 fetch: let --refmap apply to configured refspecs

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

"git fetch --refmap=<map> <remote>" without refspecs on the command line dies with "--refmap option is only meaningful with command-line refspec(s)". So there is no way to fetch what remote.<name>.fetch normally fetches and store it somewhere else for one invocation.

Let the option apply there as well. The configured refspecs still decide what is fetched, and the refs are stored through <map> instead of at the destinations configured in remote.<name>.fetch. The command line overrides the configuration, the same way refspecs given on the command line override remote.<name>.fetch.

Tags are followed when the fetched refs end up stored somewhere, so --refmap="" follows none. --prune leaves the configured destinations alone, since this fetch does not store there. Destinations that come from the refmap are not pruned either, the same as with refspecs on the command line.

The option is still rejected when it has nothing to map, as in "git fetch --refmap=<map> <url>" for a URL with no refspecs configured.

---
 Documentation/fetch-options.adoc |  9 ++++--
 builtin/fetch.c                  | 27 +++++++++++++---
 t/t5510-fetch.sh                 | 53 ++++++++++++++++++++++++++++++--
 3 files changed, 81 insertions(+), 8 deletions(-)
Documentation/fetch-options.adoc +7 -2
index 54c5c8f671..e4b9e47806 100644
@@ -239,8 +239,13 @@ endif::git-pull[]
 	specified refspec (can be given more than once) to map the
 	refs to remote-tracking branches, instead of the values of
 	`remote.<name>.fetch` configuration variables for the remote
-	repository.  A ref fetched with a refspec `<src>:<dst>` with
-	non-empty `<dst>` is stored only there and is not mapped.
+	repository.  A ref fetched with a refspec `<src>:<dst>` on the
+	command line with non-empty `<dst>` is stored only there and is
+	not mapped.  Without refspecs on the command line, the
+	`remote.<name>.fetch` values still decide what is fetched, and
+	the refs are stored through the specified refspec instead of
+	at the destinations configured there.  `--prune` then leaves
+	the refs at those configured destinations alone.
 	Providing an empty _<refspec>_ to the
 	`--refmap` option causes Git to ignore the configured
 	refspecs and rely entirely on the refspecs supplied as
builtin/fetch.c +23 -4
index 0ee252e353..00f427800e 100644
@@ -291,6 +291,14 @@ static void map_refs_without_dst(const struct ref *ref_map,
 	free_refs(unmapped);
 }
 
+static void drop_destinations(struct ref *ref_map)
+{
+	for (struct ref *rm = ref_map; rm; rm = rm->next) {
+		free_one_ref(rm->peer_ref);
+		rm->peer_ref = NULL;
+	}
+}
+
 static void create_fetch_oidset(struct ref **head, struct oidset *out)
 {
 	struct ref *rm = *head;
@@ -585,8 +593,6 @@ static struct ref *get_ref_map(struct remote *remote,
 			for (i = 0; i < remote->fetch.nr; i++)
 				get_fetch_map(ref_map, &remote->fetch.items[i],
 					      &oref_tail, 1);
-	} else if (refmap.nr) {
-		die("--refmap option is only meaningful with command-line refspec(s)");
 	} else {
 		/* Use the defaults */
 		struct branch *branch = branch_get(NULL);
@@ -597,7 +603,7 @@ static struct ref *get_ref_map(struct remote *remote,
 		     (has_merge && !strcmp(branch->remote_name, remote->name)))) {
 			for (i = 0; i < remote->fetch.nr; i++) {
 				get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);
-				if (remote->fetch.items[i].dst &&
+				if (!refmap.nr && remote->fetch.items[i].dst &&
 				    remote->fetch.items[i].dst[0])
 					*autotags = 1;
 				if (!i && !has_merge && ref_map &&
@@ -614,6 +620,8 @@ static struct ref *get_ref_map(struct remote *remote,
 			if (has_merge &&
 			    !strcmp(branch->remote_name, remote->name))
 				add_merge_config(&ref_map, remote_refs, branch, &tail);
+		} else if (refmap.nr) {
+			die("--refmap option is only meaningful with refspec(s) to fetch");
 		} else if (!prefetch) {
 			ref_map = get_remote_ref(remote_refs, "HEAD");
 			if (!ref_map)
@@ -621,6 +629,13 @@ static struct ref *get_ref_map(struct remote *remote,
 			ref_map->fetch_head_status = FETCH_HEAD_MERGE;
 			tail = &ref_map->next;
 		}
+
+		if (refmap.nr) {
+			drop_destinations(ref_map);
+			map_refs_without_dst(ref_map, &refmap, &oref_tail);
+			if (orefs)
+				*autotags = 1;
+		}
 	}
 
 	if (tags == TAGS_SET) {
@@ -2091,10 +2106,14 @@ static int do_fetch(struct transport *transport,
 		 * We only prune based on refspecs specified
 		 * explicitly (via command line or configuration); we
 		 * don't care whether --tags was specified.
+		 *
+		 * With --refmap and no command-line refspecs, the
+		 * configured destinations are not where we store, so
+		 * they are left alone.
 		 */
 		if (rs->nr) {
 			retcode = prune_refs(&display_state, rs, transaction, ref_map);
-		} else {
+		} else if (!refmap.nr) {
 			retcode = prune_refs(&display_state, &transport->remote->fetch,
 					     transaction, ref_map);
 		}
t/t5510-fetch.sh +51 -2

61 lines not on this page · raw message

Signature · 1 line
gitgitgadget
#171 lore raw

PATCH v16 3/7 fetch: add remote.<name>.fetchRefmap

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

Add a per-remote config variable, remote.<name>.fetchRefmap, that provides the default value for --refmap the same way remote.<name>.fetch already provides the default refspecs to fetch. --refmap on the command line overrides it.

The refmap only says where fetched refs are stored. Configuring it does not change what a fetch without refspecs fetches.

When remote.<name>.fetch is also configured, it still decides what to fetch. Its entries without a destination are stored through the refmap, and an entry with its own destination is stored only there. This lets a remote name only a few branches and store them all through one refmap:

[remote "cabal"]
fetch = master
fetch = next
fetch = seen
fetchRefmap = +refs/heads/*:refs/remotes/cabal/*

Tags are followed for refs stored through the refmap, the same as for refs stored through remote.<name>.fetch.

Looking up the remote-tracking branch for a remote branch, as @{upstream} and "git branch --track" do, falls back to the refmap when remote.<name>.fetch has no match, so that these work for such branches too. A branch excluded by a negative refspec in remote.<name>.fetch stays excluded.

---
 Documentation/config/remote.adoc |   9 ++
 Documentation/fetch-options.adoc |   3 +
 builtin/fetch.c                  |  26 ++++--
 remote.c                         |  20 +++-
 remote.h                         |   6 ++
 t/t5510-fetch.sh                 | 155 +++++++++++++++++++++++++++++++
 6 files changed, 210 insertions(+), 9 deletions(-)
Documentation/config/remote.adoc +9 -0
index 3a20d0f752..c088d6257d 100644
@@ -33,6 +33,15 @@ remote.<name>.fetch::
 	The default set of "refspec" for linkgit:git-fetch[1]. See
 	linkgit:git-fetch[1].
 
+remote.<name>.fetchRefmap::
+	The default value of the `--refmap` option, used to map refs being
+	fetched to remote-tracking refs to store, the same way
+	`remote.<name>.fetch` does for an explicit refspec.
+	`remote.<name>.fetch`, if also configured, still decides what is
+	fetched, and an entry there with its own destination is stored
+	only at that destination. See the `--refmap` entry in
+	linkgit:git-fetch[1].
+
 remote.<name>.push::
 	The default set of "refspec" for linkgit:git-push[1]. See
 	linkgit:git-push[1].
Documentation/fetch-options.adoc +3 -0
index e4b9e47806..aed38790bb 100644
@@ -251,6 +251,9 @@ endif::git-pull[]
 	refspecs and rely entirely on the refspecs supplied as
 	command-line arguments. See section on "Configured Remote-tracking
 	Branches" for details.
++
+`remote.<name>.fetchRefmap` provides the default value for this option, the
+same way `remote.<name>.fetch` provides the default refspecs to fetch.
 
 `-t`::
 `--tags`::
builtin/fetch.c +18 -8
index 00f427800e..d9c0e8c2f7 100644
@@ -543,6 +543,12 @@ static struct ref *get_ref_map(struct remote *remote,
 	struct ref *rm;
 	struct ref *ref_map = NULL;
 	struct ref **tail = &ref_map;
+	/*
+	 * The --refmap command line option, if given, takes precedence
+	 * over remote.<name>.fetchRefmap.
+	 */
+	struct refspec *effective_refmap =
+		refmap.nr ? &refmap : remote ? &remote->refmap : NULL;
 
 	/* opportunistically-updated references: */
 	struct ref *orefs = NULL, **oref_tail = &orefs;
@@ -553,6 +559,8 @@ static struct ref *get_ref_map(struct remote *remote,
 	filter_prefetch_refspec(rs);
 	if (remote)
 		filter_prefetch_refspec(&remote->fetch);
+	if (effective_refmap)
+		filter_prefetch_refspec(effective_refmap);
 
 	if (rs->nr) {
 		for (i = 0; i < rs->nr; i++) {
@@ -585,10 +593,11 @@ static struct ref *get_ref_map(struct remote *remote,
 		 * opportunistic entries with FETCH_HEAD_IGNORE.
 		 *
 		 * A refspec that names its own destination is not
-		 * mapped through --refmap.
+		 * mapped through the refmap.
 		 */
-		if (refmap.nr)
-			map_refs_without_dst(ref_map, &refmap, &oref_tail);
+		if (effective_refmap && effective_refmap->nr)
+			map_refs_without_dst(ref_map, effective_refmap,
+					     &oref_tail);
 		else
 			for (i = 0; i < remote->fetch.nr; i++)
 				get_fetch_map(ref_map, &remote->fetch.items[i],
@@ -630,12 +639,13 @@ static struct ref *get_ref_map(struct remote *remote,
 			tail = &ref_map->next;
 		}
 
-		if (refmap.nr) {
+		if (refmap.nr)
 			drop_destinations(ref_map);
-			map_refs_without_dst(ref_map, &refmap, &oref_tail);
-			if (orefs)
-				*autotags = 1;
-		}
+		if (effective_refmap)
+			map_refs_without_dst(ref_map, effective_refmap,
+					     &oref_tail);
+		if (orefs)
+			*autotags = 1;
 	}
 
 	if (tags == TAGS_SET) {
remote.c +19 -1
index 2b7d26b844..d3bb874ed5 100644
@@ -153,6 +153,7 @@ static struct remote *make_remote(struct remote_state *remote_state,
 	ret->name = xstrndup(name, len);
 	refspec_init_push(&ret->push, the_hash_algo);
 	refspec_init_fetch(&ret->fetch, the_hash_algo);
+	refspec_init_fetch(&ret->refmap, the_hash_algo);
 	string_list_init_dup(&ret->server_options);
 	string_list_init_dup(&ret->negotiation_restrict);
 	string_list_init_dup(&ret->negotiation_include);
@@ -177,6 +178,7 @@ static void remote_clear(struct remote *remote)
 
 	refspec_clear(&remote->push);
 	refspec_clear(&remote->fetch);
+	refspec_clear(&remote->refmap);
 
 	free((char *)remote->receivepack);
 	free((char *)remote->uploadpack);
@@ -540,6 +542,12 @@ static int handle_config(const char *key, const char *value,
 			return -1;
 		refspec_append(&remote->fetch, v);
 		free(v);
+	} else if (!strcmp(subkey, "fetchrefmap")) {
+		char *v;
+		if (git_config_string(&v, key, value))
+			return -1;
+		refspec_append(&remote->refmap, v);
+		free(v);
 	} else if (!strcmp(subkey, "receivepack")) {
 		char *v;
 		if (git_config_string(&v, key, value))
@@ -1000,7 +1008,17 @@ void ref_push_report_free(struct ref_push_report *report)
 
 int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
 {
-	return refspec_find_match(&remote->fetch, refspec);
+	char **result = refspec->src ? &refspec->dst : &refspec->src;
+
+	if (remote->fetch.nr && !refspec_find_match(&remote->fetch, refspec))
+		return 0;
+	if (refspec_find_match(&remote->refmap, refspec))
+		return -1;
+	if (refname_matches_negative_refspec_item(refspec->src, &remote->fetch)) {
+		FREE_AND_NULL(*result);
+		return -1;
+	}
+	return 0;
 }
 
 static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
remote.h +6 -0
index cca02033b9..ac485a584d 100644
@@ -90,6 +90,12 @@ struct remote {
 
 	struct refspec fetch;
 
+	/*
+	 * How to map refs fetched without an explicit destination into our
+	 * own namespace, the same as the --refmap command line option.
+	 */
+	struct refspec refmap;
+
 	/*
 	 * The setting for whether to fetch tags (as a separate rule from the
 	 * configured refspecs);
t/t5510-fetch.sh +155 -0

161 lines not on this page · raw message

Signature · 1 line
gitgitgadget
#173 lore raw

PATCH v16 4/7 fetch: extract collect_upstream_from_remote() helper

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

When a fetch has no refspec to work from, we decide what to fetch from two sources: the remote's configured fetch refspec, and the current branch's upstream, if it lives on that remote. For example, after

git clone --single-branch <url>
git switch -c work
git push -u origin work

the remote's refspec only covers "main", but a refspec-less "git fetch origin" still fetches "work" into FETCH_HEAD, so that a plain "git pull" finds the branch to merge. See 49ca2fba39 (fetch: add branch.*.merge to default ref-prefix extension, 2022-09-08).

That "does this branch build on this remote" check was inlined for just the current branch. Move it into its own function, collect_upstream_from_remote(), so the same logic can be reused for branches other than just the current one.

---
 builtin/fetch.c | 18 ++++++++----------
 remote.c        | 15 +++++++++++++++
 remote.h        |  9 +++++++++
 3 files changed, 32 insertions(+), 10 deletions(-)
builtin/fetch.c +8 -10
index d9c0e8c2f7..0f91bccf1e 100644
@@ -2032,8 +2032,6 @@ static int do_fetch(struct transport *transport,
 	if (rs->nr) {
 		refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);
 	} else {
-		struct branch *branch = branch_get(NULL);
-
 		if (transport->remote->fetch.nr) {
 			refspec_ref_prefixes(&transport->remote->fetch,
 					     &transport_ls_refs_options.ref_prefixes);
@@ -2048,14 +2046,14 @@ static int do_fetch(struct transport *transport,
 			else
 				follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;
 		}
-		if (branch && branch_has_merge_config(branch) &&
-		    !strcmp(branch->remote_name, transport->remote->name)) {
-			int i;
-			for (i = 0; i < branch->merge_nr; i++) {
-				strvec_push(&transport_ls_refs_options.ref_prefixes,
-					    branch->merge[i]->src);
-			}
-		}
+
+		/*
+		 * The configured refspec may not cover the current
+		 * branch's upstream (e.g. a narrowed -t refspec), so
+		 * make sure we can still fetch it regardless.
+		 */
+		collect_upstream_from_remote(the_repository, transport->remote,
+					      NULL, &transport_ls_refs_options.ref_prefixes);
 
 		/*
 		 * If there are no refs specified to fetch, then we just
remote.c +15 -0
index d3bb874ed5..92fe44c026 100644
@@ -1971,6 +1971,21 @@ int branch_merge_matches(struct branch *branch,
 	return refname_match(branch->merge[i]->src, refname);
 }
 
+void collect_upstream_from_remote(struct repository *repo,
+				   const struct remote *remote,
+				   const char *refname,
+				   struct strvec *tracked)
+{
+	struct branch *branch = repo_branch_get(repo, refname);
+
+	if (!branch_has_merge_config(branch) ||
+	    strcmp(branch->remote_name, remote->name))
+		return;
+
+	for (int i = 0; i < branch->merge_nr; i++)
+		strvec_push(tracked, branch->merge[i]->src);
+}
+
 __attribute__((format (printf,2,3)))
 static char *error_buf(struct strbuf *err, const char *fmt, ...)
 {
remote.h +9 -0
index ac485a584d..003c8fd180 100644
@@ -359,6 +359,15 @@ int branch_has_merge_config(struct branch *branch);
 
 int branch_merge_matches(struct branch *, int n, const char *);
 
+/*
+ * If refname's branch builds on remote, add its upstream on remote to
+ * tracked. A NULL refname means the current branch.
+ */
+void collect_upstream_from_remote(struct repository *repo,
+				   const struct remote *remote,
+				   const char *refname,
+				   struct strvec *tracked);
+
 /* list of the remote in a group as configured */
 struct remote_group_data {
 	const char *name;
Signature · 1 line
gitgitgadget
#174 lore raw

PATCH v16 5/7 fetch: add remote.<name>.fetchUpstreams

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

In a repository that only works with a few branches of a large remote, the default refspec makes every "git fetch" download all of that remote's branches. Narrowing remote.<name>.fetch by hand works, but the list has to be kept up to date whenever a branch is started or dropped.

Add remote.<name>.fetchUpstreams. When it is set, "git fetch <name>" without refspecs on the command line also fetches the upstream branches that local branches have on that remote, in addition to what remote.<name>.fetch asks for.

The setting only decides what to fetch. Where the result is stored is still up to remote.<name>.fetch and remote.<name>.fetchRefmap, so a remote with fetchUpstreams, a fetchRefmap and no fetch refspec follows exactly the branches in use locally. Only the upstream of the current branch is marked for merge, so "git pull" keeps integrating that branch alone.

Let @{push} use the refmap too, so a branch pushed to such a remote resolves to the remote-tracking branch the push updated, instead of failing with "has no local tracking branch".

---
 Documentation/config/remote.adoc |   8 ++
 builtin/fetch.c                  |  37 ++++++-
 remote.c                         |  29 +++++
 remote.h                         |  11 ++
 t/meson.build                    |   1 +
 t/t5586-fetch-refmap.sh          | 183 +++++++++++++++++++++++++++++++
 6 files changed, 265 insertions(+), 4 deletions(-)
 create mode 100755 t/t5586-fetch-refmap.sh
Documentation/config/remote.adoc +8 -0
index c088d6257d..cd9a86c8bb 100644
@@ -42,6 +42,14 @@ remote.<name>.fetchRefmap::
 	only at that destination. See the `--refmap` entry in
 	linkgit:git-fetch[1].
 
+remote.<name>.fetchUpstreams::
+	If true, a `git fetch <name>` without refspecs on the command
+	line also fetches the upstream branches that local branches
+	have on this remote, in addition to what `remote.<name>.fetch`
+	asks for. Combined with `remote.<name>.fetchRefmap` and no
+	`remote.<name>.fetch`, the remote is limited to the branches in
+	use locally. Defaults to false.
+
 remote.<name>.push::
 	The default set of "refspec" for linkgit:git-push[1]. See
 	linkgit:git-push[1].
builtin/fetch.c +33 -4
index 0f91bccf1e..d9a51e5ff7 100644
@@ -274,6 +274,27 @@ static void add_merge_config(struct ref **head,
 	}
 }
 
+static void add_branch_upstreams(struct ref **head,
+				 const struct ref *remote_refs,
+				 struct remote *remote,
+				 struct ref ***tail)
+{
+	struct strvec upstreams = STRVEC_INIT;
+
+	branches_tracking_remote(the_repository, remote, &upstreams);
+	for (size_t i = 0; i < upstreams.nr; i++) {
+		struct refspec_item refspec = { .src = (char *)upstreams.v[i] };
+		struct ref *rm;
+
+		for (rm = *head; rm; rm = rm->next)
+			if (refname_match(upstreams.v[i], rm->name))
+				break;
+		if (!rm)
+			get_fetch_map(remote_refs, &refspec, tail, 1);
+	}
+	strvec_clear(&upstreams);
+}
+
 static void map_refs_without_dst(const struct ref *ref_map,
 				 const struct refspec *refmap,
 				 struct ref ***tail)
@@ -607,7 +628,7 @@ static struct ref *get_ref_map(struct remote *remote,
 		struct branch *branch = branch_get(NULL);
 		int has_merge = branch_has_merge_config(branch);
 		if (remote &&
-		    (remote->fetch.nr ||
+		    (remote->fetch.nr || remote->fetch_upstreams ||
 		     /* Note: has_merge implies non-NULL branch->remote_name */
 		     (has_merge && !strcmp(branch->remote_name, remote->name)))) {
 			for (i = 0; i < remote->fetch.nr; i++) {
@@ -619,6 +640,9 @@ static struct ref *get_ref_map(struct remote *remote,
 				    !remote->fetch.items[0].pattern)
 					ref_map->fetch_head_status = FETCH_HEAD_MERGE;
 			}
+			if (remote->fetch_upstreams)
+				add_branch_upstreams(&ref_map, remote_refs,
+						     remote, &tail);
 			/*
 			 * if the remote we're fetching from is the same
 			 * as given in branch.<name>.remote, we add the
@@ -2032,7 +2056,8 @@ static int do_fetch(struct transport *transport,
 	if (rs->nr) {
 		refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);
 	} else {
-		if (transport->remote->fetch.nr) {
+		if (transport->remote->fetch.nr ||
+		    transport->remote->fetch_upstreams) {
 			refspec_ref_prefixes(&transport->remote->fetch,
 					     &transport_ls_refs_options.ref_prefixes);
 			/*
@@ -2052,8 +2077,12 @@ static int do_fetch(struct transport *transport,
 		 * branch's upstream (e.g. a narrowed -t refspec), so
 		 * make sure we can still fetch it regardless.
 		 */
-		collect_upstream_from_remote(the_repository, transport->remote,
-					      NULL, &transport_ls_refs_options.ref_prefixes);
+		if (transport->remote->fetch_upstreams)
+			branches_tracking_remote(the_repository, transport->remote,
+						  &transport_ls_refs_options.ref_prefixes);
+		else
+			collect_upstream_from_remote(the_repository, transport->remote,
+						      NULL, &transport_ls_refs_options.ref_prefixes);
 
 		/*
 		 * If there are no refs specified to fetch, then we just
remote.c +29 -0
index 92fe44c026..c9ea6deae5 100644
@@ -548,6 +548,8 @@ static int handle_config(const char *key, const char *value,
 			return -1;
 		refspec_append(&remote->refmap, v);
 		free(v);
+	} else if (!strcmp(subkey, "fetchupstreams")) {
+		remote->fetch_upstreams = git_config_bool(key, value);
 	} else if (!strcmp(subkey, "receivepack")) {
 		char *v;
 		if (git_config_string(&v, key, value))
@@ -1986,6 +1988,31 @@ void collect_upstream_from_remote(struct repository *repo,
 		strvec_push(tracked, branch->merge[i]->src);
 }
 
+struct branches_tracking_remote_cb_data {
+	struct repository *repo;
+	const struct remote *remote;
+	struct strvec *tracked;
+};
+
+static int add_if_tracking_remote(const struct reference *ref, void *cb_data)
+{
+	struct branches_tracking_remote_cb_data *data = cb_data;
+
+	collect_upstream_from_remote(data->repo, data->remote, ref->name,
+				      data->tracked);
+	return 0;
+}
+
+void branches_tracking_remote(struct repository *repo,
+			       const struct remote *remote,
+			       struct strvec *tracked)
+{
+	struct branches_tracking_remote_cb_data data = { repo, remote, tracked };
+
+	refs_for_each_branch_ref(get_main_ref_store(repo),
+				  add_if_tracking_remote, &data);
+}
+
 __attribute__((format (printf,2,3)))
 static char *error_buf(struct strbuf *err, const char *fmt, ...)
 {
@@ -2065,6 +2092,8 @@ static char *tracking_for_push_dest(struct repository *repo,
 
 	remote = repo_remote_for_push_tracking(repo, remote);
 	ret = apply_refspecs(&remote->fetch, refname);
+	if (!ret)
+		ret = apply_refspecs(&remote->refmap, refname);
 	if (!ret)
 		return error_buf(err,
 				 _("push destination '%s' on remote '%s' has no local tracking branch"),
remote.h +11 -0
index 003c8fd180..73bbe729f5 100644
@@ -96,6 +96,12 @@ struct remote {
 	 */
 	struct refspec refmap;
 
+	/*
+	 * Whether a fetch without refspecs on the command line also
+	 * fetches the upstreams that local branches have on this remote.
+	 */
+	bool fetch_upstreams;
+
 	/*
 	 * The setting for whether to fetch tags (as a separate rule from the
 	 * configured refspecs);
@@ -368,6 +374,11 @@ void collect_upstream_from_remote(struct repository *repo,
 				   const char *refname,
 				   struct strvec *tracked);
 
+/* Same as collect_upstream_from_remote(), but checked for every local branch. */
+void branches_tracking_remote(struct repository *repo,
+			       const struct remote *remote,
+			       struct strvec *tracked);
+
 /* list of the remote in a group as configured */
 struct remote_group_data {
 	const char *name;
t/meson.build +1 -0
index 1cf1d95f70..92f877da1c 100644
@@ -737,6 +737,7 @@ integration_tests = [
   't5582-fetch-negative-refspec.sh',
   't5583-push-branches.sh',
   't5584-http-429-retry.sh',
+  't5586-fetch-refmap.sh',
   't5600-clone-fail-cleanup.sh',
   't5601-clone.sh',
   't5602-clone-remote-exec.sh',
t/t5586-fetch-refmap.sh new file +183 -0

183 lines not on this page · raw message

Signature · 1 line
gitgitgadget
#175 lore raw

PATCH v16 6/7 remote: add "git remote add --limited-fetch"

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

A remote added the ordinary way tracks every branch it has, via a wildcard remote.<name>.fetch refspec. That is wasteful for a remote whose history is only worth following for the branches actually in use locally, and it can make "git fetch" negotiate history for branches nobody asked for.

Give "git remote add" a --limited-fetch option that sets up remote.<name>.fetchRefmap and remote.<name>.fetchUpstreams instead of remote.<name>.fetch, so that "git fetch <name>" only fetches the branches already tracked, as described in the previous commit. It is rejected together with -t/--track or --mirror, since those already say explicitly what to fetch.

---
 Documentation/git-remote.adoc |  8 +++++++-
 builtin/remote.c              | 31 +++++++++++++++++++++++++------
 t/t5505-remote.sh             | 17 +++++++++++++++++
 3 files changed, 49 insertions(+), 7 deletions(-)
Documentation/git-remote.adoc +7 -1
index 315100409d..39b9281293 100644
@@ -10,7 +10,7 @@ SYNOPSIS
 --------
 [synopsis]
 git remote [-v | --verbose]
-git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] <name> <URL>
+git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=(fetch|push)] [--[no-]limited-fetch] <name> <URL>
 git remote rename [--[no-]progress] <old> <new>
 git remote remove <name>
 git remote set-head <name> (-a | --auto | -d | --delete | <branch>)
@@ -75,6 +75,12 @@ the `refs/remotes/<name>/` namespace, a refspec to track only _<branch>_
 is created.  You can give more than one `-t <branch>` to track
 multiple branches without grabbing all branches.
 +
+With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec
+that tracks all branches, `remote.<name>.fetchRefmap` and
+`remote.<name>.fetchUpstreams` are set up so that a refspec-less
+`git fetch <name>` only fetches branches our local branches are built
+on. See linkgit:git-config[1] for details.
++
 With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set
 up to point at remote's _<master>_ branch. See also the set-head command.
 +
builtin/remote.c +25 -6
index fb7e5b114f..18fe6f697c 100644
@@ -179,6 +179,7 @@ static int add(int argc, const char **argv, const char *prefix,
 {
 	int fetch = 0, fetch_tags = TAGS_DEFAULT;
 	unsigned mirror = MIRROR_NONE;
+	int limited_fetch = -1; /* unspecified */
 	struct string_list track = STRING_LIST_INIT_NODUP;
 	const char *master = NULL;
 	struct remote *remote;
@@ -198,6 +199,8 @@ static int add(int argc, const char **argv, const char *prefix,
 		OPT_CALLBACK_F(0, "mirror", &mirror, "(push|fetch)",
 			N_("set up remote as a mirror to push to or fetch from"),
 			PARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),
+		OPT_BOOL(0, "limited-fetch", &limited_fetch,
+			N_("fetch only the branches we build on, instead of every branch")),
 		OPT_END()
 	};
 
@@ -211,6 +214,10 @@ static int add(int argc, const char **argv, const char *prefix,
 		die(_("specifying a master branch makes no sense with --mirror"));
 	if (mirror && !(mirror & MIRROR_FETCH) && track.nr)
 		die(_("specifying branches to track makes sense only with fetch mirrors"));
+	if (limited_fetch == 1 && track.nr)
+		die(_("--limited-fetch does not make sense with -t/--track"));
+	if (limited_fetch == 1 && mirror)
+		die(_("--limited-fetch does not make sense with --mirror"));
 
 	name = argv[0];
 	url = argv[1];
@@ -230,13 +237,25 @@ static int add(int argc, const char **argv, const char *prefix,
 	repo_config_set(the_repository, buf.buf, url);
 
 	if (!mirror || mirror & MIRROR_FETCH) {
+		bool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&
+			limited_fetch == 1;
+
 		strbuf_reset(&buf);
-		strbuf_addf(&buf, "remote.%s.fetch", name);
-		if (track.nr == 0)
-			string_list_append(&track, "*");
-		for (size_t i = 0; i < track.nr; i++) {
-			add_branch(buf.buf, track.items[i].string,
-				   name, mirror, &buf2);
+		if (use_limited_fetch) {
+			strbuf_addf(&buf, "remote.%s.fetchRefmap", name);
+			strbuf_reset(&buf2);
+			strbuf_addf(&buf2, "+refs/heads/*:refs/remotes/%s/*", name);
+			repo_config_set(the_repository, buf.buf, buf2.buf);
+			strbuf_reset(&buf);
+			strbuf_addf(&buf, "remote.%s.fetchUpstreams", name);
+			repo_config_set(the_repository, buf.buf, "true");
+		} else {
+			strbuf_addf(&buf, "remote.%s.fetch", name);
+			if (track.nr == 0)
+				string_list_append(&track, "*");
+			for (size_t i = 0; i < track.nr; i++)
+				add_branch(buf.buf, track.items[i].string,
+					   name, mirror, &buf2);
 		}
 	}
 
t/t5505-remote.sh +17 -0
index 8b4c7006ec..679d22fcec 100755
@@ -144,6 +144,23 @@ test_expect_success 'filters are listed by git remote -v only' '
 	test_grep ! "\[blob:none\]" out
 '
 
+test_expect_success '--limited-fetch works in a full repository too' '
+	test_when_finished "rm -rf full-add" &&
+	git clone --no-local one full-add &&
+	(
+		cd full-add &&
+		git remote add --limited-fetch upstream ../two &&
+		test_cmp_config "+refs/heads/*:refs/remotes/upstream/*" \
+			remote.upstream.fetchRefmap &&
+		test_cmp_config true remote.upstream.fetchUpstreams &&
+		test_must_fail git config get remote.upstream.fetch
+	)
+'
+
+test_expect_success '--limited-fetch conflicts with -t' '
+	test_must_fail git remote add --limited-fetch -t main upstream ../two
+'
+
 test_expect_success 'check remote-tracking' '
 	(
 		cd test &&
Signature · 1 line
gitgitgadget
#176 lore raw

PATCH v16 7/7 remote: default to --limited-fetch in a shallow repository

In reply to Harald Nordgren via GitGitGadget

From: Harald Nordgren <[email protected]>

Adding a second remote to a shallow clone used to still fetch every branch that remote has, since "git remote add" always set up a wildcard remote.<name>.fetch refspec regardless of how shallow the repository already was. That defeats the purpose of having cloned shallow in the first place (for example with --single-branch), and can make a plain "git fetch" on that remote hang or take a very long time on a repository with many branches.

Turn --limited-fetch on by default when the repository is already shallow and neither -t/--track nor --mirror was given, so that adding a remote there does not by itself commit to following every branch it has. --no-limited-fetch keeps the previous behavior for whoever wants it.

---
 Documentation/git-remote.adoc |  9 +++++-
 builtin/remote.c              |  5 ++-
 t/t5505-remote.sh             | 60 +++++++++++++++++++++++++++++++++++
 3 files changed, 72 insertions(+), 2 deletions(-)
Documentation/git-remote.adoc +8 -1
index 39b9281293..2216a2a752 100644
@@ -57,6 +57,11 @@ Add a remote named _<name>_ for the repository at
 _<URL>_.  The command `git fetch <name>` can then be used to create and
 update remote-tracking branches `<name>/<branch>`.
 +
+If the repository is already a shallow repository (see linkgit:git-clone[1]
+`--depth`) and neither `-t`, `--mirror` nor `--no-limited-fetch` is given,
+`--limited-fetch` is turned on by default, so that `git fetch <name>` does
+not need to negotiate history for every branch the remote has.
++
 With `-f` option, `git fetch <name>` is run immediately after
 the remote information is set up.
 +
@@ -79,7 +84,9 @@ With `--limited-fetch` option, instead of a `remote.<name>.fetch` refspec
 that tracks all branches, `remote.<name>.fetchRefmap` and
 `remote.<name>.fetchUpstreams` are set up so that a refspec-less
 `git fetch <name>` only fetches branches our local branches are built
-on. See linkgit:git-config[1] for details.
+on. See linkgit:git-config[1] for details. `--no-limited-fetch`
+explicitly disables this, overriding the shallow-repository default
+described above.
 +
 With `-m <master>` option, a symbolic-ref `refs/remotes/<name>/HEAD` is set
 up to point at remote's _<master>_ branch. See also the set-head command.
builtin/remote.c +4 -1
index 18fe6f697c..8163fcc618 100644
@@ -16,6 +16,7 @@
 #include "rebase.h"
 #include "refs.h"
 #include "refspec.h"
+#include "shallow.h"
 #include "odb.h"
 #include "strvec.h"
 #include "commit-reach.h"
@@ -238,7 +239,9 @@ static int add(int argc, const char **argv, const char *prefix,
 
 	if (!mirror || mirror & MIRROR_FETCH) {
 		bool use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 &&
-			limited_fetch == 1;
+			(limited_fetch == 1 ||
+			 (limited_fetch == -1 &&
+			  is_repository_shallow(the_repository)));
 
 		strbuf_reset(&buf);
 		if (use_limited_fetch) {
t/t5505-remote.sh +60 -0

66 lines not on this page · raw message

Signature · 1 line
gitgitgadget
#172 lore raw

Re: [PATCH v16 3/7] fetch: add remote.<name>.fetchRefmap

In reply to Harald Nordgren via GitGitGadget

builtin/fetch.c static struct ref *get_ref_map(struct remote *remote, · line 563

Quoted 2 lines · Harald Nordgren via GitGitGadget
builtin/fetch.c +2 -0
+	if (effective_refmap)
+		filter_prefetch_refspec(effective_refmap);

Doesn't it break "--refmap=" and "remote.<name>.fetchRefmap =" used with "--prefetch" if you call filter_prefetch_refspec() on effective_refmap here? In filter_prefetch_refspec(), any item without a destination is removed.

builtin/fetch.c static struct ref *get_ref_map(struct remote *remote, · line 646

Quoted 9 lines
builtin/fetch.c +4 -4
+		if (refmap.nr)
 			drop_destinations(ref_map);
-			map_refs_without_dst(ref_map, &refmap, &oref_tail);
-			if (orefs)
-				*autotags = 1;
-		}
+		if (effective_refmap)
+			map_refs_without_dst(ref_map, effective_refmap,
+					     &oref_tail);

This does not check effective_refmap->nr, so when no refmap is configured it still copies and frees every unmapped ref in map_refs_without_dst().

builtin/fetch.c static struct ref *get_ref_map(struct remote *remote, · line 648

Quoted 2 lines
builtin/fetch.c +2 -0
+		if (orefs)
+			*autotags = 1;

This probably has the same issue as [2/7]?

#170 lore raw

Re: [PATCH v16 2/7] fetch: let --refmap apply to configured refspecs

In reply to Harald Nordgren via GitGitGadget

builtin/fetch.c static struct ref *get_ref_map(struct remote *remote, · line 638

Quoted 9 lines · Harald Nordgren via GitGitGadget

Tags are followed when the fetched refs end up stored somewhere, so --refmap="" follows none.
...

builtin/fetch.c +6 -0
+		if (refmap.nr) {
+			drop_destinations(ref_map);
+			map_refs_without_dst(ref_map, &refmap, &oref_tail);
+			if (orefs)
+				*autotags = 1;
+		}

Is "if (orefs)" here enough to tell whether anything was actually mapped for storage? Because get_fetch_map() still appends entries with peer_ref == NULL when a refmap item has no destination, a non-empty orefs does not necessarily mean fetched refs end up stored anywhere, no?

A refmap entry without a colon or with an empty right-hand side (such as "--refmap=refs/heads/main" or "--refmap=refs/heads/main:") likewise creates an orefs entry with peer_ref == NULL and triggers tag following without storing any ref. Only setting "*autotags = 1" when some entry in orefs has a non-NULL peer_ref (or skipping refmap items without a non-empty dst in map_refs_without_dst()) would make "--refmap=" and destinationless refmap items behave as documented, I guess?

We would want to have tests to cover such cases, as apparently the ones in this patch did not catch such a corner case?