From: D. Ben Knoble Date: Sat, 24 Oct 2026 15:09:16 GMT Subject: Re: [PATCH v10 3/5] fetch: infer branches to fetch from a refmap-only remote Message-ID: In-Reply-To: <9fb7e77a862640007339df639d03e61db37b6f70.1792503778.git.gitgitgadget@gmail.com> On Tue, Oct 20, 2026 at 9:43 AM Harald Nordgren via GitGitGadget wrote: > > From: Harald Nordgren > > Configuring remote..refmap without remote..fetch used to > make a refspec-less "git fetch " fail with "--refmap option is > only meaningful with command-line refspec(s)", since a refmap only > says where to put fetched refs, not what to fetch. This makes this commit sound like a fixup of [1/5], but I think it's actually one of the main goals of the series! Can we flip the justification around to say something like "we want to allow refmaps to do XYZ […] Incidentally, we also make sense of the case when refmap is configured without fetch" ? That would make the purpose clearer to me, at least. > Make that case infer what to fetch: the local branches whose > @{upstream} is already on that remote. This lets a remote be > configured to fetch only the branches actually in use, without > listing them by hand in remote..fetch, and without needing to > touch the command line every time. > > Signed-off-by: Harald Nordgren > --- > Documentation/config/remote.adoc | 5 +- > Documentation/fetch-options.adoc | 6 +- > builtin/fetch.c | 75 +++++++++++++++---- > remote.c | 24 +++++++ > remote.h | 4 ++ > t/meson.build | 1 + > t/t5586-fetch-refmap.sh | 119 +++++++++++++++++++++++++++++++ > 7 files changed, 219 insertions(+), 15 deletions(-) > create mode 100755 t/t5586-fetch-refmap.sh > > diff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc > index 103eada406..7c52e443a5 100644 > --- a/Documentation/config/remote.adoc > +++ b/Documentation/config/remote.adoc > @@ -36,7 +36,10 @@ remote..fetch:: > remote..refmap:: > The default value of the `--refmap` option for linkgit:git-fetch[1]. > Used to map remote refs being fetched to remote-tracking refs to > - store. See the `--refmap` entry in linkgit:git-fetch[1]. > + store. If `remote..fetch` is not set either, a refspec-less > + fetch infers what to fetch from local branches built on this > + remote, instead of fetching every branch it has. See the > + `--refmap` entry in linkgit:git-fetch[1]. > > remote..push:: > The default set of "refspec" for linkgit:git-push[1]. See > diff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc > index c2101a7b39..90b9e13ac2 100644 > --- a/Documentation/fetch-options.adoc > +++ b/Documentation/fetch-options.adoc > @@ -245,8 +245,10 @@ endif::git-pull[] > command-line arguments. See section on "Configured Remote-tracking > Branches" for details. > + > -`remote..refmap` provides the default value for this option, the > -same way `remote..fetch` provides the default refspecs to fetch. > +When a refmap is active (from `--refmap` or `remote..refmap`) but > +nothing to fetch is specified on the command line, nor is there a > +`remote..fetch`, branches from the remote that are used as the "remote-tracking branches" ?? > +`@{upstream}` of our local branches are fetched. The clause here is complicated ':D I found "infer what to fetch from local branches built on this remote" to be a bit clearer, but we definitely still need to get to the point, which is that remote-tracking branches from this remote that are also configured as upstreams of local branches are fetched. Now, I wonder a bit: the doc says "when a refmap is active" we do that "fetch remote-tracking branches that are upstream of local branches" [all scoped to the current remote]---but it doesn't really say how the refmap affects that? I mean, I imagine it's used for mapping (duh), but I don't know if it also narrows the scope, or if the scope is deliberately all remote-tracking branches that meet the requirements, or…? IOW, it seems to beg the question "why does the presence of a refmap control this if it has no effect on the set of selected branches," and I think the answer is partly "because we designed it that way" and possibly found in the mails leading up to this point, but we might want to try to connect the dots for folks? Idk. > > `-t`:: > `--tags`:: > diff --git a/builtin/fetch.c b/builtin/fetch.c > index 136a59154a..0fc1fb66d5 100644 > --- a/builtin/fetch.c > +++ b/builtin/fetch.c > @@ -524,6 +524,8 @@ 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; > @@ -531,15 +533,32 @@ 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 (!rs->nr && remote && !remote->fetch.nr && > + effective_refmap && effective_refmap->nr) { > + struct string_list tracked = STRING_LIST_INIT_DUP; > + struct string_list_item *item; > + > + branches_tracking_remote(the_repository, remote, &tracked); > + for_each_string_list_item(item, &tracked) > + refspec_append(&inferred_rs, item->string); > + string_list_clear(&tracked, 0); Interesting choice of string_list again ;) do we need the sorting behavior? (In fact, since it could come from an "ordered" config, perhaps we should use that order?) A strvec seems more appropriate then… > + > + rs = &inferred_rs; > + infer_from_refmap = 1; > + } > + > 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; > } > @@ -574,6 +593,8 @@ 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 { > @@ -669,6 +690,7 @@ 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; > } > > @@ -1978,13 +2000,47 @@ 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) { > + struct string_list tracked = STRING_LIST_INIT_DUP; > + struct string_list_item *item; > + > + refspec_ref_prefixes(&transport->remote->fetch, > + &transport_ls_refs_options.ref_prefixes); > + /* > + * See remote.c's handling of remote..followRemoteHEAD > + * for the analogous, still-unresolved case. Is this a "we haven't resolved what to do" case, or a "some remote (or something) hasn't resolved" case? I'm not sure what I should be looking at here. Oh, I see. It's a duplicate of comment and code from the else{effective_refmap->nr} block below. Hrm… probably it would be nice not to duplicate that? I can't spot any differences between the code blocks. > + */ > + 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; > + > + /* > + * 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, &tracked, > + transport->remote, NULL); > + for_each_string_list_item(item, &tracked) > + strvec_push(&transport_ls_refs_options.ref_prefixes, > + item->string); > + string_list_clear(&tracked, 0); > } else { > + /* > + * The --refmap command line option, if given, takes > + * precedence over remote..refmap. > + */ > + struct refspec *effective_refmap = refmap.nr ? &refmap : > + &transport->remote->refmap; > struct string_list tracked = STRING_LIST_INIT_DUP; > struct string_list_item *item; > > - if (transport->remote->fetch.nr) { > - refspec_ref_prefixes(&transport->remote->fetch, > - &transport_ls_refs_options.ref_prefixes); > + if (effective_refmap->nr) { > + branches_tracking_remote(the_repository, > + transport->remote, &tracked); > /* > * See remote.c's handling of remote..followRemoteHEAD > * for the analogous, still-unresolved case. > @@ -1995,15 +2051,10 @@ static int do_fetch(struct transport *transport, > follow_remote_head = get_follow_remote_head(config->follow_remote_head_raw); > else > follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT; > + } else { > + collect_upstream_from_remote(the_repository, &tracked, > + transport->remote, NULL); > } > - > - /* > - * 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, &tracked, > - transport->remote, NULL); > for_each_string_list_item(item, &tracked) > strvec_push(&transport_ls_refs_options.ref_prefixes, > item->string); > diff --git a/remote.c b/remote.c > index 965e42ff52..6467496a0f 100644 > --- a/remote.c > +++ b/remote.c > @@ -1906,6 +1906,30 @@ void collect_upstream_from_remote(struct repository *repo, > string_list_insert(tracked, branch->merge[i]->src); > } > > +struct branches_tracking_remote_cb_data { > + struct repository *repo; > + struct remote *remote; > + struct string_list *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->tracked, data->remote, > + ref->name); > + return 0; > +} > + > +void branches_tracking_remote(struct repository *repo, struct remote *remote, > + struct string_list *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, ...) > { > diff --git a/remote.h b/remote.h > index 7c86c529b6..62265346d3 100644 > --- a/remote.h > +++ b/remote.h > @@ -368,6 +368,10 @@ void collect_upstream_from_remote(struct repository *repo, > struct remote *remote, > const char *refname); > > +/* fills tracked with the refname of every local branch's upstream on remote */ > +void branches_tracking_remote(struct repository *repo, struct remote *remote, > + struct string_list *tracked); > + > /* list of the remote in a group as configured */ > struct remote_group_data { > const char *name; I stopped reading here, but it made a bit of sense up to this point. I wonder if the remote.h comment on the new function could be more clear, but :shrug: -- D. Ben Knoble