From: Harald Nordgren <hara…@gmail.com> Add a per-remote config variable, remote.<name>.fetchRefmap, that provides the default value for --refmap the same way remote.<name>.fetch already provides the default refspecs to fetch. --refmap on the command line overrides it. The refmap only says where fetched refs are stored. Configuring it does not change what a fetch without refspecs fetches. When remote.<name>.fetch is also configured, it still decides what to fetch. Its entries without a destination are stored through the refmap, and an entry with its own destination is stored only there. This lets a remote name only a few branches and store them all through one refmap: [remote "cabal"] fetch = master fetch = next fetch = seen fetchRefmap = +refs/heads/*:refs/remotes/cabal/* Tags are followed for refs stored through the refmap, the same as for refs stored through remote.<name>.fetch. With --prefetch the refmap's destinations move under refs/prefetch/ in the same way, and an empty refmap stays empty. Looking up the remote-tracking branch for a remote branch, as @{upstream} and "git branch --track" do, falls back to the refmap when remote.<name>.fetch has no match, so that these work for such branches too. A branch excluded by a negative refspec in remote.<name>.fetch stays excluded. Signed-off-by: Harald Nordgren <hara…@gmail.com> --- Documentation/config/remote.adoc | 9 ++ Documentation/fetch-options.adoc | 3 + builtin/fetch.c | 78 ++++++++----- remote.c | 20 +++- remote.h | 6 + t/t5510-fetch.sh | 181 +++++++++++++++++++++++++++++++ 6 files changed, 269 insertions(+), 28 deletions(-) diff --git a/Documentation/config/remote.adoc b/Documentation/config/remote.adoc index 3a20d0f752..c088d6257d 100644 --- a/Documentation/config/remote.adoc +++ b/Documentation/config/remote.adoc @@ -33,6 +33,15 @@ remote.<name>.fetch:: The default set of "refspec" for linkgit:git-fetch[1]. See linkgit:git-fetch[1]. +remote.<name>.fetchRefmap:: + The default value of the `--refmap` option, used to map refs being + fetched to remote-tracking refs to store, the same way + `remote.<name>.fetch` does for an explicit refspec. + `remote.<name>.fetch`, if also configured, still decides what is + fetched, and an entry there with its own destination is stored + only at that destination. See the `--refmap` entry in + linkgit:git-fetch[1]. + remote.<name>.push:: The default set of "refspec" for linkgit:git-push[1]. See linkgit:git-push[1]. diff --git a/Documentation/fetch-options.adoc b/Documentation/fetch-options.adoc index e4b9e47806..aed38790bb 100644 --- a/Documentation/fetch-options.adoc +++ b/Documentation/fetch-options.adoc @@ -251,6 +251,9 @@ endif::git-pull[] refspecs and rely entirely on the refspecs supplied as command-line arguments. See section on "Configured Remote-tracking Branches" for details. ++ +`remote.<name>.fetchRefmap` provides the default value for this option, the +same way `remote.<name>.fetch` provides the default refspecs to fetch. `-t`:: `--tags`:: diff --git a/builtin/fetch.c b/builtin/fetch.c index fe026aca1f..53957e6b18 100644 --- a/builtin/fetch.c +++ b/builtin/fetch.c @@ -487,6 +487,29 @@ static void find_non_local_tags(const struct ref *refs, oidset_clear(&fetch_oids); } +static void move_dst_to_prefetch(struct refspec_item *item) +{ + struct strbuf new_dst = STRBUF_INIT; + char *old_dst = item->dst; + const char *sub = NULL; + + strbuf_addstr(&new_dst, ref_namespace[NAMESPACE_PREFETCH].ref); + + /* + * If old_dst starts with "refs/", then place + * sub after that prefix. Otherwise, start at + * the beginning of the string. + */ + if (!skip_prefix(old_dst, "refs/", &sub)) + sub = old_dst; + strbuf_addstr(&new_dst, sub); + + item->dst = strbuf_detach(&new_dst, NULL); + item->force = 1; + + free(old_dst); +} + static void filter_prefetch_refspec(struct refspec *rs) { int i; @@ -495,10 +518,6 @@ static void filter_prefetch_refspec(struct refspec *rs) return; for (i = 0; i < rs->nr; i++) { - struct strbuf new_dst = STRBUF_INIT; - char *old_dst; - const char *sub = NULL; - if (rs->items[i].negative) continue; if (!rs->items[i].dst || @@ -516,23 +535,18 @@ static void filter_prefetch_refspec(struct refspec *rs) continue; } - old_dst = rs->items[i].dst; - strbuf_addstr(&new_dst, ref_namespace[NAMESPACE_PREFETCH].ref); - - /* - * If old_dst starts with "refs/", then place - * sub after that prefix. Otherwise, start at - * the beginning of the string. - */ - if (!skip_prefix(old_dst, "refs/", &sub)) - sub = old_dst; - strbuf_addstr(&new_dst, sub); + move_dst_to_prefetch(&rs->items[i]); + } +} - rs->items[i].dst = strbuf_detach(&new_dst, NULL); - rs->items[i].force = 1; +static void filter_prefetch_refmap(struct refspec *refmap) +{ + if (!prefetch) + return; - free(old_dst); - } + for (int i = 0; i < refmap->nr; i++) + if (refmap->items[i].dst && refmap->items[i].dst[0]) + move_dst_to_prefetch(&refmap->items[i]); } static struct ref *get_ref_map(struct remote *remote, @@ -544,6 +558,12 @@ static struct ref *get_ref_map(struct remote *remote, struct ref *rm; struct ref *ref_map = NULL; struct ref **tail = &ref_map; + /* + * The --refmap command line option, if given, takes precedence + * over remote.<name>.fetchRefmap. + */ + struct refspec *effective_refmap = + refmap.nr ? &refmap : remote ? &remote->refmap : NULL; /* opportunistically-updated references: */ struct ref *orefs = NULL, **oref_tail = &orefs; @@ -554,6 +574,8 @@ static struct ref *get_ref_map(struct remote *remote, filter_prefetch_refspec(rs); if (remote) filter_prefetch_refspec(&remote->fetch); + if (effective_refmap) + filter_prefetch_refmap(effective_refmap); if (rs->nr) { for (i = 0; i < rs->nr; i++) { @@ -586,10 +608,11 @@ static struct ref *get_ref_map(struct remote *remote, * opportunistic entries with FETCH_HEAD_IGNORE. * * A refspec that names its own destination is not - * mapped through --refmap. + * mapped through the refmap. */ - if (refmap.nr) - map_refs_without_dst(ref_map, &refmap, &oref_tail); + if (effective_refmap && effective_refmap->nr) + map_refs_without_dst(ref_map, effective_refmap, + &oref_tail); else for (i = 0; i < remote->fetch.nr; i++) get_fetch_map(ref_map, &remote->fetch.items[i], @@ -631,12 +654,13 @@ static struct ref *get_ref_map(struct remote *remote, tail = &ref_map->next; } - if (refmap.nr) { + if (refmap.nr) drop_destinations(ref_map); - map_refs_without_dst(ref_map, &refmap, &oref_tail); - if (orefs) - *autotags = 1; - } + if (effective_refmap && effective_refmap->nr) + map_refs_without_dst(ref_map, effective_refmap, + &oref_tail); + if (orefs) + *autotags = 1; } if (tags == TAGS_SET) { diff --git a/remote.c b/remote.c index 2b7d26b844..d3bb874ed5 100644 --- a/remote.c +++ b/remote.c @@ -153,6 +153,7 @@ static struct remote *make_remote(struct remote_state *remote_state, ret->name = xstrndup(name, len); refspec_init_push(&ret->push, the_hash_algo); refspec_init_fetch(&ret->fetch, the_hash_algo); + refspec_init_fetch(&ret->refmap, the_hash_algo); string_list_init_dup(&ret->server_options); string_list_init_dup(&ret->negotiation_restrict); string_list_init_dup(&ret->negotiation_include); @@ -177,6 +178,7 @@ static void remote_clear(struct remote *remote) refspec_clear(&remote->push); refspec_clear(&remote->fetch); + refspec_clear(&remote->refmap); free((char *)remote->receivepack); free((char *)remote->uploadpack); @@ -540,6 +542,12 @@ static int handle_config(const char *key, const char *value, return -1; refspec_append(&remote->fetch, v); free(v); + } else if (!strcmp(subkey, "fetchrefmap")) { + char *v; + if (git_config_string(&v, key, value)) + return -1; + refspec_append(&remote->refmap, v); + free(v); } else if (!strcmp(subkey, "receivepack")) { char *v; if (git_config_string(&v, key, value)) @@ -1000,7 +1008,17 @@ void ref_push_report_free(struct ref_push_report *report) int remote_find_tracking(struct remote *remote, struct refspec_item *refspec) { - return refspec_find_match(&remote->fetch, refspec); + char **result = refspec->src ? &refspec->dst : &refspec->src; + + if (remote->fetch.nr && !refspec_find_match(&remote->fetch, refspec)) + return 0; + if (refspec_find_match(&remote->refmap, refspec)) + return -1; + if (refname_matches_negative_refspec_item(refspec->src, &remote->fetch)) { + FREE_AND_NULL(*result); + return -1; + } + return 0; } static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen, diff --git a/remote.h b/remote.h index cca02033b9..ac485a584d 100644 --- a/remote.h +++ b/remote.h @@ -90,6 +90,12 @@ struct remote { struct refspec fetch; + /* + * How to map refs fetched without an explicit destination into our + * own namespace, the same as the --refmap command line option. + */ + struct refspec refmap; + /* * The setting for whether to fetch tags (as a separate rule from the * configured refspecs); diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh index b21fafc5f7..4e01201222 100755 --- a/t/t5510-fetch.sh +++ b/t/t5510-fetch.sh @@ -995,6 +995,187 @@ test_expect_success '--refmap is not applied to a refspec with a destination' ' ) ' +test_expect_success 'remote.*.fetchRefmap acts like --refmap on the command line' ' + test_when_finished "git -C three config --unset remote.origin.fetchRefmap" && + git branch -f side && + git -C three config remote.origin.fetchRefmap \ + "refs/heads/*:refs/remotes/other/*" && + ( + cd three && + git update-ref refs/remotes/origin/main base-origin-main && + o=$(git rev-parse --verify refs/remotes/origin/main) && + git fetch origin main && + n=$(git rev-parse --verify refs/remotes/origin/main) && + test "$o" = "$n" && + test_must_fail git rev-parse --verify refs/remotes/origin/side && + git rev-parse --verify refs/remotes/other/main + ) +' + +check_fetched_refs () { + git for-each-ref --format="%(refname)" refs/remotes/ >actual && + cat >expect && + test_cmp expect actual +} + +setup_refmap_repos () { + test_when_finished "rm -fr fetch-refmap-upstream downstream" && + git init -b "$1" fetch-refmap-upstream && + test_commit -C fetch-refmap-upstream base && + shift && + for branch + do + git -C fetch-refmap-upstream branch "$branch" || return 1 + done && + git init downstream && + ( + cd downstream && + git remote add cabal ../fetch-refmap-upstream && + git config --unset-all remote.cabal.fetch && + git config remote.cabal.fetchRefmap \ + "+refs/heads/*:refs/remotes/cabal/*" + ) +} + +test_expect_success 'remote.<name>.fetchRefmap without tracking (baseline)' ' + setup_refmap_repos main other && + ( + cd downstream && + + # Nothing tracked, nothing fetched, no error. + git fetch cabal && + check_fetched_refs <<-\EOF && + EOF + + # An ordinary remote.<name>.fetch still decides what is + # fetched and where it is stored. + git config remote.cabal.fetch "+refs/heads/*:refs/remotes/cabal/*" && + git fetch cabal && + check_fetched_refs <<-\EOF + refs/remotes/cabal/HEAD + refs/remotes/cabal/main + refs/remotes/cabal/other + EOF + ) +' + +test_expect_success 'remote.<name>.fetchRefmap stores branches named in remote.<name>.fetch' ' + setup_refmap_repos master next seen other && + ( + cd downstream && + git config --add remote.cabal.fetch master && + git config --add remote.cabal.fetch next && + git config --add remote.cabal.fetch seen && + git fetch cabal && + check_fetched_refs <<-\EOF && + refs/remotes/cabal/HEAD + refs/remotes/cabal/master + refs/remotes/cabal/next + refs/remotes/cabal/seen + EOF + git rev-parse --verify refs/tags/base && + git branch --track topic cabal/next && + echo refs/remotes/cabal/next >expect && + git rev-parse --symbolic-full-name topic@{upstream} >actual && + test_cmp expect actual + ) +' + +test_expect_success '--refmap on the command line overrides remote.<name>.fetchRefmap' ' + setup_refmap_repos master next && + ( + cd downstream && + git config --add remote.cabal.fetch master && + git fetch --refmap="refs/heads/*:refs/remotes/other/*" cabal && + check_fetched_refs <<-\EOF + refs/remotes/other/master + EOF + ) +' + +test_expect_success '--prefetch keeps an empty --refmap empty' ' + setup_refmap_repos master && + ( + cd downstream && + git config --add remote.cabal.fetch \ + "+refs/heads/*:refs/remotes/cabal/*" && + git fetch --prefetch --refmap= cabal && + git for-each-ref refs/prefetch refs/remotes >actual && + test_must_be_empty actual + ) +' + +test_expect_success '--prefetch keeps an empty remote.<name>.fetchRefmap empty' ' + setup_refmap_repos master && + ( + cd downstream && + git config --add remote.cabal.fetch \ + "+refs/heads/*:refs/remotes/cabal/*" && + git config remote.cabal.fetchRefmap "" && + git fetch --prefetch cabal master:refs/remotes/custom/master && + git for-each-ref --format="%(refname)" refs/prefetch refs/remotes >actual && + echo refs/prefetch/remotes/custom/master >expect && + test_cmp expect actual + ) +' + +test_expect_success 'remote.<name>.fetchRefmap is not applied to a remote.<name>.fetch entry with a destination' ' + setup_refmap_repos master next && + ( + cd downstream && + git config --add remote.cabal.fetch \ + "+refs/heads/master:refs/remotes/custom/master" && + git config --add remote.cabal.fetch next && + git fetch cabal && + check_fetched_refs <<-\EOF + refs/remotes/cabal/next + refs/remotes/custom/master + EOF + ) +' + +test_expect_success 'remote.<name>.fetchRefmap is not applied to a command-line refspec with a destination' ' + setup_refmap_repos master next && + ( + cd downstream && + git fetch cabal master:refs/remotes/custom/master next && + check_fetched_refs <<-\EOF + refs/remotes/cabal/next + refs/remotes/custom/master + EOF + ) +' + +test_expect_success 'remote.<name>.fetchRefmap tracks a branch outside remote.<name>.fetch' ' + setup_refmap_repos main other && + ( + cd downstream && + git config --add remote.cabal.fetch \ + "+refs/heads/main:refs/remotes/cabal/main" && + git fetch cabal other && + git branch --track topic cabal/other && + test_cmp_config cabal branch.topic.remote && + test_cmp_config refs/heads/other branch.topic.merge && + echo refs/remotes/cabal/other >expect && + git rev-parse --symbolic-full-name topic@{upstream} >actual && + test_cmp expect actual + ) +' + +test_expect_success 'remote.<name>.fetchRefmap does not track a branch excluded by remote.<name>.fetch' ' + setup_refmap_repos main secret && + ( + cd downstream && + git config --add remote.cabal.fetch \ + "+refs/heads/*:refs/remotes/cabal/*" && + git config --add remote.cabal.fetch "^refs/heads/secret" && + git fetch cabal secret && + git rev-parse --verify refs/remotes/cabal/secret && + test_must_fail git branch --track topic cabal/secret 2>err && + test_grep "not a branch" err + ) +' + test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' ' git branch -f side && (
-- gitgitgadget