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

[PATCH] Adds 'stash.index' configuration option

From
David Pisoni <dpisoni@gmail.com>
Date
May 11, 2011, 22:57 UTC
Message-ID
<D80C1130-8DE6-457E-B203-FCF25B8ED72C@gmail.com>

Setting 'stash.index' config option changes 'git-stash pop|apply' to behave as if '--index' switch is always supplied. 'git-stash pop|apply' provides a --no-index switch to circumvent config default.

Signed-off-by: David Pisoni <dpisoni@gmail.com>
---
Documentation/config.txt    |    5 +++++
Documentation/git-stash.txt |   10 +++++++---
git-stash.sh                |    7 ++++++-
t/t3903-stash.sh            |   28 ++++++++++++++++++++++++++++
4 files changed, 46 insertions(+), 4 deletions(-)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index 285c7f7..d794c40 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -1746,6 +1746,11 @@ showbranch.default::
	The default set of branches for linkgit:git-show-branch[1].
	See linkgit:git-show-branch[1].

+stash.index::
+    A boolean to make linkgit:git-stash[1] default to the behavior of  
--index
+	when applying a stash to the working copy.  Can be circumvented by  
using
+	--no-index switch to linkgit:git-stash[1].  Defaults to false.
+
status.relativePaths::
	By default, linkgit:git-status[1] shows paths relative to the
	current directory. Setting this variable to `false` shows paths
diff --git a/Documentation/git-stash.txt b/Documentation/git-stash.txt
index 15f051f..de086ee 100644
--- a/Documentation/git-stash.txt
+++ b/Documentation/git-stash.txt
@@ -11,7 +11,7 @@ SYNOPSIS
'git stash' list [<options>]
'git stash' show [<stash>]
'git stash' drop [-q|--quiet] [<stash>]
-'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]
+'git stash' ( pop | apply ) [--[no-]index] [-q|--quiet] [<stash>]
'git stash' branch <branchname> [<stash>]
'git stash' [save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]  
[<message>]]
'git stash' clear
@@ -89,7 +89,7 @@ show [<stash>]::
	it will accept any format known to 'git diff' (e.g., `git stash show
	-p stash@\{1}` to view the second most recent stash in patch form).

-pop [--index] [-q|--quiet] [<stash>]::
+pop [--[no-]index] [-q|--quiet] [<stash>]::

	Remove a single stashed state from the stash list and apply it
	on top of the current working tree state, i.e., do the inverse
@@ -105,10 +105,14 @@ tree's changes, but also the index's ones.  
However, this can fail, when you
have conflicts (which are stored in the index, where you therefore can  
no
longer apply the changes as they were originally).
+
+If the configuration option `stash.index` is set `pop` will behave as  
if the
+`--index` option is always in use, unless explicitly overridden with
+`--no-index`.
++
When no `<stash>` is given, `stash@\{0}` is assumed, otherwise  
`<stash>` must
be a reference of the form `stash@\{<revision>}`.

-apply [--index] [-q|--quiet] [<stash>]::
+apply [--[no-]index] [-q|--quiet] [<stash>]::

	Like `pop`, but do not remove the state from the stash list. Unlike  
`pop`,
	`<stash>` may be any commit that looks like a commit created by
diff --git a/git-stash.sh b/git-stash.sh
index 0a94036..7711bf6 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -31,6 +31,8 @@ else
        reset_color=
fi

+CONFIG_INDEX="$(git config stash.index)"
+
no_changes () {
	git diff-index --quiet --cached HEAD --ignore-submodules -- &&
	git diff-files --quiet --ignore-submodules
@@ -256,7 +258,7 @@ parse_flags_and_rev()

	IS_STASH_LIKE=
	IS_STASH_REF=
-	INDEX_OPTION=
+	INDEX_OPTION=$CONFIG_INDEX
	s=
	w_commit=
	b_commit=
@@ -277,6 +279,9 @@ parse_flags_and_rev()
			--index)
				INDEX_OPTION=--index
			;;
+			--no-index)
+				unset INDEX_OPTION
+			;;
			-*)
				FLAGS="${FLAGS}${FLAGS:+ }$opt"
			;;
diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index 5c72540..4999682 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -84,6 +84,34 @@ test_expect_success 'apply stashed changes  
(including index)' '
	test 1 = $(git show HEAD:file)
'

+test_expect_success 'apply stashed changes (including index) with  
index config set' '
+    git config stash.index yes &&
+	git reset --hard HEAD^ &&
+	echo 7 > other-file &&
+	git add other-file &&
+	test_tick &&
+	git commit -m other-file &&
+	git stash apply &&
+	test 3 = $(cat file) &&
+	test 2 = $(git show :file) &&
+	test 1 = $(git show HEAD:file) &&
+	git config --unset stash.index
+'
+
+test_expect_success 'apply stashed changes (excluding index) with  
index config set' '
+    git config stash.index yes &&
+	git reset --hard &&
+	echo 8 >other-file &&
+	git add other-file &&
+	test_tick &&
+	git commit -m other-file &&
+	git stash apply --no-index &&
+	test 3 = $(cat file) &&
+	test 1 = $(git show :file) &&
+	test 1 = $(git show HEAD:file) &&
+	git config --unset stash.index
+'
+
test_expect_success 'unstashing in a subdirectory' '
	git reset --hard HEAD &&
	mkdir subdir &&
-- 
1.7.5
Next: Junio C Hamano
Message 1 of 14 in “Adds 'stash.index' configuration option”
  1. Adds 'stash.index' configuration optionDavid Pisoni, May 11, 2011
  2. Junio C HamanoMay 11, 2011
  3. Junio C HamanoMay 12, 2011
  4. David PisoniMay 12, 2011
  5. Junio C HamanoMay 12, 2011
  6. Michael J GruberMay 12, 2011
  7. Jeff KingMay 12, 2011
  8. Michael J GruberMay 12, 2011
  9. Jeff KingMay 12, 2011
  10. RFC proposal: set git defaults options from configMichael J Gruber, May 12, 2011
  11. David PisoniMay 12, 2011
  12. Jeff KingMay 16, 2011
  13. Jeff KingMay 16, 2011
  14. Michael J GruberMay 16, 2011

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.