Skip to content

pr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v16

tagged this 25 Nov 19:06
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

Submitted-As: https://lore.kernel.org/git/pull.2412.v16.git.git.1795633584.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.2412.git.git.1789829246437.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.2412.v2.git.git.1790195720941.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.2412.v3.git.git.1790333402.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.2412.v4.git.git.1790673598.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.2412.v5.git.git.1790925198.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.2412.v6.git.git.1791102684.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.2412.v7.git.git.1791410164.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.2412.v8.git.git.1791619334.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.2412.v9.git.git.1791961452.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.2412.v10.git.git.1792503778.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.2412.v11.git.git.1792959921.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.2412.v12.git.git.1793103884.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.2412.v13.git.git.1793357550.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.2412.v14.git.git.1794681496.gitgitgadget@gmail.com
In-Reply-To: https://lore.kernel.org/git/pull.2412.v15.git.git.1795348644.gitgitgadget@gmail.com
Assets 2
Loading