[PATCH v2 3/7] stash push: remove duplicate changes detection
- From
- Phillip Wood <phillip.wood123@gmail.com>
- Date
- Oct 14, 2026, 15:40 UTC
- Message-ID
- <36fc0eb9130bda0ccae507433b6780c01fb28509.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. "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 | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-)
diff --git a/builtin/stash.c b/builtin/stash.c index f461f002e5..aec79ed473 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; } @@ -1724,18 +1726,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) @@ -1748,7 +1738,12 @@ 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) { + if (!quiet) + printf_ln(_("No local changes to save")); + ret = 0; + goto done; + } else if (create_res != CREATE_SUCCESS) { ret = -1; goto done; }
-- 2.56.0.134.g299a3c16181