# [PATCH v2 2/3] stash: Don't overwrite files that have gone from the index

4 messages from 2010-04-18 to 2010-04-18. Participants: Charles Bailey, Junio C Hamano.
Thread: https://gitlist.dev/t/23515

## Charles Bailey, 2010-04-18 18:28

Subject: [PATCH v2 2/3] stash: Don't overwrite files that have gone from the index
Message-ID: <20100418182805.GB29329@hashpling.org>
URL: https://gitlist.dev/e/20100418182805.GB29329%40hashpling.org
In-Reply-To: <9eaa7a4672f2010c9e6e3b3f473f1c09abcfcc45.1271614677.git.charles@hashpling.org>

```
The use of git add -u in create_stash isn't always complete. In
particular, if a file has been removed from the index but changed in the
work tree it will not be added to the stash's saved work tree tree
object. When stash then resets the work tree to match HEAD, any changes
will be lost.

To be complete, any work tree file which differs from HEAD needs to be
saved, regardless of whether it still appears in the index or not.

This is achieved with a combination of a diff against HEAD and a call to
update-index with an explicit list of paths that have changed.

Signed-off-by: Charles Bailey <charles@hashpling.org>
---

Since the last version of this patch I've followed Jeff King's advice
and used update-index. This should be much more portable and reliable.

I believe the fix is now complete as it is, although as I noted we
still have an issue when stashing file <-> directory changes.

 git-stash.sh |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/git-stash.sh b/git-stash.sh
index 908aab2..1d95447 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -86,7 +86,7 @@ create_stash () {
 			GIT_INDEX_FILE="$TMP-index" &&
 			export GIT_INDEX_FILE &&
 			git read-tree -m $i_tree &&
-			git add -u &&
+			git diff --name-only -z HEAD | git update-index -z --add --remove --stdin &&
 			git write-tree &&
 			rm -f "$TMP-index"
 		) ) ||
-- 
1.7.1.rc1.241.g4e72f

```

## Charles Bailey, 2010-04-18 18:28

Subject: [PATCH v2 3/3] Documentation: Describe other situations where -z affects git diff
Message-ID: <20100418182817.GC29329@hashpling.org>
URL: https://gitlist.dev/e/20100418182817.GC29329%40hashpling.org
In-Reply-To: <9eaa7a4672f2010c9e6e3b3f473f1c09abcfcc45.1271614677.git.charles@hashpling.org>

```
-z also alters the behaviour of --name-only and --name-status.

Signed-off-by: Charles Bailey <charles@hashpling.org>
---

As I'm relying on diff --name-only -z "doing the right thing", I quite
wanted it to be part of the documented interface.

 Documentation/diff-options.txt |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/Documentation/diff-options.txt b/Documentation/diff-options.txt
index 60e922e..c9c6c2b 100644
--- a/Documentation/diff-options.txt
+++ b/Documentation/diff-options.txt
@@ -94,8 +94,8 @@ Also, when `--raw` or `--numstat` has been given, do not munge
 pathnames and use NULs as output field terminators.
 endif::git-log[]
 ifndef::git-log[]
-	When `--raw` or `--numstat` has been given, do not munge
-	pathnames and use NULs as output field terminators.
+	When `--raw`, `--numstat`, `--name-only` or `--name-status` has been
+	given, do not munge pathnames and use NULs as output field terminators.
 endif::git-log[]
 +
 Without this option, each pathname output will have TAB, LF, double quotes,
-- 
1.7.1.rc1.241.g4e72f

```

## Junio C Hamano, 2010-04-18 20:44

Subject: Re: [PATCH v2 3/3] Documentation: Describe other situations where -z affects git diff
Message-ID: <7vbpdgwkw8.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vbpdgwkw8.fsf%40alter.siamese.dyndns.org
In-Reply-To: <20100418182817.GC29329@hashpling.org>

```
Charles Bailey <charles@hashpling.org> writes:

> -z also alters the behaviour of --name-only and --name-status.
>
> Signed-off-by: Charles Bailey <charles@hashpling.org>

Thanks; this is good, independent from the other patches, for 'maint'.

```

## Junio C Hamano, 2010-04-18 23:11

Subject: Re: [PATCH v2 2/3] stash: Don't overwrite files that have gone from the index
Message-ID: <7vpr1wtkyu.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vpr1wtkyu.fsf%40alter.siamese.dyndns.org
In-Reply-To: <20100418182805.GB29329@hashpling.org>

```
Charles Bailey <charles@hashpling.org> writes:

> To be complete, any work tree file which differs from HEAD needs to be
> saved, regardless of whether it still appears in the index or not.

Sounds quite sane.

> -			git add -u &&
> +			git diff --name-only -z HEAD | git update-index -z --add --remove --stdin &&

Paths that exist in HEAD but not in the index will be caught with this;
what about paths that do not exist in HEAD but is in the index---they will
be kept in the index so they are fine as well.  Good.

Thanks.

```
