🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.git
Article #4118

[PATCH v17 4/7] fetch: extract collect_upstream_from_remote() helper

#4118
From: "Harald Nordgren via GitGitGadge...
Date: Mon, 30 Nov 2026 13:46
112 lines
14749 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 53957e6b18..68bcf9d88e 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -2047,8 +2047,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);
@@ -2063,14 +2061,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: <2e124f7c381224e2051ed4020ddf7f0e627fc7bb.1796046376.git.gitgitgadget@gmail.com>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!git+bounces-76676-linux-kernel-git=dewy.ru
References: <pull.2412.git.git.1789829246437.gitgitgadget@gmail.com> <pull.2412.v17.git.git.1796046376.gitgitgadget@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!git+bounces-76676-linux-kernel-git=dewy.ru
From: gitgitgadget@gmail.com ("Harald Nordgren via GitGitGadget")
Newsgroups: linux.kernel.git
Subject: [PATCH v17 4/7] fetch: extract collect_upstream_from_remote() helper
Date: Mon, 30 Nov 2026 13:46:13 +0000
Organization: Usenet Network
Message-ID: <2e124f7c381224e2051ed4020ddf7f0e627fc7bb.1796046376.git.gitgitgadget@gmail.com>
References: <pull.2412.git.git.1789829246437.gitgitgadget@gmail.com>
	<pull.2412.v17.git.git.1796046376.gitgitgadget@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="1437613"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:kzUfzfm16BJ9BKZVkhC+F86ulMY= sha256:sgBc/qzt9v5ho/EfqMtOy0+A3iydiHn2Nkbh7f9gwmM=
	sha1:65QcN74DT3wAuA+NJvxrwKKjzt0= sha256:k0TgxZmrwwnn90BQkWihL/qQGCT6dBd5o3w/4SHKB8Q=
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 35F97125FF2; Mon, 30 Nov 2026 16:46:46 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.049
X-Spam-Status: No, score=-3.049 tagged_above=-9999 required=6.31
	tests=[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 J7L9kQjJMuCV;
	Mon, 30 Nov 2026 16:46:43 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=git+bounces-76676-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 33B03125FED; Mon, 30 Nov 2026 16:46:35 +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 99AC42EB64; Mon, 30 Nov 2026 13:46:25 +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=1796046384;
	b=2NDz/XqBKlWt+D0THyIn8jmFeCpD82gGa2Cs38L2GjrhJvQrdC3A09TnHFF0yEA8AcR8
	 vieW+xZoT/rdqgXVI61yBHflxbjzwu9j6p2gapa5QNmFFfDWtZFAWOx+nk+10hKiPO49T
	 ZJHRJE3esmgGXS95BD8Kqyq7tkWCTgw4jBuBUX+L40zboTgX6kE3pb4RvX8BjCRzb24+p
	 L48JPiBGvQLmc3rxj63ZRstW+Jh5qRoIuBD0ebJMeibUoPU/91WF/wNBTl7fojs3XI/bP
	 rqRcIDXlScMz/uglrOHx9BQvVaVqkuKmWGwGWCJYnmzlB/ZbIHpMQDkm2AoM8NJo5dw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796046384;
	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=vI9+djxfaAMaWapb3LcWxeHMgFPcw9dEuvFdSLaz2+c=;
	b=Te499sftFb27JlNdI3yHBMVs9f7L7KQAOeZrg0K2wYsDERwqf6VeimLgT/pb83FljbTz
	 hnxHRVCgsCUVQaSVl0DC6+PXPAt20dmHKVA5+aLrV8eY0eXEP+u4c83pnmWAjqRlHZfgy
	 3yvN9uJ4MVnXYjwGgzBZY0CdvYnEYU+PTpiVsgUtMFi2zmynJywD/0ggexcMX+JnGcM1G
	 HrWjbPINTJVlfrCsrwB8gFC6W6Yz3QeOcxzcxqLpq3V+wHBHhXYhzfPF5QnB1woNopU+z
	 q6vhvI954vwpXgT2TYB3KN2/lSV2A0OrDZpc8vAWCTl4Wch1Bp6PvcJ15OCpaX/W/Kw==
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=PdmGWRbC;
	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 6DB26606A32D;
	Mon, 30 Nov 2026 13:46:24 +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=PdmGWRbC
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.224.52
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1796046383;
	b=YeXi4kuZ242GsqEf/eN0wNKnIADuo2t1cpGyb/yoLw4C6EwzhTFCAfkDIPqr44ENzES4
	 a+wgKliNj6mnHg7YPl9QeM3hIw9+inL/OFsSN3DbYwO4f1napLz4/iqFDrkEa73am9dx0
	 SgQYkdewEEVoXxx31xMj01gBZif4GQRzkrgB/H13wLE2MYr6wvJfp3w4U03wzNyU2Ktk/
	 Tc0LY7DeooLYUSciUdJVMxE2jpAlAQXC4MuzYCzDbdN4XWK6Po1WTCKmZ86GqW1e4IAxy
	 8COB2x9zp/JleygRsIBwJXokNub64sguwNiv6IusYv8HD/Z0Tw3r7REhwv3wAOdTFPA==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796046383;
	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=vI9+djxfaAMaWapb3LcWxeHMgFPcw9dEuvFdSLaz2+c=;
	b=ZLJsNF0sjZMoUn0luw9494PNo6zQeWaLnvI3YD4hsEhkaPPzQwzoAGPB1BrDtqZW/SAK
	 DE0RJHsaZTNAoGSIG5BZy47oz3EnyZRgjhAsVW1utWBYMb37xuU8Ns7Bsk2E9mvaqaJfN
	 6wal6iY1C/0+0SpStyE2x4ifxov5dq975MTtD5+raOdHQ+APgdq6rl5bttD/ifLZJidfI
	 GSxS3yOz5chlinuqbn34ZbHBUxrZrZK+9plhKsMThZw8aaatkfz0+7hxZJT8ajDaJfOrb
	 tD9wUXozSPMKf7oB543ICH1QNSKMI9LQMMfZFpw7es4NAm/50YdBvkvfIQrkAOPL4Og==
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=PdmGWRbC;
	arc=none smtp.remote-ip=74.125.224.52
X-Received: from mail-yx1-f52.google.com (mail-yx1-f52.google.com [74.125.224.52])
	(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 45F5B606A333
	for <git@vger.kernel.org>; Mon, 30 Nov 2026 13:46:23 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 45F5B606A333
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-yx1-f52.google.com with SMTP id 956f58d0204a3-67c6a0abc2eso10859402d50.0
        for <git@vger.kernel.org>; Mon, 30 Nov 2026 05:46:23 -0800 (PST)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20260707; t=1796046382; x=1796651182; 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=vI9+djxfaAMaWapb3LcWxeHMgFPcw9dEuvFdSLaz2+c=;
        b=PdmGWRbCB/tkpmqM99XXTDYOUk09AaiBGWTUPsyEvH1w8dBmLMVV18qJEzmeR2Sy26
         jHULIo5+Mcl4YVDbYfIKKohXgOV6EP75RNjDkOSmQAydFwM9zReyzMxry+XCYXd8wAIt
         uGJaIpL+tAfpXt8D45oauofYm7iUxW00h/E6bvmQKR505HLnl4+Ps3N9eS2TGtlBCJ0X
         LXXmJs0Imeql3KhtyCZu2hRKaGcleHnavNsrf2842rQ64xCb03rLJtb/Ktd1L3yHK/Pf
         4twn5J08iCMscR/luJhlwx8wC78dSbBDq/IWaAXENlVQ1EWH+sHeB2Glw/mMu2btPw1a
         YCjw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1796046382; x=1796651182;
        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=vI9+djxfaAMaWapb3LcWxeHMgFPcw9dEuvFdSLaz2+c=;
        b=XaTEMez8ZpVQU4FSkue2hyVyGPwkpliVWIICTWTsuLrier0SiDBaJ9tGmO4elw8WxE
         /xIaqfkruEvnjsLN1j7RryjWIVkqOINUTo9TT9AAVAgEQEGIhDtnMZnj/Ppo3EazO5hz
         PF+ZomgiDH3/THC7hJnac/qzVTIUEeeO3k9l9Fm3LwB8HroCCD0swhaJmuGkBxP6YE5Y
         0hc/6kE9krblzxLC6ZRpersL0/ssYoKgnHlqc+TCftDtR48KR26ibN8RQrqpKH5O/Vsk
         dM2hpYMoMvJMMrTTJDAjsXuRNVzAiFvX20NZwA5IAxPExyIsa1Epe3dqqn3TGYfixwj9
         HABg==
X-Gm-Message-State: APuU4Z9FjAFbK4wlTJT2fo27vFNCsReefH/8wCt6nPvAYZabWGWLbTM8
	4/LeX4+5eNnHha230UiurW9RymbwHB7XHdqnztlo2nnw7YoXj78K4l2dFQDZy/+d
X-Gm-Gg: AZFdTZchYjsowv53MbGUi+iEuwVfCraGIi/YXFOYkX9xIOASmeJpOM6/2mxYu6PstQJ
	xyV7YZJdhpq1raJZRLw07Yls/KteC3Ojx0hyyEMnNSOefsvWSm4B+rWh+L4KY33tX5BHBCCT8lT
	Ywn2KSvmYPzI1nHqHZ1v6X0hbzucxoEU6OxLtKCjtzpm9p/XodwNFoktCKc9tdQ/T3mu9MIEDHQ
	uRT8xCgr+rMQ7ol93ZdxZBCYljxz1UJxklrLqlOG3OKRnPbPEsetniQPnT3DTj1DNnZbc0dGb0q
	10WYAGCdPwWNbQZ7wyOa5fibokGdxxbsVKWTYOv+swqy+r8c1U0c/4rTHPhbLE3eQEJVLuLrhEO
	ngbGxirGOQM0mtw686p4AU+sKZzyCBwdJmRhbqP1Oo/jUygMB/HRS6c2zX18dM7JBK8xeVOX5PH
	BtfaO9CPFHVDIOZcZBYuG4P9h9TdtUWK3CczgX04+aOwKPJNfRspX+lVc5b/BO+wQoNJaButOaQ
	KdkqcaKowGif5RSXULk3JDDNqku0dXhJ1ck/NlvPzSUQaVBCMOaRn0d5owLE4HaaGuA8FtIb1EM
	S8BlgTHawIyGyy/gz4gu9cmwilHFYulYAx2zWuWgvRkkYGBVVPjG
X-Received: by 2002:a05:690e:1287:b0:675:6f9d:dd53 with SMTP id 956f58d0204a3-67df42c4501mr10973589d50.8.1796046381701;
        Mon, 30 Nov 2026 05:46:21 -0800 (PST)
X-Received: from [127.0.0.1] ([20.49.63.207])
        by smtp.gmail.com with ESMTPSA id 6a1803df08f44-92134796329sm292866326d6.2.2026.11.30.05.46.21
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Mon, 30 Nov 2026 05:46:21 -0800 (PST)
X-In-Reply-To: <pull.2412.v17.git.git.1796046376.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>