From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy2-f43.google.com (mail-dy2-f43.google.com [74.125.229.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 51FCE41B8E6 for ; Fri, 2 Oct 2026 07:13:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.229.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790925213; cv=none; b=bhL8xxZis0qUGEPlVNJNQcO9LnEvE4uS1kZ1fV96wLOO/NHhv3sbA4XL+ktWdNooq1HWEBAphs/JY0Rjm91coUw/7sKAmLh/hlMGNtQP4hEwyBICtGhOPfM5sFyZ7fA1ioVGqdP4F+pmgN6DWS/3RvDFiL1Q2+xk8M+ZZlXJ9JQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790925213; c=relaxed/simple; bh=u1Z/zX+e6NWvt4IOCIVzKyw45OZupocQvN1Q0HPAu4c=; h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type: MIME-Version:To:Cc; b=QAODXo5adxlbajylrWZkiIteGYxWx9Oz0QOGB6osI3Uft4/hidT2AXx5AfZRMM37M0aJ4XGGfJmW4qwmWvjnyiYzE6NibtqQYr5q7u3/MXo0hwetMUaMyZtlT9iGVLJthBWN7I8EwptXzd7nGjB3nU0pYtAl4IQXExBCwMqvF/s= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Tvev9gQr; arc=none smtp.client-ip=74.125.229.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Tvev9gQr" Received: by mail-dy2-f43.google.com with SMTP id 5a478bee46e88-33e46a15703so6829821eec.0 for ; Fri, 02 Oct 2026 00:13:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790925203; x=1791530003; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:content-type:fcc :subject:date:from:references:in-reply-to:message-id:from:to:cc :subject:date:message-id:reply-to:content-type; bh=8WcV+oBJ8b55vGV2a8RS5BTy1zMlLal07hlBrzo2qMo=; b=Tvev9gQrTAU5adsdH34UDRcJb6IopJ7ThWotbQqjXiwhTJHWVmLK7S7LdPnswgrxF2 uFmvIoVZlxprzHfXtgE6oNHow/OLhdNhf8yphWUy9jAF69SmC5CAHuMVGjF4vWxWOVm5 hKlHLdzU6dYBSzSuUpqAruMeji0A8Duinwvg+dg4GszSSyv5rem+VaI1p0Eyc0ZPYRzG w9Lf6HvVTmXfm9MN7HFZVB/YPsb5PzfzqXH5JclSZCrizZwKgIqUDtIFn+FX3DtiEacK NrmC7+PCxt9Gaf4fKkY2epesTiXxIsMEKkpLVE0EQ298hathrn/vOsKX9E8jp7OBIFdO zeTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790925203; x=1791530003; h=cc:to:mime-version:content-transfer-encoding:content-type:fcc :subject:date:from:references:in-reply-to:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=8WcV+oBJ8b55vGV2a8RS5BTy1zMlLal07hlBrzo2qMo=; b=CAUWOzihoXHfSN4V+5QCzBQQgfHESbEiigGj95bF+TYJk3JqSBS38W1WJcR2Zwm8SY SCFtmEVmBttJGE+iAKHC53oo6OS9vhLpN77DS8LMkXoj67vYy2y+2eZQRs7izA3/7Nr9 5R+UgovHQkslh6ctv99WTmwHO9PG42hHwfNWmlO2gYMc2nrFSX542iiq24lwnbyt38X5 ihmnyrB3lXGhgRPaLMyFs18XNbmZDCTCTS0i2YfQ8C0BCnPzRW7DB/577CKcT59bAnRD cwG2msnnbQGsa8uNkNEQlKYf4osQ1QGQGakSA127eGI/pwDw3h7FB8TES+Au8RD5iJBl y8oQ== X-Gm-Message-State: AFuF++ni01GmYQdGnMmfwgBg1A5YmwnN7p+e0y7e0OkLQaAGtACyqUj0 Tn5kBW+fBq5qeqCodqoWe5c7GzGisXr6ILM1anhryFxl7DVo7lZdgqFquRk8SA== X-Gm-Gg: AYBFou0dptDgXkYxe+p3AvFlcwJ7qBdeacRk/qt0sDV+JKPKs0CzQzXYc1pPg/Ap/tI s2saKDiCCa8J6sol6dlRJo9+6HQ2DJeZuhczmVFJv9AEZrcM+0VeE04Cza2P46BId4Jp2SO8sUZ IENiLApksUY2+b2B1fZrcAc6JncjO7iFTtW//HnDr72/E37kpOPIQ3WihHn7gnYbXZmikOH8zVX qF296Lho0RMgR/jqnc6hzQ3ynxb/H7oD8M71qEq1YN7fn5Y348l3gjLST6iSuGughiz0AgxF3Np JA/F+i5oo/vNDrKt/+vcfTwTunxP1xsOEy5EAO1bp+glWAqbHrsd/Zdi0l0TfqFT9xk5peWM93i e4Y8JPmDEocrbAXQE1ekUM9AyrdHCrNkOmLmOI7++PJKqws5QuIpdcx86653JP4B/aoQ8Nf6CHi /yKtEyErcdYWj2dPHVeWsbb0c4i3FW1JWAQplU6HDTTMMP2POdVuGqSny/gu2+nkCKNLR1pPFQH g== X-Received: by 2002:a05:7300:8252:b0:342:4d8:ccd9 with SMTP id 5a478bee46e88-34f17aec58fmr1952696eec.27.1790925202992; Fri, 02 Oct 2026 00:13:22 -0700 (PDT) Received: from [127.0.0.1] ([172.215.209.71]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-35105835bf8sm2120016eec.9.2026.10.02.00.13.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Oct 2026 00:13:22 -0700 (PDT) Message-Id: In-Reply-To: References: From: "Harald Nordgren via GitGitGadget" Date: Fri, 02 Oct 2026 07:13:16 +0000 Subject: [PATCH v5 2/4] fetch: infer branches to fetch from a refmap-only remote Fcc: Sent Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Phillip Wood , "D. Ben Knoble" , Harald Nordgren , Harald Nordgren 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. 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 | 52 ++++++++++++-- remote.c | 29 ++++++++ remote.h | 3 + t/meson.build | 1 + t/t5586-fetch-refmap.sh | 119 +++++++++++++++++++++++++++++++ 7 files changed, 205 insertions(+), 10 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 538914bc6e..315daf97d7 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 +there is nothing to fetch, neither on the command line nor from +`remote..fetch`, Git infers what to fetch from the local branches +whose `@{upstream}` is on that remote. `-t`:: `--tags`:: diff --git a/builtin/fetch.c b/builtin/fetch.c index 7651b41139..7ef2776f17 100644 --- a/builtin/fetch.c +++ b/builtin/fetch.c @@ -511,6 +511,8 @@ static struct ref *get_ref_map(struct remote *remote, struct ref **tail = &ref_map; struct refspec *effective_refmap = refmap.nr ? &refmap : remote ? &remote->refmap : NULL; + struct refspec inferred_rs; + int inferred_branches = 0; /* opportunistically-updated references: */ struct ref *orefs = NULL, **oref_tail = &orefs; @@ -518,15 +520,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(remote, &tracked); + for_each_string_list_item(item, &tracked) + refspec_append(&inferred_rs, item->string); + string_list_clear(&tracked, 0); + + rs = &inferred_rs; + inferred_branches = 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, + inferred_branches); if (rs->items[i].dst && rs->items[i].dst[0]) *autotags = 1; } @@ -561,6 +580,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 (inferred_branches) { + /* Already fully handled above. */ } else if (effective_refmap && effective_refmap->nr) { die("--refmap option is only meaningful with command-line refspec(s)"); } else { @@ -656,6 +677,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; } @@ -1960,15 +1982,30 @@ static int do_fetch(struct transport *transport, refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes); } else { struct branch *branch = branch_get(NULL); - - if (transport->remote->fetch.nr) { + int tracks_this_remote = branch && branch_has_merge_config(branch) && + !strcmp(branch->remote_name, transport->remote->name); + struct refspec *effective_refmap = refmap.nr ? &refmap : + &transport->remote->refmap; + int inferred_branches = !transport->remote->fetch.nr && + effective_refmap->nr; + + if (inferred_branches) { + struct string_list tracked = STRING_LIST_INIT_DUP; + struct string_list_item *item; + + branches_tracking_remote(transport->remote, &tracked); + for_each_string_list_item(item, &tracked) + strvec_push(&transport_ls_refs_options.ref_prefixes, + item->string); + string_list_clear(&tracked, 0); + } else if (transport->remote->fetch.nr) { refspec_ref_prefixes(&transport->remote->fetch, &transport_ls_refs_options.ref_prefixes); - if (follow_remote_head != FOLLOW_REMOTE_NEVER) - do_set_head = 1; } - if (branch && branch_has_merge_config(branch) && - !strcmp(branch->remote_name, transport->remote->name)) { + if ((transport->remote->fetch.nr || inferred_branches) && + follow_remote_head != FOLLOW_REMOTE_NEVER) + do_set_head = 1; + if (tracks_this_remote) { int i; for (i = 0; i < branch->merge_nr; i++) { strvec_push(&transport_ls_refs_options.ref_prefixes, @@ -2009,6 +2046,7 @@ static int do_fetch(struct transport *transport, ref_map = get_ref_map(transport->remote, remote_refs, rs, tags, &autotags); + if (!update_head_ok) check_not_current_branch(ref_map); diff --git a/remote.c b/remote.c index 017cd9d13e..a5deb9b929 100644 --- a/remote.c +++ b/remote.c @@ -1884,6 +1884,35 @@ int branch_merge_matches(struct branch *branch, return refname_match(branch->merge[i]->src, refname); } +struct branches_tracking_remote_cb_data { + 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; + struct branch *branch; + + branch = branch_get(ref->name); + if (!branch_has_merge_config(branch) || + strcmp(branch->remote_name, data->remote->name)) + return 0; + + for (int i = 0; i < branch->merge_nr; i++) + string_list_insert(data->tracked, branch->merge[i]->src); + + return 0; +} + +void branches_tracking_remote(struct remote *remote, struct string_list *tracked) +{ + struct branches_tracking_remote_cb_data data = { remote, tracked }; + + refs_for_each_branch_ref(get_main_ref_store(the_repository), + 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 ac485a584d..da6c3ef53a 100644 --- a/remote.h +++ b/remote.h @@ -359,6 +359,9 @@ int branch_has_merge_config(struct branch *branch); int branch_merge_matches(struct branch *, int n, const char *); +/* fills tracked with the refname of every local branch's upstream on remote */ +void branches_tracking_remote(struct remote *remote, struct string_list *tracked); + /* list of the remote in a group as configured */ struct remote_group_data { const char *name; diff --git a/t/meson.build b/t/meson.build index 3ca7b27104..a1c7465c2b 100644 --- a/t/meson.build +++ b/t/meson.build @@ -728,6 +728,7 @@ integration_tests = [ 't5582-fetch-negative-refspec.sh', 't5583-push-branches.sh', 't5584-http-429-retry.sh', + 't5586-fetch-refmap.sh', 't5600-clone-fail-cleanup.sh', 't5601-clone.sh', 't5602-clone-remote-exec.sh', diff --git a/t/t5586-fetch-refmap.sh b/t/t5586-fetch-refmap.sh new file mode 100755 index 0000000000..b81fc48cbe --- /dev/null +++ b/t/t5586-fetch-refmap.sh @@ -0,0 +1,119 @@ +#!/bin/sh + +test_description='"git fetch" with a remote..refmap but no remote..fetch + +When a remote has a refmap configured but no fetch refspec, a +refspec-less fetch infers what to fetch from the local branches whose +@{upstream} is on that remote. +' + +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main +export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME + +. ./test-lib.sh + +test_expect_success 'setup' ' + test_commit main-1 && + test_commit main-2 && + git checkout -b side main-1 && + test_commit side-1 && + git checkout -b next main-1 && + test_commit next-1 && + git checkout main +' + +test_expect_success 'clone shallow and single-branch, then add a second remote' ' + git clone --no-local --depth=1 --branch main --single-branch . client && + ( + cd client && + git remote add upstream .. && + test_might_fail git config unset remote.upstream.fetch && + git config remote.upstream.refmap \ + "+refs/heads/*:refs/remotes/upstream/*" + ) +' + +test_expect_success 'a bare fetch needs nothing until a branch is tracked' ' + ( + cd client && + git fetch upstream && + git for-each-ref --format="%(refname)" refs/remotes/upstream >actual && + test_must_be_empty actual + ) +' + +test_expect_success 'an explicit one-time fetch lets a branch be tracked' ' + ( + cd client && + git fetch upstream main && + git branch --set-upstream-to=upstream/main && + test_cmp_config upstream branch.main.remote && + test_cmp_config refs/heads/main branch.main.merge + ) +' + +test_expect_success 'a branch checked out from a one-time fetch is kept updated by later plain fetches' ' + ( + cd client && + git fetch upstream side:refs/remotes/upstream/side && + git branch side-topic upstream/side + ) && + git checkout side && + test_commit side-2 && + git checkout main && + ( + cd client && + git fetch upstream && + git for-each-ref --format="%(refname)" refs/remotes/upstream >actual && + cat >expect <<-\EOF && + refs/remotes/upstream/HEAD + refs/remotes/upstream/main + refs/remotes/upstream/side + EOF + test_cmp expect actual && + git rev-parse refs/remotes/upstream/side >actual-oid && + git -C .. rev-parse side >expect-oid && + test_cmp expect-oid actual-oid + ) +' + +test_expect_success 'a second branch tracking the same upstream branch does not fetch it twice' ' + ( + cd client && + git branch side-topic-2 upstream/side && + git fetch upstream && + git for-each-ref --format="%(refname)" refs/remotes/upstream >actual && + cat >expect <<-\EOF && + refs/remotes/upstream/HEAD + refs/remotes/upstream/main + refs/remotes/upstream/side + EOF + test_cmp expect actual + ) +' + +test_expect_success 'a branch tracking a different remote is not fetched from upstream' ' + ( + cd client && + git remote add other .. && + git fetch other next:refs/remotes/other/next && + git branch next-topic other/next && + git fetch upstream && + git for-each-ref --format="%(refname)" refs/remotes/upstream >actual && + cat >expect <<-\EOF && + refs/remotes/upstream/HEAD + refs/remotes/upstream/main + refs/remotes/upstream/side + EOF + test_cmp expect actual + ) +' + +test_expect_success 'git remote show does not choke on a refmap-only remote' ' + ( + cd client && + git remote show upstream + ) +' + +test_done -- gitgitgadget