Article View: linux.kernel.git
Article #3836[PATCH v16 4/7] fetch: extract collect_upstream_from_remote() helper
From: "Harald Nordgren via GitGitGadge...
Date: Wed, 25 Nov 2026 19:06
Date: Wed, 25 Nov 2026 19:06
112 lines
14723 bytes
14723 bytes
From: Harald Nordgren <haraldnordgren@gmail.com>
When a fetch has no refspec to work from, we decide what to fetch from
two sources: the remote's configured fetch refspec, and the current
branch's upstream, if it lives on that remote. For example, after
git clone --single-branch <url>
git switch -c work
git push -u origin work
the remote's refspec only covers "main", but a refspec-less "git fetch
origin" still fetches "work" into FETCH_HEAD, so that a plain "git
pull" finds the branch to merge. See 49ca2fba39 (fetch: add
branch.*.merge to default ref-prefix extension, 2022-09-08).
That "does this branch build on this remote" check was inlined for
just the current branch. Move it into its own function,
collect_upstream_from_remote(), so the same logic can be reused for
branches other than just the current one.
Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>
---
builtin/fetch.c | 18 ++++++++----------
remote.c | 15 +++++++++++++++
remote.h | 9 +++++++++
3 files changed, 32 insertions(+), 10 deletions(-)
diff --git a/builtin/fetch.c b/builtin/fetch.c
index d9c0e8c2f7..0f91bccf1e 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -2032,8 +2032,6 @@ static int do_fetch(struct transport *transport,
if (rs->nr) {
refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);
} else {
- struct branch *branch = branch_get(NULL);
-
if (transport->remote->fetch.nr) {
refspec_ref_prefixes(&transport->remote->fetch,
&transport_ls_refs_options.ref_prefixes);
@@ -2048,14 +2046,14 @@ static int do_fetch(struct transport *transport,
else
follow_remote_head = BUILTIN_FOLLOW_REMOTE_HEAD_DFLT;
}
- if (branch && branch_has_merge_config(branch) &&
- !strcmp(branch->remote_name, transport->remote->name)) {
- int i;
- for (i = 0; i < branch->merge_nr; i++) {
- strvec_push(&transport_ls_refs_options.ref_prefixes,
- branch->merge[i]->src);
- }
- }
+
+ /*
+ * 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, transport->remote,
+ NULL, &transport_ls_refs_options.ref_prefixes);
/*
* If there are no refs specified to fetch, then we just
diff --git a/remote.c b/remote.c
index d3bb874ed5..92fe44c026 100644
--- a/remote.c
+++ b/remote.c
@@ -1971,6 +1971,21 @@ int branch_merge_matches(struct branch *branch,
return refname_match(branch->merge[i]->src, refname);
}
+void collect_upstream_from_remote(struct repository *repo,
+ const struct remote *remote,
+ const char *refname,
+ struct strvec *tracked)
+{
+ struct branch *branch = repo_branch_get(repo, refname);
+
+ if (!branch_has_merge_config(branch) ||
+ strcmp(branch->remote_name, remote->name))
+ return;
+
+ for (int i = 0; i < branch->merge_nr; i++)
+ strvec_push(tracked, branch->merge[i]->src);
+}
+
__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..003c8fd180 100644
--- a/remote.h
+++ b/remote.h
@@ -359,6 +359,15 @@ int branch_has_merge_config(struct branch *branch);
int branch_merge_matches(struct branch *, int n, const char *);
+/*
+ * If refname's branch builds on remote, add its upstream on remote to
+ * tracked. A NULL refname means the current branch.
+ */
+void collect_upstream_from_remote(struct repository *repo,
+ const struct remote *remote,
+ const char *refname,
+ struct strvec *tracked);
+
/* list of the remote in a group as configured */
struct remote_group_data {
const char *name;
--
gitgitgadget
Technical Details
Message-ID:
<f019dd4529ce4cbb448a14eee72379f2663e0f55.1795633584.git.gitgitgadget@gmail.com>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!git+bounces-76371-linux-kernel-git=dewy.ruReferences:
<pull.2412.git.git.1789829246437.gitgitgadget@gmail.com> <pull.2412.v16.git.git.1795633584.gitgitgadget@gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!git+bounces-76371-linux-kernel-git=dewy.ru
From: gitgitgadget@gmail.com ("Harald Nordgren via GitGitGadget")
Newsgroups: linux.kernel.git
Subject: [PATCH v16 4/7] fetch: extract collect_upstream_from_remote() helper
Date: Wed, 25 Nov 2026 19:06:21 +0000
Organization: Usenet Network
Message-ID: <f019dd4529ce4cbb448a14eee72379f2663e0f55.1795633584.git.gitgitgadget@gmail.com>
References: <pull.2412.git.git.1789829246437.gitgitgadget@gmail.com>
<pull.2412.v16.git.git.1795633584.gitgitgadget@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
logging-data="281348"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:KcRoPjNGo9mmUqCvMwPx116tm1U= sha256:+H0MqdXkJBtfNX9IphMq9JeDZM0fy2Gy2K4VXO88bh8=
sha1:l3QLN6o06GTQ9c9GWjQ+jpbv3cM= sha256:6CYXvFnZaN6YSyoINHmD12iM8C71lPGT8Ek+dlOsh4g=
List-ID: <git.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
by mail.dewy.ru (Postfix) with ESMTP id 5BD00125E95; Wed, 25 Nov 2026 22:06:53 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -1.949
X-Spam-Status: No, score=-1.949 tagged_above=-9999 required=6.31
tests=[DCC_CHECK=1.1, DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1,
DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
header.d=gmail.com
X-Received: from mail.dewy.ru ([127.0.0.1])
by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id xy_cd5hWzTCy;
Wed, 25 Nov 2026 22:06:49 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=git+bounces-76371-linux-kernel-git=dewy.ru@vger.kernel.org; receiver=<UNKNOWN>
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=gmail.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
by mail.dewy.ru (Postfix) with ESMTPS id 6CFE3125E98; Wed, 25 Nov 2026 22:06:37 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
by sea.lore.kernel.org (Postfix) with ESMTP id 4E2943F747; Wed, 25 Nov 2026 19:06:36 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
t=1795633596;
b=FFr1qO0YW9XOoru0wnoc6dzMJg4/9vGPr6rP1oebdzduA3uVvx7H2suaGtllvu666Zx7
227dFfYMg8pPVPb91CZd7YXWX27OAYp5Uc31nxLd6j5579QVEgfpNdMacXtE2XzlypDKZ
V73uYwj2Ov/MZOCR3au0l+8VY6rtBVDjd+NEard+9wfZTmVEPFjjOmxlyhow7/swvqGH6
0j9Kj92qzBqzrgDrjLTpjFYNhs68KkDg6vWd8UWx4n+9AqTpH28NBae+kiKh2Sj15Ug2o
RQpQ+gkOTg78AZb/OlRG9fkqxr4fKkj2yS6UUZ7WoFVqrmBxDV+aziwN8tN4a9KfxqA==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1795633596;
h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
X-Google-DKIM-Signature:X-Gm-Message-State:X-Gm-Gg:X-Received:
Received:Message-Id:In-Reply-To:References:From:Date:Subject:Fcc:
Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id:
List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner:
Precedence:MIME-Version:To:Cc;
bh=P5M0rbfF1vYO4G48sEGgR/KVoSAxkdFihHbb4El+od0=;
b=enqfQ8uqabHbPd3EG23iThX3mwc6l0cB5XWWhwjGR200FfjPiBptG1XJv3m4fokOlFTn
0DigQiu9+wpQY9FcD6lkVfZ3+6reMsTzPCbF0+A0MWSMoF8lZ7S5viSS8SmVRaIDGsdCT
W7pWeLFTxjUBPKNxpI7yh4g3ujskN4+8e7SEIY57fFV7ma5N/TTH4OrAgWdajNLjtoZrL
mbTmFkAyY7MzEePM240K4Vj4+9XFIJ4aw9BXbsnMn8sF4zpCYJqxSYP0i+tym9Q/1Se1a
p4VpW+HSzudbrtt04hdYGfdQI79pHSg7KPqmNcSd+zL42hG+qzqmN7p7g3GzymbjXjw==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org;
dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=jPkZ5kSq;
dmarc=pass header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.com;
arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id 24D89606A323;
Wed, 25 Nov 2026 19:06:36 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=jPkZ5kSq
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.160.42
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
t=1795633595;
b=fkQK3IPZhQQCBj4QFHMkxipZSyzlhXpRC5O/nZrbbsPMFFaVN2vBB/rrRhbCRsRjxdqg
o87xzf3H5wv6ovvp/S6QqUh8jD33ev7WBO/fJXOTecWdzSgUhYM2ax7s16xmZEzHvoazf
+PL6aOyCUYB/M5bXFSBKrMau5FDuigrdyOh7Qp01URpW8ZqVnZ+HicDQMEtJcSz8MiUZx
ZX6i31OAoOly6wcPXTKGTg2vAqpoQIeeA3FQ8EY7tZgLY4kez7R8+cCoRXAmhsJhzdcwI
0XRBNisP7QIaSJl00dfFcf/t0HsDHr5WLKcAfZ2aIUVYKp1mSnVAjttg+YhGZ9ZYo0w==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
a=rsa-sha256; c=relaxed/relaxed; t=1795633595;
h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
X-Gm-Message-State:X-Gm-Gg:X-Received:Received:Message-Id:In-Reply-To:
References:From:Date:Subject:Fcc:Content-Type:
Content-Transfer-Encoding:MIME-Version:To:Cc;
bh=P5M0rbfF1vYO4G48sEGgR/KVoSAxkdFihHbb4El+od0=;
b=Kgibqknv5z/RO3h1xtY3iSiF/ckgHqPCxSpqQvIMAALQCpDc8HPpyosIVQC2ei579Tnk
pXZ5lpQkEWdZhaqQ4My+5lmM1Jr+q+krwwoh6vj4CdwnCserZxEMyvxXpeKAoSjsFnr+H
neHg+hzFDy77JHFxQtKBgeXvSTniuHhQ3Y/lpxLmUkqs7EDR2H3v+UU5Atz3kYkjfQFQX
ESfcmnbeW9YCuSHF6iTaHw7WrPxKO3Sn5/0nZD9ZJMZfD8Aibx6OyueRro2EfRkIv6r+/
l5MFBz8NpxHzAir8U4N3mVmZb4O66fFkvu3KIWwMCeFRLHE7IX8fkwSZwUMowW4249Q==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org;
dmarc=pass header.from=gmail.com;
spf=pass smtp.mailfrom=gmail.com;
dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=jPkZ5kSq;
arc=none smtp.remote-ip=209.85.160.42
X-Received: from mail-oa1-f42.google.com (mail-oa1-f42.google.com [209.85.160.42])
(using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)
key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
(No client certificate requested)
by smtp.subspace.kernel.org (Postfix) with ESMTPS id E5555606A31E
for <git@vger.kernel.org>; Wed, 25 Nov 2026 19:06:34 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E5555606A31E
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com
X-Received: by mail-oa1-f42.google.com with SMTP id 586e51a60fabf-475920b9ca2so2095585fac.1
for <git@vger.kernel.org>; Wed, 25 Nov 2026 11:06:34 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20260707; t=1795633594; x=1796238394; 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=P5M0rbfF1vYO4G48sEGgR/KVoSAxkdFihHbb4El+od0=;
b=jPkZ5kSqeDOt+dd/TCh9w0QVA7jWtxtKMMpmXZJ7yhjqSrkobdPU3c4Q8ijYWuIDcl
LhaWIRPbj8KB7iP8kEp92PkDgwKgJ708uBCvGFLlDGbzFA029lM+dZ49xg0REmHar5gP
6480Txon1x9XqQx4xL29zWNCTVHnjnsYE6ZuNxC4aCLQ5JjN3QVaLlX1ourXXSwJirqf
fkxz/ai/AR1fxyMN7ue5sAivgKkR/Ke85xaGTU0ynGw7M0ePaQHRs3/PzojikOofdU++
Gpuuw/jt3p4TmX5AgJ3lahXF/ds/QY0XagFW9sF/8YX2qKoex8DKaL3s4Kwa+0RR4AJ4
nXzw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1795633594; x=1796238394;
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=P5M0rbfF1vYO4G48sEGgR/KVoSAxkdFihHbb4El+od0=;
b=XFfk36IPYtNshlMdkTfcfdJvEzTu3EjMQerS2gz1zBtjYwJhRSqLQHv51lyYHlOJmP
ZPwu9WWbVndrSsuijzQRRoBq80dCcNeBGTCrrUDg9ySK6Xi3G9asILKDKJa8tlC6/Hr3
VnESayr+gWCb0ktG4z6ZHHWlgMjT5p3rJwSgoO3ktQHh3VJaJvfI3vabJ3xDG6N5XDlT
tLpy+Y9rPwtOy0zJbf9rMqzu8qrOwX2JS98eUIPsoWTh4WZMLLs8yZFNQ9ORotEATPsO
4WdO6z5JVucG2qPJU7T/exkHTWSO4b3oW7ML701culZiepKgYxGXc3nj5D0EvxSPBNz7
o3xA==
X-Gm-Message-State: APuU4Z/fu9ZeJzCRAg2sq+Rj9FEL1lOjl+SqyMjdAYa+zhifTZvkiMIZ
KiHe9LLExZAItJzj8e/8BczEt+irRhpINgfBasp89oyby0PdzZTHRfn38CXTzlpw
X-Gm-Gg: AZFdTZdVxE0bxbZ/JO7EkbVRKARsJaOs7dcGaSVk5KCm9o13VxlMZkj4ybn7Iumi1PT
+PPR7xeRESgyORqSsxE1K72ydHxrSMjSzpEWomP9sZ7YctLcZuF3j5vx9ZWG71K/OWRfuu4aQli
/Js8f5JyuC0dtLX3F27oScx8D67UuP1rLTJ9drCw8t0M/WEMAdSIgeJLVlEaIq7W4Two0dLo7YC
qHQMhJMEJf8L6kmil5u1Kl8JfG6KcDbOPjBZKdgoRgGQHYiOSML08nkuGp+GLPtl5GvqeDVXsK4
EO7vOl3RxgN/scdmrz7L8Cd4pjRfUCxyEseaJwa/B+zNf3O2bbmm4mFrY7CfGYQJzqGoYyZZcf+
cjn08CxBHHH3OuyCm/t2m57svds3i43KHP/R5dt71CprjeqIL9j/7yZb3E7eicKBk5aYaZW2vrg
Z9AG6DpIF6XpHJJwfXfZe6ny7EhWx98MQH58CUXphpzpRWAXtcxgvRq4Le4UXD+yLGZnCjckyLY
AtXR17064Cip9/JKZuhJj8rfFmCIMFZVxLLtLvgpAfIPIAXRInT+37lNOVibQuxi24qPiETujKL
dx06C9nvfl0mmh85lQpbZuRYyAQcK3vM
X-Received: by 2002:a05:6870:de0e:b0:49e:d4c:e6f0 with SMTP id 586e51a60fabf-4c9f67d1172mr3741784fac.46.1795633593688;
Wed, 25 Nov 2026 11:06:33 -0800 (PST)
X-Received: from [127.0.0.1] ([40.124.78.245])
by smtp.gmail.com with ESMTPSA id 586e51a60fabf-4bc5fc97bb2sm17005137fac.9.2026.11.25.11.06.31
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Wed, 25 Nov 2026 11:06:32 -0800 (PST)
X-In-Reply-To: <pull.2412.v16.git.git.1795633584.gitgitgadget@gmail.com>
X-Fcc: Sent
X-Mailing-List: git@vger.kernel.org
X-List-Post: <mailto:git@vger.kernel.org>
X-List-Help: <mailto:git+help@vger.kernel.org>
X-List-Subscribe: <mailto:git+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:git+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:git+owner@vger.kernel.org>
X-Precedence: list
X-Cc: Phillip Wood <phillip.wood123@gmail.com>,
"D. Ben Knoble" <ben.knoble@gmail.com>,
Matt Hunter <m@lfurio.us>,
Kristoffer Haugsbakk <kristofferhaugsbakk@fastmail.com>,
Harald Nordgren <haraldnordgren@gmail.com>,
Harald Nordgren <haraldnordgren@gmail.com>