[PATCH v2 2/7] stash create: remove duplicate changes detection
- From
- Phillip Wood <phillip.wood123@gmail.com>
- Date
- Oct 14, 2026, 15:40 UTC
- Message-ID
- <d56b7c6f67274175416f4d5344675a9679d31f96.1791992404.git.phillip.wood@dunelm.org.uk>
- In-Reply-To
- <cover.1791992404.git.phillip.wood@dunelm.org.uk>
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 3e50ccfb40..f461f002e5 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); @@ -1662,8 +1659,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; ret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info, NULL, 0);
-- 2.56.0.134.g299a3c16181