threads / patch / 23515

v2, 3 partsstash: Don't overwrite files that have gone from the index

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

## tl;dr

4 messages between Apr 18, 2010 and Apr 18, 2010. Diffs are folded; open one to read it.

replies: 3people: 2as markdown or json

Charles Bailey· Apr 18, 2010, 18:28 UTC · lore

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(-)
Show changes to git-stash.sh +1 −1
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
Junio C Hamano· Apr 18, 2010, 23:11 UTC · re: Charles Bailey · lore

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

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.
Charles Bailey· Apr 18, 2010, 18:28 UTC · lore

[PATCH v2 3/3] Documentation: Describe other situations where -z affects git diff

-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(-)
Show changes to Documentation/diff-options.txt +2 −2
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· Apr 18, 2010, 20:44 UTC · re: Charles Bailey · lore

Re: [PATCH v2 3/3] Documentation: Describe other situations where -z affects git diff

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'.

← back to recent threads