From: D. Ben Knoble Date: Sat, 24 Oct 2026 15:11:41 GMT Subject: Re: [PATCH v10 4/5] remote: add "git remote add --limited-fetch" Message-ID: In-Reply-To: <80028d4f8638f766a73b84a5631d21101a413490.1792503778.git.gitgitgadget@gmail.com> On Tue, Oct 20, 2026 at 9:43 AM Harald Nordgren via GitGitGadget wrote: > > From: Harald Nordgren > > A remote added the ordinary way tracks every branch it has, via a > wildcard remote..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..refmap instead of remote..fetch, so that > "git fetch " 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. > > Signed-off-by: Harald Nordgren > --- > diff --git a/builtin/remote.c b/builtin/remote.c > index fb7e5b114f..a7c13946d1 100644 > --- a/builtin/remote.c > +++ b/builtin/remote.c > @@ -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")); We need "== 1" because default to "-1" for unspecified, which is a bit atypical of boolean flags unless we need to know about that case. Reading ahead, 5/5 wants to know if limited-fetch was unspecified so we can act on it. Ok. > > 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) { > + int use_limited_fetch = mirror == MIRROR_NONE && track.nr == 0 && > + limited_fetch == 1; > + :\ I don't have a better suggestion for formatting this line, but it's… odd. > 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.refmap", 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); > } > } > > diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh > index 5fbfcb0848..0168d5abfe 100755 > --- a/t/t5505-remote.sh > +++ b/t/t5505-remote.sh > @@ -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.refmap && > + 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 && > -- > gitgitgadget Looks reasonable. -- D. Ben Knoble