From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.224.52 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== 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== 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 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 ; Mon, 30 Nov 2026 13:46:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 45F5B606A333 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.a=rsa-sha256 header.s=20260707 header.b=PdmGWRbC Received: by mail-yx1-f52.google.com with SMTP id 956f58d0204a3-67c6a0abc2eso10859402d50.0 for ; Mon, 30 Nov 2026 05:46:23 -0800 (PST) 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) 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) Message-Id: <2e124f7c381224e2051ed4020ddf7f0e627fc7bb.1796046376.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Harald Nordgren via GitGitGadget" Date: Mon, 30 Nov 2026 13:46:13 +0000 Subject: [PATCH v17 4/7] fetch: extract collect_upstream_from_remote() helper Fcc: Sent Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Mailing-List: git@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 To: git@vger.kernel.org Cc: Phillip Wood , "D. Ben Knoble" , Matt Hunter , Kristoffer Haugsbakk , Harald Nordgren , Harald Nordgren From: Harald Nordgren 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 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 --- 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