From: Harald Nordgren <hara…@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 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. Signed-off-by: Harald Nordgren <hara…@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..54c5c8f671 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 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 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..38185e192d 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