Article View: linux.kernel.git
Article #737[PATCH v3 2/5] worktree repair: centralize inferred backlink selection
From: "Yoichi NAKAYAMA via GitGitGadge...
Date: Sat, 17 Oct 2026 12:54
Date: Sat, 17 Oct 2026 12:54
121 lines
12848 bytes
12848 bytes
From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
Reorder the processing steps to centralize the logic for preferring
the inferred_backlink.
Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
---
worktree.c | 69 +++++++++++++++++++++++-------------------------------
1 file changed, 29 insertions(+), 40 deletions(-)
diff --git a/worktree.c b/worktree.c
index 4492ca835d..f8e9850f6f 100644
--- a/worktree.c
+++ b/worktree.c
@@ -841,6 +841,7 @@ void repair_worktree_at_path(struct repository *repo,
struct strbuf gitdir = STRBUF_INIT;
struct strbuf olddotgit = STRBUF_INIT;
struct strbuf contents = STRBUF_INIT;
+ const char *dotgit_contents = NULL;
const char *repair = NULL;
int err;
@@ -856,43 +857,13 @@ void repair_worktree_at_path(struct repository *repo,
goto done;
}
- infer_backlink(repo, dotgit.buf, &inferred_backlink);
- strbuf_realpath_forgiving(&inferred_backlink, inferred_backlink.buf, 0);
err = read_gitfile_raw(&contents, dotgit.buf);
- if (!err) {
- const char *dotgit_contents = contents.buf;
- if (is_absolute_path(dotgit_contents)) {
- strbuf_addstr(&backlink, dotgit_contents);
- } else {
- strbuf_addbuf(&backlink, &dotgit);
- strbuf_strip_suffix(&backlink, ".git");
- strbuf_addstr(&backlink, dotgit_contents);
- strbuf_realpath_forgiving(&backlink, backlink.buf, 0);
- }
- }
if (err == READ_GITFILE_ERR_NOT_A_FILE ||
err == READ_GITFILE_ERR_IS_A_DIR) {
fn(1, dotgit.buf,
_("unable to locate repository; .git is not a file"),
cb_data);
goto done;
- } else if (!err && !is_git_directory(backlink.buf)) {
- strbuf_release(&backlink);
- if (inferred_backlink.len) {
- /*
- * Worktree's .git file does not point at a repository
- * but we found a .git/worktrees/<id> in this
- * repository with the same <id> as recorded in the
- * worktree's .git file so make the worktree point at
- * the discovered .git/worktrees/<id>.
- */
- strbuf_swap(&backlink, &inferred_backlink);
- } else {
- fn(1, dotgit.buf,
- _("unable to locate repository; .git file does not reference a repository"),
- cb_data);
- goto done;
- }
} else if (err) {
fn(1, dotgit.buf,
_("unable to locate repository; .git file broken"),
@@ -900,23 +871,41 @@ void repair_worktree_at_path(struct repository *repo,
goto done;
}
+ dotgit_contents = contents.buf;
+ infer_backlink(repo, dotgit.buf, &inferred_backlink);
+ strbuf_realpath_forgiving(&inferred_backlink, inferred_backlink.buf, 0);
+
+ if (is_absolute_path(dotgit_contents)) {
+ strbuf_addstr(&backlink, dotgit_contents);
+ } else {
+ strbuf_addbuf(&backlink, &dotgit);
+ strbuf_strip_suffix(&backlink, ".git");
+ strbuf_addstr(&backlink, dotgit_contents);
+ strbuf_realpath_forgiving(&backlink, backlink.buf, 0);
+ }
+
+ if (!is_git_directory(backlink.buf) && !inferred_backlink.len) {
+ fn(1, dotgit.buf,
+ _("unable to locate repository; .git file does not reference a repository"),
+ cb_data);
+ goto done;
+ }
+
/*
* If we got this far, either the worktree's .git file pointed at a
- * valid repository (i.e. read_gitfile_gently() returned success) or
+ * valid repository (i.e. is_git_directory() returned true) or
* the .git file did not point at a repository but we were able to
* infer a suitable new value for the .git file by locating a
* .git/worktrees/<id> in *this* repository corresponding to the <id>
* recorded in the worktree's .git file.
*
- * However, if, at this point, inferred_backlink is non-NULL (i.e. we
- * found a suitable .git/worktrees/<id> in *this* repository) *and* the
- * worktree's .git file points at a valid repository *and* those two
- * paths differ, then that indicates that the user probably *copied*
- * the main and linked worktrees to a new location as a unit rather
- * than *moving* them. Thus, the copied worktree's .git file actually
- * points at the .git/worktrees/<id> in the *original* repository, not
- * in the "copy" repository. In this case, point the "copy" worktree's
- * .git file at the "copy" repository.
+ * Even if the worktree's .git file pointed at a valid repository,
+ * it doesn't always mean that the backlink is correct. For example,
+ * the user might have *copied* the main and linked worktrees to a
+ * new location as a unit rather than *moving* them (the copied
+ * worktree's .git file actually points at the .git/worktrees/<id>
+ * in the *original* repository, not in the "copy" repository).
+ * Therefore, we prioritize inferred_backlink over backlink.
*/
if (inferred_backlink.len && fspathcmp(backlink.buf, inferred_backlink.buf))
strbuf_swap(&backlink, &inferred_backlink);
--
gitgitgadget
Technical Details
Message-ID:
<86021f31af4985d9c2a42e2b96031605652837bf.1792241702.git.gitgitgadget@gmail.com>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-72310-linux-kernel-git=dewy.ruReferences:
<pull.2225.git.1789269613.gitgitgadget@gmail.com> <pull.2225.v3.git.1792241702.gitgitgadget@gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-72310-linux-kernel-git=dewy.ru
From: gitgitgadget@gmail.com ("Yoichi NAKAYAMA via GitGitGadget")
Newsgroups: linux.kernel.git
Subject: [PATCH v3 2/5] worktree repair: centralize inferred backlink
selection
Date: Sat, 17 Oct 2026 12:54:59 +0000
Organization: Usenet Network
Message-ID: <86021f31af4985d9c2a42e2b96031605652837bf.1792241702.git.gitgitgadget@gmail.com>
References: <pull.2225.git.1789269613.gitgitgadget@gmail.com>
<pull.2225.v3.git.1792241702.gitgitgadget@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
logging-data="3043411"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:yh1eN0Lih2/6ugvIlpK4w+Hhs64= sha256:ZkZ/Whu/fDFyWfdZRoSYpe7SKqqVSuPi4IXztf4gQIQ=
sha1:EWRQfQ4E1MhUg5/02zs++cKV1Dg= sha256:rg9vLKWQUutdUhwT7xiXCqySNsunqx7bhpHb/Qlwu+Q=
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 A5E411106E1; Sat, 17 Oct 2026 15:55:26 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -2.048
X-Spam-Status: No, score=-2.048 tagged_above=-9999 required=6.31
tests=[DCC_CHECK=1.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.25,
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 unJidEt_T1bc;
Sat, 17 Oct 2026 15:55:25 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.232.135.74; helo=sto.lore.kernel.org; envelope-from=git+bounces-72310-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 sto.lore.kernel.org (sto.lore.kernel.org [172.232.135.74])
by mail.dewy.ru (Postfix) with ESMTPS id 1BC3E1106B3; Sat, 17 Oct 2026 15:55:16 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1])
by sto.lore.kernel.org (Postfix) with ESMTP id D4B3D607F28; Sat, 17 Oct 2026 12:55:15 +0000 (UTC)
X-Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
by smtp.subspace.kernel.org (Postfix) with ESMTP id 90FB14921A1;
Sat, 17 Oct 2026 12:55:09 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="kEq2A++4"
X-Received: from mail-qv1-f54.google.com (mail-qv1-f54.google.com [209.85.219.54])
(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 337BC487CFD
for <git@vger.kernel.org>; Sat, 17 Oct 2026 12:55:07 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.54
X-ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
t=1792241708; cv=none; b=eO7eAKBZiULJoIMQG2oyAjyrrGitGTNMFh9xztZZkJcwBiubiH+VMBIV9Ckz0HV1fU+e5d0vjNP88qSCmPstPHyZyFUY6NDHn0T8dvSDvdLRi8d5BGDGoJKZDapUBdl3KoJ54k3GRVCepc0WAtr7EAZq1PV1WmXhfXt/urIx+RY=
X-ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
s=arc-20240116; t=1792241708; c=relaxed/simple;
bh=tStAA+iV5JYyj+xhQQf6ESMDZYPaFwuHsLf7tjEdd3E=;
h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type:
MIME-Version:To:Cc; b=aL6BPg8/TBj/5amtU3FjKNur///AxIO881qtn+Yp9mnJBdGvzrCuNv5dJirKXR5Cr9utxhuSQi1+WNrwGUDaKY6Xolfw3Ds2932P30o/gHa+2tbsohMMUHz9LkSAiqiLvpUNQGXW+YRqOOS8EqZDmFoH562KXv9XHPoZZdA2EL4=
X-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=kEq2A++4; arc=none smtp.client-ip=209.85.219.54
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-qv1-f54.google.com with SMTP id 6a1803df08f44-9144e8021d3so9782426d6.3
for <git@vger.kernel.org>; Sat, 17 Oct 2026 05:55:07 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20251104; t=1792241706; x=1792846506; 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=+9r8bfcUTktv16qLYUr988CQtHLw8NXFURbVjaGD42o=;
b=kEq2A++4kxoXbc8PTAzAb15an0SwVv0unubS42cU6KpUaahtSXdSbVw0oOO7myuRXV
dSyVx172H0W4FM7qBC5BczWX0fRlAoJSdnO3QkshYKYn/VUWfJHh9XtQIFtO99VtTkhM
637FETjvgGzrxF7gzt3005lzjxLQTbHFHHpMDmSWTm/qGAG6E1FeLXQMMTCFg1+gsKi7
1QGpGWneXOCNn9sjSAbxc/fKJ8404hKmTQiisJMPGgIpP5PLPaeuDq1l5hUGxS+xncyT
E4UclJdVzK+HWSYirNMjk9hqWrRtBjX06lGbdecgkyqbFTWpV7QYSidp4NM+NLxde1TK
BSSw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20260707; t=1792241706; x=1792846506;
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=+9r8bfcUTktv16qLYUr988CQtHLw8NXFURbVjaGD42o=;
b=kNSM1/h14mnmAe3FJnWb5etI6caY8xwmSkSOoMC3AjT4rj9WTAHNYiV6mqGonaOWEZ
Une12HtQJtVXBKTP973eoneg2+AJykXAH4rkSOyX7LXd7HPVcneyHYmcnPY5/pEw3POg
fUK56zQcgwC/+YwS3uU8c+FbJW3dHMcQs9tSUlcO18lY6UcWeYl8KOXlSiI1CAQceETo
gN2rAaKFAguyyabIWwTEmqHiK6jlULkBKWvZNXPZ68y99mB0HBaRBkDmci8dzqTiEum1
IfnFy0hBIV4Hum3LjXc736Gs6nUK4jWz5bETx3et+swBqsetnvNqKM2zuOFRCkTp7p6B
IzaA==
X-Gm-Message-State: AFq9FYJMmzszWLrZ9sd6cLElKci+Mg6veb9IR5Xt/2jUHj30IE6sxnAx
ooniEHFqJoPk1NuFTH0Ad1cUMEf+8kR4XuT0dyrYx4FS0mpcICrvhnPr2EKO0HA+
X-Gm-Gg: AYBFou1kNY33V2NCgZkptloEi0y+tku6/s6F6+jcd58I6sShOoyAnyXs2Iug/i/Qt2L
XZZwVGky9KmkRQnxGZBNLR6VUQaKlOwOg1aYuS8Kh//unhyXPIvhH4JAbTbl11uccRaqzhAXgXA
ZzhwCCZzPy+YcmDZ8kUOxVwEAn7f9998sy+3axvMdZQTqCT3EgEJ9vXt0pOYRtGJMYhQ4yS6P91
X9wX7KRwMFtD4B7pzcwCaVswlIqPx8t1ZBpuovG/Wo1i1uyC/WYZJ8yVKUWIR4meLe9XvaBXXRf
CzScDsbH5EX7hzddFdBiCfRxLZoRbUWQ3y4mpssCCIcNpgDkI8RXj6JnuPgbgkT16W+Wvl+TKUW
hujJGBflZl6Ue95yxDkrwd4d2JMMASi/7mkDBRlShdHsCyIe298750pgSri/LwgX0b9YD9AacO3
Lfqy1kT406kGGUEew2JgFm7eonHWiqijocl6wxBCATmDs7R2e3RUp9ar/As31tdJKBkzYK4kBpp
YrJ
X-Received: by 2002:a05:620a:3196:b0:93e:c12c:9b71 with SMTP id af79cd13be357-93f29dfc421mr446280385a.76.1792241706075;
Sat, 17 Oct 2026 05:55:06 -0700 (PDT)
X-Received: from [127.0.0.1] ([52.152.180.199])
by smtp.gmail.com with ESMTPSA id 6a1803df08f44-91bcbb4f865sm46652626d6.37.2026.10.17.05.55.05
(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
Sat, 17 Oct 2026 05:55:05 -0700 (PDT)
X-In-Reply-To: <pull.2225.v3.git.1792241702.gitgitgadget@gmail.com>
X-Fcc: Sent
X-Precedence: bulk
X-Mailing-List: git@vger.kernel.org
X-List-Subscribe: <mailto:git+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:git+unsubscribe@vger.kernel.org>
X-Cc: Eric Sunshine <sunshine@sunshineco.com>,
Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>,
Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>,
Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>