Skip to content

Commit a5d4779

Browse files
committed
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
2 parents cff1e3c + 9842c0c commit a5d4779

File tree

4 files changed

+56
-7
lines changed

4 files changed

+56
-7
lines changed

Documentation/config/stash.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
stash.index::
2+
If this is set to true, `git stash apply` and `git stash pop` will
3+
behave as if `--index` was supplied. Defaults to false. See the
4+
descriptions in linkgit:git-stash[1].
5+
16
stash.showIncludeUntracked::
27
If this is set to true, the `git stash show` command will show
38
the untracked files of a stash entry. Defaults to false. See

builtin/stash.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ static const char * const git_stash_import_usage[] = {
146146
static const char ref_stash[] = "refs/stash";
147147
static struct strbuf stash_index_path = STRBUF_INIT;
148148

149+
static int show_stat = 1;
150+
static int show_patch;
151+
static int show_include_untracked;
152+
static int use_index;
153+
149154
/*
150155
* w_commit is set to the commit containing the working tree
151156
* b_commit is set to the base commit
@@ -717,7 +722,7 @@ static int apply_stash(int argc, const char **argv, const char *prefix,
717722
{
718723
int ret = -1;
719724
int quiet = 0;
720-
int index = 0;
725+
int index = use_index;
721726
struct stash_info info = STASH_INFO_INIT;
722727
struct option options[] = {
723728
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,
815820
struct repository *repo UNUSED)
816821
{
817822
int ret = -1;
818-
int index = 0;
823+
int index = use_index;
819824
int quiet = 0;
820825
struct stash_info info = STASH_INFO_INIT;
821826
struct option options[] = {
@@ -905,10 +910,6 @@ static int list_stash(int argc, const char **argv, const char *prefix,
905910
return run_command(&cp);
906911
}
907912

908-
static int show_stat = 1;
909-
static int show_patch;
910-
static int show_include_untracked;
911-
912913
static int git_stash_config(const char *var, const char *value,
913914
const struct config_context *ctx, void *cb)
914915
{
@@ -924,6 +925,10 @@ static int git_stash_config(const char *var, const char *value,
924925
show_include_untracked = git_config_bool(var, value);
925926
return 0;
926927
}
928+
if (!strcmp(var, "stash.index")) {
929+
use_index = git_config_bool(var, value);
930+
return 0;
931+
}
927932
return git_diff_basic_config(var, value, ctx, cb);
928933
}
929934

t/t3903-stash.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,7 @@ test_expect_success 'branch: should not drop the stash if the apply fails' '
902902

903903
test_expect_success 'apply: show same status as git status (relative to ./)' '
904904
git stash clear &&
905+
mkdir -p subdir &&
905906
echo 1 >subdir/subfile1 &&
906907
echo 2 >subdir/subfile2 &&
907908
git add subdir/subfile1 &&
@@ -1356,6 +1357,7 @@ test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
13561357

13571358
test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
13581359
git reset &&
1360+
mkdir -p subdir &&
13591361
>subdir/untracked &&
13601362
>subdir/tracked1 &&
13611363
>subdir/tracked2 &&
@@ -1372,6 +1374,7 @@ test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact'
13721374

13731375
test_expect_success 'stash -- <subdir> works with binary files' '
13741376
git reset &&
1377+
mkdir -p subdir &&
13751378
>subdir/untracked &&
13761379
>subdir/tracked &&
13771380
cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
@@ -1750,4 +1753,41 @@ test_expect_success 'controlled error return on unrecognized option' '
17501753
grep -e "^usage: git stash show" usage
17511754
'
17521755

1756+
test_expect_success 'stash.index=true implies --index' '
1757+
# setup for a few related tests
1758+
test_commit file base &&
1759+
echo index >file &&
1760+
git add file &&
1761+
echo working >file &&
1762+
git stash &&
1763+
1764+
test_when_finished "git reset --hard" &&
1765+
git -c stash.index=true stash apply &&
1766+
echo index >expect &&
1767+
git show :0:file >actual &&
1768+
test_cmp expect actual &&
1769+
echo working >expect &&
1770+
test_cmp expect file
1771+
'
1772+
1773+
test_expect_success 'stash.index=true overridden by --no-index' '
1774+
test_when_finished "git reset --hard" &&
1775+
git -c stash.index=true stash apply --no-index &&
1776+
echo base >expect &&
1777+
git show :0:file >actual &&
1778+
test_cmp expect actual &&
1779+
echo working >expect &&
1780+
test_cmp expect file
1781+
'
1782+
1783+
test_expect_success 'stash.index=false overridden by --index' '
1784+
test_when_finished "git reset --hard" &&
1785+
git -c stash.index=false stash apply --index &&
1786+
echo index >expect &&
1787+
git show :0:file >actual &&
1788+
test_cmp expect actual &&
1789+
echo working >expect &&
1790+
test_cmp expect file
1791+
'
1792+
17531793
test_done

t/t3905-stash-include-untracked.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ test_expect_success 'stash save --patch --all fails' '
8787

8888
test_expect_success 'clean up untracked/untracked file to prepare for next tests' '
8989
git clean --force --quiet
90-
9190
'
9291

9392
test_expect_success 'stash pop after save --include-untracked leaves files untracked again' '

0 commit comments

Comments
 (0)