πŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.git
Article #911

[PATCH v3 5/7] stash create: rework temporary index creation

#911
From: Phillip Wood
Date: Tue, 20 Oct 2026 15:20
95 lines
14556 bytes
From: Phillip Wood <phillip.wood@dunelm.org.uk>

We use reset_tree() to create the temporary index file used to stash
uncommitted working tree changes. Unfortunately, reset_tree() reads
the tree into the repository's in-memory index and writes the index
to the repository's default index path. In this case, we read the
tree that's already in the index and copy the existing stat data
across, so overwriting the in-memory index should not change its
contents. To write to a temporary index file, the caller has to
use set_alternate_index_output(). While that writes the index to a
temporary file, it updates the timestamp of the in-memory index so
that the timestamp no longer represents the index file the in-memory
index was read from. In practice that probably does not matter, but
set_alternate_index_output() also relies on a global variable which
makes the code harder to reason about.

Use create_index_from_tree() instead of reset_tree() so that we
can create temporary index file without overwriting the in-memory
index or relying on set_alternate_index_output(). This also makes
the implementation consistent with the way we create the temporary
index for "git stash push -p". As create_index_from_tree() does not
modify the in-memory index it is clearly safe to remove the call to
repo_read_index_preload() which saves one lstat() call per changed
path. It would probably be safe to remove that call without moving away
from reset_tree() but it is much easier to reason about now that the
in-memory index is not modified when we create the temporary index file.

In order to use create_index_from_tree() we need to add a new
parameter to control whether the index is reset, as the new caller
needs to merge the index rather than reset it so that we still detect
intent-to-add entries.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
 builtin/stash.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)

diff --git a/builtin/stash.c b/builtin/stash.c
index b4fe7465e4..ddeb5a5d2a 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -374,7 +374,7 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)
 }
 
 static int create_index_from_tree(const struct object_id *tree_id,
-				  const char *index_path)
+				  const char *index_path, bool reset)
 {
 	int nr_trees = 1;
 	int ret = 0;
@@ -403,7 +403,7 @@ static int create_index_from_tree(const struct object_id *tree_id,
 	opts.src_index = the_repository->index;
 	opts.dst_index = &dst_istate;
 	opts.merge = 1;
-	opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;
+	opts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0;
 	opts.fn = oneway_merge;
 
 	if (unpack_trees(nr_trees, t, &opts)) {
@@ -1399,7 +1399,7 @@ static enum create_result stash_patch(struct stash_info *info,
 		goto done;
 	}
 
-	if (create_index_from_tree(head_tree, stash_index_path.buf)) {
+	if (create_index_from_tree(head_tree, stash_index_path.buf, true)) {
 		ret = CREATE_ERROR;
 		goto done;
 	}
@@ -1458,22 +1458,15 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps
 	repo_init_revisions(the_repository, &rev, NULL);
 	copy_pathspec(&rev.prune_data, ps);
 
-	set_alternate_index_output(stash_index_path.buf);
-	if (reset_tree(&info->i_tree, 0, 0)) {
+	if (create_index_from_tree(&info->i_tree, stash_index_path.buf, false)) {
 		ret = -1;
 		goto done;
 	}
-	set_alternate_index_output(NULL);
 
 	rev.diffopt.output_format = DIFF_FORMAT_CALLBACK;
 	rev.diffopt.format_callback = add_diff_to_buf;
 	rev.diffopt.format_callback_data = &diff_output;
 
-	if (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {
-		ret = -1;
-		goto done;
-	}
-
 	add_pending_object(&rev, parse_object(the_repository, &info->b_commit),
 			   "");
 	run_diff_index(&rev, 0);
-- 
2.56.0.134.g299a3c16181
Technical Details
Message-ID: <de5e0c196a9ff241861f5598ac9f6bc5c960f6ec.1792509606.git.phillip.wood@dunelm.org.uk>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!git+bounces-72553-linux-kernel-git=dewy.ru
References: <cover.1791218125.git.phillip.wood@dunelm.org.uk> <cover.1792509606.git.phillip.wood@dunelm.org.uk>

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-72553-linux-kernel-git=dewy.ru
From: phillip.wood123@gmail.com (Phillip Wood)
Newsgroups: linux.kernel.git
Subject: [PATCH v3 5/7] stash create: rework temporary index creation
Date: Tue, 20 Oct 2026 16:20:40 +0100
Organization: Usenet Network
Message-ID: <de5e0c196a9ff241861f5598ac9f6bc5c960f6ec.1792509606.git.phillip.wood@dunelm.org.uk>
References: <cover.1791218125.git.phillip.wood@dunelm.org.uk> <cover.1792509606.git.phillip.wood@dunelm.org.uk>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="3884854"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:x4PJP9lDGqCsPvgO2KmaRR+lHR8= sha256:PY0rDMg6QNEOFYMsJ4dIEmTsihGKqj4cuSUNsAh9KrY=
	sha1:vLBxpbAsxjCwJJqLRBMR0OZWJvs= sha256:NnqBcBHJGk+Y8+P9Lhl6du98NiGsH/Xj36tb/fS2uDw=
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 B9F8B110D57; Tue, 20 Oct 2026 18:35:04 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -1.948
X-Spam-Status: No, score=-1.948 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.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 eRVOL1yAsODA;
	Tue, 20 Oct 2026 18:35:02 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=104.64.211.4; helo=sin.lore.kernel.org; envelope-from=git+bounces-72553-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 sin.lore.kernel.org (sin.lore.kernel.org [104.64.211.4])
	by mail.dewy.ru (Postfix) with ESMTPS id 7087B110D53; Tue, 20 Oct 2026 18:34:52 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sin.lore.kernel.org (Postfix) with ESMTP id 93DE2366A4; Tue, 20 Oct 2026 15:21:21 +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=1792509665;
	b=wGeiI+3pPxWgQpOGXwymF/a29+8MAPVBQAoeHEsI43MnR+AYksIy8kz0qK7r+T1nDxTf
	 ST76GhDJa5JNy9gn/OenHxIJ74NIfzYEFfzceXDBPQ4Vfok/NYbyYsXJRbQMtYhcTEs8W
	 5YE7YSOpDdlClK2h5ygyImXqWkt2hIi+6WGTTGwqJSk+D5F73WX92SMLVxPgOYvrprX41
	 Swe95mvdLk29fErK/REZ3VeCpP3EclQPBLxRQ91T3Hwz4+OO5NBE059JnI1jtT+r/evbS
	 hy5M7/IkStNzxXmk14NJfYXy3TXTzh+DDIxLtREB39nN/YcJ0sYq0HuTEX8hB3Q6pzA==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1792509665;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:
	 X-Google-DKIM-Signature:X-Gm-Message-State:X-Gm-Gg:X-Received:
	 Received:From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:
	 References:Reply-To:X-Mailing-List:List-Id:List-Post:List-Help:
	 List-Subscribe:List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding;
	bh=n/xzFBhBid8rjWWdzadBRA9xnCIBg67YKwhkDAQRonc=;
	b=0o+XRTV5DpJszxlR/MFVnFIsDswDvzUgN62rP16tGhBQWKLIrcsjvHudQ5/fuvFiOecy
	 rPCCpZvYvBAVM2sJmY7UjOdlB7Ws/AF4L+tJE21uKrkpWUkBurC+mMNQwiuhNUXG4hK/r
	 N+g/lmFpsQ94mMYJJqilo5VfGZePacILVyCg7P65+mnSGMrAE4e4y3wTqVLUu2waBo+Iw
	 EOt1m4ipSJvtgp2ba2N+5gtoiuwNV3AQvrSpAlD5CIXaM99qMOH93cyjaxbM/ztcGgXk6
	 Yr3+q1n7gHbKDdMWu305l3Fts1dxsLUlHLX0NXvKLzrC7A9ZtrAZxE/YkmLVSKshVpw==
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=20251104 header.b=YWsst2Et;
	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 93DB960A3815;
	Tue, 20 Oct 2026 15:21:04 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20251104 header.b=YWsst2Et
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.41
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1792509663;
	b=uLCRUUR/IgUFFBuMZDowSH0c6UHeLZ2BTGkeu8AUGzJ9EZWntIqz+zqEEbWRZjiY8mId
	 Zk8+K1XwPmNtHE14PV8OkvD+kE74L3v3OwL1jhDTG1s93EyK7hUbosHftz6MqcfEw7UNI
	 gR671XMbfryWasj5OlPgzc62gkyrSkTR6Ot+eb+TWu+ijPImUTbvEl71E/iy6dZRPFPck
	 rTgqSmB8tHuMi4ZU+u5+BDdeaV329cfqzP0TKGGKcKj1sqXmjtUnYf03w1T65lOS21EPK
	 qkgTtm9ajEMnAdj5WWDe2GxwAZE/hoJNKEMG6+/wqzWZJZzDVeALzXcMkB9cgVT6J5A==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1792509663;
	h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature:
	 X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject:
	 Date:Message-ID:X-Mailer:In-Reply-To:References:Reply-To:MIME-Version:
	 Content-Transfer-Encoding;
	bh=n/xzFBhBid8rjWWdzadBRA9xnCIBg67YKwhkDAQRonc=;
	b=e9rFBEbAexmepCzcDVmnqMt1yTOFDrH2eDiuENJK3RgCy7yRVm7OB9WVb1pBKdrESytR
	 YH+Zg+4eLbSwLb/Czb/YwCJcnCwsxOzTlcrAdKCZT96hyIIyf2DIX2BmwzjwrSkPpkbth
	 N7orDnZaMsxEXCCnwGZq6OKoP3PUSsewgag24z1CVMMNiGI4OUEd3rfRMyXIOtomWwNSU
	 A03IF80DblRGbYGAUUInWLp8NbtqF/3Hw/+F/gN4rVZnxlqmyvSROFJaOxjks2Rps3JcT
	 hlDNE+IVIJgrDQmMuVwD3eHhY2qWmraiKIO4ds47ktuiR+oNKAN+zAupX5aanXJJ7Zw==
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=20251104 header.b=YWsst2Et;
	arc=none smtp.remote-ip=209.85.221.41
X-Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41])
	(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 5D83B606A321
	for <git@vger.kernel.org>; Tue, 20 Oct 2026 15:21:03 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5D83B606A321
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-wr1-f41.google.com with SMTP id ffacd0b85a97d-48dfc0421d9so2437282f8f.1
        for <git@vger.kernel.org>; Tue, 20 Oct 2026 08:21:03 -0700 (PDT)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20251104; t=1792509662; x=1793114462; darn=vger.kernel.org;
        h=content-transfer-encoding:mime-version:reply-to:references
         :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject
         :date:message-id:reply-to:content-type;
        bh=n/xzFBhBid8rjWWdzadBRA9xnCIBg67YKwhkDAQRonc=;
        b=YWsst2EtgZ0shqMt0/do8LWJ38meTIvBtTGXdbOWXaw87pUl3/BfuoxtcQEDjASccp
         /R2iRT4b7PSEE3OP4NeFNVlUwWkZYlOeQ+1B6u3IObGfdVPgHwca7dU32QK5o/+Di6RT
         ozDNal8oDG6BsnvVutKuid3Le9bbpXfHSUaLOTO5DTmPrfc5ubfSxII/3KFWJks+IBFY
         4pZnAqz9EGzVnetZTK/v4Zd3oOI951YIRYjNtGKPbw6pHKYmqN+XZTjowzH9iVHxL3bQ
         Nn0dUad1x4oIklIe1XTEeSqj4cM4VmyD/LoHc08J90Fmhj8T/rfzGOFuJe8SgA7z75hw
         YusA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20260707; t=1792509662; x=1793114462;
        h=content-transfer-encoding:mime-version:reply-to:references
         :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg
         :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to
         :content-type;
        bh=n/xzFBhBid8rjWWdzadBRA9xnCIBg67YKwhkDAQRonc=;
        b=pZlDVA9FribIv/jIqniNYLancvr3GiwWYTvpyy08V6Pc2gYyBrAhR6cO0oSX9hoegW
         0ysELWOZRRR7eZD6eipxJMd0VE9XmIJOmg+PgNzZd9IQDKITcwHb/wmbZ6sSvPmuqKrK
         LGy7JU4tIXRL/03NjfBImlnbNvc/16AHgHixigtU29dlclhbwhX/2smGu9OG3nmnS6GE
         iui5qzvywsETo3e60+9UTpoLp7m0CooXkOquduUOUq//eOdYkzgOB+l0n0I8Qeq0JW6t
         7fNH7TkhRKun38C4eG64qiKp/eQh+W8zos3UNOeoxssQfW8F40pl2QeJ3byfrG6W/R7k
         8MHw==
X-Gm-Message-State: AFq9FYL9E9A1QdZo19xfBjesvN8Y+ilO0qceH9jmTzQANAv8VI5Nztgo
	MtuYwD3/xkunULdG/W51N3MzB/KEoAWCgnWs6NVxWVPRDz3sIpUiGC/2HFBfLQ==
X-Gm-Gg: AYBFou2jMAwVjJnM182mCiVSDDmOwmUiJZbtz5BHfvQl8i30lXERqg5U75rAjuwYFIw
	oOPRI+uOGN3QQxszE4DA1NQ8ViEixpBIOzWkPsI3DXMYQgomYPgg83XCVP8E0uJJkdD+S8wny3r
	NXMxM7Vv6QXTpoI6omR3D91wjSzQrtjuuNCUQusJZFH9VSm02wyikrcK4XLM7kmWaC/8i3GtDqS
	J07hrwXRh9PM2DqFQ9vu/wxa7IpBar3UNSLdvpigr5crtRjiH/eFUihhUkazWF28U0C6pcEimAV
	6xGYre4K8ZRNBpUFUqb25wGZWqu+QaiFhXKwFy64UKWofVq84sQ1nCWzuQZPbwmww6PoXHElKX1
	546X5ZU4IYmmzKWsWzjtMIom9pDE5YOv+nhS58NcBsM+YCMSXJbQVc4S1/+b5tw7fhVDtrOW+eG
	KV8c7/m/QiYx+ZkTCw0pO5nPo4IwrybR3N4MiSk4qppt8Ntup/wmfPpS9wLjTXqGOR/NmL2BKcI
	g==
X-Received: by 2002:a05:6000:1865:b0:48d:f7ef:cd1d with SMTP id ffacd0b85a97d-48e07bb2cc8mr27666724f8f.9.1792509661582;
        Tue, 20 Oct 2026 08:21:01 -0700 (PDT)
X-Received: from berwick ([2a0a:ef40:724:6601:f3ff:aebc:61f8:d91f])
        by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-48e1d823ca7sm6307355f8f.38.2026.10.20.08.21.00
        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
        Tue, 20 Oct 2026 08:21:01 -0700 (PDT)
X-Cc: 重田一聖 <kazumasa.shigeta@kanamei.com>,
	Phillip Wood <phillip.wood123@gmail.com>
X-Mailer: git-send-email 2.56.0.134.g299a3c16181
X-In-Reply-To: <cover.1792509606.git.phillip.wood@dunelm.org.uk>
X-Reply-To: Phillip Wood <phillip.wood@dunelm.org.uk>
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