🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.git
Article #547

[PATCH v2 1/5] worktree repair: align .git file handling with repair_gitfile()

#547
From: "Yoichi NAKAYAMA via GitGitGadge...
Date: Wed, 14 Oct 2026 21:20
99 lines
11544 bytes
From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>

Use read_gitfile_raw() instead of read_gitfile_gently() in
repair_worktree_at_path(), moving path resolution and repository
validation to the caller. This makes .git file handling consistent
with repair_gitfile() and avoids using the non-reentrant
read_gitfile_gently().

A subsequent commit will update infer_backlink() to use
read_gitfile_raw() as well.

Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
---
 worktree.c | 40 ++++++++++++++++++++++++++++------------
 1 file changed, 28 insertions(+), 12 deletions(-)

diff --git a/worktree.c b/worktree.c
index 8cb8637b18..4492ca835d 100644
--- a/worktree.c
+++ b/worktree.c
@@ -840,7 +840,7 @@ void repair_worktree_at_path(struct repository *repo,
 	struct strbuf inferred_backlink = STRBUF_INIT;
 	struct strbuf gitdir = STRBUF_INIT;
 	struct strbuf olddotgit = STRBUF_INIT;
-	char *dotgit_contents = NULL;
+	struct strbuf contents = STRBUF_INIT;
 	const char *repair = NULL;
 	int err;
 
@@ -858,14 +858,26 @@ void repair_worktree_at_path(struct repository *repo,
 
 	infer_backlink(repo, dotgit.buf, &inferred_backlink);
 	strbuf_realpath_forgiving(&inferred_backlink, inferred_backlink.buf, 0);
-	dotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
-	if (dotgit_contents) {
-		strbuf_addstr(&backlink, dotgit_contents);
-	} else 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);
+	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 == READ_GITFILE_ERR_NOT_A_REPO) {
+	} 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
@@ -876,11 +888,15 @@ void repair_worktree_at_path(struct repository *repo,
 			 */
 			strbuf_swap(&backlink, &inferred_backlink);
 		} else {
-			fn(1, dotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data);
+			fn(1, dotgit.buf,
+			   _("unable to locate repository; .git file does not reference a repository"),
+			   cb_data);
 			goto done;
 		}
-	} else {
-		fn(1, dotgit.buf, _("unable to locate repository; .git file broken"), cb_data);
+	} else if (err) {
+		fn(1, dotgit.buf,
+		   _("unable to locate repository; .git file broken"),
+		   cb_data);
 		goto done;
 	}
 
@@ -926,12 +942,12 @@ void repair_worktree_at_path(struct repository *repo,
 					     gitdir.buf, use_relative_paths);
 	}
 done:
-	free(dotgit_contents);
 	strbuf_release(&olddotgit);
 	strbuf_release(&backlink);
 	strbuf_release(&inferred_backlink);
 	strbuf_release(&gitdir);
 	strbuf_release(&dotgit);
+	strbuf_release(&contents);
 }
 
 int should_prune_worktree(struct repository *repo,
-- 
gitgitgadget
Technical Details
Message-ID: <21b2878aa552a931788aa7bb14720b24e7809c75.1792012836.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-72061-linux-kernel-git=dewy.ru
References: <pull.2225.git.1789269613.gitgitgadget@gmail.com> <pull.2225.v2.git.1792012836.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-72061-linux-kernel-git=dewy.ru
From: gitgitgadget@gmail.com ("Yoichi NAKAYAMA via GitGitGadget")
Newsgroups: linux.kernel.git
Subject: [PATCH v2 1/5] worktree repair: align .git file handling with
 repair_gitfile()
Date: Wed, 14 Oct 2026 21:20:32 +0000
Organization: Usenet Network
Message-ID: <21b2878aa552a931788aa7bb14720b24e7809c75.1792012836.git.gitgitgadget@gmail.com>
References: <pull.2225.git.1789269613.gitgitgadget@gmail.com>
	<pull.2225.v2.git.1792012836.gitgitgadget@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2390706"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:UTG+LnS3JonPNq2X+CNZY+FSK8A= sha256:xvomp2sVPN/FersXRqxriWQDqbNU0A4KjBO0Mh4vPWw=
	sha1:zEiFDqGJhz1afVGQC/m8nK34554= sha256:Jq5TMU0e/jY2ILQDMP7tCjdvdlP/mdQji32HPofmmJ8=
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 496E31101DD; Thu, 15 Oct 2026 00:21:11 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: 0.252
X-Spam-Status: No, score=0.252 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, FORGED_GMAIL_RCVD=1,
	FREEMAIL_FORGED_FROMDOMAIN=0.001, FREEMAIL_FROM=0.001,
	HEADER_FROM_DIFFERENT_DOMAINS=0.25, MAILING_LIST_MULTI=-1,
	SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=no 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 brxCmxx265ob;
	Thu, 15 Oct 2026 00:21:09 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=git+bounces-72061-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 8AE451101D7; Thu, 15 Oct 2026 00:21:00 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1])
	by sea.lore.kernel.org (Postfix) with ESMTP id B8AB032194; Wed, 14 Oct 2026 21:20:43 +0000 (UTC)
X-Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 977B448EC62;
	Wed, 14 Oct 2026 21:20:43 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IBfclZPk"
X-Received: from mail-qt1-f175.google.com (mail-qt1-f175.google.com [209.85.160.175])
	(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 34D1C485CD8
	for <git@vger.kernel.org>; Wed, 14 Oct 2026 21:20:41 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.175
X-ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;
	t=1792012842; cv=none; b=hiO/s3KzRm2vBs9avkScxSXnJgIRWglMxFmKERjVxtc7/HUqo2rKC7LN8bkMBeSR0mlCvxUh/jkcqE2ENVLCfDUmyB0lj30JjC3hYFiSmzJAIH6pgOoPpQ5Zt2KrlHMlf35FuLN4ZnSYMTHJ/mxnEAhFvmZxnExCFPSLmIDgT5g=
X-ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org;
	s=arc-20240116; t=1792012842; c=relaxed/simple;
	bh=z35Wx7semWt29C11Yq0qQBgAQeiotPruzmdh/SBQo4E=;
	h=Message-Id:In-Reply-To:References:From:Date:Subject:Content-Type:
	 MIME-Version:To:Cc; b=jVOa8mvZAnF8jA2JPNdIeGdpCjxZ1QWhcl/9q8oovb13dPgN0LlS7wP3YxASw/K8DlcBmtud2nor2Jk5bkJA3/LeGTvM3y14veoSKT56adOJDD50CXH+NIdwqswLmDAv0OonZ4FB/Mmu4pIibJMdn/RSCl5Y/rcsuNoeMexM7s0=
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=IBfclZPk; arc=none smtp.client-ip=209.85.160.175
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-qt1-f175.google.com with SMTP id d75a77b69052e-533797e72e8so5157501cf.2
        for <git@vger.kernel.org>; Wed, 14 Oct 2026 14:20:41 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20251104; t=1792012840; x=1792617640; 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=5gaIh/JHtphdvwHA8UXktSPDIZrBmDqB9dgEzHUu1LM=;
        b=IBfclZPk4JiSC/uB0guQ7hdR/+cNU5pG01qPaQpuocTSR6lMOKvgPC61XfT7JIe5w8
         gwRytI1K+q98M+rIcc1S4lSTiGsV6h5PqD8gbONs1CfhRQL76pxKCdiTG7VVWI2tyMrR
         505xrA2+hlSnKdBPAjftdxdieS8mRdx43KK5fntqc7pjhmF15Rf5GroUPcKYJ/6Kero9
         csaQq82u7rgI9T3QBmjtu6Jp+jkVmGc6hLyrKIuy7jxHipOsR/zOIzhb1oLuO6IycVeF
         HXfzVdNxpfi6IkZiD1gmH90noBZ4kU1vA61TMTOeAyPtOrrO2H8a5pZ0U1X/2SdVghcw
         bUoA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1792012840; x=1792617640;
        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=5gaIh/JHtphdvwHA8UXktSPDIZrBmDqB9dgEzHUu1LM=;
        b=m685rd4Xyxa7FsJjeTRu3bnVyFWyODLgeXB86LDnblMnQsrMyBzBbIuJbmH1dKmUPA
         D/Tow+k2A6kADzwqay5dezaMh7Fl7VzLcuX1GzHsUyZx90krl8yonsT1jXnaljS5YZS1
         eACWaA6EdSH36gEv3wb79X/3NdN0HSbOQTEHduXKkTgww3gtivYFaCyxSjY+ozCuPpYq
         1no6zI9satim4kNxSC+EU1fyfIHRpaxYvRsmSRFeOMqiXceKlci1Ut9ErTDFyzV5NUt5
         KU8UupBLkCOZsM2gjEGVA+4twqHl/LEePEATsl6jniFx3d1V5jKaKjZFXHqumni3WotP
         RLjA==
X-Gm-Message-State: AFq9FYJ3MPXjCsRRr0ipJ4IMMral6hlwBp8nKg5a74e2oi4DaOVjxI9M
	UQoiWJnJVe+6h6M+YlXF0bqL5z7Kzq33cLy4S/0IbIqLqPYp1nd8UucMS5mzr/w/DH0=
X-Gm-Gg: AYBFou3HiqY4tWFE+JAo0Y0NZGGqSjanADr9Cp1dYhmIqkJFAyfl37bNtrahni3EHNB
	BJco0I5Yrx+GF2XoYwFhj0mX8Z4YTCTlqESlFf1qbBmqtwNKRavqB/rQHhQ43uN8Jtu+zED/0r3
	3g+fh6zTrm42smJ95+Ou+WsMIUyvJuhSknWv3ttdWckPQlTOUhag/5hdALU9lfDR5luDyWKE82c
	M+wR0W2K7TSrhzh8c2tZjsFKXn/x5S5YLsUi1UT9Vy25ypxWk7bd68m22WJomf2qHNRbvec6H4l
	7678NITODlM3GTPVIt708KTG4go0QtbfwaL3g+0AirfSe75Czt4Gl4yZaW9tYVBDgLsCUs/fLeu
	V/Q5LtEdvQi6YmlEE/B8ybqrIF3i2gvwPizdY0NlDbbEjm/oeZGpi3vXqkjE/4G+hv/MoC1qs8A
	XXi0vvqAMYEzqNY4ohP0dTjpCkwNZwWGzuIAQnVbxC66o4UKfEDzk/y27Pgztto55tgJG3y5+KL
	g==
X-Received: by 2002:ac8:6f0a:0:b0:533:3d48:37f0 with SMTP id d75a77b69052e-5359fb5a466mr258947411cf.47.1792012839945;
        Wed, 14 Oct 2026 14:20:39 -0700 (PDT)
X-Received: from [127.0.0.1] ([20.220.125.197])
        by smtp.gmail.com with ESMTPSA id d75a77b69052e-536127baba0sm7102361cf.13.2026.10.14.14.20.38
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Wed, 14 Oct 2026 14:20:38 -0700 (PDT)
X-In-Reply-To: <pull.2225.v2.git.1792012836.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>