summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2025-09-29 11:40:35 -0700
committerJunio C Hamano <gitster@pobox.com>2025-09-29 11:40:35 -0700
commita5d4779e6ed1bebf56ab90bd264e437ac2232b98 (patch)
treea2f8b05345648e4d8ba8e4109d743bfb2ad44ce4
parentcff1e3c870705cce828ab8b7d59ce4c2a785b9b1 (diff)
parent9842c0c7492d2858d64ef81128f7b1f0b38e326b (diff)
Merge branch 'dk/stash-apply-index'
The stash.index configuration variable can be set to make "git stash pop/apply" pretend that it was invoked with "--index". * dk/stash-apply-index: stash: honor stash.index in apply, pop modes stash: refactor private config globals t3905: remove unneeded blank line t3903: reduce dependencies on previous tests
-rw-r--r--Documentation/config/stash.adoc5
-rw-r--r--builtin/stash.c17
-rwxr-xr-xt/t3903-stash.sh40
-rwxr-xr-xt/t3905-stash-include-untracked.sh1
4 files changed, 56 insertions, 7 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 bea64d04a0..948eba06fb 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -146,6 +146,11 @@ static const char * const git_stash_import_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;
+static int use_index;
+
/*
* w_commit is set to the commit containing the working tree
* b_commit is set to the base commit
@@ -717,7 +722,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")),
@@ -815,7 +820,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[] = {
@@ -905,10 +910,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)
{
@@ -924,6 +925,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 930c31e547..70879941c2 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -902,6 +902,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 &&
@@ -1356,6 +1357,7 @@ test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
git reset &&
+ mkdir -p subdir &&
>subdir/untracked &&
>subdir/tracked1 &&
>subdir/tracked2 &&
@@ -1372,6 +1374,7 @@ test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact'
test_expect_success 'stash -- <subdir> works with binary files' '
git reset &&
+ mkdir -p subdir &&
>subdir/untracked &&
>subdir/tracked &&
cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
@@ -1750,4 +1753,41 @@ test_expect_success 'controlled error return on unrecognized option' '
grep -e "^usage: git stash show" usage
'
+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
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' '