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

[PATCH 4/9] stash: restore the index by default when breaking changes are enabled

From
D. Ben Knoble <ben.knoble+github@gmail.com>
Date
May 10, 2025, 18:33 UTC
Message-ID
<20250510183358.36806-5-ben.knoble+github@gmail.com>
In-Reply-To
<20250510183358.36806-1-ben.knoble+github@gmail.com>

This footgun is described in the previous commit to Documentation/BreakingChanges.adoc; without --index, stash apply and stash pop confusingly drop stashed index changes.

When compiling with breaking changes, instead restore the index unless asked not to. Adjust both argument parsing and handling, as well as help synopses. Don't forget to adjust the error message when application fails to point to --no-index instead.

Signed-off-by: D. Ben Knoble <ben.knoble+github@gmail.com>
---
Notes:
    Tests will follow in separate patches, as some changes are rather complicated.
 Documentation/git-stash.adoc |  6 ++++++
 builtin/stash.c              | 38 ++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
diff --git a/Documentation/git-stash.adoc b/Documentation/git-stash.adoc
index 1a5177f498..3b01f494a3 100644
--- a/Documentation/git-stash.adoc
+++ b/Documentation/git-stash.adoc
@@ -11,8 +11,14 @@ SYNOPSIS
 'git stash' list [<log-options>]
 'git stash' show [-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>]
 'git stash' drop [-q | --quiet] [<stash>]
+ifndef::with-breaking-changes[]
 'git stash' pop [--index] [-q | --quiet] [<stash>]
 'git stash' apply [--index] [-q | --quiet] [<stash>]
+endif::with-breaking-changes[]
+ifdef::with-breaking-changes[]
+'git stash' pop [--no-index] [-q | --quiet] [<stash>]
+'git stash' apply [--no-index] [-q | --quiet] [<stash>]
+endif::with-breaking-changes[]
 'git stash' branch <branchname> [<stash>]
 'git stash' [push [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]
 	     [-u | --include-untracked] [-a | --all] [(-m | --message) <message>]
diff --git a/builtin/stash.c b/builtin/stash.c
index cfbd92852a..4ffa586d07 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -38,10 +38,17 @@
 	N_("git stash show [-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>]")
 #define BUILTIN_STASH_DROP_USAGE \
 	N_("git stash drop [-q | --quiet] [<stash>]")
+#ifdef WITH_BREAKING_CHANGES
+#define BUILTIN_STASH_POP_USAGE \
+	N_("git stash pop [--no-index] [-q | --quiet] [<stash>]")
+#define BUILTIN_STASH_APPLY_USAGE \
+	N_("git stash apply [--no-index] [-q | --quiet] [<stash>]")
+#else
 #define BUILTIN_STASH_POP_USAGE \
 	N_("git stash pop [--index] [-q | --quiet] [<stash>]")
 #define BUILTIN_STASH_APPLY_USAGE \
 	N_("git stash apply [--index] [-q | --quiet] [<stash>]")
+#endif /* WITH_BREAKING_CHANGES */
 #define BUILTIN_STASH_BRANCH_USAGE \
 	N_("git stash branch <branchname> [<stash>]")
 #define BUILTIN_STASH_STORE_USAGE \
@@ -562,8 +569,13 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
 			ret = apply_cached(&out);
 			strbuf_release(&out);
 			if (ret)
+#ifdef WITH_BREAKING_CHANGES
+				return error(_("conflicts in index. "
+					       "Try with --no-index."));
+#else
 				return error(_("conflicts in index. "
 					       "Try without --index."));
+#endif /* WITH_BREAKING_CHANGES */
 
 			discard_index(the_repository->index);
 			repo_read_index(the_repository);
@@ -658,12 +670,21 @@ static int apply_stash(int argc, const char **argv, const char *prefix,
 {
 	int ret = -1;
 	int quiet = 0;
+#ifdef WITH_BREAKING_CHANGES
+	int no_index = 0;
+#else
 	int index = 0;
+#endif /* WITH_BREAKING_CHANGES */
 	struct stash_info info = STASH_INFO_INIT;
 	struct option options[] = {
 		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
+#ifdef WITH_BREAKING_CHANGES
+		OPT_BOOL(0, "no-index", &no_index,
+			 N_("do not attempt to recreate the index")),
+#else
 		OPT_BOOL(0, "index", &index,
 			 N_("attempt to recreate the index")),
+#endif /* WITH_BREAKING_CHANGES */
 		OPT_END()
 	};
 
@@ -673,7 +694,11 @@ static int apply_stash(int argc, const char **argv, const char *prefix,
 	if (get_stash_info(&info, argc, argv))
 		goto cleanup;
 
+#ifdef WITH_BREAKING_CHANGES
+	ret = do_apply_stash(prefix, &info, !no_index, quiet);
+#else
 	ret = do_apply_stash(prefix, &info, index, quiet);
+#endif /* WITH_BREAKING_CHANGES */
 cleanup:
 	free_stash_info(&info);
 	return ret;
@@ -755,13 +780,22 @@ static int pop_stash(int argc, const char **argv, const char *prefix,
 		     struct repository *repo UNUSED)
 {
 	int ret = -1;
+#ifdef WITH_BREAKING_CHANGES
+	int no_index = 0;
+#else
 	int index = 0;
+#endif /* WITH_BREAKING_CHANGES */
 	int quiet = 0;
 	struct stash_info info = STASH_INFO_INIT;
 	struct option options[] = {
 		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
+#ifdef WITH_BREAKING_CHANGES
+		OPT_BOOL(0, "no-index", &no_index,
+			 N_("do not attempt to recreate the index")),
+#else
 		OPT_BOOL(0, "index", &index,
 			 N_("attempt to recreate the index")),
+#endif /* WITH_BREAKING_CHANGES */
 		OPT_END()
 	};
 
@@ -771,7 +805,11 @@ static int pop_stash(int argc, const char **argv, const char *prefix,
 	if (get_stash_info_assert(&info, argc, argv))
 		goto cleanup;
 
+#ifdef WITH_BREAKING_CHANGES
+	if ((ret = do_apply_stash(prefix, &info, !no_index, quiet)))
+#else
 	if ((ret = do_apply_stash(prefix, &info, index, quiet)))
+#endif /* WITH_BREAKING_CHANGES */
 		printf_ln(_("The stash entry is kept in case "
 			    "you need it again."));
 	else
-- 
2.48.1
Previous: D. Ben KnobleNext: D. Ben Knoble
Message 5 of 38 in “make stash apply with --index by default”
  1. 0/9 make stash apply with --index by defaultD. Ben Knoble, May 10, 2025
  2. 1/9 t3903: reduce dependencies on previous testsD. Ben Knoble, May 10, 2025
  3. 2/9 t3905: remove unneeded blank lineD. Ben Knoble, May 10, 2025
  4. 3/9 BreakingChanges: announce stash {apply,pop} will imply --indexD. Ben Knoble, May 10, 2025
  5. 4/9 stash: restore the index by default when breaking changes are enabledD. Ben Knoble, May 10, 2025
  6. 5/9 t0450: mark stash documentation as a known discrepancyD. Ben Knoble, May 10, 2025
  7. 6/9 t3903: adjust stash test to account for --[no-]index with breaking changesD. Ben Knoble, May 10, 2025
  8. 7/9 t3904: adjust stash -p test to account for index states with breaking changesD. Ben Knoble, May 10, 2025
  9. 8/9 t3905: adjust stash -u tests for breaking changesD. Ben Knoble, May 10, 2025
  10. 9/9 t3906: adjust stash submodule tests to account for breaking changesD. Ben Knoble, May 10, 2025
  11. Junio C HamanoMay 12, 2025
  12. D. Ben KnobleMay 20, 2025
  13. D. Ben KnobleMay 20, 2025
  14. 0/4 Teach git-stash to use --index from configD. Ben Knoble, Sep 16, 2025
  15. 1/4 t3903: reduce dependencies on previous testsD. Ben Knoble, Sep 16, 2025
  16. 2/4 t3905: remove unneeded blank lineD. Ben Knoble, Sep 16, 2025
  17. 3/4 stash: refactor private config globalsD. Ben Knoble, Sep 16, 2025
  18. 4/4 stash: honor stash.index in apply, pop modesD. Ben Knoble, Sep 16, 2025
  19. Phillip WoodSep 16, 2025
  20. D. Ben KnobleSep 16, 2025
  21. Phillip WoodSep 16, 2025
  22. D. Ben KnobleSep 16, 2025
  23. Junio C HamanoSep 16, 2025
  24. 0/4 Teach git-stash to use --index from configD. Ben Knoble, Sep 22, 2025
  25. 1/4 t3903: reduce dependencies on previous testsD. Ben Knoble, Sep 22, 2025
  26. 2/4 t3905: remove unneeded blank lineD. Ben Knoble, Sep 22, 2025
  27. 3/4 stash: refactor private config globalsD. Ben Knoble, Sep 22, 2025
  28. 4/4 stash: honor stash.index in apply, pop modesD. Ben Knoble, Sep 22, 2025
  29. Phillip WoodSep 22, 2025
  30. D. Ben KnobleSep 24, 2025
  31. Phillip WoodSep 29, 2025
  32. doc: explain the impact of stash.index on --autostash optionsD. Ben Knoble, Oct 6, 2025
  33. Kristoffer HaugsbakkOct 9, 2025
  34. D. Ben KnobleOct 11, 2025
  35. Junio C HamanoOct 11, 2025
  36. Ben KnobleOct 12, 2025
  37. Kristoffer HaugsbakkOct 13, 2025
  38. D. Ben KnobleOct 14, 2025

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

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