{"thread":{"id":"66466","subject":"[PATCH 0/2] stash: stop checking for changes twice","startedAt":"2026-10-05T16:35:29Z","lastAt":"2026-11-06T01:59:12Z","messageCount":47,"participants":["Phillip Wood","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"554193","messageId":"cover.1791218125.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":null,"subject":"[PATCH 0/2] stash: stop checking for changes twice","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-05T16:35:29Z","receivedAt":"2026-10-05T16:35:29Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"\"git stash push\" and \"git stash create\" check if there are\nany unstaged or uncommitted changes at startup, and then again\nwhen they try to create the stash. This short series removes\nthat duplication of effort which I spotted while looking at\n<20260929074222.11942-1-kazumasa.shigeta@kanamei.com>.\n\nbase-commit: c46c1e37724f0478939de636ab8ea5a89086d532\nPublished-As: https://github.com/phillipwood/git/releases/tag/pw%2Fstash-optimize-check_changes-calls%2Fv1\nView-Changes-At: https://github.com/phillipwood/git/compare/c46c1e377...95b7d582a\nFetch-It-Via: git fetch https://github.com/phillipwood/git pw/stash-optimize-check_changes-calls/v1\n\n\nPhillip Wood (2):\n  stash create: remove duplicate changes detection\n  stash push: remove duplicate changes detection\n\n builtin/stash.c | 27 +++++++++++++++------------\n 1 file changed, 15 insertions(+), 12 deletions(-)\n\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"554194","messageId":"1617d92942d283017272ca6f27f1254f8f9389b0.1791218125.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1791218125.git.phillip.wood@dunelm.org.uk","subject":"[PATCH 1/2] stash create: remove duplicate changes detection","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-05T16:35:30Z","receivedAt":"2026-10-05T16:35:30Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nBefore it creates a stash, git checks if there are any unstaged,\nor uncommitted changes. If there isn't anything to stash it bails\nout. Since ef0f0b4509 (stash: optimize `get_untracked_files()`\nand `check_changes()`, 2019-02-25) \"git stash store\" has checked\nfor changes twice, once in create_stash() before we refresh the\nindex and then again in do_create_stash() after the index has been\nrefreshed. That commit claims it is an optimization but it is not\nclear what it is trying to optimize by checking for changes twice,\nespecially as checking for changes before refreshing the index is\nunreliable (the scripted version of \"git stash store\", called \"git\nupdate-index -q --refresh\" before looking for any changes).\n\nAvoid checking for changes twice by removing the call to\ncheck_changes_tracked_files() from store_stash() and restore the return\ncode handling in store_stash() that was removed by ef0f0b4509 so that\nwe continue to exit 0 when there are no changes to stash. In principle\nwe could remove the call to check_changes() from do_store_stash()\ninstead, but then we'd need to pass in the list of untracked files.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 8 +++++---\n 1 file changed, 5 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex 7a9843413b1..9a5006e3d92 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -1655,8 +1655,6 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n \tstrbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');\n \n \tmemset(&ps, 0, sizeof(ps));\n-\tif (!check_changes_tracked_files(&ps))\n-\t\treturn 0;\n \n \tret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n \t\t\t      NULL, 0);\n@@ -1665,7 +1663,11 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n \n \tfree_stash_info(&info);\n \tstrbuf_release(&stash_msg_buf);\n-\treturn ret;\n+\t/*\n+\t * ret is 1 if there were no changes. In this case, we should\n+\t * not error out.\n+\t */\n+\treturn ret < 0;\n }\n \n static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"554195","messageId":"95b7d582a2f86a3db4a9e182e482e9eb904ddeee.1791218125.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1791218125.git.phillip.wood@dunelm.org.uk","subject":"[PATCH 2/2] stash push: remove duplicate changes detection","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-05T16:35:31Z","receivedAt":"2026-10-05T16:35:31Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nBefore it creates a stash, git checks if there are any unstaged,\nor uncommitted changes. If there isn't anything to stash it bails\nout. \"git stash push\" checks for changes twice, once in do_push_stash()\nand then again in do_create_stash(). Avoid that by removing the call\nto check_changes() from do_push_stash() and checking the return value\nof do_create_stash() to see if there were any changes to stash. If\ncheck_changes() finds there are no changes do_create_stash() now\nreturns 2 rather than 1. This enables us to distinguish between there\nbeing no changes and there being nothing stashed so that \"git stash\npush --patch\" when nothing is select, and \"git stash push --staged\"\nwhen the index matches HEAD still exit 1 rather than 0.\n\nThere is still one small change in behavior as, if there is nothing\nto stash, we'll try now to create the reflog for stashes before we\nrealize that there is nothing to stash. I don't think that should\nmatter in practice.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 23 ++++++++++++-----------\n 1 file changed, 12 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex 9a5006e3d92..79fdfff09a2 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -1538,7 +1538,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n \t}\n \n \tif (!check_changes(ps, include_untracked, &untracked_files)) {\n-\t\tret = 1;\n+\t\tret = 2;\n \t\tgoto done;\n \t}\n \n@@ -1664,8 +1664,8 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n \tfree_stash_info(&info);\n \tstrbuf_release(&stash_msg_buf);\n \t/*\n-\t * ret is 1 if there were no changes. In this case, we should\n-\t * not error out.\n+\t * ret is greater than zero if there were no changes. In this case,\n+\t * we should not error out.\n \t */\n \treturn ret < 0;\n }\n@@ -1728,12 +1728,6 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n \t\tgoto done;\n \t}\n \n-\tif (!check_changes(ps, include_untracked, &untracked_files)) {\n-\t\tif (!quiet)\n-\t\t\tprintf_ln(_(\"No local changes to save\"));\n-\t\tgoto done;\n-\t}\n-\n \tif (!refs_reflog_exists(get_main_ref_store(the_repository), ref_stash) && do_clear_stash()) {\n \t\tret = -1;\n \t\tif (!quiet)\n@@ -1743,8 +1737,15 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n \n \tif (stash_msg)\n \t\tstrbuf_addstr(&stash_msg_buf, stash_msg);\n-\tif (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,\n-\t\t\t    interactive_opts, only_staged, &info, &patch, quiet)) {\n+\tret =  do_create_stash(ps, &stash_msg_buf, include_untracked,\n+\t\t\t       patch_mode, interactive_opts, only_staged, &info,\n+\t\t\t       &patch, quiet);\n+\tif (ret == 2) {\n+\t\tif (!quiet)\n+\t\t\tprintf_ln(_(\"No local changes to save\"));\n+\t\tret = 0;\n+\t\tgoto done;\n+\t} else if (ret) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"554278","messageId":"xmqqpkxngfrw.fsf@gitster.g","threadId":"66466","inReplyTo":"1617d92942d283017272ca6f27f1254f8f9389b0.1791218125.git.phillip.wood@dunelm.org.uk","subject":"Re: [PATCH 1/2] stash create: remove duplicate changes detection","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-06T12:44:19Z","receivedAt":"2026-10-06T12:44:19Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> From: Phillip Wood <phillip.wood@dunelm.org.uk>\n>\n> Before it creates a stash, git checks if there are any unstaged,\n> or uncommitted changes. If there isn't anything to stash it bails\n> out. Since ef0f0b4509 (stash: optimize `get_untracked_files()`\n> and `check_changes()`, 2019-02-25) \"git stash store\" has checked\n\n\"store\"?  Aren't we talking about \"create\"?\n\n> unreliable (the scripted version of \"git stash store\", called \"git\n> update-index -q --refresh\" before looking for any changes).\n\nDitto.\n\n> Avoid checking for changes twice by removing the call to\n> check_changes_tracked_files() from store_stash() and restore the return\n> code handling in store_stash() that was removed by ef0f0b4509 so that\n> we continue to exit 0 when there are no changes to stash. In principle\n> we could remove the call to check_changes() from do_store_stash()\n> instead, but then we'd need to pass in the list of untracked files.\n\nAgain \"(do_)?store\" -> \"\\1create\"?\n\n\n"},{"id":"554283","messageId":"xmqqpkxmgb00.fsf@gitster.g","threadId":"66466","inReplyTo":"95b7d582a2f86a3db4a9e182e482e9eb904ddeee.1791218125.git.phillip.wood@dunelm.org.uk","subject":"Re: [PATCH 2/2] stash push: remove duplicate changes detection","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-06T14:27:27Z","receivedAt":"2026-10-06T14:27:27Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> diff --git a/builtin/stash.c b/builtin/stash.c\n> index 9a5006e3d92..79fdfff09a2 100644\n> --- a/builtin/stash.c\n> +++ b/builtin/stash.c\n> @@ -1538,7 +1538,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n>  \t}\n>  \n>  \tif (!check_changes(ps, include_untracked, &untracked_files)) {\n> -\t\tret = 1;\n> +\t\tret = 2;\n>  \t\tgoto done;\n>  \t}\n\nIt may be time for us to introduce symbolic constants once we have\nthree choices instead of two.\n\n> @@ -1664,8 +1664,8 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n>  \tfree_stash_info(&info);\n>  \tstrbuf_release(&stash_msg_buf);\n>  \t/*\n> -\t * ret is 1 if there were no changes. In this case, we should\n> -\t * not error out.\n> +\t * ret is greater than zero if there were no changes. In this case,\n> +\t * we should not error out.\n>  \t */\n>  \treturn ret < 0;\n>  }\n> @@ -1728,12 +1728,6 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n>  \t\tgoto done;\n>  \t}\n>  \n> -\tif (!check_changes(ps, include_untracked, &untracked_files)) {\n> -\t\tif (!quiet)\n> -\t\t\tprintf_ln(_(\"No local changes to save\"));\n> -\t\tgoto done;\n> -\t}\n> -\n>  \tif (!refs_reflog_exists(get_main_ref_store(the_repository), ref_stash) && do_clear_stash()) {\n>  \t\tret = -1;\n>  \t\tif (!quiet)\n\nBefore the precontext of this hunk, repo_refresh_and_write_index()\nis called to refresh the index.  We used to leave early when\ncheck_changes() saw no need to save.  We no longer do so, and\ninstead keep going.\n\n> @@ -1743,8 +1737,15 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n>  \n>  \tif (stash_msg)\n>  \t\tstrbuf_addstr(&stash_msg_buf, stash_msg);\n> -\tif (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,\n> -\t\t\t    interactive_opts, only_staged, &info, &patch, quiet)) {\n> +\tret =  do_create_stash(ps, &stash_msg_buf, include_untracked,\n> +\t\t\t       patch_mode, interactive_opts, only_staged, &info,\n> +\t\t\t       &patch, quiet);\n\nAnd we call do_create_stash().  The first thing it does is to call\nrepo_read_index_preload() and repo_refresh_and_write_index().\n\nAre we refreshing the index twice now, even though we know nothing\nhas changed in between, when we run \"git stash push\"?\n\ndo_create_stash() does call check_changes() to return early without\ncreating stash, so we did save the cost of check_changes() with this\npatch, though.\n\n> +\tif (ret == 2) {\n> +\t\tif (!quiet)\n> +\t\t\tprintf_ln(_(\"No local changes to save\"));\n> +\t\tret = 0;\n> +\t\tgoto done;\n> +\t} else if (ret) {\n>  \t\tret = -1;\n>  \t\tgoto done;\n>  \t}\n\n"},{"id":"554372","messageId":"725487d3-5a07-40c6-a603-990a661e0193@gmail.com","threadId":"66466","inReplyTo":"xmqqpkxmgb00.fsf@gitster.g","subject":"Re: [PATCH 2/2] stash push: remove duplicate changes detection","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-07T13:46:08Z","receivedAt":"2026-10-07T13:46:08Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 06/10/2026 15:27, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n> >> diff --git a/builtin/stash.c b/builtin/stash.c\n>> index 9a5006e3d92..79fdfff09a2 100644\n>> --- a/builtin/stash.c\n>> +++ b/builtin/stash.c\n>> @@ -1538,7 +1538,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n>>   \t}\n>>   >>   \tif (!check_changes(ps, include_untracked, &untracked_files)) {\n>> -\t\tret = 1;\n>> +\t\tret = 2;\n>>   \t\tgoto done;\n>>   \t}\n> > It may be time for us to introduce symbolic constants once we have\n> three choices instead of two.\n\nThat makes sense\n\n>> @@ -1728,12 +1728,6 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n>>   \t\tgoto done;\n>>   \t}\n>>   >> -\tif (!check_changes(ps, include_untracked, &untracked_files)) {\n>> -\t\tif (!quiet)\n>> -\t\t\tprintf_ln(_(\"No local changes to save\"));\n>> -\t\tgoto done;\n>> -\t}\n>> -\n>>   \tif (!refs_reflog_exists(get_main_ref_store(the_repository), ref_stash) && do_clear_stash()) {\n>>   \t\tret = -1;\n>>   \t\tif (!quiet)\n> > Before the precontext of this hunk, repo_refresh_and_write_index()\n> is called to refresh the index.  We used to leave early when\n> check_changes() saw no need to save.  We no longer do so, and\n> instead keep going.\n> >> @@ -1743,8 +1737,15 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n>>   >>   \tif (stash_msg)\n>>   \t\tstrbuf_addstr(&stash_msg_buf, stash_msg);\n>> -\tif (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,\n>> -\t\t\t    interactive_opts, only_staged, &info, &patch, quiet)) {\n>> +\tret =  do_create_stash(ps, &stash_msg_buf, include_untracked,\n>> +\t\t\t       patch_mode, interactive_opts, only_staged, &info,\n>> +\t\t\t       &patch, quiet);\n> > And we call do_create_stash().  The first thing it does is to call\n> repo_read_index_preload() and repo_refresh_and_write_index().\n> > Are we refreshing the index twice now, even though we know nothing\n> has changed in between, when we run \"git stash push\"?\n\nWe've always been doing that when there is something to stash (which is the normal case). To avoid that we need to make the callers of do_create_stash() responsible for refreshing the index. At the moment create_stash() calls do_create_stash() without evening reading the index, but do_push_stash() needs to read the index to check if it the pathspec contains paths that don't match the index. That would also avoid calling preload_index() multiple times.\n\n> do_create_stash() does call check_changes() to return early without\n> creating stash, so we did save the cost of check_changes() with this\n> patch, though.\n\nYes, lets add another patch to avoid unnecessarily refreshing the index as well.\n\nThanks\n\nPhillip\n\n>> +\tif (ret == 2) {\n>> +\t\tif (!quiet)\n>> +\t\t\tprintf_ln(_(\"No local changes to save\"));\n>> +\t\tret = 0;\n>> +\t\tgoto done;\n>> +\t} else if (ret) {\n>>   \t\tret = -1;\n>>   \t\tgoto done;\n>>   \t}\n\n\n"},{"id":"554374","messageId":"771a2364-7e5e-4d6f-be34-1764e609c514@gmail.com","threadId":"66466","inReplyTo":"xmqqpkxngfrw.fsf@gitster.g","subject":"Re: [PATCH 1/2] stash create: remove duplicate changes detection","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-07T13:49:16Z","receivedAt":"2026-10-07T13:49:16Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 06/10/2026 13:44, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n> >> From: Phillip Wood <phillip.wood@dunelm.org.uk>\n>>\n>> Before it creates a stash, git checks if there are any unstaged,\n>> or uncommitted changes. If there isn't anything to stash it bails\n>> out. Since ef0f0b4509 (stash: optimize `get_untracked_files()`\n>> and `check_changes()`, 2019-02-25) \"git stash store\" has checked\n> > \"store\"?  Aren't we talking about \"create\"?\n\nSorry, it looks like I managed to confuse \"create\" with \"store\" when I wrote the message. It should be\n\nstash create: remove duplicate changes detection\n\nBefore it creates a stash, git checks if there are any unstaged,\nor uncommitted changes. If there isn't anything to stash it bails\nout. Since ef0f0b4509 (stash: optimize `get_untracked_files()`\nand `check_changes()`, 2019-02-25) \"git stash create\" has checked\nfor changes twice, once in create_stash() before we refresh the\nindex and then again in do_create_stash() after the index has been\nrefreshed. That commit claims it is an optimization but it is not\nclear what it is trying to optimize by checking for changes twice,\nespecially as checking for changes before refreshing the index is\nunreliable (the scripted version of \"git stash create\", called \"git\nupdate-index -q --refresh\" before looking for any changes).\n\nAvoid checking for changes twice by removing the call to\ncheck_changes_tracked_files() from create_stash() and restore the return\ncode handling in create_stash() that was removed by ef0f0b4509 so that\nwe continue to exit 0 when there are no changes to stash. In principle\nwe could remove the call to check_changes() from do_create_stash()\ninstead, but then we'd need to pass in the list of untracked files.\n\nThanks\n\nPhillip\n\n> >> unreliable (the scripted version of \"git stash store\", called \"git\n>> update-index -q --refresh\" before looking for any changes).\n> > Ditto.\n> >> Avoid checking for changes twice by removing the call to\n>> check_changes_tracked_files() from store_stash() and restore the return\n>> code handling in store_stash() that was removed by ef0f0b4509 so that\n>> we continue to exit 0 when there are no changes to stash. In principle\n>> we could remove the call to check_changes() from do_store_stash()\n>> instead, but then we'd need to pass in the list of untracked files.\n> > Again \"(do_)?store\" -> \"\\1create\"?\n\n\n"},{"id":"554393","messageId":"xmqqa4opa0up.fsf@gitster.g","threadId":"66466","inReplyTo":"725487d3-5a07-40c6-a603-990a661e0193@gmail.com","subject":"Re: [PATCH 2/2] stash push: remove duplicate changes detection","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-07T17:15:26Z","receivedAt":"2026-10-07T17:15:26Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n>> Before the precontext of this hunk, repo_refresh_and_write_index()\n>> is called to refresh the index.  We used to leave early when\n>> check_changes() saw no need to save.  We no longer do so, and\n>> instead keep going.\n>> \n>>> @@ -1743,8 +1737,15 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n>>>   \n>>>   \tif (stash_msg)\n>>>   \t\tstrbuf_addstr(&stash_msg_buf, stash_msg);\n>>> -\tif (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,\n>>> -\t\t\t    interactive_opts, only_staged, &info, &patch, quiet)) {\n>>> +\tret =  do_create_stash(ps, &stash_msg_buf, include_untracked,\n>>> +\t\t\t       patch_mode, interactive_opts, only_staged, &info,\n>>> +\t\t\t       &patch, quiet);\n>> \n>> And we call do_create_stash().  The first thing it does is to call\n>> repo_read_index_preload() and repo_refresh_and_write_index().\n>> \n>> Are we refreshing the index twice now, even though we know nothing\n>> has changed in between, when we run \"git stash push\"?\n>\n> We've always been doing that when there is something to stash (which is \n> the normal case).\n\nSo when there is nothing to stash, we only refreshed once but now\nrefreshing twice is not a regression?\n\n> To avoid that we need to make the callers of do_create_stash()\n> responsible for refreshing the index.  At the moment\n> create_stash() calls do_create_stash() without evening reading the\n> index, but do_push_stash() needs to read the index to check if it\n> the pathspec contains paths that don't match the index. That would\n> also avoid calling preload_index() multiple times.\n\nYes, that exactly is the right direction.  Then we can reduce the\nnumber of check_changes() without increasing the number of\nrefreshes, right?\n\nThanks.\n\n"},{"id":"554499","messageId":"82bf1054-8455-489d-b832-302909711b2a@gmail.com","threadId":"66466","inReplyTo":"xmqqa4opa0up.fsf@gitster.g","subject":"Re: [PATCH 2/2] stash push: remove duplicate changes detection","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-08T13:35:40Z","receivedAt":"2026-10-08T13:35:40Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 07/10/2026 18:15, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n> >>> Before the precontext of this hunk, repo_refresh_and_write_index()\n>>> is called to refresh the index.  We used to leave early when\n>>> check_changes() saw no need to save.  We no longer do so, and\n>>> instead keep going.\n>>>\n>>>> @@ -1743,8 +1737,15 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n>>>>    >>>>    \tif (stash_msg)\n>>>>    \t\tstrbuf_addstr(&stash_msg_buf, stash_msg);\n>>>> -\tif (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,\n>>>> -\t\t\t    interactive_opts, only_staged, &info, &patch, quiet)) {\n>>>> +\tret =  do_create_stash(ps, &stash_msg_buf, include_untracked,\n>>>> +\t\t\t       patch_mode, interactive_opts, only_staged, &info,\n>>>> +\t\t\t       &patch, quiet);\n>>>\n>>> And we call do_create_stash().  The first thing it does is to call\n>>> repo_read_index_preload() and repo_refresh_and_write_index().\n>>>\n>>> Are we refreshing the index twice now, even though we know nothing\n>>> has changed in between, when we run \"git stash push\"?\n>>\n>> We've always been doing that when there is something to stash (which is\n>> the normal case).\n> > So when there is nothing to stash, we only refreshed once but now\n> refreshing twice is not a regression?\n\nIf there are no changes then the first refresh will mark all the index entries as up-to-date, which means the second refresh wont stat anything so I'm not sure there is a noticeable change. To me the more important problem is that we're lstat()ing each changed file half a dozen times before we call check_changes() when we should be lstat()ing them twice.\n\n>> To avoid that we need to make the callers of do_create_stash()\n>> responsible for refreshing the index.  At the moment\n>> create_stash() calls do_create_stash() without evening reading the\n>> index, but do_push_stash() needs to read the index to check if it\n>> the pathspec contains paths that don't match the index. That would\n>> also avoid calling preload_index() multiple times.\n> > Yes, that exactly is the right direction.  Then we can reduce the\n> number of check_changes() without increasing the number of\n> refreshes, right?\n\nYes and improve things for the case where we actually have something to stash.\n\nThanks\n\nPhillip\n\n\n"},{"id":"555035","messageId":"cover.1791992404.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1791218125.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v2 0/7] stash creation: avoid unecessary index loads and refreshes","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-14T15:40:12Z","receivedAt":"2026-10-14T15:40:12Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Thanks to Junio for his comments on V1, this series has expanded in\nscope to avoid unecessary index refreshes.\n\n\"git stash push\" and \"git stash create\" load and refresh the index\nmultiple times when they only need to do it once. This series removes\na number of calls to repo_read_preload_index() and refresh_index()\nwhich reduces the number of lstat() calls on each changed path by\nfour for \"git stash create\" and eight for \"git stash push\". It also\nremoves a duplicate call to check_changes() so that when stashing\nuntracked files we only walk them once.\n\nChanges since V1:\n - Added an enum for the various return values from do_create_stash()\n - Fixed typos in the commit message of patch 2 (was patch 1)\n - Removed an unnecessary index refresh and several unnecessary index\n   preloads\n\nCover letter for V1:\n\n\"git stash push\" and \"git stash create\" check if there are\nany unstaged or uncommitted changes at startup, and then again\nwhen they try to create the stash. This short series removes\nthat duplication of effort which I spotted while looking at\n<20260929074222.11942-1-kazumasa.shigeta@kanamei.com>.\n\nbase-commit: c46c1e37724f0478939de636ab8ea5a89086d532\nPublished-As: https://github.com/phillipwood/git/releases/tag/pw%2Fstash-optimize-check_changes-calls%2Fv2\nView-Changes-At: https://github.com/phillipwood/git/compare/c46c1e377...db53bf3b5\nFetch-It-Via: git fetch https://github.com/phillipwood/git pw/stash-optimize-check_changes-calls/v2\n\n\nPhillip Wood (7):\n  stash: use an enum for create status\n  stash create: remove duplicate changes detection\n  stash push: remove duplicate changes detection\n  stash push: avoid duplicate calls to preload_index()\n  stash create: rework temporary index creation\n  stash create: remove unnecessary index load and refresh\n  stash: simplify reset_tree() function signature\n\n builtin/stash.c | 137 ++++++++++++++++++++++--------------------------\n 1 file changed, 62 insertions(+), 75 deletions(-)\n\nRange-diff against v1:\n-:  ---------- > 1:  9e27bcd676 stash: use an enum for create status\n1:  1617d92942 ! 2:  d56b7c6f67 stash create: remove duplicate changes detection\n    @@ Commit message\n         Before it creates a stash, git checks if there are any unstaged,\n         or uncommitted changes. If there isn't anything to stash it bails\n         out. Since ef0f0b4509 (stash: optimize `get_untracked_files()`\n    -    and `check_changes()`, 2019-02-25) \"git stash store\" has checked\n    +    and `check_changes()`, 2019-02-25) \"git stash create\" has checked\n         for changes twice, once in create_stash() before we refresh the\n         index and then again in do_create_stash() after the index has been\n         refreshed. That commit claims it is an optimization but it is not\n         clear what it is trying to optimize by checking for changes twice,\n         especially as checking for changes before refreshing the index is\n    -    unreliable (the scripted version of \"git stash store\", called \"git\n    +    unreliable (the scripted version of \"git stash create\", called \"git\n         update-index -q --refresh\" before looking for any changes).\n     \n         Avoid checking for changes twice by removing the call to\n    -    check_changes_tracked_files() from store_stash() and restore the return\n    -    code handling in store_stash() that was removed by ef0f0b4509 so that\n    -    we continue to exit 0 when there are no changes to stash. In principle\n    -    we could remove the call to check_changes() from do_store_stash()\n    -    instead, but then we'd need to pass in the list of untracked files.\n    +    check_changes_tracked_files() from create_stash(). In principle we\n    +    could remove the call to check_changes() from do_create_stash()\n    +    instead, but it is more convenient to keep it there, rather than\n    +    require each caller to call it instead.\n    +\n    +    The remaining callers of check_changes_tracked_files() sensibly read\n    +    and refresh the index before calling it, so it no-longer needs to\n    +    call to repo_read_index().\n     \n         Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n     \n      ## builtin/stash.c ##\n    +@@ builtin/stash.c: static int check_changes_tracked_files(const struct pathspec *ps)\n    + \tif (repo_get_oid(the_repository, \"HEAD\", &dummy))\n    + \t\treturn -1;\n    + \n    +-\tif (repo_read_index(the_repository) < 0)\n    +-\t\treturn -1;\n    +-\n    + \trepo_init_revisions(the_repository, &rev, NULL);\n    + \tcopy_pathspec(&rev.prune_data, ps);\n    + \n     @@ builtin/stash.c: static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n      \tstrbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');\n      \n    @@ builtin/stash.c: static int create_stash(int argc, const char **argv, const char\n      \n      \tret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n      \t\t\t      NULL, 0);\n    -@@ builtin/stash.c: static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n    - \n    - \tfree_stash_info(&info);\n    - \tstrbuf_release(&stash_msg_buf);\n    --\treturn ret;\n    -+\t/*\n    -+\t * ret is 1 if there were no changes. In this case, we should\n    -+\t * not error out.\n    -+\t */\n    -+\treturn ret < 0;\n    - }\n    - \n    - static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,\n2:  95b7d582a2 ! 3:  36fc0eb913 stash push: remove duplicate changes detection\n    @@ Metadata\n      ## Commit message ##\n         stash push: remove duplicate changes detection\n     \n    -    Before it creates a stash, git checks if there are any unstaged,\n    -    or uncommitted changes. If there isn't anything to stash it bails\n    -    out. \"git stash push\" checks for changes twice, once in do_push_stash()\n    -    and then again in do_create_stash(). Avoid that by removing the call\n    -    to check_changes() from do_push_stash() and checking the return value\n    -    of do_create_stash() to see if there were any changes to stash. If\n    -    check_changes() finds there are no changes do_create_stash() now\n    -    returns 2 rather than 1. This enables us to distinguish between there\n    -    being no changes and there being nothing stashed so that \"git stash\n    -    push --patch\" when nothing is select, and \"git stash push --staged\"\n    -    when the index matches HEAD still exit 1 rather than 0.\n    +    Before it creates a stash, git checks if there are any unstaged, or\n    +    uncommitted changes. If there isn't anything to stash it bails out. \"git\n    +    stash push\" checks for changes twice, once in do_push_stash() and then\n    +    again in do_create_stash(). While duplicating the checks for staged and\n    +    unstaged charges is fairly cheap because they stop after finding the\n    +    first changed path, if we're stashing untracked files then we visit\n    +    them all twice. Avoid that by removing the call to check_changes()\n    +    from do_push_stash() and checking the return value of do_create_stash()\n    +    to see if there were any changes to stash. If check_changes() finds\n    +    there are no changes do_create_stash() now returns CREATE_NO_CHANGES\n    +    rather than CREATE_EMPTY. This enables us to distinguish between there\n    +    being no changes and there being nothing stashed, so that \"git stash\n    +    push --patch\" when nothing is selected, and \"git stash push --staged\"\n    +    when the index matches HEAD, still exit 1 rather than 0.\n    +\n    +    As we're not calling check_changes() in do_push_stash() anymore, we do\n    +    not need to refresh the index there either because do_create_stash()\n    +    refreshes it again before calling check_changes(). Avoiding the extra\n    +    index refresh saves us two lstat() calls per changed path.\n     \n         There is still one small change in behavior as, if there is nothing\n    -    to stash, we'll try now to create the reflog for stashes before we\n    +    to stash, we'll now try to create the reflog for stashes before we\n         realize that there is nothing to stash. I don't think that should\n         matter in practice.\n     \n         Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n     \n      ## builtin/stash.c ##\n    -@@ builtin/stash.c: static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n    +@@ builtin/stash.c: enum create_result {\n    + \tCREATE_SUCCESS,\n    + \t/* No changes were stashed */\n    + \tCREATE_EMPTY,\n    ++\t/* There were no changes to stash */\n    ++\tCREATE_NO_CHANGES,\n    + };\n    + \n    + static enum create_result stash_staged(struct stash_info *info,\n    +@@ builtin/stash.c: static enum create_result do_create_stash(const struct pathspec *ps,\n      \t}\n      \n      \tif (!check_changes(ps, include_untracked, &untracked_files)) {\n    --\t\tret = 1;\n    -+\t\tret = 2;\n    +-\t\tret = CREATE_EMPTY;\n    ++\t\tret = CREATE_NO_CHANGES;\n      \t\tgoto done;\n      \t}\n      \n    -@@ builtin/stash.c: static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n    - \tfree_stash_info(&info);\n    - \tstrbuf_release(&stash_msg_buf);\n    - \t/*\n    --\t * ret is 1 if there were no changes. In this case, we should\n    --\t * not error out.\n    -+\t * ret is greater than zero if there were no changes. In this case,\n    -+\t * we should not error out.\n    - \t */\n    - \treturn ret < 0;\n    - }\n     @@ builtin/stash.c: static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n    - \t\tgoto done;\n    + \t\tfree(ps_matched);\n      \t}\n      \n    +-\tif (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,\n    +-\t\t\t\t\t NULL, NULL, NULL)) {\n    +-\t\tret = error(_(\"could not write index\"));\n    +-\t\tgoto done;\n    +-\t}\n    +-\n     -\tif (!check_changes(ps, include_untracked, &untracked_files)) {\n     -\t\tif (!quiet)\n     -\t\t\tprintf_ln(_(\"No local changes to save\"));\n    @@ builtin/stash.c: static int do_push_stash(const struct pathspec *ps, const char\n      \t\tret = -1;\n      \t\tif (!quiet)\n     @@ builtin/stash.c: static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n    - \n    - \tif (stash_msg)\n    - \t\tstrbuf_addstr(&stash_msg_buf, stash_msg);\n    --\tif (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,\n    --\t\t\t    interactive_opts, only_staged, &info, &patch, quiet)) {\n    -+\tret =  do_create_stash(ps, &stash_msg_buf, include_untracked,\n    -+\t\t\t       patch_mode, interactive_opts, only_staged, &info,\n    -+\t\t\t       &patch, quiet);\n    -+\tif (ret == 2) {\n    + \tcreate_res =  do_create_stash(ps, &stash_msg_buf, include_untracked,\n    + \t\t\t\t      patch_mode, interactive_opts, only_staged,\n    + \t\t\t\t      &info, &patch, quiet);\n    +-\tif (create_res != CREATE_SUCCESS) {\n    ++\tif (create_res == CREATE_NO_CHANGES) {\n     +\t\tif (!quiet)\n     +\t\t\tprintf_ln(_(\"No local changes to save\"));\n     +\t\tret = 0;\n     +\t\tgoto done;\n    -+\t} else if (ret) {\n    ++\t} else if (create_res != CREATE_SUCCESS) {\n      \t\tret = -1;\n      \t\tgoto done;\n      \t}\n-:  ---------- > 4:  6ca7e1c567 stash push: avoid duplicate calls to preload_index()\n-:  ---------- > 5:  f799d86f52 stash create: rework temporary index creation\n-:  ---------- > 6:  768d6bc7e5 stash create: remove unnecessary index load and refresh\n-:  ---------- > 7:  db53bf3b57 stash: simplify reset_tree() function signature\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"555036","messageId":"9e27bcd676ee763ed499fb337b1497f3fe2405b2.1791992404.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1791992404.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v2 1/7] stash: use an enum for create status","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-14T15:40:13Z","receivedAt":"2026-10-14T15:40:13Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nChange the return type of do_create_stash() to an enum in preparation\nfor adding a separate value for \"there are no changes to stash\" in a\nfuture commit. There is a small change to behavior here which fixes\nan anomaly with the exit status of create_stash(). That function calls\ncheck_changes() without refreshing the index and exits 0 if there are\nno changes. However, because it does not refresh the index, that check\nis unreliable which means do_create_stash() can return 1 when there\naren't any changes, and then create_stash() exits 1 rather than 0.\nThis commit fixes that anomaly.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 83 ++++++++++++++++++++++++++++---------------------\n 1 file changed, 47 insertions(+), 36 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex 7a9843413b..3e50ccfb40 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -1336,16 +1336,23 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n \treturn ret;\n }\n \n-static int stash_staged(struct stash_info *info, struct strbuf *out_patch,\n-\t\t\tint quiet)\n+enum create_result {\n+\tCREATE_ERROR = -1,\n+\tCREATE_SUCCESS,\n+\t/* No changes were stashed */\n+\tCREATE_EMPTY,\n+};\n+\n+static enum create_result stash_staged(struct stash_info *info,\n+\t\t\t\t  struct strbuf *out_patch, int quiet)\n {\n-\tint ret = 0;\n+\tenum create_result ret = 0;\n \tstruct child_process cp_diff_tree = CHILD_PROCESS_INIT;\n \tstruct index_state istate = INDEX_STATE_INIT(the_repository);\n \n \tif (write_index_as_tree(&info->w_tree, &istate, the_repository->index_file,\n \t\t\t\t0, NULL)) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n@@ -1354,26 +1361,26 @@ static int stash_staged(struct stash_info *info, struct strbuf *out_patch,\n \t\t     \"--no-color\",\n \t\t     \"-U1\", \"HEAD\", oid_to_hex(&info->w_tree), \"--\", NULL);\n \tif (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n \tif (!out_patch->len) {\n \t\tif (!quiet)\n \t\t\tfprintf_ln(stderr, _(\"No staged changes\"));\n-\t\tret = 1;\n+\t\tret = CREATE_EMPTY;\n \t}\n \n done:\n \trelease_index(&istate);\n \treturn ret;\n }\n \n-static int stash_patch(struct stash_info *info, const struct pathspec *ps,\n-\t\t       struct strbuf *out_patch, int quiet,\n-\t\t       struct interactive_options *interactive_opts)\n+static enum create_result stash_patch(struct stash_info *info,\n+\t\tconst struct pathspec *ps, struct strbuf *out_patch, int quiet,\n+\t\tstruct interactive_options *interactive_opts)\n {\n-\tint ret = 0;\n+\tenum create_result ret = 0;\n \tstruct child_process cp_diff_tree = CHILD_PROCESS_INIT;\n \tstruct commit *head_commit;\n \tconst struct object_id *head_tree;\n@@ -1384,17 +1391,17 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,\n \n \thead_commit = lookup_commit(the_repository, &info->b_commit);\n \tif (!head_commit || repo_parse_commit(the_repository, head_commit)) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \thead_tree = get_commit_tree_oid(head_commit);\n \tif (!head_tree) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n \tif (create_index_from_tree(head_tree, stash_index_path.buf)) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n@@ -1416,7 +1423,7 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,\n \t/* State of the working tree. */\n \tif (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,\n \t\t\t\tNULL)) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n@@ -1425,14 +1432,14 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,\n \t\t     \"--no-color\",\n \t\t     oid_to_hex(&info->w_tree), \"--\", NULL);\n \tif (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n \tif (!out_patch->len) {\n \t\tif (!quiet)\n \t\t\tfprintf_ln(stderr, _(\"No changes selected\"));\n-\t\tret = 1;\n+\t\tret = CREATE_EMPTY;\n \t}\n \n done:\n@@ -1499,13 +1506,13 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps\n \treturn ret;\n }\n \n-static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_buf,\n-\t\t\t   int include_untracked, int patch_mode,\n-\t\t\t   struct interactive_options *interactive_opts,\n-\t\t\t   int only_staged, struct stash_info *info, struct strbuf *patch,\n-\t\t\t   int quiet)\n+static enum create_result do_create_stash(const struct pathspec *ps,\n+\t\tstruct strbuf *stash_msg_buf, int include_untracked,\n+\t\tint patch_mode, struct interactive_options *interactive_opts,\n+\t\tint only_staged, struct stash_info *info, struct strbuf *patch,\n+\t\tint quiet)\n {\n-\tint ret = 0;\n+\tenum create_result ret = 0;\n \tint flags = 0;\n \tint untracked_commit_option = 0;\n \tconst char *head_short_sha1 = NULL;\n@@ -1531,14 +1538,14 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n \t\tif (!quiet)\n \t\t\tfprintf_ln(stderr, _(\"You do not have \"\n \t\t\t\t\t     \"the initial commit yet\"));\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t} else {\n \t\thead_commit = lookup_commit(the_repository, &info->b_commit);\n \t}\n \n \tif (!check_changes(ps, include_untracked, &untracked_files)) {\n-\t\tret = 1;\n+\t\tret = CREATE_EMPTY;\n \t\tgoto done;\n \t}\n \n@@ -1565,7 +1572,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n \t\tif (!quiet)\n \t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n \t\t\t\t\t     \"index state\"));\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n@@ -1577,37 +1584,37 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n \t\t\tif (!quiet)\n \t\t\t\tfprintf_ln(stderr, _(\"Cannot save \"\n \t\t\t\t\t\t     \"the untracked files\"));\n-\t\t\tret = -1;\n+\t\t\tret = CREATE_ERROR;\n \t\t\tgoto done;\n \t\t}\n \t\tuntracked_commit_option = 1;\n \t}\n \tif (patch_mode) {\n \t\tret = stash_patch(info, ps, patch, quiet, interactive_opts);\n-\t\tif (ret < 0) {\n+\t\tif (ret == CREATE_ERROR) {\n \t\t\tif (!quiet)\n \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n \t\t\t\t\t\t     \"worktree state\"));\n \t\t\tgoto done;\n-\t\t} else if (ret > 0) {\n+\t\t} else if (ret != CREATE_SUCCESS) {\n \t\t\tgoto done;\n \t\t}\n \t} else if (only_staged) {\n \t\tret = stash_staged(info, patch, quiet);\n-\t\tif (ret < 0) {\n+\t\tif (ret == CREATE_ERROR) {\n \t\t\tif (!quiet)\n \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n \t\t\t\t\t\t     \"staged state\"));\n \t\t\tgoto done;\n-\t\t} else if (ret > 0) {\n+\t\t} else if (ret != CREATE_SUCCESS) {\n \t\t\tgoto done;\n \t\t}\n \t} else {\n \t\tif (stash_working_tree(info, ps)) {\n \t\t\tif (!quiet)\n \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n \t\t\t\t\t\t     \"worktree state\"));\n-\t\t\tret = -1;\n+\t\t\tret = CREATE_ERROR;\n \t\t\tgoto done;\n \t\t}\n \t}\n@@ -1630,7 +1637,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n \t\tif (!quiet)\n \t\t\tfprintf_ln(stderr, _(\"Cannot record \"\n \t\t\t\t\t     \"working tree state\"));\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n@@ -1660,12 +1667,13 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n \n \tret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n \t\t\t      NULL, 0);\n-\tif (!ret)\n+\tif (ret == CREATE_SUCCESS)\n \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n \n \tfree_stash_info(&info);\n \tstrbuf_release(&stash_msg_buf);\n-\treturn ret;\n+\t/* Do not return an error if there are no changes */\n+\treturn ret == CREATE_ERROR ? -1 : 0;\n }\n \n static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,\n@@ -1674,6 +1682,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n \t\t\t int include_untracked, int only_staged)\n {\n \tint ret = 0;\n+\tenum create_result create_res;\n \tstruct stash_info info = STASH_INFO_INIT;\n \tstruct strbuf patch = STRBUF_INIT;\n \tstruct strbuf stash_msg_buf = STRBUF_INIT;\n@@ -1741,8 +1750,10 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n \n \tif (stash_msg)\n \t\tstrbuf_addstr(&stash_msg_buf, stash_msg);\n-\tif (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,\n-\t\t\t    interactive_opts, only_staged, &info, &patch, quiet)) {\n+\tcreate_res =  do_create_stash(ps, &stash_msg_buf, include_untracked,\n+\t\t\t\t      patch_mode, interactive_opts, only_staged,\n+\t\t\t\t      &info, &patch, quiet);\n+\tif (create_res != CREATE_SUCCESS) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"555037","messageId":"d56b7c6f67274175416f4d5344675a9679d31f96.1791992404.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1791992404.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v2 2/7] stash create: remove duplicate changes detection","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-14T15:40:14Z","receivedAt":"2026-10-14T15:40:14Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nBefore it creates a stash, git checks if there are any unstaged,\nor uncommitted changes. If there isn't anything to stash it bails\nout. Since ef0f0b4509 (stash: optimize `get_untracked_files()`\nand `check_changes()`, 2019-02-25) \"git stash create\" has checked\nfor changes twice, once in create_stash() before we refresh the\nindex and then again in do_create_stash() after the index has been\nrefreshed. That commit claims it is an optimization but it is not\nclear what it is trying to optimize by checking for changes twice,\nespecially as checking for changes before refreshing the index is\nunreliable (the scripted version of \"git stash create\", called \"git\nupdate-index -q --refresh\" before looking for any changes).\n\nAvoid checking for changes twice by removing the call to\ncheck_changes_tracked_files() from create_stash(). In principle we\ncould remove the call to check_changes() from do_create_stash()\ninstead, but it is more convenient to keep it there, rather than\nrequire each caller to call it instead.\n\nThe remaining callers of check_changes_tracked_files() sensibly read\nand refresh the index before calling it, so it no-longer needs to\ncall to repo_read_index().\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 5 -----\n 1 file changed, 5 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex 3e50ccfb40..f461f002e5 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -1248,9 +1248,6 @@ static int check_changes_tracked_files(const struct pathspec *ps)\n \tif (repo_get_oid(the_repository, \"HEAD\", &dummy))\n \t\treturn -1;\n \n-\tif (repo_read_index(the_repository) < 0)\n-\t\treturn -1;\n-\n \trepo_init_revisions(the_repository, &rev, NULL);\n \tcopy_pathspec(&rev.prune_data, ps);\n \n@@ -1662,8 +1659,6 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n \tstrbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');\n \n \tmemset(&ps, 0, sizeof(ps));\n-\tif (!check_changes_tracked_files(&ps))\n-\t\treturn 0;\n \n \tret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n \t\t\t      NULL, 0);\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"555038","messageId":"36fc0eb9130bda0ccae507433b6780c01fb28509.1791992404.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1791992404.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v2 3/7] stash push: remove duplicate changes detection","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-14T15:40:15Z","receivedAt":"2026-10-14T15:40:15Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nBefore it creates a stash, git checks if there are any unstaged, or\nuncommitted changes. If there isn't anything to stash it bails out. \"git\nstash push\" checks for changes twice, once in do_push_stash() and then\nagain in do_create_stash(). While duplicating the checks for staged and\nunstaged charges is fairly cheap because they stop after finding the\nfirst changed path, if we're stashing untracked files then we visit\nthem all twice. Avoid that by removing the call to check_changes()\nfrom do_push_stash() and checking the return value of do_create_stash()\nto see if there were any changes to stash. If check_changes() finds\nthere are no changes do_create_stash() now returns CREATE_NO_CHANGES\nrather than CREATE_EMPTY. This enables us to distinguish between there\nbeing no changes and there being nothing stashed, so that \"git stash\npush --patch\" when nothing is selected, and \"git stash push --staged\"\nwhen the index matches HEAD, still exit 1 rather than 0.\n\nAs we're not calling check_changes() in do_push_stash() anymore, we do\nnot need to refresh the index there either because do_create_stash()\nrefreshes it again before calling check_changes(). Avoiding the extra\nindex refresh saves us two lstat() calls per changed path.\n\nThere is still one small change in behavior as, if there is nothing\nto stash, we'll now try to create the reflog for stashes before we\nrealize that there is nothing to stash. I don't think that should\nmatter in practice.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 23 +++++++++--------------\n 1 file changed, 9 insertions(+), 14 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex f461f002e5..aec79ed473 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -1338,6 +1338,8 @@ enum create_result {\n \tCREATE_SUCCESS,\n \t/* No changes were stashed */\n \tCREATE_EMPTY,\n+\t/* There were no changes to stash */\n+\tCREATE_NO_CHANGES,\n };\n \n static enum create_result stash_staged(struct stash_info *info,\n@@ -1542,7 +1544,7 @@ static enum create_result do_create_stash(const struct pathspec *ps,\n \t}\n \n \tif (!check_changes(ps, include_untracked, &untracked_files)) {\n-\t\tret = CREATE_EMPTY;\n+\t\tret = CREATE_NO_CHANGES;\n \t\tgoto done;\n \t}\n \n@@ -1724,18 +1726,6 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n \t\tfree(ps_matched);\n \t}\n \n-\tif (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,\n-\t\t\t\t\t NULL, NULL, NULL)) {\n-\t\tret = error(_(\"could not write index\"));\n-\t\tgoto done;\n-\t}\n-\n-\tif (!check_changes(ps, include_untracked, &untracked_files)) {\n-\t\tif (!quiet)\n-\t\t\tprintf_ln(_(\"No local changes to save\"));\n-\t\tgoto done;\n-\t}\n-\n \tif (!refs_reflog_exists(get_main_ref_store(the_repository), ref_stash) && do_clear_stash()) {\n \t\tret = -1;\n \t\tif (!quiet)\n@@ -1748,7 +1738,12 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n \tcreate_res =  do_create_stash(ps, &stash_msg_buf, include_untracked,\n \t\t\t\t      patch_mode, interactive_opts, only_staged,\n \t\t\t\t      &info, &patch, quiet);\n-\tif (create_res != CREATE_SUCCESS) {\n+\tif (create_res == CREATE_NO_CHANGES) {\n+\t\tif (!quiet)\n+\t\t\tprintf_ln(_(\"No local changes to save\"));\n+\t\tret = 0;\n+\t\tgoto done;\n+\t} else if (create_res != CREATE_SUCCESS) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"555039","messageId":"6ca7e1c567a9e31d6668e59df81cd793e2b3411e.1791992404.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1791992404.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v2 4/7] stash push: avoid duplicate calls to preload_index()","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-14T15:40:16Z","receivedAt":"2026-10-14T15:40:16Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\ndo_stash_push() needs to load the index so it can check that any\npathspec argument refers to tracked files. To do that it calls\nrepo_read_index_preload() which does not just load the index, it also\ncalls preload_index() which lstat()s all the files to see which ones\nare up-to-date. do_create_stash() then calls repo_read_index_preload()\nfor a second time which lstat()s all the changed paths again and then\ncalls repo_refresh_and_write_index() which invokes preload_index() for a\nthird time. So we're preloading the index three times when we only need\nto do it once. Avoid this by making the callers of do_create_stash()\nresponsible for reading the index and use repo_read_index() rather\nthan repo_read_index_preload(). This saves two lstat() calls per\nchanged path.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 4 ++--\n 1 file changed, 2 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex aec79ed473..b4fe7465e4 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -1526,7 +1526,6 @@ static enum create_result do_create_stash(const struct pathspec *ps,\n \n \tprepare_fallback_ident(\"git stash\", \"git@stash\");\n \n-\trepo_read_index_preload(the_repository, NULL, 0);\n \tif (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,\n \t\t\t\t\t NULL, NULL, NULL) < 0) {\n \t\tret = error(_(\"could not write index\"));\n@@ -1662,6 +1661,7 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n \n \tmemset(&ps, 0, sizeof(ps));\n \n+\trepo_read_index(the_repository);\n \tret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n \t\t\t      NULL, 0);\n \tif (ret == CREATE_SUCCESS)\n@@ -1707,7 +1707,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n \t\tgoto done;\n \t}\n \n-\trepo_read_index_preload(the_repository, NULL, 0);\n+\trepo_read_index(the_repository);\n \tif (!include_untracked && ps->nr) {\n \t\tchar *ps_matched = xcalloc(ps->nr, 1);\n \n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"555040","messageId":"f799d86f52796cc1b83788460568cc0d9849bed9.1791992404.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1791992404.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v2 5/7] stash create: rework temporary index creation","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-14T15:40:17Z","receivedAt":"2026-10-14T15:40:17Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nUse create_index_from_tree() instead of reset_tree() when we create a\ntemporary index file to stash the working tree changes. This matches\nwhat we do for \"git stash push -p\" and means we can avoid messing with\nset_alternate_index_output(). Add a new parameter to control whether\nthe index is reset, as we need to merge the index rather than reset\nit so that we still detect intent-to-add entries. We do not alter the\nin-memory index so we can remove the call to repo_read_index_preload()\nwhich saves one lstat() call per changed path.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 15 ++++-----------\n 1 file changed, 4 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex b4fe7465e4..ddeb5a5d2a 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -374,7 +374,7 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)\n }\n \n static int create_index_from_tree(const struct object_id *tree_id,\n-\t\t\t\t  const char *index_path)\n+\t\t\t\t  const char *index_path, bool reset)\n {\n \tint nr_trees = 1;\n \tint ret = 0;\n@@ -403,7 +403,7 @@ static int create_index_from_tree(const struct object_id *tree_id,\n \topts.src_index = the_repository->index;\n \topts.dst_index = &dst_istate;\n \topts.merge = 1;\n-\topts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n+\topts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0;\n \topts.fn = oneway_merge;\n \n \tif (unpack_trees(nr_trees, t, &opts)) {\n@@ -1399,7 +1399,7 @@ static enum create_result stash_patch(struct stash_info *info,\n \t\tgoto done;\n \t}\n \n-\tif (create_index_from_tree(head_tree, stash_index_path.buf)) {\n+\tif (create_index_from_tree(head_tree, stash_index_path.buf, true)) {\n \t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n@@ -1458,22 +1458,15 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps\n \trepo_init_revisions(the_repository, &rev, NULL);\n \tcopy_pathspec(&rev.prune_data, ps);\n \n-\tset_alternate_index_output(stash_index_path.buf);\n-\tif (reset_tree(&info->i_tree, 0, 0)) {\n+\tif (create_index_from_tree(&info->i_tree, stash_index_path.buf, false)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n-\tset_alternate_index_output(NULL);\n \n \trev.diffopt.output_format = DIFF_FORMAT_CALLBACK;\n \trev.diffopt.format_callback = add_diff_to_buf;\n \trev.diffopt.format_callback_data = &diff_output;\n \n-\tif (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {\n-\t\tret = -1;\n-\t\tgoto done;\n-\t}\n-\n \tadd_pending_object(&rev, parse_object(the_repository, &info->b_commit),\n \t\t\t   \"\");\n \trun_diff_index(&rev, 0);\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"555041","messageId":"768d6bc7e5a8c47e1d727917cbd8818f1906cb17.1791992404.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1791992404.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v2 6/7] stash create: remove unnecessary index load and refresh","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-14T15:40:18Z","receivedAt":"2026-10-14T15:40:18Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\ncreate_index_from_tree() is called either directly or indirectly\nfrom do_create_stash() after it has loaded and refreshed the index,\nso we do not need to refresh it again here. This saves three lstat()\ncalls per changed path.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 3 ---\n 1 file changed, 3 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex ddeb5a5d2a..55916259c3 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -384,9 +384,6 @@ static int create_index_from_tree(const struct object_id *tree_id,\n \tstruct index_state dst_istate = INDEX_STATE_INIT(the_repository);\n \tstruct lock_file lock_file = LOCK_INIT;\n \n-\trepo_read_index_preload(the_repository, NULL, 0);\n-\trefresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);\n-\n \thold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);\n \n \tmemset(&opts, 0, sizeof(opts));\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"555042","messageId":"db53bf3b57e2bef504ad137fc1d896a2bc840e14.1791992404.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1791992404.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v2 7/7] stash: simplify reset_tree() function signature","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-14T15:40:19Z","receivedAt":"2026-10-14T15:40:19Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nThere is only one caller and it does not want to update the worktree or\nreset the index so remove those unused parameters.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 8 ++------\n 1 file changed, 2 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex 55916259c3..bc1252790b 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -332,7 +332,7 @@ static int clear_stash(int argc, const char **argv, const char *prefix,\n \treturn do_clear_stash();\n }\n \n-static int reset_tree(struct object_id *i_tree, int update, int reset)\n+static int reset_tree(struct object_id *i_tree)\n {\n \tint nr_trees = 1;\n \tstruct unpack_trees_options opts;\n@@ -358,10 +358,6 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)\n \topts.src_index = the_repository->index;\n \topts.dst_index = the_repository->index;\n \topts.merge = 1;\n-\topts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0;\n-\topts.update = update;\n-\tif (update)\n-\t\topts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n \topts.fn = oneway_merge;\n \n \tif (unpack_trees(nr_trees, t, &opts))\n@@ -738,7 +734,7 @@ static enum stash_apply_result do_apply_stash(const char *prefix,\n \t}\n \n \tif (has_index) {\n-\t\tif (reset_tree(&index_tree, 0, 0))\n+\t\tif (reset_tree(&index_tree))\n \t\t\tret = -1;\n \t} else {\n \t\tunstage_changes_unless_new(&c_tree);\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"555054","messageId":"xmqqy0c0rw63.fsf@gitster.g","threadId":"66466","inReplyTo":"6ca7e1c567a9e31d6668e59df81cd793e2b3411e.1791992404.git.phillip.wood@dunelm.org.uk","subject":"Re: [PATCH v2 4/7] stash push: avoid duplicate calls to preload_index()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-14T18:09:40Z","receivedAt":"2026-10-14T18:09:40Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> From: Phillip Wood <phillip.wood@dunelm.org.uk>\n>\n> do_stash_push() needs to load the index so it can check that any\n\nPerhaps you meant do_push_stash()?\n\n> pathspec argument refers to tracked files. To do that it calls\n> repo_read_index_preload() which does not just load the index, it also\n> calls preload_index() which lstat()s all the files to see which ones\n> are up-to-date. do_create_stash() then calls repo_read_index_preload()\n> for a second time which lstat()s all the changed paths again and then\n> calls repo_refresh_and_write_index() which invokes preload_index() for a\n> third time. So we're preloading the index three times when we only need\n> to do it once. Avoid this by making the callers of do_create_stash()\n> responsible for reading the index and use repo_read_index() rather\n> than repo_read_index_preload(). This saves two lstat() calls per\n> changed path.\n\nThe other caller of do_create_stash() is create_stash().  And it\ndoes not need to call the preload version for the same reason.\n\n> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n> ---\n>  builtin/stash.c | 4 ++--\n>  1 file changed, 2 insertions(+), 2 deletions(-)\n>\n> diff --git a/builtin/stash.c b/builtin/stash.c\n> index aec79ed473..b4fe7465e4 100644\n> --- a/builtin/stash.c\n> +++ b/builtin/stash.c\n> @@ -1526,7 +1526,6 @@ static enum create_result do_create_stash(const struct pathspec *ps,\n>  \n>  \tprepare_fallback_ident(\"git stash\", \"git@stash\");\n>  \n> -\trepo_read_index_preload(the_repository, NULL, 0);\n>  \tif (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,\n>  \t\t\t\t\t NULL, NULL, NULL) < 0) {\n>  \t\tret = error(_(\"could not write index\"));\n> @@ -1662,6 +1661,7 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n>  \n>  \tmemset(&ps, 0, sizeof(ps));\n>  \n> +\trepo_read_index(the_repository);\n>  \tret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n>  \t\t\t      NULL, 0);\n>  \tif (ret == CREATE_SUCCESS)\n> @@ -1707,7 +1707,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n>  \t\tgoto done;\n>  \t}\n>  \n> -\trepo_read_index_preload(the_repository, NULL, 0);\n> +\trepo_read_index(the_repository);\n>  \tif (!include_untracked && ps->nr) {\n>  \t\tchar *ps_matched = xcalloc(ps->nr, 1);\n\n"},{"id":"555058","messageId":"xmqqtsmorvux.fsf@gitster.g","threadId":"66466","inReplyTo":"f799d86f52796cc1b83788460568cc0d9849bed9.1791992404.git.phillip.wood@dunelm.org.uk","subject":"Re: [PATCH v2 5/7] stash create: rework temporary index creation","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-14T18:16:22Z","receivedAt":"2026-10-14T18:16:22Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> From: Phillip Wood <phillip.wood@dunelm.org.uk>\n>\n> Use create_index_from_tree() instead of reset_tree() when we create a\n> temporary index file to stash the working tree changes. This matches\n> what we do for \"git stash push -p\" and means we can avoid messing with\n> set_alternate_index_output(). Add a new parameter to control whether\n> the index is reset, as we need to merge the index rather than reset\n> it so that we still detect intent-to-add entries. We do not alter the\n> in-memory index so we can remove the call to repo_read_index_preload()\n> which saves one lstat() call per changed path.\n\nIt is unclear what problem we are trying to solve.  Obviously we can\nenhance create_index_from_tree() and replace reset_tree() just fine,\nbut the proposed log message should make it clear why we are doing so.\n\nPlease follow the usual pattern to state \"observation\" (e.g., \"we\nuse reset_tree() when we do X\"), \"problem\" (e.g., \"use of\nreset_tree() is undesirable because ...\"), and \"solution\" (e.g, \"we\ncan improve by replacing reset_tree() with create_index_from_tree()\nbecause the latter has these desirable characteristics\") in this\norder, perhaps in separate paragraphs for extra readability.\n\n\n\n> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n> ---\n>  builtin/stash.c | 15 ++++-----------\n>  1 file changed, 4 insertions(+), 11 deletions(-)\n>\n> diff --git a/builtin/stash.c b/builtin/stash.c\n> index b4fe7465e4..ddeb5a5d2a 100644\n> --- a/builtin/stash.c\n> +++ b/builtin/stash.c\n> @@ -374,7 +374,7 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)\n>  }\n>  \n>  static int create_index_from_tree(const struct object_id *tree_id,\n> -\t\t\t\t  const char *index_path)\n> +\t\t\t\t  const char *index_path, bool reset)\n>  {\n>  \tint nr_trees = 1;\n>  \tint ret = 0;\n> @@ -403,7 +403,7 @@ static int create_index_from_tree(const struct object_id *tree_id,\n>  \topts.src_index = the_repository->index;\n>  \topts.dst_index = &dst_istate;\n>  \topts.merge = 1;\n> -\topts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n> +\topts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0;\n>  \topts.fn = oneway_merge;\n>  \n>  \tif (unpack_trees(nr_trees, t, &opts)) {\n> @@ -1399,7 +1399,7 @@ static enum create_result stash_patch(struct stash_info *info,\n>  \t\tgoto done;\n>  \t}\n>  \n> -\tif (create_index_from_tree(head_tree, stash_index_path.buf)) {\n> +\tif (create_index_from_tree(head_tree, stash_index_path.buf, true)) {\n>  \t\tret = CREATE_ERROR;\n>  \t\tgoto done;\n>  \t}\n> @@ -1458,22 +1458,15 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps\n>  \trepo_init_revisions(the_repository, &rev, NULL);\n>  \tcopy_pathspec(&rev.prune_data, ps);\n>  \n> -\tset_alternate_index_output(stash_index_path.buf);\n> -\tif (reset_tree(&info->i_tree, 0, 0)) {\n> +\tif (create_index_from_tree(&info->i_tree, stash_index_path.buf, false)) {\n>  \t\tret = -1;\n>  \t\tgoto done;\n>  \t}\n> -\tset_alternate_index_output(NULL);\n>  \n>  \trev.diffopt.output_format = DIFF_FORMAT_CALLBACK;\n>  \trev.diffopt.format_callback = add_diff_to_buf;\n>  \trev.diffopt.format_callback_data = &diff_output;\n>  \n> -\tif (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {\n> -\t\tret = -1;\n> -\t\tgoto done;\n> -\t}\n> -\n>  \tadd_pending_object(&rev, parse_object(the_repository, &info->b_commit),\n>  \t\t\t   \"\");\n>  \trun_diff_index(&rev, 0);\n\n"},{"id":"555060","messageId":"xmqqmrsgrvlw.fsf@gitster.g","threadId":"66466","inReplyTo":"768d6bc7e5a8c47e1d727917cbd8818f1906cb17.1791992404.git.phillip.wood@dunelm.org.uk","subject":"Re: [PATCH v2 6/7] stash create: remove unnecessary index load and refresh","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-14T18:21:47Z","receivedAt":"2026-10-14T18:21:47Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> From: Phillip Wood <phillip.wood@dunelm.org.uk>\n>\n> create_index_from_tree() is called either directly or indirectly\n> from do_create_stash() after it has loaded and refreshed the index,\n> so we do not need to refresh it again here. This saves three lstat()\n> calls per changed path.\n>\n> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n> ---\n>  builtin/stash.c | 3 ---\n>  1 file changed, 3 deletions(-)\n>\n> diff --git a/builtin/stash.c b/builtin/stash.c\n> index ddeb5a5d2a..55916259c3 100644\n> --- a/builtin/stash.c\n> +++ b/builtin/stash.c\n> @@ -384,9 +384,6 @@ static int create_index_from_tree(const struct object_id *tree_id,\n>  \tstruct index_state dst_istate = INDEX_STATE_INIT(the_repository);\n>  \tstruct lock_file lock_file = LOCK_INIT;\n>  \n> -\trepo_read_index_preload(the_repository, NULL, 0);\n> -\trefresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);\n> -\n\nOh, it is emrarrasingly stupid follow read_index_preload() with\nrefresh_index(), since the reason former is not read_index() is\nbecause we want it refreshed.  The original is doubly bad.\n\n>  \thold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);\n>  \n>  \tmemset(&opts, 0, sizeof(opts));\n\n"},{"id":"555554","messageId":"4124485d-b613-4b11-9fde-158a271b7a8b@gmail.com","threadId":"66466","inReplyTo":"xmqqtsmorvux.fsf@gitster.g","subject":"Re: [PATCH v2 5/7] stash create: rework temporary index creation","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-20T14:54:54Z","receivedAt":"2026-10-20T14:54:54Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 14/10/2026 19:16, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n> > It is unclear what problem we are trying to solve.  Obviously we can\n> enhance create_index_from_tree() and replace reset_tree() just fine,\n> but the proposed log message should make it clear why we are doing so.\n> > Please follow the usual pattern to state \"observation\" (e.g., \"we\n> use reset_tree() when we do X\"), \"problem\" (e.g., \"use of\n> reset_tree() is undesirable because ...\"), and \"solution\" (e.g, \"we\n> can improve by replacing reset_tree() with create_index_from_tree()\n> because the latter has these desirable characteristics\") in this\n> order, perhaps in separate paragraphs for extra readability.\n\nI'll reword following that pattern. The gist is that the code is easier to reason about if we use create_index_from_tree() because we avoid depending on global variables for the index path and we don't overwrite the in-memory index.\n\nThanks\n\nPhillip\n\n> > > >> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n>> ---\n>>   builtin/stash.c | 15 ++++-----------\n>>   1 file changed, 4 insertions(+), 11 deletions(-)\n>>\n>> diff --git a/builtin/stash.c b/builtin/stash.c\n>> index b4fe7465e4..ddeb5a5d2a 100644\n>> --- a/builtin/stash.c\n>> +++ b/builtin/stash.c\n>> @@ -374,7 +374,7 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)\n>>   }\n>>   >>   static int create_index_from_tree(const struct object_id *tree_id,\n>> -\t\t\t\t  const char *index_path)\n>> +\t\t\t\t  const char *index_path, bool reset)\n>>   {\n>>   \tint nr_trees = 1;\n>>   \tint ret = 0;\n>> @@ -403,7 +403,7 @@ static int create_index_from_tree(const struct object_id *tree_id,\n>>   \topts.src_index = the_repository->index;\n>>   \topts.dst_index = &dst_istate;\n>>   \topts.merge = 1;\n>> -\topts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n>> +\topts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0;\n>>   \topts.fn = oneway_merge;\n>>   >>   \tif (unpack_trees(nr_trees, t, &opts)) {\n>> @@ -1399,7 +1399,7 @@ static enum create_result stash_patch(struct stash_info *info,\n>>   \t\tgoto done;\n>>   \t}\n>>   >> -\tif (create_index_from_tree(head_tree, stash_index_path.buf)) {\n>> +\tif (create_index_from_tree(head_tree, stash_index_path.buf, true)) {\n>>   \t\tret = CREATE_ERROR;\n>>   \t\tgoto done;\n>>   \t}\n>> @@ -1458,22 +1458,15 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps\n>>   \trepo_init_revisions(the_repository, &rev, NULL);\n>>   \tcopy_pathspec(&rev.prune_data, ps);\n>>   >> -\tset_alternate_index_output(stash_index_path.buf);\n>> -\tif (reset_tree(&info->i_tree, 0, 0)) {\n>> +\tif (create_index_from_tree(&info->i_tree, stash_index_path.buf, false)) {\n>>   \t\tret = -1;\n>>   \t\tgoto done;\n>>   \t}\n>> -\tset_alternate_index_output(NULL);\n>>   >>   \trev.diffopt.output_format = DIFF_FORMAT_CALLBACK;\n>>   \trev.diffopt.format_callback = add_diff_to_buf;\n>>   \trev.diffopt.format_callback_data = &diff_output;\n>>   >> -\tif (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {\n>> -\t\tret = -1;\n>> -\t\tgoto done;\n>> -\t}\n>> -\n>>   \tadd_pending_object(&rev, parse_object(the_repository, &info->b_commit),\n>>   \t\t\t   \"\");\n>>   \trun_diff_index(&rev, 0);\n\n\n"},{"id":"555560","messageId":"cover.1792509606.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1791218125.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v3 0/7] stash creation: avoid unecessary index loads and refreshes","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-20T15:20:35Z","receivedAt":"2026-10-20T15:20:35Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Thanks to Junio for his comments on V2, I've fixed a typo in patch\n4 and rewritten the commit message for patch 5.\n\n\"git stash push\" and \"git stash create\" load and refresh the index\nmultiple times when they only need to do it once. This series removes\na number of calls to repo_read_preload_index() and refresh_index()\nwhich reduces the number of lstat() calls on each changed path by\nfour for \"git stash create\" and eight for \"git stash push\". It also\nremoves a duplicate call to check_changes() so that when stashing\nuntracked files we only walk them once.\n\nChanges since V1:\n - Added an enum for the various return values from do_create_stash()\n - Fixed typos in the commit message of patch 2 (was patch 1)\n - Removed an unnecessary index refresh and several unnecessary index\n   preloads\n\nCover letter for V1:\n\n\"git stash push\" and \"git stash create\" check if there are\nany unstaged or uncommitted changes at startup, and then again\nwhen they try to create the stash. This short series removes\nthat duplication of effort which I spotted while looking at\n<20260929074222.11942-1-kazumasa.shigeta@kanamei.com>.\n\nbase-commit: c46c1e37724f0478939de636ab8ea5a89086d532\nPublished-As: https://github.com/phillipwood/git/releases/tag/pw%2Fstash-optimize-check_changes-calls%2Fv3\nView-Changes-At: https://github.com/phillipwood/git/compare/c46c1e377...a486a625c\nFetch-It-Via: git fetch https://github.com/phillipwood/git pw/stash-optimize-check_changes-calls/v3\n\n\nPhillip Wood (7):\n  stash: use an enum for create status\n  stash create: remove duplicate changes detection\n  stash push: remove duplicate changes detection\n  stash push: avoid duplicate calls to preload_index()\n  stash create: rework temporary index creation\n  stash create: remove unnecessary index load and refresh\n  stash: simplify reset_tree() function signature\n\n builtin/stash.c | 137 ++++++++++++++++++++++--------------------------\n 1 file changed, 62 insertions(+), 75 deletions(-)\n\nRange-diff against v2:\n1:  9e27bcd676 = 1:  9e27bcd676 stash: use an enum for create status\n2:  d56b7c6f67 = 2:  d56b7c6f67 stash create: remove duplicate changes detection\n3:  36fc0eb913 = 3:  36fc0eb913 stash push: remove duplicate changes detection\n4:  6ca7e1c567 ! 4:  a9875ba5b3 stash push: avoid duplicate calls to preload_index()\n    @@ Metadata\n      ## Commit message ##\n         stash push: avoid duplicate calls to preload_index()\n     \n    -    do_stash_push() needs to load the index so it can check that any\n    +    do_push_stash() needs to load the index so it can check that any\n         pathspec argument refers to tracked files. To do that it calls\n         repo_read_index_preload() which does not just load the index, it also\n         calls preload_index() which lstat()s all the files to see which ones\n5:  f799d86f52 ! 5:  de5e0c196a stash create: rework temporary index creation\n    @@ Metadata\n      ## Commit message ##\n         stash create: rework temporary index creation\n     \n    -    Use create_index_from_tree() instead of reset_tree() when we create a\n    -    temporary index file to stash the working tree changes. This matches\n    -    what we do for \"git stash push -p\" and means we can avoid messing with\n    -    set_alternate_index_output(). Add a new parameter to control whether\n    -    the index is reset, as we need to merge the index rather than reset\n    -    it so that we still detect intent-to-add entries. We do not alter the\n    -    in-memory index so we can remove the call to repo_read_index_preload()\n    -    which saves one lstat() call per changed path.\n    +    We use reset_tree() to create the temporary index file used to stash\n    +    uncommitted working tree changes. Unfortunately, reset_tree() reads\n    +    the tree into the repository's in-memory index and writes the index\n    +    to the repository's default index path. In this case, we read the\n    +    tree that's already in the index and copy the existing stat data\n    +    across, so overwriting the in-memory index should not change its\n    +    contents. To write to a temporary index file, the caller has to\n    +    use set_alternate_index_output(). While that writes the index to a\n    +    temporary file, it updates the timestamp of the in-memory index so\n    +    that the timestamp no longer represents the index file the in-memory\n    +    index was read from. In practice that probably does not matter, but\n    +    set_alternate_index_output() also relies on a global variable which\n    +    makes the code harder to reason about.\n    +\n    +    Use create_index_from_tree() instead of reset_tree() so that we\n    +    can create temporary index file without overwriting the in-memory\n    +    index or relying on set_alternate_index_output(). This also makes\n    +    the implementation consistent with the way we create the temporary\n    +    index for \"git stash push -p\". As create_index_from_tree() does not\n    +    modify the in-memory index it is clearly safe to remove the call to\n    +    repo_read_index_preload() which saves one lstat() call per changed\n    +    path. It would probably be safe to remove that call without moving away\n    +    from reset_tree() but it is much easier to reason about now that the\n    +    in-memory index is not modified when we create the temporary index file.\n    +\n    +    In order to use create_index_from_tree() we need to add a new\n    +    parameter to control whether the index is reset, as the new caller\n    +    needs to merge the index rather than reset it so that we still detect\n    +    intent-to-add entries.\n     \n         Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n     \n6:  768d6bc7e5 = 6:  61e4ce22a0 stash create: remove unnecessary index load and refresh\n7:  db53bf3b57 = 7:  a486a625c9 stash: simplify reset_tree() function signature\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"555561","messageId":"9e27bcd676ee763ed499fb337b1497f3fe2405b2.1792509606.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1792509606.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v3 1/7] stash: use an enum for create status","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-20T15:20:36Z","receivedAt":"2026-10-20T15:20:36Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nChange the return type of do_create_stash() to an enum in preparation\nfor adding a separate value for \"there are no changes to stash\" in a\nfuture commit. There is a small change to behavior here which fixes\nan anomaly with the exit status of create_stash(). That function calls\ncheck_changes() without refreshing the index and exits 0 if there are\nno changes. However, because it does not refresh the index, that check\nis unreliable which means do_create_stash() can return 1 when there\naren't any changes, and then create_stash() exits 1 rather than 0.\nThis commit fixes that anomaly.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 83 ++++++++++++++++++++++++++++---------------------\n 1 file changed, 47 insertions(+), 36 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex 7a9843413b..3e50ccfb40 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -1336,16 +1336,23 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n \treturn ret;\n }\n \n-static int stash_staged(struct stash_info *info, struct strbuf *out_patch,\n-\t\t\tint quiet)\n+enum create_result {\n+\tCREATE_ERROR = -1,\n+\tCREATE_SUCCESS,\n+\t/* No changes were stashed */\n+\tCREATE_EMPTY,\n+};\n+\n+static enum create_result stash_staged(struct stash_info *info,\n+\t\t\t\t  struct strbuf *out_patch, int quiet)\n {\n-\tint ret = 0;\n+\tenum create_result ret = 0;\n \tstruct child_process cp_diff_tree = CHILD_PROCESS_INIT;\n \tstruct index_state istate = INDEX_STATE_INIT(the_repository);\n \n \tif (write_index_as_tree(&info->w_tree, &istate, the_repository->index_file,\n \t\t\t\t0, NULL)) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n@@ -1354,26 +1361,26 @@ static int stash_staged(struct stash_info *info, struct strbuf *out_patch,\n \t\t     \"--no-color\",\n \t\t     \"-U1\", \"HEAD\", oid_to_hex(&info->w_tree), \"--\", NULL);\n \tif (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n \tif (!out_patch->len) {\n \t\tif (!quiet)\n \t\t\tfprintf_ln(stderr, _(\"No staged changes\"));\n-\t\tret = 1;\n+\t\tret = CREATE_EMPTY;\n \t}\n \n done:\n \trelease_index(&istate);\n \treturn ret;\n }\n \n-static int stash_patch(struct stash_info *info, const struct pathspec *ps,\n-\t\t       struct strbuf *out_patch, int quiet,\n-\t\t       struct interactive_options *interactive_opts)\n+static enum create_result stash_patch(struct stash_info *info,\n+\t\tconst struct pathspec *ps, struct strbuf *out_patch, int quiet,\n+\t\tstruct interactive_options *interactive_opts)\n {\n-\tint ret = 0;\n+\tenum create_result ret = 0;\n \tstruct child_process cp_diff_tree = CHILD_PROCESS_INIT;\n \tstruct commit *head_commit;\n \tconst struct object_id *head_tree;\n@@ -1384,17 +1391,17 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,\n \n \thead_commit = lookup_commit(the_repository, &info->b_commit);\n \tif (!head_commit || repo_parse_commit(the_repository, head_commit)) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \thead_tree = get_commit_tree_oid(head_commit);\n \tif (!head_tree) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n \tif (create_index_from_tree(head_tree, stash_index_path.buf)) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n@@ -1416,7 +1423,7 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,\n \t/* State of the working tree. */\n \tif (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,\n \t\t\t\tNULL)) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n@@ -1425,14 +1432,14 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,\n \t\t     \"--no-color\",\n \t\t     oid_to_hex(&info->w_tree), \"--\", NULL);\n \tif (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n \tif (!out_patch->len) {\n \t\tif (!quiet)\n \t\t\tfprintf_ln(stderr, _(\"No changes selected\"));\n-\t\tret = 1;\n+\t\tret = CREATE_EMPTY;\n \t}\n \n done:\n@@ -1499,13 +1506,13 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps\n \treturn ret;\n }\n \n-static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_buf,\n-\t\t\t   int include_untracked, int patch_mode,\n-\t\t\t   struct interactive_options *interactive_opts,\n-\t\t\t   int only_staged, struct stash_info *info, struct strbuf *patch,\n-\t\t\t   int quiet)\n+static enum create_result do_create_stash(const struct pathspec *ps,\n+\t\tstruct strbuf *stash_msg_buf, int include_untracked,\n+\t\tint patch_mode, struct interactive_options *interactive_opts,\n+\t\tint only_staged, struct stash_info *info, struct strbuf *patch,\n+\t\tint quiet)\n {\n-\tint ret = 0;\n+\tenum create_result ret = 0;\n \tint flags = 0;\n \tint untracked_commit_option = 0;\n \tconst char *head_short_sha1 = NULL;\n@@ -1531,14 +1538,14 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n \t\tif (!quiet)\n \t\t\tfprintf_ln(stderr, _(\"You do not have \"\n \t\t\t\t\t     \"the initial commit yet\"));\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t} else {\n \t\thead_commit = lookup_commit(the_repository, &info->b_commit);\n \t}\n \n \tif (!check_changes(ps, include_untracked, &untracked_files)) {\n-\t\tret = 1;\n+\t\tret = CREATE_EMPTY;\n \t\tgoto done;\n \t}\n \n@@ -1565,7 +1572,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n \t\tif (!quiet)\n \t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n \t\t\t\t\t     \"index state\"));\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n@@ -1577,37 +1584,37 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n \t\t\tif (!quiet)\n \t\t\t\tfprintf_ln(stderr, _(\"Cannot save \"\n \t\t\t\t\t\t     \"the untracked files\"));\n-\t\t\tret = -1;\n+\t\t\tret = CREATE_ERROR;\n \t\t\tgoto done;\n \t\t}\n \t\tuntracked_commit_option = 1;\n \t}\n \tif (patch_mode) {\n \t\tret = stash_patch(info, ps, patch, quiet, interactive_opts);\n-\t\tif (ret < 0) {\n+\t\tif (ret == CREATE_ERROR) {\n \t\t\tif (!quiet)\n \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n \t\t\t\t\t\t     \"worktree state\"));\n \t\t\tgoto done;\n-\t\t} else if (ret > 0) {\n+\t\t} else if (ret != CREATE_SUCCESS) {\n \t\t\tgoto done;\n \t\t}\n \t} else if (only_staged) {\n \t\tret = stash_staged(info, patch, quiet);\n-\t\tif (ret < 0) {\n+\t\tif (ret == CREATE_ERROR) {\n \t\t\tif (!quiet)\n \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n \t\t\t\t\t\t     \"staged state\"));\n \t\t\tgoto done;\n-\t\t} else if (ret > 0) {\n+\t\t} else if (ret != CREATE_SUCCESS) {\n \t\t\tgoto done;\n \t\t}\n \t} else {\n \t\tif (stash_working_tree(info, ps)) {\n \t\t\tif (!quiet)\n \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n \t\t\t\t\t\t     \"worktree state\"));\n-\t\t\tret = -1;\n+\t\t\tret = CREATE_ERROR;\n \t\t\tgoto done;\n \t\t}\n \t}\n@@ -1630,7 +1637,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n \t\tif (!quiet)\n \t\t\tfprintf_ln(stderr, _(\"Cannot record \"\n \t\t\t\t\t     \"working tree state\"));\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n@@ -1660,12 +1667,13 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n \n \tret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n \t\t\t      NULL, 0);\n-\tif (!ret)\n+\tif (ret == CREATE_SUCCESS)\n \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n \n \tfree_stash_info(&info);\n \tstrbuf_release(&stash_msg_buf);\n-\treturn ret;\n+\t/* Do not return an error if there are no changes */\n+\treturn ret == CREATE_ERROR ? -1 : 0;\n }\n \n static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,\n@@ -1674,6 +1682,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n \t\t\t int include_untracked, int only_staged)\n {\n \tint ret = 0;\n+\tenum create_result create_res;\n \tstruct stash_info info = STASH_INFO_INIT;\n \tstruct strbuf patch = STRBUF_INIT;\n \tstruct strbuf stash_msg_buf = STRBUF_INIT;\n@@ -1741,8 +1750,10 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n \n \tif (stash_msg)\n \t\tstrbuf_addstr(&stash_msg_buf, stash_msg);\n-\tif (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,\n-\t\t\t    interactive_opts, only_staged, &info, &patch, quiet)) {\n+\tcreate_res =  do_create_stash(ps, &stash_msg_buf, include_untracked,\n+\t\t\t\t      patch_mode, interactive_opts, only_staged,\n+\t\t\t\t      &info, &patch, quiet);\n+\tif (create_res != CREATE_SUCCESS) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"555562","messageId":"d56b7c6f67274175416f4d5344675a9679d31f96.1792509606.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1792509606.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v3 2/7] stash create: remove duplicate changes detection","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-20T15:20:37Z","receivedAt":"2026-10-20T15:20:37Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nBefore it creates a stash, git checks if there are any unstaged,\nor uncommitted changes. If there isn't anything to stash it bails\nout. Since ef0f0b4509 (stash: optimize `get_untracked_files()`\nand `check_changes()`, 2019-02-25) \"git stash create\" has checked\nfor changes twice, once in create_stash() before we refresh the\nindex and then again in do_create_stash() after the index has been\nrefreshed. That commit claims it is an optimization but it is not\nclear what it is trying to optimize by checking for changes twice,\nespecially as checking for changes before refreshing the index is\nunreliable (the scripted version of \"git stash create\", called \"git\nupdate-index -q --refresh\" before looking for any changes).\n\nAvoid checking for changes twice by removing the call to\ncheck_changes_tracked_files() from create_stash(). In principle we\ncould remove the call to check_changes() from do_create_stash()\ninstead, but it is more convenient to keep it there, rather than\nrequire each caller to call it instead.\n\nThe remaining callers of check_changes_tracked_files() sensibly read\nand refresh the index before calling it, so it no-longer needs to\ncall to repo_read_index().\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 5 -----\n 1 file changed, 5 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex 3e50ccfb40..f461f002e5 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -1248,9 +1248,6 @@ static int check_changes_tracked_files(const struct pathspec *ps)\n \tif (repo_get_oid(the_repository, \"HEAD\", &dummy))\n \t\treturn -1;\n \n-\tif (repo_read_index(the_repository) < 0)\n-\t\treturn -1;\n-\n \trepo_init_revisions(the_repository, &rev, NULL);\n \tcopy_pathspec(&rev.prune_data, ps);\n \n@@ -1662,8 +1659,6 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n \tstrbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');\n \n \tmemset(&ps, 0, sizeof(ps));\n-\tif (!check_changes_tracked_files(&ps))\n-\t\treturn 0;\n \n \tret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n \t\t\t      NULL, 0);\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"555563","messageId":"36fc0eb9130bda0ccae507433b6780c01fb28509.1792509606.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1792509606.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v3 3/7] stash push: remove duplicate changes detection","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-20T15:20:38Z","receivedAt":"2026-10-20T15:20:38Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nBefore it creates a stash, git checks if there are any unstaged, or\nuncommitted changes. If there isn't anything to stash it bails out. \"git\nstash push\" checks for changes twice, once in do_push_stash() and then\nagain in do_create_stash(). While duplicating the checks for staged and\nunstaged charges is fairly cheap because they stop after finding the\nfirst changed path, if we're stashing untracked files then we visit\nthem all twice. Avoid that by removing the call to check_changes()\nfrom do_push_stash() and checking the return value of do_create_stash()\nto see if there were any changes to stash. If check_changes() finds\nthere are no changes do_create_stash() now returns CREATE_NO_CHANGES\nrather than CREATE_EMPTY. This enables us to distinguish between there\nbeing no changes and there being nothing stashed, so that \"git stash\npush --patch\" when nothing is selected, and \"git stash push --staged\"\nwhen the index matches HEAD, still exit 1 rather than 0.\n\nAs we're not calling check_changes() in do_push_stash() anymore, we do\nnot need to refresh the index there either because do_create_stash()\nrefreshes it again before calling check_changes(). Avoiding the extra\nindex refresh saves us two lstat() calls per changed path.\n\nThere is still one small change in behavior as, if there is nothing\nto stash, we'll now try to create the reflog for stashes before we\nrealize that there is nothing to stash. I don't think that should\nmatter in practice.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 23 +++++++++--------------\n 1 file changed, 9 insertions(+), 14 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex f461f002e5..aec79ed473 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -1338,6 +1338,8 @@ enum create_result {\n \tCREATE_SUCCESS,\n \t/* No changes were stashed */\n \tCREATE_EMPTY,\n+\t/* There were no changes to stash */\n+\tCREATE_NO_CHANGES,\n };\n \n static enum create_result stash_staged(struct stash_info *info,\n@@ -1542,7 +1544,7 @@ static enum create_result do_create_stash(const struct pathspec *ps,\n \t}\n \n \tif (!check_changes(ps, include_untracked, &untracked_files)) {\n-\t\tret = CREATE_EMPTY;\n+\t\tret = CREATE_NO_CHANGES;\n \t\tgoto done;\n \t}\n \n@@ -1724,18 +1726,6 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n \t\tfree(ps_matched);\n \t}\n \n-\tif (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,\n-\t\t\t\t\t NULL, NULL, NULL)) {\n-\t\tret = error(_(\"could not write index\"));\n-\t\tgoto done;\n-\t}\n-\n-\tif (!check_changes(ps, include_untracked, &untracked_files)) {\n-\t\tif (!quiet)\n-\t\t\tprintf_ln(_(\"No local changes to save\"));\n-\t\tgoto done;\n-\t}\n-\n \tif (!refs_reflog_exists(get_main_ref_store(the_repository), ref_stash) && do_clear_stash()) {\n \t\tret = -1;\n \t\tif (!quiet)\n@@ -1748,7 +1738,12 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n \tcreate_res =  do_create_stash(ps, &stash_msg_buf, include_untracked,\n \t\t\t\t      patch_mode, interactive_opts, only_staged,\n \t\t\t\t      &info, &patch, quiet);\n-\tif (create_res != CREATE_SUCCESS) {\n+\tif (create_res == CREATE_NO_CHANGES) {\n+\t\tif (!quiet)\n+\t\t\tprintf_ln(_(\"No local changes to save\"));\n+\t\tret = 0;\n+\t\tgoto done;\n+\t} else if (create_res != CREATE_SUCCESS) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"555564","messageId":"a9875ba5b3595adde9e830c1ccb641633bab7454.1792509606.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1792509606.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v3 4/7] stash push: avoid duplicate calls to preload_index()","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-20T15:20:39Z","receivedAt":"2026-10-20T15:20:39Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\ndo_push_stash() needs to load the index so it can check that any\npathspec argument refers to tracked files. To do that it calls\nrepo_read_index_preload() which does not just load the index, it also\ncalls preload_index() which lstat()s all the files to see which ones\nare up-to-date. do_create_stash() then calls repo_read_index_preload()\nfor a second time which lstat()s all the changed paths again and then\ncalls repo_refresh_and_write_index() which invokes preload_index() for a\nthird time. So we're preloading the index three times when we only need\nto do it once. Avoid this by making the callers of do_create_stash()\nresponsible for reading the index and use repo_read_index() rather\nthan repo_read_index_preload(). This saves two lstat() calls per\nchanged path.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 4 ++--\n 1 file changed, 2 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex aec79ed473..b4fe7465e4 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -1526,7 +1526,6 @@ static enum create_result do_create_stash(const struct pathspec *ps,\n \n \tprepare_fallback_ident(\"git stash\", \"git@stash\");\n \n-\trepo_read_index_preload(the_repository, NULL, 0);\n \tif (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,\n \t\t\t\t\t NULL, NULL, NULL) < 0) {\n \t\tret = error(_(\"could not write index\"));\n@@ -1662,6 +1661,7 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n \n \tmemset(&ps, 0, sizeof(ps));\n \n+\trepo_read_index(the_repository);\n \tret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n \t\t\t      NULL, 0);\n \tif (ret == CREATE_SUCCESS)\n@@ -1707,7 +1707,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n \t\tgoto done;\n \t}\n \n-\trepo_read_index_preload(the_repository, NULL, 0);\n+\trepo_read_index(the_repository);\n \tif (!include_untracked && ps->nr) {\n \t\tchar *ps_matched = xcalloc(ps->nr, 1);\n \n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"555565","messageId":"de5e0c196a9ff241861f5598ac9f6bc5c960f6ec.1792509606.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1792509606.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v3 5/7] stash create: rework temporary index creation","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-20T15:20:40Z","receivedAt":"2026-10-20T15:20:40Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nWe use reset_tree() to create the temporary index file used to stash\nuncommitted working tree changes. Unfortunately, reset_tree() reads\nthe tree into the repository's in-memory index and writes the index\nto the repository's default index path. In this case, we read the\ntree that's already in the index and copy the existing stat data\nacross, so overwriting the in-memory index should not change its\ncontents. To write to a temporary index file, the caller has to\nuse set_alternate_index_output(). While that writes the index to a\ntemporary file, it updates the timestamp of the in-memory index so\nthat the timestamp no longer represents the index file the in-memory\nindex was read from. In practice that probably does not matter, but\nset_alternate_index_output() also relies on a global variable which\nmakes the code harder to reason about.\n\nUse create_index_from_tree() instead of reset_tree() so that we\ncan create temporary index file without overwriting the in-memory\nindex or relying on set_alternate_index_output(). This also makes\nthe implementation consistent with the way we create the temporary\nindex for \"git stash push -p\". As create_index_from_tree() does not\nmodify the in-memory index it is clearly safe to remove the call to\nrepo_read_index_preload() which saves one lstat() call per changed\npath. It would probably be safe to remove that call without moving away\nfrom reset_tree() but it is much easier to reason about now that the\nin-memory index is not modified when we create the temporary index file.\n\nIn order to use create_index_from_tree() we need to add a new\nparameter to control whether the index is reset, as the new caller\nneeds to merge the index rather than reset it so that we still detect\nintent-to-add entries.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 15 ++++-----------\n 1 file changed, 4 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex b4fe7465e4..ddeb5a5d2a 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -374,7 +374,7 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)\n }\n \n static int create_index_from_tree(const struct object_id *tree_id,\n-\t\t\t\t  const char *index_path)\n+\t\t\t\t  const char *index_path, bool reset)\n {\n \tint nr_trees = 1;\n \tint ret = 0;\n@@ -403,7 +403,7 @@ static int create_index_from_tree(const struct object_id *tree_id,\n \topts.src_index = the_repository->index;\n \topts.dst_index = &dst_istate;\n \topts.merge = 1;\n-\topts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n+\topts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0;\n \topts.fn = oneway_merge;\n \n \tif (unpack_trees(nr_trees, t, &opts)) {\n@@ -1399,7 +1399,7 @@ static enum create_result stash_patch(struct stash_info *info,\n \t\tgoto done;\n \t}\n \n-\tif (create_index_from_tree(head_tree, stash_index_path.buf)) {\n+\tif (create_index_from_tree(head_tree, stash_index_path.buf, true)) {\n \t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n@@ -1458,22 +1458,15 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps\n \trepo_init_revisions(the_repository, &rev, NULL);\n \tcopy_pathspec(&rev.prune_data, ps);\n \n-\tset_alternate_index_output(stash_index_path.buf);\n-\tif (reset_tree(&info->i_tree, 0, 0)) {\n+\tif (create_index_from_tree(&info->i_tree, stash_index_path.buf, false)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n-\tset_alternate_index_output(NULL);\n \n \trev.diffopt.output_format = DIFF_FORMAT_CALLBACK;\n \trev.diffopt.format_callback = add_diff_to_buf;\n \trev.diffopt.format_callback_data = &diff_output;\n \n-\tif (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {\n-\t\tret = -1;\n-\t\tgoto done;\n-\t}\n-\n \tadd_pending_object(&rev, parse_object(the_repository, &info->b_commit),\n \t\t\t   \"\");\n \trun_diff_index(&rev, 0);\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"555566","messageId":"61e4ce22a0fe228047c666c30d814b2541c8e932.1792509606.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1792509606.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v3 6/7] stash create: remove unnecessary index load and refresh","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-20T15:20:41Z","receivedAt":"2026-10-20T15:20:41Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\ncreate_index_from_tree() is called either directly or indirectly\nfrom do_create_stash() after it has loaded and refreshed the index,\nso we do not need to refresh it again here. This saves three lstat()\ncalls per changed path.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 3 ---\n 1 file changed, 3 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex ddeb5a5d2a..55916259c3 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -384,9 +384,6 @@ static int create_index_from_tree(const struct object_id *tree_id,\n \tstruct index_state dst_istate = INDEX_STATE_INIT(the_repository);\n \tstruct lock_file lock_file = LOCK_INIT;\n \n-\trepo_read_index_preload(the_repository, NULL, 0);\n-\trefresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);\n-\n \thold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);\n \n \tmemset(&opts, 0, sizeof(opts));\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"555567","messageId":"a486a625c95cd7493e9cae424bb3a4fdc35c9e66.1792509606.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1792509606.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v3 7/7] stash: simplify reset_tree() function signature","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-20T15:20:42Z","receivedAt":"2026-10-20T15:20:42Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nThere is only one caller and it does not want to update the worktree or\nreset the index so remove those unused parameters.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 8 ++------\n 1 file changed, 2 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex 55916259c3..bc1252790b 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -332,7 +332,7 @@ static int clear_stash(int argc, const char **argv, const char *prefix,\n \treturn do_clear_stash();\n }\n \n-static int reset_tree(struct object_id *i_tree, int update, int reset)\n+static int reset_tree(struct object_id *i_tree)\n {\n \tint nr_trees = 1;\n \tstruct unpack_trees_options opts;\n@@ -358,10 +358,6 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)\n \topts.src_index = the_repository->index;\n \topts.dst_index = the_repository->index;\n \topts.merge = 1;\n-\topts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0;\n-\topts.update = update;\n-\tif (update)\n-\t\topts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n \topts.fn = oneway_merge;\n \n \tif (unpack_trees(nr_trees, t, &opts))\n@@ -738,7 +734,7 @@ static enum stash_apply_result do_apply_stash(const char *prefix,\n \t}\n \n \tif (has_index) {\n-\t\tif (reset_tree(&index_tree, 0, 0))\n+\t\tif (reset_tree(&index_tree))\n \t\t\tret = -1;\n \t} else {\n \t\tunstage_changes_unless_new(&c_tree);\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"555630","messageId":"xmqqse20rpq3.fsf@gitster.g","threadId":"66466","inReplyTo":"de5e0c196a9ff241861f5598ac9f6bc5c960f6ec.1792509606.git.phillip.wood@dunelm.org.uk","subject":"Re: [PATCH v3 5/7] stash create: rework temporary index creation","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-20T22:07:00Z","receivedAt":"2026-10-20T22:07:00Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> In order to use create_index_from_tree() we need to add a new\n> parameter to control whether the index is reset, as the new caller\n> needs to merge the index rather than reset it so that we still detect\n> intent-to-add entries.\n>\n> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n> ---\n>  builtin/stash.c | 15 ++++-----------\n>  1 file changed, 4 insertions(+), 11 deletions(-)\n\nThis is the most interesting step in the series.  The patch itself\nis very small, but it frees us from flipping the in-core index and\nits side effects.  Nicely done.\n\n> diff --git a/builtin/stash.c b/builtin/stash.c\n> index b4fe7465e4..ddeb5a5d2a 100644\n> --- a/builtin/stash.c\n> +++ b/builtin/stash.c\n> @@ -374,7 +374,7 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)\n>  }\n>  \n>  static int create_index_from_tree(const struct object_id *tree_id,\n> -\t\t\t\t  const char *index_path)\n> +\t\t\t\t  const char *index_path, bool reset)\n>  {\n>  \tint nr_trees = 1;\n>  \tint ret = 0;\n> @@ -403,7 +403,7 @@ static int create_index_from_tree(const struct object_id *tree_id,\n>  \topts.src_index = the_repository->index;\n>  \topts.dst_index = &dst_istate;\n>  \topts.merge = 1;\n> -\topts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n> +\topts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0;\n>  \topts.fn = oneway_merge;\n>  \n>  \tif (unpack_trees(nr_trees, t, &opts)) {\n> @@ -1399,7 +1399,7 @@ static enum create_result stash_patch(struct stash_info *info,\n>  \t\tgoto done;\n>  \t}\n>  \n> -\tif (create_index_from_tree(head_tree, stash_index_path.buf)) {\n> +\tif (create_index_from_tree(head_tree, stash_index_path.buf, true)) {\n>  \t\tret = CREATE_ERROR;\n>  \t\tgoto done;\n>  \t}\n> @@ -1458,22 +1458,15 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps\n>  \trepo_init_revisions(the_repository, &rev, NULL);\n>  \tcopy_pathspec(&rev.prune_data, ps);\n>  \n> -\tset_alternate_index_output(stash_index_path.buf);\n> -\tif (reset_tree(&info->i_tree, 0, 0)) {\n> +\tif (create_index_from_tree(&info->i_tree, stash_index_path.buf, false)) {\n>  \t\tret = -1;\n>  \t\tgoto done;\n>  \t}\n> -\tset_alternate_index_output(NULL);\n>  \n>  \trev.diffopt.output_format = DIFF_FORMAT_CALLBACK;\n>  \trev.diffopt.format_callback = add_diff_to_buf;\n>  \trev.diffopt.format_callback_data = &diff_output;\n>  \n> -\tif (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {\n> -\t\tret = -1;\n> -\t\tgoto done;\n> -\t}\n> -\n>  \tadd_pending_object(&rev, parse_object(the_repository, &info->b_commit),\n>  \t\t\t   \"\");\n>  \trun_diff_index(&rev, 0);\n\n"},{"id":"555639","messageId":"xmqqo6cnsyqj.fsf@gitster.g","threadId":"66466","inReplyTo":"cover.1792509606.git.phillip.wood@dunelm.org.uk","subject":"Re: [PATCH v3 0/7] stash creation: avoid unecessary index loads and refreshes","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-21T00:07:00Z","receivedAt":"2026-10-21T00:07:00Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> \"git stash push\" and \"git stash create\" load and refresh the index\n> multiple times when they only need to do it once. This series removes\n> a number of calls to repo_read_preload_index() and refresh_index()\n> which reduces the number of lstat() calls on each changed path by\n> four for \"git stash create\" and eight for \"git stash push\". It also\n> removes a duplicate call to check_changes() so that when stashing\n> untracked files we only walk them once.\n>\n> Changes since V1:\n>  - Added an enum for the various return values from do_create_stash()\n>  - Fixed typos in the commit message of patch 2 (was patch 1)\n>  - Removed an unnecessary index refresh and several unnecessary index\n>    preloads\n\nNicely split and described.  Looked quite well done.\n\nWill queue.\n\nThanks.\n\n"},{"id":"555660","messageId":"xmqq5wyvsi2j.fsf@gitster.g","threadId":"66466","inReplyTo":"9e27bcd676ee763ed499fb337b1497f3fe2405b2.1792509606.git.phillip.wood@dunelm.org.uk","subject":"Re: [PATCH v3 1/7] stash: use an enum for create status","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-21T06:07:00Z","receivedAt":"2026-10-21T06:07:00Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> From: Phillip Wood <phillip.wood@dunelm.org.uk>\n>\n> Change the return type of do_create_stash() to an enum in preparation\n> for adding a separate value for \"there are no changes to stash\" in a\n> future commit. There is a small change to behavior here which fixes\n> an anomaly with the exit status of create_stash(). That function calls\n> check_changes() without refreshing the index and exits 0 if there are\n> no changes. However, because it does not refresh the index, that check\n> is unreliable which means do_create_stash() can return 1 when there\n> aren't any changes, and then create_stash() exits 1 rather than 0.\n> This commit fixes that anomaly.\n>\n> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n> ---\n>  builtin/stash.c | 83 ++++++++++++++++++++++++++++---------------------\n>  1 file changed, 47 insertions(+), 36 deletions(-)\n\nThe patch makes it look more scary larger than it actually is.\n\n> +enum create_result {\n> +\tCREATE_ERROR = -1,\n> +\tCREATE_SUCCESS,\n> +\t/* No changes were stashed */\n> +\tCREATE_EMPTY,\n> +};\n\nSUCCESS immediatly following ERROR that is explicitly defined to be\n(-1) may qualify as explicitly defined to be 0, but personally I\nprobably prefer to be extra explicit here, like\n\n\tCREATE_ERROR = -1,\n\tCREATE_SUCCESS = 0,\n\neven though this is not worth rerolling the series for.\n\n> +static enum create_result stash_staged(struct stash_info *info,\n> +\t\t\t\t  struct strbuf *out_patch, int quiet)\n>  {\n> -\tint ret = 0;\n> +\tenum create_result ret = 0;\n\nNot CREATE_SUCCESS?  This can be returned if various checks in the\nfunction assigns different values to this variable, right?\n\n> +static enum create_result stash_patch(struct stash_info *info,\n> +\t\tconst struct pathspec *ps, struct strbuf *out_patch, int quiet,\n> +\t\tstruct interactive_options *interactive_opts)\n>  {\n> -\tint ret = 0;\n> +\tenum create_result ret = 0;\n\nCREATE_SUCCESS?\n\n> +static enum create_result do_create_stash(const struct pathspec *ps,\n> +\t\tstruct strbuf *stash_msg_buf, int include_untracked,\n> +\t\tint patch_mode, struct interactive_options *interactive_opts,\n> +\t\tint only_staged, struct stash_info *info, struct strbuf *patch,\n> +\t\tint quiet)\n>  {\n> -\tint ret = 0;\n> +\tenum create_result ret = 0;\n\nCREATE_SUCCESS?\n\n>  \tif (patch_mode) {\n>  \t\tret = stash_patch(info, ps, patch, quiet, interactive_opts);\n> -\t\tif (ret < 0) {\n> +\t\tif (ret == CREATE_ERROR) {\n>  \t\t\tif (!quiet)\n>  \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n>  \t\t\t\t\t\t     \"worktree state\"));\n>  \t\t\tgoto done;\n> -\t\t} else if (ret > 0) {\n> +\t\t} else if (ret != CREATE_SUCCESS) {\n>  \t\t\tgoto done;\n>  \t\t}\n\nI wonder if switch/case is easier to read for these users of these\nenum return values.  Some check explicitly against CREATE_ERROR\n(like the one above), some treat anything other than CREATE_SUCCESS\nas undesirable, etc.\n\n>  \t} else if (only_staged) {\n>  \t\tret = stash_staged(info, patch, quiet);\n> -\t\tif (ret < 0) {\n> +\t\tif (ret == CREATE_ERROR) {\n>  \t\t\tif (!quiet)\n>  \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n>  \t\t\t\t\t\t     \"staged state\"));\n>  \t\t\tgoto done;\n> -\t\t} else if (ret > 0) {\n> +\t\t} else if (ret != CREATE_SUCCESS) {\n>  \t\t\tgoto done;\n>  \t\t}\n>  \t} else {\n\nDitto.\n\n> @@ -1660,12 +1667,13 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n>  \n>  \tret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n>  \t\t\t      NULL, 0);\n> -\tif (!ret)\n> +\tif (ret == CREATE_SUCCESS)\n>  \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n>  \n>  \tfree_stash_info(&info);\n>  \tstrbuf_release(&stash_msg_buf);\n> -\treturn ret;\n> +\t/* Do not return an error if there are no changes */\n> +\treturn ret == CREATE_ERROR ? -1 : 0;\n>  }\n\nDitto.\n\n"},{"id":"555876","messageId":"7ffce2cb-07de-4bd3-a9b4-81fd27b920f5@gmail.com","threadId":"66466","inReplyTo":"xmqq5wyvsi2j.fsf@gitster.g","subject":"Re: [PATCH v3 1/7] stash: use an enum for create status","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-22T15:57:32Z","receivedAt":"2026-10-22T15:57:32Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 21/10/2026 07:07, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n> >> +enum create_result {\n>> +\tCREATE_ERROR = -1,\n>> +\tCREATE_SUCCESS,\n>> +\t/* No changes were stashed */\n>> +\tCREATE_EMPTY,\n>> +};\n> > SUCCESS immediatly following ERROR that is explicitly defined to be\n> (-1) may qualify as explicitly defined to be 0, but personally I\n> probably prefer to be extra explicit here, like\n> > \tCREATE_ERROR = -1,\n> \tCREATE_SUCCESS = 0,\n> > even though this is not worth rerolling the series for.\n\nI can do that, though we really shouldn't be relying on the value for any of the member other than CREATE_ERROR which we want to match the return value of error(...).\n\n> >> +static enum create_result stash_staged(struct stash_info *info,\n>> +\t\t\t\t  struct strbuf *out_patch, int quiet)\n>>   {\n>> -\tint ret = 0;\n>> +\tenum create_result ret = 0;\n> > Not CREATE_SUCCESS?  This can be returned if various checks in the\n> function assigns different values to this variable, right?\n\nYes, I forgot to convert the initializer when I changed the type, will fix.\n\n>>   \tif (patch_mode) {\n>>   \t\tret = stash_patch(info, ps, patch, quiet, interactive_opts);\n>> -\t\tif (ret < 0) {\n>> +\t\tif (ret == CREATE_ERROR) {\n>>   \t\t\tif (!quiet)\n>>   \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n>>   \t\t\t\t\t\t     \"worktree state\"));\n>>   \t\t\tgoto done;\n>> -\t\t} else if (ret > 0) {\n>> +\t\t} else if (ret != CREATE_SUCCESS) {\n>>   \t\t\tgoto done;\n>>   \t\t}\n> > I wonder if switch/case is easier to read for these users of these\n> enum return values.  Some check explicitly against CREATE_ERROR\n> (like the one above), some treat anything other than CREATE_SUCCESS\n> as undesirable, etc.\n\nI've had a play with this and I think the enum is better here\n\n>>   \t} else if (only_staged) {\n>>   \t\tret = stash_staged(info, patch, quiet);\n>> -\t\tif (ret < 0) {\n>> +\t\tif (ret == CREATE_ERROR) {\n>>   \t\t\tif (!quiet)\n>>   \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n>>   \t\t\t\t\t\t     \"staged state\"));\n>>   \t\t\tgoto done;\n>> -\t\t} else if (ret > 0) {\n>> +\t\t} else if (ret != CREATE_SUCCESS) {\n>>   \t\t\tgoto done;\n>>   \t\t}\n>>   \t} else {\n> > Ditto.\n\nand here\n\n> >> @@ -1660,12 +1667,13 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n>>   >>   \tret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n>>   \t\t\t      NULL, 0);\n>> -\tif (!ret)\n>> +\tif (ret == CREATE_SUCCESS)\n>>   \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n>>   >>   \tfree_stash_info(&info);\n>>   \tstrbuf_release(&stash_msg_buf);\n>> -\treturn ret;\n>> +\t/* Do not return an error if there are no changes */\n>> +\treturn ret == CREATE_ERROR ? -1 : 0;\n>>   }\n> > Ditto.\n\nbut here we're checking for a single value, so using a switch seems like overkill.\n\nI'll try and push out a new version tomorrow\n\nThanks\n\nPhillip\n\n\n"},{"id":"555882","messageId":"xmqqpkx1hdph.fsf@gitster.g","threadId":"66466","inReplyTo":"7ffce2cb-07de-4bd3-a9b4-81fd27b920f5@gmail.com","subject":"Re: [PATCH v3 1/7] stash: use an enum for create status","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-22T17:01:14Z","receivedAt":"2026-10-22T17:01:14Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> On 21/10/2026 07:07, Junio C Hamano wrote:\n>> Phillip Wood <phillip.wood123@gmail.com> writes:\n>> \n>>> +enum create_result {\n>>> +\tCREATE_ERROR = -1,\n>>> +\tCREATE_SUCCESS,\n>>> +\t/* No changes were stashed */\n>>> +\tCREATE_EMPTY,\n>>> +};\n>> \n>> SUCCESS immediatly following ERROR that is explicitly defined to be\n>> (-1) may qualify as explicitly defined to be 0, but personally I\n>> probably prefer to be extra explicit here, like\n>> \n>> \tCREATE_ERROR = -1,\n>> \tCREATE_SUCCESS = 0,\n>> \n>> even though this is not worth rerolling the series for.\n>\n> I can do that, though we really shouldn't be relying on the value for \n> any of the member other than CREATE_ERROR which we want to match the \n> return value of error(...).\n\nThe suggestion was merely because I thought that we also expect\nSUCCESS to be 0 to write something like this:\n\n\tif (!attempt_to_create(...))\n\t\thappy;\n\nIf we truly care about ERROR == -1 and nothing else, then please\nignore the above.\n\n>>>   \t\tret = stash_patch(info, ps, patch, quiet, interactive_opts);\n>>> -\t\tif (ret < 0) {\n>>> +\t\tif (ret == CREATE_ERROR) {\n>>>   \t\t\tif (!quiet)\n>>>   \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n>>>   \t\t\t\t\t\t     \"worktree state\"));\n>>>   \t\t\tgoto done;\n>>> -\t\t} else if (ret > 0) {\n>>> +\t\t} else if (ret != CREATE_SUCCESS) {\n>>>   \t\t\tgoto done;\n>>>   \t\t}\n>> \n>> I wonder if switch/case is easier to read for these users of these\n>> enum return values.  Some check explicitly against CREATE_ERROR\n>> (like the one above), some treat anything other than CREATE_SUCCESS\n>> as undesirable, etc.\n>\n> I've had a play with this and I think the enum is better here\n\nPuzzled.  I wasn't contrasting enum vs switch.  I was contrasting\nif/else if cascade against switch, assuming that both use enum.\n\n"},{"id":"555894","messageId":"f01f4d8b-bbdd-4629-a123-40ae560f22a3@gmail.com","threadId":"66466","inReplyTo":"xmqqpkx1hdph.fsf@gitster.g","subject":"Re: [PATCH v3 1/7] stash: use an enum for create status","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-22T18:33:25Z","receivedAt":"2026-10-22T18:33:25Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"\n\nOn 22/10/2026 18:01, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n> >> On 21/10/2026 07:07, Junio C Hamano wrote:\n>>> Phillip Wood <phillip.wood123@gmail.com> writes:\n>>>\n>>>>    \t\tret = stash_patch(info, ps, patch, quiet, interactive_opts);\n>>>> -\t\tif (ret < 0) {\n>>>> +\t\tif (ret == CREATE_ERROR) {\n>>>>    \t\t\tif (!quiet)\n>>>>    \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n>>>>    \t\t\t\t\t\t     \"worktree state\"));\n>>>>    \t\t\tgoto done;\n>>>> -\t\t} else if (ret > 0) {\n>>>> +\t\t} else if (ret != CREATE_SUCCESS) {\n>>>>    \t\t\tgoto done;\n>>>>    \t\t}\n>>>\n>>> I wonder if switch/case is easier to read for these users of these\n>>> enum return values.  Some check explicitly against CREATE_ERROR\n>>> (like the one above), some treat anything other than CREATE_SUCCESS\n>>> as undesirable, etc.\n>>\n>> I've had a play with this and I think the enum is better here\n> > Puzzled.  I wasn't contrasting enum vs switch.  I was contrasting\n> if/else if cascade against switch, assuming that both use enum.\n\nSorry s/enum/switch/ i.e I think the switch is better here (but not for all of them - see my previous mail)\n\nThanks\n\nPhillip\n\n\n"},{"id":"556052","messageId":"cover.1792767794.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1791218125.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v4 0/7] stash creation: avoid unecessary index loads and refreshes","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-23T15:03:28Z","receivedAt":"2026-10-23T15:03:28Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"\"git stash push\" and \"git stash create\" load and refresh the index\nmultiple times when they only need to do it once. This series removes\na number of calls to repo_read_preload_index() and refresh_index()\nwhich reduces the number of lstat() calls on each changed path by\nfour for \"git stash create\" and eight for \"git stash push\". It also\nremoves a duplicate call to check_changes() so that when stashing\nuntracked files we only walk them once.\n\nThanks to Junio for his comments on V3\n\nChanges since V3:\n - Avoid implict cast of enum create_result to int in create_stash()\n - Fixed enum initializers\n - Use switch() rather than if() when checking enum create_result\n   return values\n\nChanges since V2:\n - Fixed a typo in patch 4\n - Rewrote commit message for patch 5\n\nChanges since V1:\n - Added an enum for the various return values from do_create_stash()\n - Fixed typos in the commit message of patch 2 (was patch 1)\n - Removed an unnecessary index refresh and several unnecessary index\n   preloads\n\nCover letter for V1:\n\n\"git stash push\" and \"git stash create\" check if there are\nany unstaged or uncommitted changes at startup, and then again\nwhen they try to create the stash. This short series removes\nthat duplication of effort which I spotted while looking at\n<20260929074222.11942-1-kazumasa.shigeta@kanamei.com>.\n\nbase-commit: c46c1e37724f0478939de636ab8ea5a89086d532\nPublished-As: https://github.com/phillipwood/git/releases/tag/pw%2Fstash-optimize-check_changes-calls%2Fv4\nView-Changes-At: https://github.com/phillipwood/git/compare/c46c1e377...f61ebbf9a\nFetch-It-Via: git fetch https://github.com/phillipwood/git pw/stash-optimize-check_changes-calls/v4\n\n\nPhillip Wood (7):\n  stash: use an enum for create status\n  stash create: remove duplicate changes detection\n  stash push: remove duplicate changes detection\n  stash push: avoid duplicate calls to preload_index()\n  stash create: rework temporary index creation\n  stash create: remove unnecessary index load and refresh\n  stash: simplify reset_tree() function signature\n\n builtin/stash.c | 167 ++++++++++++++++++++++++------------------------\n 1 file changed, 85 insertions(+), 82 deletions(-)\n\nRange-diff against v3:\n1:  9e27bcd676e ! 1:  685914011f9 stash: use an enum for create status\n    @@ builtin/stash.c: static int save_untracked_files(struct stash_info *info, struct\n     +\t\t\t\t  struct strbuf *out_patch, int quiet)\n      {\n     -\tint ret = 0;\n    -+\tenum create_result ret = 0;\n    ++\tenum create_result ret = CREATE_SUCCESS;\n      \tstruct child_process cp_diff_tree = CHILD_PROCESS_INIT;\n      \tstruct index_state istate = INDEX_STATE_INIT(the_repository);\n      \n    @@ builtin/stash.c: static int stash_staged(struct stash_info *info, struct strbuf\n     +\t\tstruct interactive_options *interactive_opts)\n      {\n     -\tint ret = 0;\n    -+\tenum create_result ret = 0;\n    ++\tenum create_result ret = CREATE_SUCCESS;\n      \tstruct child_process cp_diff_tree = CHILD_PROCESS_INIT;\n      \tstruct commit *head_commit;\n      \tconst struct object_id *head_tree;\n    @@ builtin/stash.c: static int stash_working_tree(struct stash_info *info, const st\n     +\t\tint quiet)\n      {\n     -\tint ret = 0;\n    -+\tenum create_result ret = 0;\n    ++\tenum create_result ret = CREATE_SUCCESS;\n      \tint flags = 0;\n      \tint untracked_commit_option = 0;\n      \tconst char *head_short_sha1 = NULL;\n    @@ builtin/stash.c: static int do_create_stash(const struct pathspec *ps, struct st\n      \tif (patch_mode) {\n      \t\tret = stash_patch(info, ps, patch, quiet, interactive_opts);\n     -\t\tif (ret < 0) {\n    -+\t\tif (ret == CREATE_ERROR) {\n    ++\t\tswitch (ret) {\n    ++\t\tcase CREATE_ERROR:\n      \t\t\tif (!quiet)\n      \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n      \t\t\t\t\t\t     \"worktree state\"));\n    - \t\t\tgoto done;\n    +-\t\t\tgoto done;\n     -\t\t} else if (ret > 0) {\n    -+\t\t} else if (ret != CREATE_SUCCESS) {\n    - \t\t\tgoto done;\n    +-\t\t\tgoto done;\n    ++\t\t\t/* fallthrough */\n    ++\t\tcase CREATE_EMPTY:\n    ++\t\t\tgoto done;\n    ++\n    ++\t\tcase CREATE_SUCCESS:\n    ++\t\t\tbreak;\n      \t\t}\n      \t} else if (only_staged) {\n      \t\tret = stash_staged(info, patch, quiet);\n     -\t\tif (ret < 0) {\n    -+\t\tif (ret == CREATE_ERROR) {\n    ++\t\tswitch (ret) {\n    ++\t\t\tcase CREATE_ERROR:\n      \t\t\tif (!quiet)\n      \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n      \t\t\t\t\t\t     \"staged state\"));\n    - \t\t\tgoto done;\n    +-\t\t\tgoto done;\n     -\t\t} else if (ret > 0) {\n    -+\t\t} else if (ret != CREATE_SUCCESS) {\n    - \t\t\tgoto done;\n    +-\t\t\tgoto done;\n    ++\t\t\t/* fallthrough */\n    ++\t\t\tcase CREATE_EMPTY:\n    ++\t\t\t\tgoto done;\n    ++\n    ++\t\t\tcase CREATE_SUCCESS:\n    ++\t\t\t\tbreak;\n      \t\t}\n      \t} else {\n      \t\tif (stash_working_tree(info, ps)) {\n    @@ builtin/stash.c: static int do_create_stash(const struct pathspec *ps, struct st\n      \t\tgoto done;\n      \t}\n      \n    +@@ builtin/stash.c: static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n    + static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n    + \t\t\tstruct repository *repo UNUSED)\n    + {\n    +-\tint ret;\n    ++\tenum create_result create_res;\n    + \tstruct strbuf stash_msg_buf = STRBUF_INIT;\n    + \tstruct stash_info info = STASH_INFO_INIT;\n    + \tstruct pathspec ps;\n     @@ builtin/stash.c: static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n    + \tif (!check_changes_tracked_files(&ps))\n    + \t\treturn 0;\n      \n    - \tret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n    - \t\t\t      NULL, 0);\n    +-\tret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n    +-\t\t\t      NULL, 0);\n     -\tif (!ret)\n    -+\tif (ret == CREATE_SUCCESS)\n    ++\tcreate_res = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n    ++\t\t\t\t     NULL, 0);\n    ++\tif (create_res == CREATE_SUCCESS)\n      \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n      \n      \tfree_stash_info(&info);\n      \tstrbuf_release(&stash_msg_buf);\n     -\treturn ret;\n     +\t/* Do not return an error if there are no changes */\n    -+\treturn ret == CREATE_ERROR ? -1 : 0;\n    ++\treturn create_res == CREATE_ERROR ? -1 : 0;\n      }\n      \n      static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,\n    @@ builtin/stash.c: static int do_push_stash(const struct pathspec *ps, const char\n     +\tcreate_res =  do_create_stash(ps, &stash_msg_buf, include_untracked,\n     +\t\t\t\t      patch_mode, interactive_opts, only_staged,\n     +\t\t\t\t      &info, &patch, quiet);\n    -+\tif (create_res != CREATE_SUCCESS) {\n    ++\tswitch (create_res) {\n    ++\tcase CREATE_ERROR:\n    ++\tcase CREATE_EMPTY:\n      \t\tret = -1;\n      \t\tgoto done;\n    ++\n    ++\tcase CREATE_SUCCESS:\n    ++\t\tbreak;\n      \t}\n    + \n    + \tif (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {\n2:  d56b7c6f672 ! 2:  d2773fc82eb stash create: remove duplicate changes detection\n    @@ builtin/stash.c: static int create_stash(int argc, const char **argv, const char\n     -\tif (!check_changes_tracked_files(&ps))\n     -\t\treturn 0;\n      \n    - \tret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n    - \t\t\t      NULL, 0);\n    + \tcreate_res = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n    + \t\t\t\t     NULL, 0);\n3:  36fc0eb9130 ! 3:  785d2a663da stash push: remove duplicate changes detection\n    @@ builtin/stash.c: static enum create_result do_create_stash(const struct pathspec\n      \t\tgoto done;\n      \t}\n      \n    +@@ builtin/stash.c: static enum create_result do_create_stash(const struct pathspec *ps,\n    + \t\t\t\t\t\t     \"worktree state\"));\n    + \t\t\t/* fallthrough */\n    + \t\tcase CREATE_EMPTY:\n    ++\t\tcase CREATE_NO_CHANGES:\n    + \t\t\tgoto done;\n    + \n    + \t\tcase CREATE_SUCCESS:\n    +@@ builtin/stash.c: static enum create_result do_create_stash(const struct pathspec *ps,\n    + \t\t\t\t\t\t     \"staged state\"));\n    + \t\t\t/* fallthrough */\n    + \t\t\tcase CREATE_EMPTY:\n    ++\t\t\tcase CREATE_NO_CHANGES:\n    + \t\t\t\tgoto done;\n    + \n    + \t\t\tcase CREATE_SUCCESS:\n     @@ builtin/stash.c: static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n      \t\tfree(ps_matched);\n      \t}\n    @@ builtin/stash.c: static int do_push_stash(const struct pathspec *ps, const char\n      \t\tret = -1;\n      \t\tif (!quiet)\n     @@ builtin/stash.c: static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n    - \tcreate_res =  do_create_stash(ps, &stash_msg_buf, include_untracked,\n      \t\t\t\t      patch_mode, interactive_opts, only_staged,\n      \t\t\t\t      &info, &patch, quiet);\n    --\tif (create_res != CREATE_SUCCESS) {\n    -+\tif (create_res == CREATE_NO_CHANGES) {\n    + \tswitch (create_res) {\n    ++\tcase CREATE_NO_CHANGES:\n     +\t\tif (!quiet)\n     +\t\t\tprintf_ln(_(\"No local changes to save\"));\n     +\t\tret = 0;\n     +\t\tgoto done;\n    -+\t} else if (create_res != CREATE_SUCCESS) {\n    ++\n    + \tcase CREATE_ERROR:\n    + \tcase CREATE_EMPTY:\n      \t\tret = -1;\n    - \t\tgoto done;\n    - \t}\n4:  a9875ba5b35 ! 4:  32f7c97b7c0 stash push: avoid duplicate calls to preload_index()\n    @@ builtin/stash.c: static int create_stash(int argc, const char **argv, const char\n      \tmemset(&ps, 0, sizeof(ps));\n      \n     +\trepo_read_index(the_repository);\n    - \tret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n    - \t\t\t      NULL, 0);\n    - \tif (ret == CREATE_SUCCESS)\n    + \tcreate_res = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n    + \t\t\t\t     NULL, 0);\n    + \tif (create_res == CREATE_SUCCESS)\n     @@ builtin/stash.c: static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n      \t\tgoto done;\n      \t}\n5:  de5e0c196a9 = 5:  b62d3d080a8 stash create: rework temporary index creation\n6:  61e4ce22a0f = 6:  8c54c118307 stash create: remove unnecessary index load and refresh\n7:  a486a625c95 = 7:  f61ebbf9a3e stash: simplify reset_tree() function signature\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"556053","messageId":"685914011f9fc6c425e67fc4196b83af26249a66.1792767795.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1792767794.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v4 1/7] stash: use an enum for create status","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-23T15:03:29Z","receivedAt":"2026-10-23T15:03:29Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nChange the return type of do_create_stash() to an enum in preparation\nfor adding a separate value for \"there are no changes to stash\" in a\nfuture commit. There is a small change to behavior here which fixes\nan anomaly with the exit status of create_stash(). That function calls\ncheck_changes() without refreshing the index and exits 0 if there are\nno changes. However, because it does not refresh the index, that check\nis unreliable which means do_create_stash() can return 1 when there\naren't any changes, and then create_stash() exits 1 rather than 0.\nThis commit fixes that anomaly.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 110 +++++++++++++++++++++++++++++-------------------\n 1 file changed, 67 insertions(+), 43 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex 7a9843413b1..53744d96e84 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -1336,16 +1336,23 @@ static int save_untracked_files(struct stash_info *info, struct strbuf *msg,\n \treturn ret;\n }\n \n-static int stash_staged(struct stash_info *info, struct strbuf *out_patch,\n-\t\t\tint quiet)\n+enum create_result {\n+\tCREATE_ERROR = -1,\n+\tCREATE_SUCCESS,\n+\t/* No changes were stashed */\n+\tCREATE_EMPTY,\n+};\n+\n+static enum create_result stash_staged(struct stash_info *info,\n+\t\t\t\t  struct strbuf *out_patch, int quiet)\n {\n-\tint ret = 0;\n+\tenum create_result ret = CREATE_SUCCESS;\n \tstruct child_process cp_diff_tree = CHILD_PROCESS_INIT;\n \tstruct index_state istate = INDEX_STATE_INIT(the_repository);\n \n \tif (write_index_as_tree(&info->w_tree, &istate, the_repository->index_file,\n \t\t\t\t0, NULL)) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n@@ -1354,26 +1361,26 @@ static int stash_staged(struct stash_info *info, struct strbuf *out_patch,\n \t\t     \"--no-color\",\n \t\t     \"-U1\", \"HEAD\", oid_to_hex(&info->w_tree), \"--\", NULL);\n \tif (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n \tif (!out_patch->len) {\n \t\tif (!quiet)\n \t\t\tfprintf_ln(stderr, _(\"No staged changes\"));\n-\t\tret = 1;\n+\t\tret = CREATE_EMPTY;\n \t}\n \n done:\n \trelease_index(&istate);\n \treturn ret;\n }\n \n-static int stash_patch(struct stash_info *info, const struct pathspec *ps,\n-\t\t       struct strbuf *out_patch, int quiet,\n-\t\t       struct interactive_options *interactive_opts)\n+static enum create_result stash_patch(struct stash_info *info,\n+\t\tconst struct pathspec *ps, struct strbuf *out_patch, int quiet,\n+\t\tstruct interactive_options *interactive_opts)\n {\n-\tint ret = 0;\n+\tenum create_result ret = CREATE_SUCCESS;\n \tstruct child_process cp_diff_tree = CHILD_PROCESS_INIT;\n \tstruct commit *head_commit;\n \tconst struct object_id *head_tree;\n@@ -1384,17 +1391,17 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,\n \n \thead_commit = lookup_commit(the_repository, &info->b_commit);\n \tif (!head_commit || repo_parse_commit(the_repository, head_commit)) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \thead_tree = get_commit_tree_oid(head_commit);\n \tif (!head_tree) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n \tif (create_index_from_tree(head_tree, stash_index_path.buf)) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n@@ -1416,7 +1423,7 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,\n \t/* State of the working tree. */\n \tif (write_index_as_tree(&info->w_tree, &istate, stash_index_path.buf, 0,\n \t\t\t\tNULL)) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n@@ -1425,14 +1432,14 @@ static int stash_patch(struct stash_info *info, const struct pathspec *ps,\n \t\t     \"--no-color\",\n \t\t     oid_to_hex(&info->w_tree), \"--\", NULL);\n \tif (pipe_command(&cp_diff_tree, NULL, 0, out_patch, 0, NULL, 0)) {\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n \tif (!out_patch->len) {\n \t\tif (!quiet)\n \t\t\tfprintf_ln(stderr, _(\"No changes selected\"));\n-\t\tret = 1;\n+\t\tret = CREATE_EMPTY;\n \t}\n \n done:\n@@ -1499,13 +1506,13 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps\n \treturn ret;\n }\n \n-static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_buf,\n-\t\t\t   int include_untracked, int patch_mode,\n-\t\t\t   struct interactive_options *interactive_opts,\n-\t\t\t   int only_staged, struct stash_info *info, struct strbuf *patch,\n-\t\t\t   int quiet)\n+static enum create_result do_create_stash(const struct pathspec *ps,\n+\t\tstruct strbuf *stash_msg_buf, int include_untracked,\n+\t\tint patch_mode, struct interactive_options *interactive_opts,\n+\t\tint only_staged, struct stash_info *info, struct strbuf *patch,\n+\t\tint quiet)\n {\n-\tint ret = 0;\n+\tenum create_result ret = CREATE_SUCCESS;\n \tint flags = 0;\n \tint untracked_commit_option = 0;\n \tconst char *head_short_sha1 = NULL;\n@@ -1531,14 +1538,14 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n \t\tif (!quiet)\n \t\t\tfprintf_ln(stderr, _(\"You do not have \"\n \t\t\t\t\t     \"the initial commit yet\"));\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t} else {\n \t\thead_commit = lookup_commit(the_repository, &info->b_commit);\n \t}\n \n \tif (!check_changes(ps, include_untracked, &untracked_files)) {\n-\t\tret = 1;\n+\t\tret = CREATE_EMPTY;\n \t\tgoto done;\n \t}\n \n@@ -1565,7 +1572,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n \t\tif (!quiet)\n \t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n \t\t\t\t\t     \"index state\"));\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n@@ -1577,37 +1584,45 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n \t\t\tif (!quiet)\n \t\t\t\tfprintf_ln(stderr, _(\"Cannot save \"\n \t\t\t\t\t\t     \"the untracked files\"));\n-\t\t\tret = -1;\n+\t\t\tret = CREATE_ERROR;\n \t\t\tgoto done;\n \t\t}\n \t\tuntracked_commit_option = 1;\n \t}\n \tif (patch_mode) {\n \t\tret = stash_patch(info, ps, patch, quiet, interactive_opts);\n-\t\tif (ret < 0) {\n+\t\tswitch (ret) {\n+\t\tcase CREATE_ERROR:\n \t\t\tif (!quiet)\n \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n \t\t\t\t\t\t     \"worktree state\"));\n-\t\t\tgoto done;\n-\t\t} else if (ret > 0) {\n-\t\t\tgoto done;\n+\t\t\t/* fallthrough */\n+\t\tcase CREATE_EMPTY:\n+\t\t\tgoto done;\n+\n+\t\tcase CREATE_SUCCESS:\n+\t\t\tbreak;\n \t\t}\n \t} else if (only_staged) {\n \t\tret = stash_staged(info, patch, quiet);\n-\t\tif (ret < 0) {\n+\t\tswitch (ret) {\n+\t\t\tcase CREATE_ERROR:\n \t\t\tif (!quiet)\n \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n \t\t\t\t\t\t     \"staged state\"));\n-\t\t\tgoto done;\n-\t\t} else if (ret > 0) {\n-\t\t\tgoto done;\n+\t\t\t/* fallthrough */\n+\t\t\tcase CREATE_EMPTY:\n+\t\t\t\tgoto done;\n+\n+\t\t\tcase CREATE_SUCCESS:\n+\t\t\t\tbreak;\n \t\t}\n \t} else {\n \t\tif (stash_working_tree(info, ps)) {\n \t\t\tif (!quiet)\n \t\t\t\tfprintf_ln(stderr, _(\"Cannot save the current \"\n \t\t\t\t\t\t     \"worktree state\"));\n-\t\t\tret = -1;\n+\t\t\tret = CREATE_ERROR;\n \t\t\tgoto done;\n \t\t}\n \t}\n@@ -1630,7 +1645,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n \t\tif (!quiet)\n \t\t\tfprintf_ln(stderr, _(\"Cannot record \"\n \t\t\t\t\t     \"working tree state\"));\n-\t\tret = -1;\n+\t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n \n@@ -1646,7 +1661,7 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b\n static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n \t\t\tstruct repository *repo UNUSED)\n {\n-\tint ret;\n+\tenum create_result create_res;\n \tstruct strbuf stash_msg_buf = STRBUF_INIT;\n \tstruct stash_info info = STASH_INFO_INIT;\n \tstruct pathspec ps;\n@@ -1658,14 +1673,15 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n \tif (!check_changes_tracked_files(&ps))\n \t\treturn 0;\n \n-\tret = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n-\t\t\t      NULL, 0);\n-\tif (!ret)\n+\tcreate_res = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n+\t\t\t\t     NULL, 0);\n+\tif (create_res == CREATE_SUCCESS)\n \t\tprintf_ln(\"%s\", oid_to_hex(&info.w_commit));\n \n \tfree_stash_info(&info);\n \tstrbuf_release(&stash_msg_buf);\n-\treturn ret;\n+\t/* Do not return an error if there are no changes */\n+\treturn create_res == CREATE_ERROR ? -1 : 0;\n }\n \n static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int quiet,\n@@ -1674,6 +1690,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n \t\t\t int include_untracked, int only_staged)\n {\n \tint ret = 0;\n+\tenum create_result create_res;\n \tstruct stash_info info = STASH_INFO_INIT;\n \tstruct strbuf patch = STRBUF_INIT;\n \tstruct strbuf stash_msg_buf = STRBUF_INIT;\n@@ -1741,10 +1758,17 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n \n \tif (stash_msg)\n \t\tstrbuf_addstr(&stash_msg_buf, stash_msg);\n-\tif (do_create_stash(ps, &stash_msg_buf, include_untracked, patch_mode,\n-\t\t\t    interactive_opts, only_staged, &info, &patch, quiet)) {\n+\tcreate_res =  do_create_stash(ps, &stash_msg_buf, include_untracked,\n+\t\t\t\t      patch_mode, interactive_opts, only_staged,\n+\t\t\t\t      &info, &patch, quiet);\n+\tswitch (create_res) {\n+\tcase CREATE_ERROR:\n+\tcase CREATE_EMPTY:\n \t\tret = -1;\n \t\tgoto done;\n+\n+\tcase CREATE_SUCCESS:\n+\t\tbreak;\n \t}\n \n \tif (do_store_stash(&info.w_commit, stash_msg_buf.buf, 1)) {\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"556054","messageId":"d2773fc82ebbb7bc7db0656d9025db74e0b3ec01.1792767795.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1792767794.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v4 2/7] stash create: remove duplicate changes detection","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-23T15:03:30Z","receivedAt":"2026-10-23T15:03:30Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nBefore it creates a stash, git checks if there are any unstaged,\nor uncommitted changes. If there isn't anything to stash it bails\nout. Since ef0f0b4509 (stash: optimize `get_untracked_files()`\nand `check_changes()`, 2019-02-25) \"git stash create\" has checked\nfor changes twice, once in create_stash() before we refresh the\nindex and then again in do_create_stash() after the index has been\nrefreshed. That commit claims it is an optimization but it is not\nclear what it is trying to optimize by checking for changes twice,\nespecially as checking for changes before refreshing the index is\nunreliable (the scripted version of \"git stash create\", called \"git\nupdate-index -q --refresh\" before looking for any changes).\n\nAvoid checking for changes twice by removing the call to\ncheck_changes_tracked_files() from create_stash(). In principle we\ncould remove the call to check_changes() from do_create_stash()\ninstead, but it is more convenient to keep it there, rather than\nrequire each caller to call it instead.\n\nThe remaining callers of check_changes_tracked_files() sensibly read\nand refresh the index before calling it, so it no-longer needs to\ncall to repo_read_index().\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 5 -----\n 1 file changed, 5 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex 53744d96e84..5137456da7d 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -1248,9 +1248,6 @@ static int check_changes_tracked_files(const struct pathspec *ps)\n \tif (repo_get_oid(the_repository, \"HEAD\", &dummy))\n \t\treturn -1;\n \n-\tif (repo_read_index(the_repository) < 0)\n-\t\treturn -1;\n-\n \trepo_init_revisions(the_repository, &rev, NULL);\n \tcopy_pathspec(&rev.prune_data, ps);\n \n@@ -1670,8 +1667,6 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n \tstrbuf_join_argv(&stash_msg_buf, argc - 1, ++argv, ' ');\n \n \tmemset(&ps, 0, sizeof(ps));\n-\tif (!check_changes_tracked_files(&ps))\n-\t\treturn 0;\n \n \tcreate_res = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n \t\t\t\t     NULL, 0);\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"556055","messageId":"785d2a663daf5f48cab1672e6f07d2252938ca1a.1792767795.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1792767794.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v4 3/7] stash push: remove duplicate changes detection","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-23T15:03:31Z","receivedAt":"2026-10-23T15:03:31Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nBefore it creates a stash, git checks if there are any unstaged, or\nuncommitted changes. If there isn't anything to stash it bails out. \"git\nstash push\" checks for changes twice, once in do_push_stash() and then\nagain in do_create_stash(). While duplicating the checks for staged and\nunstaged charges is fairly cheap because they stop after finding the\nfirst changed path, if we're stashing untracked files then we visit\nthem all twice. Avoid that by removing the call to check_changes()\nfrom do_push_stash() and checking the return value of do_create_stash()\nto see if there were any changes to stash. If check_changes() finds\nthere are no changes do_create_stash() now returns CREATE_NO_CHANGES\nrather than CREATE_EMPTY. This enables us to distinguish between there\nbeing no changes and there being nothing stashed, so that \"git stash\npush --patch\" when nothing is selected, and \"git stash push --staged\"\nwhen the index matches HEAD, still exit 1 rather than 0.\n\nAs we're not calling check_changes() in do_push_stash() anymore, we do\nnot need to refresh the index there either because do_create_stash()\nrefreshes it again before calling check_changes(). Avoiding the extra\nindex refresh saves us two lstat() calls per changed path.\n\nThere is still one small change in behavior as, if there is nothing\nto stash, we'll now try to create the reflog for stashes before we\nrealize that there is nothing to stash. I don't think that should\nmatter in practice.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 24 +++++++++++-------------\n 1 file changed, 11 insertions(+), 13 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex 5137456da7d..7259f6374ce 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -1338,6 +1338,8 @@ enum create_result {\n \tCREATE_SUCCESS,\n \t/* No changes were stashed */\n \tCREATE_EMPTY,\n+\t/* There were no changes to stash */\n+\tCREATE_NO_CHANGES,\n };\n \n static enum create_result stash_staged(struct stash_info *info,\n@@ -1542,7 +1544,7 @@ static enum create_result do_create_stash(const struct pathspec *ps,\n \t}\n \n \tif (!check_changes(ps, include_untracked, &untracked_files)) {\n-\t\tret = CREATE_EMPTY;\n+\t\tret = CREATE_NO_CHANGES;\n \t\tgoto done;\n \t}\n \n@@ -1595,6 +1597,7 @@ static enum create_result do_create_stash(const struct pathspec *ps,\n \t\t\t\t\t\t     \"worktree state\"));\n \t\t\t/* fallthrough */\n \t\tcase CREATE_EMPTY:\n+\t\tcase CREATE_NO_CHANGES:\n \t\t\tgoto done;\n \n \t\tcase CREATE_SUCCESS:\n@@ -1609,6 +1612,7 @@ static enum create_result do_create_stash(const struct pathspec *ps,\n \t\t\t\t\t\t     \"staged state\"));\n \t\t\t/* fallthrough */\n \t\t\tcase CREATE_EMPTY:\n+\t\t\tcase CREATE_NO_CHANGES:\n \t\t\t\tgoto done;\n \n \t\t\tcase CREATE_SUCCESS:\n@@ -1732,18 +1736,6 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n \t\tfree(ps_matched);\n \t}\n \n-\tif (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,\n-\t\t\t\t\t NULL, NULL, NULL)) {\n-\t\tret = error(_(\"could not write index\"));\n-\t\tgoto done;\n-\t}\n-\n-\tif (!check_changes(ps, include_untracked, &untracked_files)) {\n-\t\tif (!quiet)\n-\t\t\tprintf_ln(_(\"No local changes to save\"));\n-\t\tgoto done;\n-\t}\n-\n \tif (!refs_reflog_exists(get_main_ref_store(the_repository), ref_stash) && do_clear_stash()) {\n \t\tret = -1;\n \t\tif (!quiet)\n@@ -1757,6 +1749,12 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n \t\t\t\t      patch_mode, interactive_opts, only_staged,\n \t\t\t\t      &info, &patch, quiet);\n \tswitch (create_res) {\n+\tcase CREATE_NO_CHANGES:\n+\t\tif (!quiet)\n+\t\t\tprintf_ln(_(\"No local changes to save\"));\n+\t\tret = 0;\n+\t\tgoto done;\n+\n \tcase CREATE_ERROR:\n \tcase CREATE_EMPTY:\n \t\tret = -1;\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"556056","messageId":"32f7c97b7c06402aa6f2c16b3d0d3407a0421ba7.1792767795.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1792767794.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v4 4/7] stash push: avoid duplicate calls to preload_index()","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-23T15:03:32Z","receivedAt":"2026-10-23T15:03:32Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\ndo_push_stash() needs to load the index so it can check that any\npathspec argument refers to tracked files. To do that it calls\nrepo_read_index_preload() which does not just load the index, it also\ncalls preload_index() which lstat()s all the files to see which ones\nare up-to-date. do_create_stash() then calls repo_read_index_preload()\nfor a second time which lstat()s all the changed paths again and then\ncalls repo_refresh_and_write_index() which invokes preload_index() for a\nthird time. So we're preloading the index three times when we only need\nto do it once. Avoid this by making the callers of do_create_stash()\nresponsible for reading the index and use repo_read_index() rather\nthan repo_read_index_preload(). This saves two lstat() calls per\nchanged path.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 4 ++--\n 1 file changed, 2 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex 7259f6374ce..8db75d9e135 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -1526,7 +1526,6 @@ static enum create_result do_create_stash(const struct pathspec *ps,\n \n \tprepare_fallback_ident(\"git stash\", \"git@stash\");\n \n-\trepo_read_index_preload(the_repository, NULL, 0);\n \tif (repo_refresh_and_write_index(the_repository, REFRESH_QUIET, 0, 0,\n \t\t\t\t\t NULL, NULL, NULL) < 0) {\n \t\tret = error(_(\"could not write index\"));\n@@ -1672,6 +1671,7 @@ static int create_stash(int argc, const char **argv, const char *prefix UNUSED,\n \n \tmemset(&ps, 0, sizeof(ps));\n \n+\trepo_read_index(the_repository);\n \tcreate_res = do_create_stash(&ps, &stash_msg_buf, 0, 0, NULL, 0, &info,\n \t\t\t\t     NULL, 0);\n \tif (create_res == CREATE_SUCCESS)\n@@ -1717,7 +1717,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q\n \t\tgoto done;\n \t}\n \n-\trepo_read_index_preload(the_repository, NULL, 0);\n+\trepo_read_index(the_repository);\n \tif (!include_untracked && ps->nr) {\n \t\tchar *ps_matched = xcalloc(ps->nr, 1);\n \n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"556057","messageId":"b62d3d080a8ff0d9e14741a886aa45d70019bd64.1792767795.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1792767794.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v4 5/7] stash create: rework temporary index creation","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-23T15:03:33Z","receivedAt":"2026-10-23T15:03:33Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nWe use reset_tree() to create the temporary index file used to stash\nuncommitted working tree changes. Unfortunately, reset_tree() reads\nthe tree into the repository's in-memory index and writes the index\nto the repository's default index path. In this case, we read the\ntree that's already in the index and copy the existing stat data\nacross, so overwriting the in-memory index should not change its\ncontents. To write to a temporary index file, the caller has to\nuse set_alternate_index_output(). While that writes the index to a\ntemporary file, it updates the timestamp of the in-memory index so\nthat the timestamp no longer represents the index file the in-memory\nindex was read from. In practice that probably does not matter, but\nset_alternate_index_output() also relies on a global variable which\nmakes the code harder to reason about.\n\nUse create_index_from_tree() instead of reset_tree() so that we\ncan create temporary index file without overwriting the in-memory\nindex or relying on set_alternate_index_output(). This also makes\nthe implementation consistent with the way we create the temporary\nindex for \"git stash push -p\". As create_index_from_tree() does not\nmodify the in-memory index it is clearly safe to remove the call to\nrepo_read_index_preload() which saves one lstat() call per changed\npath. It would probably be safe to remove that call without moving away\nfrom reset_tree() but it is much easier to reason about now that the\nin-memory index is not modified when we create the temporary index file.\n\nIn order to use create_index_from_tree() we need to add a new\nparameter to control whether the index is reset, as the new caller\nneeds to merge the index rather than reset it so that we still detect\nintent-to-add entries.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 15 ++++-----------\n 1 file changed, 4 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex 8db75d9e135..e17f75641a4 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -374,7 +374,7 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)\n }\n \n static int create_index_from_tree(const struct object_id *tree_id,\n-\t\t\t\t  const char *index_path)\n+\t\t\t\t  const char *index_path, bool reset)\n {\n \tint nr_trees = 1;\n \tint ret = 0;\n@@ -403,7 +403,7 @@ static int create_index_from_tree(const struct object_id *tree_id,\n \topts.src_index = the_repository->index;\n \topts.dst_index = &dst_istate;\n \topts.merge = 1;\n-\topts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n+\topts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0;\n \topts.fn = oneway_merge;\n \n \tif (unpack_trees(nr_trees, t, &opts)) {\n@@ -1399,7 +1399,7 @@ static enum create_result stash_patch(struct stash_info *info,\n \t\tgoto done;\n \t}\n \n-\tif (create_index_from_tree(head_tree, stash_index_path.buf)) {\n+\tif (create_index_from_tree(head_tree, stash_index_path.buf, true)) {\n \t\tret = CREATE_ERROR;\n \t\tgoto done;\n \t}\n@@ -1458,22 +1458,15 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps\n \trepo_init_revisions(the_repository, &rev, NULL);\n \tcopy_pathspec(&rev.prune_data, ps);\n \n-\tset_alternate_index_output(stash_index_path.buf);\n-\tif (reset_tree(&info->i_tree, 0, 0)) {\n+\tif (create_index_from_tree(&info->i_tree, stash_index_path.buf, false)) {\n \t\tret = -1;\n \t\tgoto done;\n \t}\n-\tset_alternate_index_output(NULL);\n \n \trev.diffopt.output_format = DIFF_FORMAT_CALLBACK;\n \trev.diffopt.format_callback = add_diff_to_buf;\n \trev.diffopt.format_callback_data = &diff_output;\n \n-\tif (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {\n-\t\tret = -1;\n-\t\tgoto done;\n-\t}\n-\n \tadd_pending_object(&rev, parse_object(the_repository, &info->b_commit),\n \t\t\t   \"\");\n \trun_diff_index(&rev, 0);\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"556058","messageId":"8c54c118307cce6553929f5ff7242f85d0bbc733.1792767795.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1792767794.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v4 6/7] stash create: remove unnecessary index load and refresh","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-23T15:03:34Z","receivedAt":"2026-10-23T15:03:34Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\ncreate_index_from_tree() is called either directly or indirectly\nfrom do_create_stash() after it has loaded and refreshed the index,\nso we do not need to refresh it again here. This saves three lstat()\ncalls per changed path.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 3 ---\n 1 file changed, 3 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex e17f75641a4..f97cd4771a5 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -384,9 +384,6 @@ static int create_index_from_tree(const struct object_id *tree_id,\n \tstruct index_state dst_istate = INDEX_STATE_INIT(the_repository);\n \tstruct lock_file lock_file = LOCK_INIT;\n \n-\trepo_read_index_preload(the_repository, NULL, 0);\n-\trefresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);\n-\n \thold_lock_file_for_update(&lock_file, index_path, LOCK_DIE_ON_ERROR);\n \n \tmemset(&opts, 0, sizeof(opts));\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"556059","messageId":"f61ebbf9a3e1196dea36e5fbcf4294c6addad222.1792767795.git.phillip.wood@dunelm.org.uk","threadId":"66466","inReplyTo":"cover.1792767794.git.phillip.wood@dunelm.org.uk","subject":"[PATCH v4 7/7] stash: simplify reset_tree() function signature","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-23T15:03:35Z","receivedAt":"2026-10-23T15:03:35Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"From: Phillip Wood <phillip.wood@dunelm.org.uk>\n\nThere is only one caller and it does not want to update the worktree or\nreset the index so remove those unused parameters.\n\nSigned-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n---\n builtin/stash.c | 8 ++------\n 1 file changed, 2 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/stash.c b/builtin/stash.c\nindex f97cd4771a5..f0e3053ab49 100644\n--- a/builtin/stash.c\n+++ b/builtin/stash.c\n@@ -332,7 +332,7 @@ static int clear_stash(int argc, const char **argv, const char *prefix,\n \treturn do_clear_stash();\n }\n \n-static int reset_tree(struct object_id *i_tree, int update, int reset)\n+static int reset_tree(struct object_id *i_tree)\n {\n \tint nr_trees = 1;\n \tstruct unpack_trees_options opts;\n@@ -358,10 +358,6 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)\n \topts.src_index = the_repository->index;\n \topts.dst_index = the_repository->index;\n \topts.merge = 1;\n-\topts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0;\n-\topts.update = update;\n-\tif (update)\n-\t\topts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n \topts.fn = oneway_merge;\n \n \tif (unpack_trees(nr_trees, t, &opts))\n@@ -738,7 +734,7 @@ static enum stash_apply_result do_apply_stash(const char *prefix,\n \t}\n \n \tif (has_index) {\n-\t\tif (reset_tree(&index_tree, 0, 0))\n+\t\tif (reset_tree(&index_tree))\n \t\t\tret = -1;\n \t} else {\n \t\tunstage_changes_unless_new(&c_tree);\n-- \n2.56.0.134.g299a3c16181\n\n\n"},{"id":"557624","messageId":"xmqqse1fq72m.fsf@gitster.g","threadId":"66466","inReplyTo":"cover.1792767794.git.phillip.wood@dunelm.org.uk","subject":"Re: [PATCH v4 0/7] stash creation: avoid unecessary index loads and refreshes","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-05T21:52:49Z","receivedAt":"2026-11-05T21:52:49Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> Changes since V3:\n>  - Avoid implict cast of enum create_result to int in create_stash()\n>  - Fixed enum initializers\n>  - Use switch() rather than if() when checking enum create_result\n>    return values\n\nThese changes look very good.\n\n> 5:  de5e0c196a9 = 5:  b62d3d080a8 stash create: rework temporary index creation\n> 6:  61e4ce22a0f = 6:  8c54c118307 stash create: remove unnecessary index load and refresh\n> 7:  a486a625c95 = 7:  f61ebbf9a3e stash: simplify reset_tree() function signature\n\nI'll have another look at latter half of the series which I didn't\ncomment on in my earlier review separately.\n\nThanks.\n\n"},{"id":"557625","messageId":"xmqqmrrnq6xh.fsf@gitster.g","threadId":"66466","inReplyTo":"b62d3d080a8ff0d9e14741a886aa45d70019bd64.1792767795.git.phillip.wood@dunelm.org.uk","subject":"Re: [PATCH v4 5/7] stash create: rework temporary index creation","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-05T21:55:54Z","receivedAt":"2026-11-05T21:55:54Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> Use create_index_from_tree() instead of reset_tree() so that we\n> can create temporary index file without overwriting the in-memory\n> index or relying on set_alternate_index_output().\n\nThis is very nice simplification.\n\n> As create_index_from_tree() does not\n> modify the in-memory index it is clearly safe to remove the call to\n> repo_read_index_preload() which saves one lstat() call per changed\n> path.\n\n;-)\n\n> It would probably be safe to remove that call without moving away\n> from reset_tree() but it is much easier to reason about now that the\n> in-memory index is not modified when we create the temporary index file.\n\nIndeed.\n\n> @@ -1458,22 +1458,15 @@ static int stash_working_tree(struct stash_info *info, const struct pathspec *ps\n>  \trepo_init_revisions(the_repository, &rev, NULL);\n>  \tcopy_pathspec(&rev.prune_data, ps);\n>  \n> -\tset_alternate_index_output(stash_index_path.buf);\n> -\tif (reset_tree(&info->i_tree, 0, 0)) {\n> +\tif (create_index_from_tree(&info->i_tree, stash_index_path.buf, false)) {\n>  \t\tret = -1;\n>  \t\tgoto done;\n>  \t}\n> -\tset_alternate_index_output(NULL);\n>  \n>  \trev.diffopt.output_format = DIFF_FORMAT_CALLBACK;\n>  \trev.diffopt.format_callback = add_diff_to_buf;\n>  \trev.diffopt.format_callback_data = &diff_output;\n>  \n> -\tif (repo_read_index_preload(the_repository, &rev.diffopt.pathspec, 0) < 0) {\n> -\t\tret = -1;\n> -\t\tgoto done;\n> -\t}\n> -\n\nYup, removal of these is indeed very nice.\n\n>  \tadd_pending_object(&rev, parse_object(the_repository, &info->b_commit),\n>  \t\t\t   \"\");\n>  \trun_diff_index(&rev, 0);\n\n"},{"id":"557641","messageId":"xmqqse1eoh3k.fsf@gitster.g","threadId":"66466","inReplyTo":"8c54c118307cce6553929f5ff7242f85d0bbc733.1792767795.git.phillip.wood@dunelm.org.uk","subject":"Re: [PATCH v4 6/7] stash create: remove unnecessary index load and refresh","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-06T01:59:11Z","receivedAt":"2026-11-06T01:59:11Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> From: Phillip Wood <phillip.wood@dunelm.org.uk>\n>\n> create_index_from_tree() is called either directly or indirectly\n> from do_create_stash() after it has loaded and refreshed the index,\n> so we do not need to refresh it again here. This saves three lstat()\n> calls per changed path.\n>\n> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n> ---\n>  builtin/stash.c | 3 ---\n>  1 file changed, 3 deletions(-)\n>\n> diff --git a/builtin/stash.c b/builtin/stash.c\n> index e17f75641a4..f97cd4771a5 100644\n> --- a/builtin/stash.c\n> +++ b/builtin/stash.c\n> @@ -384,9 +384,6 @@ static int create_index_from_tree(const struct object_id *tree_id,\n>  \tstruct index_state dst_istate = INDEX_STATE_INIT(the_repository);\n>  \tstruct lock_file lock_file = LOCK_INIT;\n>  \n> -\trepo_read_index_preload(the_repository, NULL, 0);\n> -\trefresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL);\n\nMakes sense.  As a file-scope static function, we do not have to\nworry about calling context to change without we noticing as much as\na public function.  And I agree that with the current calling sequence,\nthis refresh is not needed.\n\n\n\n"},{"id":"557642","messageId":"xmqqo6c2oh3j.fsf@gitster.g","threadId":"66466","inReplyTo":"f61ebbf9a3e1196dea36e5fbcf4294c6addad222.1792767795.git.phillip.wood@dunelm.org.uk","subject":"Re: [PATCH v4 7/7] stash: simplify reset_tree() function signature","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-06T01:59:12Z","receivedAt":"2026-11-06T01:59:12Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> From: Phillip Wood <phillip.wood@dunelm.org.uk>\n>\n> There is only one caller and it does not want to update the worktree or\n> reset the index so remove those unused parameters.\n>\n> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>\n> ---\n>  builtin/stash.c | 8 ++------\n>  1 file changed, 2 insertions(+), 6 deletions(-)\n\nAh, an obvious simplification.  Very nice.\n\n>\n> diff --git a/builtin/stash.c b/builtin/stash.c\n> index f97cd4771a5..f0e3053ab49 100644\n> --- a/builtin/stash.c\n> +++ b/builtin/stash.c\n> @@ -332,7 +332,7 @@ static int clear_stash(int argc, const char **argv, const char *prefix,\n>  \treturn do_clear_stash();\n>  }\n>  \n> -static int reset_tree(struct object_id *i_tree, int update, int reset)\n> +static int reset_tree(struct object_id *i_tree)\n>  {\n>  \tint nr_trees = 1;\n>  \tstruct unpack_trees_options opts;\n> @@ -358,10 +358,6 @@ static int reset_tree(struct object_id *i_tree, int update, int reset)\n>  \topts.src_index = the_repository->index;\n>  \topts.dst_index = the_repository->index;\n>  \topts.merge = 1;\n> -\topts.reset = reset ? UNPACK_RESET_PROTECT_UNTRACKED : 0;\n> -\topts.update = update;\n> -\tif (update)\n> -\t\topts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n>  \topts.fn = oneway_merge;\n>  \n>  \tif (unpack_trees(nr_trees, t, &opts))\n> @@ -738,7 +734,7 @@ static enum stash_apply_result do_apply_stash(const char *prefix,\n>  \t}\n>  \n>  \tif (has_index) {\n> -\t\tif (reset_tree(&index_tree, 0, 0))\n> +\t\tif (reset_tree(&index_tree))\n>  \t\t\tret = -1;\n>  \t} else {\n>  \t\tunstage_changes_unless_new(&c_tree);\n\n"}]}