git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Oct 23, 2026, 15:03 UTC
Message-ID
<b62d3d080a8ff0d9e14741a886aa45d70019bd64.1792767795.git.phillip.wood@dunelm.org.uk>
In-Reply-To
<cover.1792767794.git.phillip.wood@dunelm.org.uk>
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 8db75d9e135..e17f75641a4 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
Previous: Phillip WoodNext: Junio C Hamano
Message 41 of 47 in “stash: stop checking for changes twice”
  1. 0/2 stash: stop checking for changes twicePhillip Wood, Oct 5, 2026
  2. 1/2 stash create: remove duplicate changes detectionPhillip Wood, Oct 5, 2026
  3. Junio C HamanoOct 6, 2026
  4. Phillip WoodOct 7, 2026
  5. 2/2 stash push: remove duplicate changes detectionPhillip Wood, Oct 5, 2026
  6. Junio C HamanoOct 6, 2026
  7. Phillip WoodOct 7, 2026
  8. Junio C HamanoOct 7, 2026
  9. Phillip WoodOct 8, 2026
  10. 0/7 stash creation: avoid unecessary index loads and refreshesPhillip Wood, Oct 14, 2026
  11. 1/7 stash: use an enum for create statusPhillip Wood, Oct 14, 2026
  12. 2/7 stash create: remove duplicate changes detectionPhillip Wood, Oct 14, 2026
  13. 3/7 stash push: remove duplicate changes detectionPhillip Wood, Oct 14, 2026
  14. 4/7 stash push: avoid duplicate calls to preload_index()Phillip Wood, Oct 14, 2026
  15. Junio C HamanoOct 14, 2026
  16. 5/7 stash create: rework temporary index creationPhillip Wood, Oct 14, 2026
  17. Junio C HamanoOct 14, 2026
  18. Phillip WoodOct 20, 2026
  19. 6/7 stash create: remove unnecessary index load and refreshPhillip Wood, Oct 14, 2026
  20. Junio C HamanoOct 14, 2026
  21. 7/7 stash: simplify reset_tree() function signaturePhillip Wood, Oct 14, 2026
  22. 0/7 stash creation: avoid unecessary index loads and refreshesPhillip Wood, Oct 20, 2026
  23. 1/7 stash: use an enum for create statusPhillip Wood, Oct 20, 2026
  24. Junio C HamanoOct 21, 2026
  25. Phillip WoodOct 22, 2026
  26. Junio C HamanoOct 22, 2026
  27. Phillip WoodOct 22, 2026
  28. 2/7 stash create: remove duplicate changes detectionPhillip Wood, Oct 20, 2026
  29. 3/7 stash push: remove duplicate changes detectionPhillip Wood, Oct 20, 2026
  30. 4/7 stash push: avoid duplicate calls to preload_index()Phillip Wood, Oct 20, 2026
  31. 5/7 stash create: rework temporary index creationPhillip Wood, Oct 20, 2026
  32. Junio C HamanoOct 20, 2026
  33. 6/7 stash create: remove unnecessary index load and refreshPhillip Wood, Oct 20, 2026
  34. 7/7 stash: simplify reset_tree() function signaturePhillip Wood, Oct 20, 2026
  35. Junio C HamanoOct 21, 2026
  36. 0/7 stash creation: avoid unecessary index loads and refreshesPhillip Wood, Oct 23, 2026
  37. 1/7 stash: use an enum for create statusPhillip Wood, Oct 23, 2026
  38. 2/7 stash create: remove duplicate changes detectionPhillip Wood, Oct 23, 2026
  39. 3/7 stash push: remove duplicate changes detectionPhillip Wood, Oct 23, 2026
  40. 4/7 stash push: avoid duplicate calls to preload_index()Phillip Wood, Oct 23, 2026
  41. 5/7 stash create: rework temporary index creationPhillip Wood, Oct 23, 2026
  42. Junio C HamanoNov 5, 2026
  43. 6/7 stash create: remove unnecessary index load and refreshPhillip Wood, Oct 23, 2026
  44. Junio C HamanoNov 6, 2026
  45. 7/7 stash: simplify reset_tree() function signaturePhillip Wood, Oct 23, 2026
  46. Junio C HamanoNov 6, 2026
  47. Junio C HamanoNov 5, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.