From 80bc042b4711b00dd35f24fd0c6ee450f41f3c2c Mon Sep 17 00:00:00 2001 From: "D. Ben Knoble" Date: Sun, 21 Sep 2025 21:39:03 -0400 Subject: [PATCH 1/4] t3903: reduce dependencies on previous tests Skipping previous tests to work through only failing tests with arguments like --run=4,122- causes some tests to fail because subdir doesn't exist yet (it is created by a previous test; typically "unstashing in a subdirectory"). Create it on demand for tests that need it, but don't fail (-p) if the directory already exists. Signed-off-by: D. Ben Knoble Signed-off-by: Junio C Hamano --- t/t3903-stash.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index 74666ff3e4..b8936a653b 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -895,6 +895,7 @@ test_expect_success 'branch: should not drop the stash if the apply fails' ' test_expect_success 'apply: show same status as git status (relative to ./)' ' git stash clear && + mkdir -p subdir && echo 1 >subdir/subfile1 && echo 2 >subdir/subfile2 && git add subdir/subfile1 && @@ -1327,6 +1328,7 @@ test_expect_success 'stash -k -- leaves unstaged files intact' ' test_expect_success 'stash -- leaves untracked files in subdir intact' ' git reset && + mkdir -p subdir && >subdir/untracked && >subdir/tracked1 && >subdir/tracked2 && @@ -1343,6 +1345,7 @@ test_expect_success 'stash -- leaves untracked files in subdir intact' test_expect_success 'stash -- works with binary files' ' git reset && + mkdir -p subdir && >subdir/untracked && >subdir/tracked && cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary && From 4b2de837ee2daa80756d8e9c3f8d9cb6bf58966f Mon Sep 17 00:00:00 2001 From: "D. Ben Knoble" Date: Sun, 21 Sep 2025 21:39:04 -0400 Subject: [PATCH 2/4] t3905: remove unneeded blank line This is leftover from 787513027a (stash: Add --include-untracked option to stash and remove all untracked files, 2011-06-24) when it was converted in bbaa45c3aa (t3905: move all commands into test cases, 2021-02-08). Signed-off-by: D. Ben Knoble Signed-off-by: Junio C Hamano --- t/t3905-stash-include-untracked.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/t/t3905-stash-include-untracked.sh b/t/t3905-stash-include-untracked.sh index 1289ae3e07..7704709054 100755 --- a/t/t3905-stash-include-untracked.sh +++ b/t/t3905-stash-include-untracked.sh @@ -87,7 +87,6 @@ test_expect_success 'stash save --patch --all fails' ' test_expect_success 'clean up untracked/untracked file to prepare for next tests' ' git clean --force --quiet - ' test_expect_success 'stash pop after save --include-untracked leaves files untracked again' ' From 88b5b8d886b54f5020040a3b3a534f7c102a6293 Mon Sep 17 00:00:00 2001 From: "D. Ben Knoble" Date: Sun, 21 Sep 2025 21:39:05 -0400 Subject: [PATCH 3/4] stash: refactor private config globals A subsequent commit will access a new config variable in the stash subcommand implementations, which requires the variables to be declared before the relevant functions. Prep with a pure refactoring change to consolidate config-related globals with the rest of the globals. Best-viewed-with: --color-moved Signed-off-by: D. Ben Knoble Signed-off-by: Junio C Hamano --- builtin/stash.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/builtin/stash.c b/builtin/stash.c index cfbd92852a..d9b478d1d1 100644 --- a/builtin/stash.c +++ b/builtin/stash.c @@ -127,6 +127,10 @@ static const char * const git_stash_save_usage[] = { static const char ref_stash[] = "refs/stash"; static struct strbuf stash_index_path = STRBUF_INIT; +static int show_stat = 1; +static int show_patch; +static int show_include_untracked; + /* * w_commit is set to the commit containing the working tree * b_commit is set to the base commit @@ -845,10 +849,6 @@ static int list_stash(int argc, const char **argv, const char *prefix, return run_command(&cp); } -static int show_stat = 1; -static int show_patch; -static int show_include_untracked; - static int git_stash_config(const char *var, const char *value, const struct config_context *ctx, void *cb) { From 9842c0c7492d2858d64ef81128f7b1f0b38e326b Mon Sep 17 00:00:00 2001 From: "D. Ben Knoble" Date: Sun, 21 Sep 2025 21:39:06 -0400 Subject: [PATCH 4/4] stash: honor stash.index in apply, pop modes With stash.index=true, git-stash(1) command now tries to reinstate the index by default in the "apply" and "pop" modes. Not doing so creates a common trap [1], [2]: "git stash apply" is not the reverse of "git stash push" because carefully staged indices are lost and have to be manually recreated. OTOH, this mode is not always desirable and may create more conflicts when applying stashes. As usual, "--no-index" will disable this behavior if you set "stash.index". [1]: https://lore.kernel.org/git/CAPx1GvcxyDDQmCssMjEnt6JoV6qPc5ZUpgPLX3mpUC_4PNYA1w@mail.gmail.com/ [2]: https://lore.kernel.org/git/c5a811ac-8cd3-c389-ac6d-29020a648c87@gmail.com/ Signed-off-by: D. Ben Knoble Signed-off-by: Junio C Hamano --- Documentation/config/stash.adoc | 5 +++++ builtin/stash.c | 9 ++++++-- t/t3903-stash.sh | 37 +++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/Documentation/config/stash.adoc b/Documentation/config/stash.adoc index ec1edaeba6..e556105a15 100644 --- a/Documentation/config/stash.adoc +++ b/Documentation/config/stash.adoc @@ -1,3 +1,8 @@ +stash.index:: + If this is set to true, `git stash apply` and `git stash pop` will + behave as if `--index` was supplied. Defaults to false. See the + descriptions in linkgit:git-stash[1]. + stash.showIncludeUntracked:: If this is set to true, the `git stash show` command will show the untracked files of a stash entry. Defaults to false. See diff --git a/builtin/stash.c b/builtin/stash.c index d9b478d1d1..8a0eef3c70 100644 --- a/builtin/stash.c +++ b/builtin/stash.c @@ -130,6 +130,7 @@ static struct strbuf stash_index_path = STRBUF_INIT; static int show_stat = 1; static int show_patch; static int show_include_untracked; +static int use_index; /* * w_commit is set to the commit containing the working tree @@ -662,7 +663,7 @@ static int apply_stash(int argc, const char **argv, const char *prefix, { int ret = -1; int quiet = 0; - int index = 0; + int index = use_index; struct stash_info info = STASH_INFO_INIT; struct option options[] = { OPT__QUIET(&quiet, N_("be quiet, only report errors")), @@ -759,7 +760,7 @@ static int pop_stash(int argc, const char **argv, const char *prefix, struct repository *repo UNUSED) { int ret = -1; - int index = 0; + int index = use_index; int quiet = 0; struct stash_info info = STASH_INFO_INIT; struct option options[] = { @@ -864,6 +865,10 @@ static int git_stash_config(const char *var, const char *value, show_include_untracked = git_config_bool(var, value); return 0; } + if (!strcmp(var, "stash.index")) { + use_index = git_config_bool(var, value); + return 0; + } return git_diff_basic_config(var, value, ctx, cb); } diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index b8936a653b..d6127173b1 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -1595,4 +1595,41 @@ test_expect_success 'stash apply reports a locked index' ' ) ' +test_expect_success 'stash.index=true implies --index' ' + # setup for a few related tests + test_commit file base && + echo index >file && + git add file && + echo working >file && + git stash && + + test_when_finished "git reset --hard" && + git -c stash.index=true stash apply && + echo index >expect && + git show :0:file >actual && + test_cmp expect actual && + echo working >expect && + test_cmp expect file +' + +test_expect_success 'stash.index=true overridden by --no-index' ' + test_when_finished "git reset --hard" && + git -c stash.index=true stash apply --no-index && + echo base >expect && + git show :0:file >actual && + test_cmp expect actual && + echo working >expect && + test_cmp expect file +' + +test_expect_success 'stash.index=false overridden by --index' ' + test_when_finished "git reset --hard" && + git -c stash.index=false stash apply --index && + echo index >expect && + git show :0:file >actual && + test_cmp expect actual && + echo working >expect && + test_cmp expect file +' + test_done