Skip to content

Commit 9842c0c

Browse files
benknoblegitster
authored andcommitted
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/[email protected]/ Signed-off-by: D. Ben Knoble <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 88b5b8d commit 9842c0c

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ static struct strbuf stash_index_path = STRBUF_INIT;
130130
static int show_stat = 1;
131131
static int show_patch;
132132
static int show_include_untracked;
133+
static int use_index;
133134

134135
/*
135136
* 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,
662663
{
663664
int ret = -1;
664665
int quiet = 0;
665-
int index = 0;
666+
int index = use_index;
666667
struct stash_info info = STASH_INFO_INIT;
667668
struct option options[] = {
668669
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,
759760
struct repository *repo UNUSED)
760761
{
761762
int ret = -1;
762-
int index = 0;
763+
int index = use_index;
763764
int quiet = 0;
764765
struct stash_info info = STASH_INFO_INIT;
765766
struct option options[] = {
@@ -864,6 +865,10 @@ static int git_stash_config(const char *var, const char *value,
864865
show_include_untracked = git_config_bool(var, value);
865866
return 0;
866867
}
868+
if (!strcmp(var, "stash.index")) {
869+
use_index = git_config_bool(var, value);
870+
return 0;
871+
}
867872
return git_diff_basic_config(var, value, ctx, cb);
868873
}
869874

t/t3903-stash.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,4 +1595,41 @@ test_expect_success 'stash apply reports a locked index' '
15951595
)
15961596
'
15971597

1598+
test_expect_success 'stash.index=true implies --index' '
1599+
# setup for a few related tests
1600+
test_commit file base &&
1601+
echo index >file &&
1602+
git add file &&
1603+
echo working >file &&
1604+
git stash &&
1605+
1606+
test_when_finished "git reset --hard" &&
1607+
git -c stash.index=true stash apply &&
1608+
echo index >expect &&
1609+
git show :0:file >actual &&
1610+
test_cmp expect actual &&
1611+
echo working >expect &&
1612+
test_cmp expect file
1613+
'
1614+
1615+
test_expect_success 'stash.index=true overridden by --no-index' '
1616+
test_when_finished "git reset --hard" &&
1617+
git -c stash.index=true stash apply --no-index &&
1618+
echo base >expect &&
1619+
git show :0:file >actual &&
1620+
test_cmp expect actual &&
1621+
echo working >expect &&
1622+
test_cmp expect file
1623+
'
1624+
1625+
test_expect_success 'stash.index=false overridden by --index' '
1626+
test_when_finished "git reset --hard" &&
1627+
git -c stash.index=false stash apply --index &&
1628+
echo index >expect &&
1629+
git show :0:file >actual &&
1630+
test_cmp expect actual &&
1631+
echo working >expect &&
1632+
test_cmp expect file
1633+
'
1634+
15981635
test_done

0 commit comments

Comments
 (0)