🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.git
12 messages
12 total messages Started by Phillip Wood Fri, 23 Oct 2026 15:03
[PATCH v4 0/7] stash creation: avoid unecessary index loads and refreshes
#1289
Author: Phillip Wood
Date: Fri, 23 Oct 2026 15:03
256 lines
20723 bytes
"git stash push" and "git stash create" load and refresh the index
multiple times when they only need to do it once. This series removes
a number of calls to repo_read_preload_index() and refresh_index()
which reduces the number of lstat() calls on each changed path by
four for "git stash create" and eight for "git stash push". It also
removes a duplicate call to check_changes() so that when stashing
untracked files we only walk them once.

Thanks to Junio for his comments on V3

Changes since V3:
 - Avoid implict cast of enum create_result to int in create_stash()
 - Fixed enum initializers
 - Use switch() rather than if() when checking enum create_result
   return values

Changes since V2:
 - Fixed a typo in patch 4
 - Rewrote commit message for patch 5

Changes since V1:
 - Added an enum for the various return values from do_create_stash()
 - Fixed typos in the commit message of patch 2 (was patch 1)
 - Removed an unnecessary index refresh and several unnecessary index
   preloads

Cover letter for V1:

"git stash push" and "git stash create" check if there are
any unstaged or uncommitted changes at startup, and then again
when they try to create the stash. This short series removes
that duplication of effort which I spotted while looking at
<20260929074222.11942-1-kazumasa.shigeta@kanamei.com>.

base-commit: c46c1e37724f0478939de636ab8ea5a89086d532
Published-As: https://github.com/phillipwood/git/releases/tag/pw%2Fstash-optimize-check_changes-calls%2Fv4
View-Changes-At: https://github.com/phillipwood/git/compare/c46c1e377...f61ebbf9a
Fetch-It-Via: git fetch https://github.com/phillipwood/git pw/stash-optimize-check_changes-calls/v4


Phillip Wood (7):
  stash: use an enum for create status
  stash create: remove duplicate changes detection
  stash push: remove duplicate changes detection
  stash push: avoid duplicate calls to preload_index()
  stash create: rework temporary index creation
  stash create: remove unnecessary index load and refresh
  stash: simplify reset_tree() function signature

 builtin/stash.c | 167 ++++++++++++++++++++++++------------------------
 1 file changed, 85 insertions(+), 82 deletions(-)

Range-diff against v3:
1:  9e27bcd676e ! 1:  685914011f9 stash: use an enum for create status
    @@ builtin/stash.c: static int save_untracked_files(struct stash_info *info, struct
     +				  struct strbuf *out_patch, int quiet)
      {
     -	int ret = 0;
    -+	enum create_result ret = 0;
    ++	enum create_result ret = CREATE_SUCCESS;
      	struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
      	struct index_state istate = INDEX_STATE_INIT(the_repository);
      
    @@ builtin/stash.c: static int stash_staged(struct stash_info *info, struct strbuf
     +		struct interactive_options *interactive_opts)
      {
     -	int ret = 0;
    -+	enum create_result ret = 0;
    ++	enum create_result ret = CREATE_SUCCESS;
      	struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
      	struct commit *head_commit;
      	const struct object_id *head_tree;
    @@ builtin/stash.c: static int stash_working_tree(struct stash_info *info, const st
     +		int quiet)
      {
     -	int ret = 0;
    -+	enum create_result ret = 0;
    ++	enum create_result ret = CREATE_SUCCESS;
      	int flags = 0;
      	int untracked_commit_option = 0;
      	const char *head_short_sha1 = NULL;
    @@ builtin/stash.c: static int do_create_stash(const struct pathspec *ps, struct st
      	if (patch_mode) {
      		ret = stash_patch(info, ps, patch, quiet, interactive_opts);
     -		if (ret < 0) {
    -+		if (ret == CREATE_ERROR) {
    ++		switch (ret) {
    ++		case CREATE_ERROR:
      			if (!quiet)
      				fprintf_ln(stderr, _("Cannot save the current "
      						     "worktree state"));
    - 			goto done;
    +-			goto done;
     -		} else if (ret > 0) {
    -+		} else if (ret != CREATE_SUCCESS) {
    - 			goto done;
    +-			goto done;
    ++			/* fallthrough */
    ++		case CREATE_EMPTY:
    ++			goto done;
    ++
    ++		case CREATE_SUCCESS:
    ++			break;
      		}
      	} else if (only_staged) {
      		ret = stash_staged(info, patch, quiet);
     -		if (ret < 0) {
    -+		if (ret == CREATE_ERROR) {
    ++		switch (ret) {
    ++			case CREATE_ERROR:
      			if (!quiet)
      				fprintf_ln(stderr, _("Cannot save the current "
      						     "staged state"));
    - 			goto done;
    +-			goto done;
     -		} else if (ret > 0) {
    -+		} else if (ret != CREATE_SUCCESS) {
    - 			goto done;
    +-			goto done;
    ++			/* fallthrough */
    ++			case CREATE_EMPTY:
    ++				goto done;
    ++
    ++			case CREATE_SUCCESS:
    ++				break;
      		}
      	} else {
      		if (stash_working_tree(info, ps)) {
    @@ builtin/stash.c: static int do_create_stash(const struct pathspec *ps, struct st
      		goto done;
      	}
      
    +@@ builtin/stash.c: static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
    + static int create_stash(int argc, const char **argv, const char *prefix UNUSED,
    + 			struct repository *repo UNUSED)
    + {
    +-	int ret;
    ++	enum create_result create_res;
    + 	struct strbuf stash_msg_buf = STRBUF_INIT;
    + 	struct stash_info info = STASH_INFO_INIT;
    + 	struct pathspec ps;
     @@ builtin/stash.c: static int create_stash(int argc, const char **argv, const char *prefix UNUSED,
    + 	if (!check_changes_tracked_files(&ps))
    + 		return 0;
      
    - 	ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,
    - 			      NULL, 0);
    +-	ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,
    +-			      NULL, 0);
     -	if (!ret)
    -+	if (ret == CREATE_SUCCESS)
    ++	create_res = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,
    ++				     NULL, 0);
    ++	if (create_res == CREATE_SUCCESS)
      		printf_ln("%s", oid_to_hex(&info.w_commit));
      
      	free_stash_info(&info);
      	strbuf_release(&stash_msg_buf);
     -	return ret;
     +	/* Do not return an error if there are no changes */
    -+	return ret == CREATE_ERROR ? -1 : 0;
    ++	return create_res == CREATE_ERROR ? -1 : 0;
      }
      
      static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,
    @@ builtin/stash.c: static int do_push_stash(const struct pathspec *ps, const char
     +	create_res =  do_create_stash(ps, &stash_msg_buf, include_untracked,
     +				      patch_mode, interactive_opts, only_staged,
     +				      &info, &patch, quiet);
    -+	if (create_res != CREATE_SUCCESS) {
    ++	switch (create_res) {
    ++	case CREATE_ERROR:
    ++	case CREATE_EMPTY:
      		ret = -1;
      		goto done;
    ++
    ++	case CREATE_SUCCESS:
    ++		break;
      	}
    + 
    + 	if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
2:  d56b7c6f672 ! 2:  d2773fc82eb stash create: remove duplicate changes detection
    @@ builtin/stash.c: static int create_stash(int argc, const char **argv, const char
     -	if (!check_changes_tracked_files(&ps))
     -		return 0;
      
    - 	ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,
    - 			      NULL, 0);
    + 	create_res = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,
    + 				     NULL, 0);
3:  36fc0eb9130 ! 3:  785d2a663da stash push: remove duplicate changes detection
    @@ builtin/stash.c: static enum create_result do_create_stash(const struct pathspec
      		goto done;
      	}
      
    +@@ builtin/stash.c: static enum create_result do_create_stash(const struct pathspec *ps,
    + 						     "worktree state"));
    + 			/* fallthrough */
    + 		case CREATE_EMPTY:
    ++		case CREATE_NO_CHANGES:
    + 			goto done;
    + 
    + 		case CREATE_SUCCESS:
    +@@ builtin/stash.c: static enum create_result do_create_stash(const struct pathspec *ps,
    + 						     "staged state"));
    + 			/* fallthrough */
    + 			case CREATE_EMPTY:
    ++			case CREATE_NO_CHANGES:
    + 				goto done;
    + 
    + 			case CREATE_SUCCESS:
     @@ builtin/stash.c: static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
      		free(ps_matched);
      	}
    @@ builtin/stash.c: static int do_push_stash(const struct pathspec *ps, const char
      		ret = -1;
      		if (!quiet)
     @@ builtin/stash.c: static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
    - 	create_res =  do_create_stash(ps, &stash_msg_buf, include_untracked,
      				      patch_mode, interactive_opts, only_staged,
      				      &info, &patch, quiet);
    --	if (create_res != CREATE_SUCCESS) {
    -+	if (create_res == CREATE_NO_CHANGES) {
    + 	switch (create_res) {
    ++	case CREATE_NO_CHANGES:
     +		if (!quiet)
     +			printf_ln(_("No local changes to save"));
     +		ret = 0;
     +		goto done;
    -+	} else if (create_res != CREATE_SUCCESS) {
    ++
    + 	case CREATE_ERROR:
    + 	case CREATE_EMPTY:
      		ret = -1;
    - 		goto done;
    - 	}
4:  a9875ba5b35 ! 4:  32f7c97b7c0 stash push: avoid duplicate calls to preload_index()
    @@ builtin/stash.c: static int create_stash(int argc, const char **argv, const char
      	memset(&ps, 0, sizeof(ps));
      
     +	repo_read_index(the_repository);
    - 	ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,
    - 			      NULL, 0);
    - 	if (ret == CREATE_SUCCESS)
    + 	create_res = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,
    + 				     NULL, 0);
    + 	if (create_res == CREATE_SUCCESS)
     @@ builtin/stash.c: static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
      		goto done;
      	}
5:  de5e0c196a9 = 5:  b62d3d080a8 stash create: rework temporary index creation
6:  61e4ce22a0f = 6:  8c54c118307 stash create: remove unnecessary index load and refresh
7:  a486a625c95 = 7:  f61ebbf9a3e stash: simplify reset_tree() function signature
-- 
2.56.0.134.g299a3c16181
[PATCH v4 1/7] stash: use an enum for create status
#1290
Author: Phillip Wood
Date: Fri, 23 Oct 2026 15:03
300 lines
20118 bytes
From: Phillip Wood <phillip.wood@dunelm.org.uk>

Change the return type of do_create_stash() to an enum in preparation
for adding a separate value for "there are no changes to stash" in a
future commit. There is a small change to behavior here which fixes
an anomaly with the exit status of create_stash(). That function calls
check_changes() without refreshing the index and exits 0 if there are
no changes. However, because it does not refresh the index, that check
is unreliable which means do_create_stash() can return 1 when there
aren't any changes, and then create_stash() exits 1 rather than 0.
This commit fixes that anomaly.

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

diff --git a/builtin/stash.c b/builtin/stash.c
index 7a9843413b1..53744d96e84 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -1336,16 +1336,23 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,
 	return ret;
 }
 
-static int stash_staged(struct stash_info *info, struct strbuf *out_patch,
-			int quiet)
+enum create_result {
+	CREATE_ERROR = -1,
+	CREATE_SUCCESS,
+	/* No changes were stashed */
+	CREATE_EMPTY,
+};
+
+static enum create_result stash_staged(struct stash_info *info,
+				  struct strbuf *out_patch, int quiet)
 {
-	int ret = 0;
+	enum create_result ret = CREATE_SUCCESS;
 	struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
 	struct index_state istate = INDEX_STATE_INIT(the_repository);
 
 	if (write_index_as_tree(&info->w_tree, &istate, the_repository->index_file,
 				0, NULL)) {
-		ret = -1;
+		ret = CREATE_ERROR;
 		goto done;
 	}
 
@@ -1354,26 +1361,26 @@ static int stash_staged(struct stash_info *info, struct strbuf *out_patch,
 		     "--no-color",
 		     "-U1", "HEAD", oid_to_hex(&info->w_tree), "--", NULL);
 	if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
-		ret = -1;
+		ret = CREATE_ERROR;
 		goto done;
 	}
 
 	if (!out_patch->len) {
 		if (!quiet)
 			fprintf_ln(stderr, _("No staged changes"));
-		ret = 1;
+		ret = CREATE_EMPTY;
 	}
 
 done:
 	release_index(&istate);
 	return ret;
 }
 
-static int stash_patch(struct stash_info *info, const struct pathspec *ps,
-		       struct strbuf *out_patch, int quiet,
-		       struct interactive_options *interactive_opts)
+static enum create_result stash_patch(struct stash_info *info,
+		const struct pathspec *ps, struct strbuf *out_patch, int quiet,
+		struct interactive_options *interactive_opts)
 {
-	int ret = 0;
+	enum create_result ret = CREATE_SUCCESS;
 	struct child_process cp_diff_tree = CHILD_PROCESS_INIT;
 	struct commit *head_commit;
 	const struct object_id *head_tree;
@@ -1384,17 +1391,17 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
 
 	head_commit = lookup_commit(the_repository, &info->b_commit);
 	if (!head_commit || repo_parse_commit(the_repository, head_commit)) {
-		ret = -1;
+		ret = CREATE_ERROR;
 		goto done;
 	}
 	head_tree = get_commit_tree_oid(head_commit);
 	if (!head_tree) {
-		ret = -1;
+		ret = CREATE_ERROR;
 		goto done;
 	}
 
 	if (create_index_from_tree(head_tree, stash_index_path.buf)) {
-		ret = -1;
+		ret = CREATE_ERROR;
 		goto done;
 	}
 
@@ -1416,7 +1423,7 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
 	/* State of the working tree. */
 	if (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,
 				NULL)) {
-		ret = -1;
+		ret = CREATE_ERROR;
 		goto done;
 	}
 
@@ -1425,14 +1432,14 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,
 		     "--no-color",
 		     oid_to_hex(&info->w_tree), "--", NULL);
 	if (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {
-		ret = -1;
+		ret = CREATE_ERROR;
 		goto done;
 	}
 
 	if (!out_patch->len) {
 		if (!quiet)
 			fprintf_ln(stderr, _("No changes selected"));
-		ret = 1;
+		ret = CREATE_EMPTY;
 	}
 
 done:
@@ -1499,13 +1506,13 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps
 	return ret;
 }
 
-static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_buf,
-			   int include_untracked, int patch_mode,
-			   struct interactive_options *interactive_opts,
-			   int only_staged, struct stash_info *info, struct strbuf *patch,
-			   int quiet)
+static enum create_result do_create_stash(const struct pathspec *ps,
+		struct strbuf *stash_msg_buf, int include_untracked,
+		int patch_mode, struct interactive_options *interactive_opts,
+		int only_staged, struct stash_info *info, struct strbuf *patch,
+		int quiet)
 {
-	int ret = 0;
+	enum create_result ret = CREATE_SUCCESS;
 	int flags = 0;
 	int untracked_commit_option = 0;
 	const char *head_short_sha1 = NULL;
@@ -1531,14 +1538,14 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
 		if (!quiet)
 			fprintf_ln(stderr, _("You do not have "
 					     "the initial commit yet"));
-		ret = -1;
+		ret = CREATE_ERROR;
 		goto done;
 	} else {
 		head_commit = lookup_commit(the_repository, &info->b_commit);
 	}
 
 	if (!check_changes(ps, include_untracked, &untracked_files)) {
-		ret = 1;
+		ret = CREATE_EMPTY;
 		goto done;
 	}
 
@@ -1565,7 +1572,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
 		if (!quiet)
 			fprintf_ln(stderr, _("Cannot save the current "
 					     "index state"));
-		ret = -1;
+		ret = CREATE_ERROR;
 		goto done;
 	}
 
@@ -1577,37 +1584,45 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
 			if (!quiet)
 				fprintf_ln(stderr, _("Cannot save "
 						     "the untracked files"));
-			ret = -1;
+			ret = CREATE_ERROR;
 			goto done;
 		}
 		untracked_commit_option = 1;
 	}
 	if (patch_mode) {
 		ret = stash_patch(info, ps, patch, quiet, interactive_opts);
-		if (ret < 0) {
+		switch (ret) {
+		case CREATE_ERROR:
 			if (!quiet)
 				fprintf_ln(stderr, _("Cannot save the current "
 						     "worktree state"));
-			goto done;
-		} else if (ret > 0) {
-			goto done;
+			/* fallthrough */
+		case CREATE_EMPTY:
+			goto done;
+
+		case CREATE_SUCCESS:
+			break;
 		}
 	} else if (only_staged) {
 		ret = stash_staged(info, patch, quiet);
-		if (ret < 0) {
+		switch (ret) {
+			case CREATE_ERROR:
 			if (!quiet)
 				fprintf_ln(stderr, _("Cannot save the current "
 						     "staged state"));
-			goto done;
-		} else if (ret > 0) {
-			goto done;
+			/* fallthrough */
+			case CREATE_EMPTY:
+				goto done;
+
+			case CREATE_SUCCESS:
+				break;
 		}
 	} else {
 		if (stash_working_tree(info, ps)) {
 			if (!quiet)
 				fprintf_ln(stderr, _("Cannot save the current "
 						     "worktree state"));
-			ret = -1;
+			ret = CREATE_ERROR;
 			goto done;
 		}
 	}
@@ -1630,7 +1645,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
 		if (!quiet)
 			fprintf_ln(stderr, _("Cannot record "
 					     "working tree state"));
-		ret = -1;
+		ret = CREATE_ERROR;
 		goto done;
 	}
 
@@ -1646,7 +1661,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
 static int create_stash(int argc, const char **argv, const char *prefix UNUSED,
 			struct repository *repo UNUSED)
 {
-	int ret;
+	enum create_result create_res;
 	struct strbuf stash_msg_buf = STRBUF_INIT;
 	struct stash_info info = STASH_INFO_INIT;
 	struct pathspec ps;
@@ -1658,14 +1673,15 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,
 	if (!check_changes_tracked_files(&ps))
 		return 0;
 
-	ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,
-			      NULL, 0);
-	if (!ret)
+	create_res = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,
+				     NULL, 0);
+	if (create_res == CREATE_SUCCESS)
 		printf_ln("%s", oid_to_hex(&info.w_commit));
 
 	free_stash_info(&info);
 	strbuf_release(&stash_msg_buf);
-	return ret;
+	/* Do not return an error if there are no changes */
+	return create_res == CREATE_ERROR ? -1 : 0;
 }
 
 static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,
@@ -1674,6 +1690,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
 			 int include_untracked, int only_staged)
 {
 	int ret = 0;
+	enum create_result create_res;
 	struct stash_info info = STASH_INFO_INIT;
 	struct strbuf patch = STRBUF_INIT;
 	struct strbuf stash_msg_buf = STRBUF_INIT;
@@ -1741,10 +1758,17 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
 
 	if (stash_msg)
 		strbuf_addstr(&stash_msg_buf, stash_msg);
-	if (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,
-			    interactive_opts, only_staged, &info, &patch, quiet)) {
+	create_res =  do_create_stash(ps, &stash_msg_buf, include_untracked,
+				      patch_mode, interactive_opts, only_staged,
+				      &info, &patch, quiet);
+	switch (create_res) {
+	case CREATE_ERROR:
+	case CREATE_EMPTY:
 		ret = -1;
 		goto done;
+
+	case CREATE_SUCCESS:
+		break;
 	}
 
 	if (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {
-- 
2.56.0.134.g299a3c16181
[PATCH v4 2/7] stash create: remove duplicate changes detection
#1291
Author: Phillip Wood
Date: Fri, 23 Oct 2026 15:03
55 lines
12863 bytes
From: Phillip Wood <phillip.wood@dunelm.org.uk>

Before it creates a stash, git checks if there are any unstaged,
or uncommitted changes. If there isn't anything to stash it bails
out. Since ef0f0b4509 (stash: optimize `get_untracked_files()`
and `check_changes()`, 2019-02-25) "git stash create" has checked
for changes twice, once in create_stash() before we refresh the
index and then again in do_create_stash() after the index has been
refreshed. That commit claims it is an optimization but it is not
clear what it is trying to optimize by checking for changes twice,
especially as checking for changes before refreshing the index is
unreliable (the scripted version of "git stash create", called "git
update-index -q --refresh" before looking for any changes).

Avoid checking for changes twice by removing the call to
check_changes_tracked_files() from create_stash(). In principle we
could remove the call to check_changes() from do_create_stash()
instead, but it is more convenient to keep it there, rather than
require each caller to call it instead.

The remaining callers of check_changes_tracked_files() sensibly read
and refresh the index before calling it, so it no-longer needs to
call to repo_read_index().

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

diff --git a/builtin/stash.c b/builtin/stash.c
index 53744d96e84..5137456da7d 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -1248,9 +1248,6 @@ static int check_changes_tracked_files(const struct pathspec *ps)
 	if (repo_get_oid(the_repository, "HEAD", &dummy))
 		return -1;
 
-	if (repo_read_index(the_repository) < 0)
-		return -1;
-
 	repo_init_revisions(the_repository, &rev, NULL);
 	copy_pathspec(&rev.prune_data, ps);
 
@@ -1670,8 +1667,6 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,
 	strbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');
 
 	memset(&ps, 0, sizeof(ps));
-	if (!check_changes_tracked_files(&ps))
-		return 0;
 
 	create_res = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,
 				     NULL, 0);
-- 
2.56.0.134.g299a3c16181
[PATCH v4 3/7] stash push: remove duplicate changes detection
#1285
Author: Phillip Wood
Date: Fri, 23 Oct 2026 15:03
105 lines
14502 bytes
From: Phillip Wood <phillip.wood@dunelm.org.uk>

Before it creates a stash, git checks if there are any unstaged, or
uncommitted changes. If there isn't anything to stash it bails out. "git
stash push" checks for changes twice, once in do_push_stash() and then
again in do_create_stash(). While duplicating the checks for staged and
unstaged charges is fairly cheap because they stop after finding the
first changed path, if we're stashing untracked files then we visit
them all twice. Avoid that by removing the call to check_changes()
from do_push_stash() and checking the return value of do_create_stash()
to see if there were any changes to stash. If check_changes() finds
there are no changes do_create_stash() now returns CREATE_NO_CHANGES
rather than CREATE_EMPTY. This enables us to distinguish between there
being no changes and there being nothing stashed, so that "git stash
push --patch" when nothing is selected, and "git stash push --staged"
when the index matches HEAD, still exit 1 rather than 0.

As we're not calling check_changes() in do_push_stash() anymore, we do
not need to refresh the index there either because do_create_stash()
refreshes it again before calling check_changes(). Avoiding the extra
index refresh saves us two lstat() calls per changed path.

There is still one small change in behavior as, if there is nothing
to stash, we'll now try to create the reflog for stashes before we
realize that there is nothing to stash. I don't think that should
matter in practice.

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

diff --git a/builtin/stash.c b/builtin/stash.c
index 5137456da7d..7259f6374ce 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -1338,6 +1338,8 @@ enum create_result {
 	CREATE_SUCCESS,
 	/* No changes were stashed */
 	CREATE_EMPTY,
+	/* There were no changes to stash */
+	CREATE_NO_CHANGES,
 };
 
 static enum create_result stash_staged(struct stash_info *info,
@@ -1542,7 +1544,7 @@ static enum create_result do_create_stash(const struct pathspec *ps,
 	}
 
 	if (!check_changes(ps, include_untracked, &untracked_files)) {
-		ret = CREATE_EMPTY;
+		ret = CREATE_NO_CHANGES;
 		goto done;
 	}
 
@@ -1595,6 +1597,7 @@ static enum create_result do_create_stash(const struct pathspec *ps,
 						     "worktree state"));
 			/* fallthrough */
 		case CREATE_EMPTY:
+		case CREATE_NO_CHANGES:
 			goto done;
 
 		case CREATE_SUCCESS:
@@ -1609,6 +1612,7 @@ static enum create_result do_create_stash(const struct pathspec *ps,
 						     "staged state"));
 			/* fallthrough */
 			case CREATE_EMPTY:
+			case CREATE_NO_CHANGES:
 				goto done;
 
 			case CREATE_SUCCESS:
@@ -1732,18 +1736,6 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
 		free(ps_matched);
 	}
 
-	if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,
-					 NULL, NULL, NULL)) {
-		ret = error(_("could not write index"));
-		goto done;
-	}
-
-	if (!check_changes(ps, include_untracked, &untracked_files)) {
-		if (!quiet)
-			printf_ln(_("No local changes to save"));
-		goto done;
-	}
-
 	if (!refs_reflog_exists(get_main_ref_store(the_repository), ref_stash) && do_clear_stash()) {
 		ret = -1;
 		if (!quiet)
@@ -1757,6 +1749,12 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
 				      patch_mode, interactive_opts, only_staged,
 				      &info, &patch, quiet);
 	switch (create_res) {
+	case CREATE_NO_CHANGES:
+		if (!quiet)
+			printf_ln(_("No local changes to save"));
+		ret = 0;
+		goto done;
+
 	case CREATE_ERROR:
 	case CREATE_EMPTY:
 		ret = -1;
-- 
2.56.0.134.g299a3c16181
[PATCH v4 4/7] stash push: avoid duplicate calls to preload_index()
#1287
Author: Phillip Wood
Date: Fri, 23 Oct 2026 15:03
52 lines
12788 bytes
From: Phillip Wood <phillip.wood@dunelm.org.uk>

do_push_stash() needs to load the index so it can check that any
pathspec argument refers to tracked files. To do that it calls
repo_read_index_preload() which does not just load the index, it also
calls preload_index() which lstat()s all the files to see which ones
are up-to-date. do_create_stash() then calls repo_read_index_preload()
for a second time which lstat()s all the changed paths again and then
calls repo_refresh_and_write_index() which invokes preload_index() for a
third time. So we're preloading the index three times when we only need
to do it once. Avoid this by making the callers of do_create_stash()
responsible for reading the index and use repo_read_index() rather
than repo_read_index_preload(). This saves two lstat() calls per
changed path.

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

diff --git a/builtin/stash.c b/builtin/stash.c
index 7259f6374ce..8db75d9e135 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -1526,7 +1526,6 @@ static enum create_result do_create_stash(const struct pathspec *ps,
 
 	prepare_fallback_ident("git stash", "git@stash");
 
-	repo_read_index_preload(the_repository, NULL, 0);
 	if (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,
 					 NULL, NULL, NULL) < 0) {
 		ret = error(_("could not write index"));
@@ -1672,6 +1671,7 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,
 
 	memset(&ps, 0, sizeof(ps));
 
+	repo_read_index(the_repository);
 	create_res = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,
 				     NULL, 0);
 	if (create_res == CREATE_SUCCESS)
@@ -1717,7 +1717,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q
 		goto done;
 	}
 
-	repo_read_index_preload(the_repository, NULL, 0);
+	repo_read_index(the_repository);
 	if (!include_untracked && ps->nr) {
 		char *ps_matched = xcalloc(ps->nr, 1);
 
-- 
2.56.0.134.g299a3c16181
[PATCH v4 5/7] stash create: rework temporary index creation
#1292
Author: Phillip Wood
Date: Fri, 23 Oct 2026 15:03
95 lines
14578 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 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
[PATCH v4 6/7] stash create: remove unnecessary index load and refresh
#1293
Author: Phillip Wood
Date: Fri, 23 Oct 2026 15:03
29 lines
11679 bytes
From: Phillip Wood <phillip.wood@dunelm.org.uk>

create_index_from_tree() is called either directly or indirectly
from do_create_stash() after it has loaded and refreshed the index,
so we do not need to refresh it again here. This saves three lstat()
calls per changed path.

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

diff --git a/builtin/stash.c b/builtin/stash.c
index e17f75641a4..f97cd4771a5 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -384,9 +384,6 @@ static int create_index_from_tree(const struct object_id *tree_id,
 	struct index_state dst_istate = INDEX_STATE_INIT(the_repository);
 	struct lock_file lock_file = LOCK_INIT;
 
-	repo_read_index_preload(the_repository, NULL, 0);
-	refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);
-
 	hold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);
 
 	memset(&opts, 0, sizeof(opts));
-- 
2.56.0.134.g299a3c16181
[PATCH v4 7/7] stash: simplify reset_tree() function signature
#1288
Author: Phillip Wood
Date: Fri, 23 Oct 2026 15:03
46 lines
12161 bytes
From: Phillip Wood <phillip.wood@dunelm.org.uk>

There is only one caller and it does not want to update the worktree or
reset the index so remove those unused parameters.

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

diff --git a/builtin/stash.c b/builtin/stash.c
index f97cd4771a5..f0e3053ab49 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -332,7 +332,7 @@ static int clear_stash(int argc, const char **argv, const char *prefix,
 	return do_clear_stash();
 }
 
-static int reset_tree(struct object_id *i_tree, int update, int reset)
+static int reset_tree(struct object_id *i_tree)
 {
 	int nr_trees = 1;
 	struct unpack_trees_options opts;
@@ -358,10 +358,6 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)
 	opts.src_index = the_repository->index;
 	opts.dst_index = the_repository->index;
 	opts.merge = 1;
-	opts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0;
-	opts.update = update;
-	if (update)
-		opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
 	opts.fn = oneway_merge;
 
 	if (unpack_trees(nr_trees, t, &opts))
@@ -738,7 +734,7 @@ static enum stash_apply_result do_apply_stash(const char *prefix,
 	}
 
 	if (has_index) {
-		if (reset_tree(&index_tree, 0, 0))
+		if (reset_tree(&index_tree))
 			ret = -1;
 	} else {
 		unstage_changes_unless_new(&c_tree);
-- 
2.56.0.134.g299a3c16181
Re: [PATCH v4 0/7] stash creation: avoid unecessary index loads and refreshes
#2472
Author: Junio C Hamano
Date: Thu, 05 Nov 2026 21:52
19 lines
13229 bytes
Phillip Wood <phillip.wood123@gmail.com> writes:

> Changes since V3:
>  - Avoid implict cast of enum create_result to int in create_stash()
>  - Fixed enum initializers
>  - Use switch() rather than if() when checking enum create_result
>    return values

These changes look very good.

> 5:  de5e0c196a9 = 5:  b62d3d080a8 stash create: rework temporary index creation
> 6:  61e4ce22a0f = 6:  8c54c118307 stash create: remove unnecessary index load and refresh
> 7:  a486a625c95 = 7:  f61ebbf9a3e stash: simplify reset_tree() function signature

I'll have another look at latter half of the series which I didn't
comment on in my earlier review separately.

Thanks.
Re: [PATCH v4 5/7] stash create: rework temporary index creation
#2473
Author: Junio C Hamano
Date: Thu, 05 Nov 2026 21:55
49 lines
14289 bytes
Phillip Wood <phillip.wood123@gmail.com> writes:

> 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 is very nice simplification.

> 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.

Indeed.

> @@ -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;
> -	}
> -

Yup, removal of these is indeed very nice.

>  	add_pending_object(&rev, parse_object(the_repository, &info->b_commit),
>  			   "");
>  	run_diff_index(&rev, 0);
Re: [PATCH v4 6/7] stash create: remove unnecessary index load and refresh
#2490
Author: Junio C Hamano
Date: Fri, 06 Nov 2026 01:59
30 lines
13846 bytes
Phillip Wood <phillip.wood123@gmail.com> writes:

> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>
> create_index_from_tree() is called either directly or indirectly
> from do_create_stash() after it has loaded and refreshed the index,
> so we do not need to refresh it again here. This saves three lstat()
> calls per changed path.
>
> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
> ---
>  builtin/stash.c | 3 ---
>  1 file changed, 3 deletions(-)
>
> diff --git a/builtin/stash.c b/builtin/stash.c
> index e17f75641a4..f97cd4771a5 100644
> --- a/builtin/stash.c
> +++ b/builtin/stash.c
> @@ -384,9 +384,6 @@ static int create_index_from_tree(const struct object_id *tree_id,
>  	struct index_state dst_istate = INDEX_STATE_INIT(the_repository);
>  	struct lock_file lock_file = LOCK_INIT;
>  
> -	repo_read_index_preload(the_repository, NULL, 0);
> -	refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);

Makes sense.  As a file-scope static function, we do not have to
worry about calling context to change without we noticing as much as
a public function.  And I agree that with the current calling sequence,
this refresh is not needed.
Re: [PATCH v4 7/7] stash: simplify reset_tree() function signature
#2489
Author: Junio C Hamano
Date: Fri, 06 Nov 2026 01:59
49 lines
14286 bytes
Phillip Wood <phillip.wood123@gmail.com> writes:

> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>
> There is only one caller and it does not want to update the worktree or
> reset the index so remove those unused parameters.
>
> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
> ---
>  builtin/stash.c | 8 ++------
>  1 file changed, 2 insertions(+), 6 deletions(-)

Ah, an obvious simplification.  Very nice.

>
> diff --git a/builtin/stash.c b/builtin/stash.c
> index f97cd4771a5..f0e3053ab49 100644
> --- a/builtin/stash.c
> +++ b/builtin/stash.c
> @@ -332,7 +332,7 @@ static int clear_stash(int argc, const char **argv, const char *prefix,
>  	return do_clear_stash();
>  }
>  
> -static int reset_tree(struct object_id *i_tree, int update, int reset)
> +static int reset_tree(struct object_id *i_tree)
>  {
>  	int nr_trees = 1;
>  	struct unpack_trees_options opts;
> @@ -358,10 +358,6 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)
>  	opts.src_index = the_repository->index;
>  	opts.dst_index = the_repository->index;
>  	opts.merge = 1;
> -	opts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0;
> -	opts.update = update;
> -	if (update)
> -		opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */
>  	opts.fn = oneway_merge;
>  
>  	if (unpack_trees(nr_trees, t, &opts))
> @@ -738,7 +734,7 @@ static enum stash_apply_result do_apply_stash(const char *prefix,
>  	}
>  
>  	if (has_index) {
> -		if (reset_tree(&index_tree, 0, 0))
> +		if (reset_tree(&index_tree))
>  			ret = -1;
>  	} else {
>  		unstage_changes_unless_new(&c_tree);
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads