Thread View: linux.kernel.git
2 messages
2 total messages
Started by "Harald Nordgren via GitGitGadge...
Sun, 22 Nov 2026 11:57
[PATCH v15 1/7] fetch: do not apply --refmap to a refspec with a destination
Author: "Harald Nordgren via GitGitGadge...
Date: Sun, 22 Nov 2026 11:57
Date: Sun, 22 Nov 2026 11:57
123 lines
15520 bytes
15520 bytes
From: Harald Nordgren <haraldnordgren@gmail.com>
"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.
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
Documentation/fetch-options.adoc | 4 +++-
builtin/fetch.c | 31 ++++++++++++++++++++++++-------
t/t5510-fetch.sh | 12 ++++++++++++
3 files changed, 39 insertions(+), 8 deletions(-)
diff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc
index 47dea1de8e..327169cee7 100644
--- a/Documentation/fetch-options.adoc
+++ b/Documentation/fetch-options.adoc
@@ -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
diff --git a/builtin/fetch.c b/builtin/fetch.c
index c646bc6698..0ee252e353 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -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 {
diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh
index 5b27f1d5bb..4b40ec965a 100755
--- a/t/t5510-fetch.sh
+++ b/t/t5510-fetch.sh
@@ -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 &&
(
--
gitgitgadget
Re: [PATCH v15 1/7] fetch: do not apply --refmap to a refspec with a destination
Author: Junio C Hamano
Date: Tue, 24 Nov 2026 14:44
Date: Tue, 24 Nov 2026 14:44
78 lines
16132 bytes
16132 bytes
"Harald Nordgren via GitGitGadget" <gitgitgadget@gmail.com> writes:
> From: Harald Nordgren <haraldnordgren@gmail.com>
>
> "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.
> diff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc
> index 47dea1de8e..327169cee7 100644
> --- a/Documentation/fetch-options.adoc
> +++ b/Documentation/fetch-options.adoc
> @@ -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.
> +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);
> +}
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.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads