threads / patch / 9269

patch, 2 partsgit-stash last-minute fixes

Subject: [PATCH 0/2] git-stash last-minute fixes

## tl;dr

4 messages between Jul 28, 2007 and Jul 28, 2007. Diffs are folded; open one to read it.

replies: 3people: 2as markdown or json

Junio C Hamano· Jul 28, 2007, 06:59 UTC · lore
Here are a small patch series to git-stash.
 [1/2] Fix git-stash apply --index
 This fixes two rather embarrasing bugs in "apply --index".
 [2/2] git-stash apply --index: optimize postprocessing
 This builds on top of the previous one to avoid unnecessary
 index manipulations that is later wiped by a read-tree.

There also is a bugfix I received privately from Nana for the breakage I introduced with 7ab3cc70. I've already queued it for 'master'.

-- >8 --
From: しらいしななこ <nanako3@bluebottle.com>
Date: Sat, 28 Jul 2007 10:44:48 +0900
Subject: [PATCH] git-stash: Make sure reflog is created for refs/stash

Earlier commit 7ab3cc70 fixed "stash clear" but broke save_stash, because it forgot to make sure the reflog file exists before saving.

Signed-off-by: Nanako Shiraishi <nanako3@bluebottle.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Show changes to git-stash.sh +3 −0
diff --git a/git-stash.sh b/git-stash.sh
index f90dffd..0073e9d 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -36,6 +36,9 @@ save_stash () {
 	test -f "$GIT_DIR/logs/$ref_stash" ||
 		clear_stash || die "Cannot initialize stash"
 
+	# Make sure the reflog for stash is kept.
+	: >>"$GIT_DIR/logs/$ref_stash"
+
 	# state of the base commit
 	if b_commit=$(git rev-parse --verify HEAD)
 	then
Junio C Hamano· Jul 28, 2007, 07:02 UTC · re: Junio C Hamano · lore

[PATCH 1/2] Fix git-stash apply --index

Two bugs that made the command practically unusable were fixed with this.

 - A stash created with a clean index does not have any
   difference between the base tree and the index tree.
   Trying to apply the diff between them to the index would
   error out with "No changes".  Even when the user asked to
   unstash with --index, do not bother with --index action if
   the base tree and the index tree match.
 - After successfully performing the working tree merge, the
   index was reloaded from an earlier state of unstashed index
   with "read-tree"; this left all the paths cache dirty.  By
   moving the call to git-status after this read-tree, match the
   cached stat information in the index.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 git-stash.sh |   23 +++++++++++++++++------
 1 files changed, 17 insertions(+), 6 deletions(-)
Show changes to git-stash.sh +17 −6
diff --git a/git-stash.sh b/git-stash.sh
index 0073e9d..873e7be 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -128,19 +128,24 @@ apply_stash () {
 	c_tree=$(git write-tree) ||
 		die 'Cannot apply a stash in the middle of a merge'
 
+	# stash records the work tree, and is a merge between the
+	# base commit (first parent) and the index tree (second parent).
 	s=$(git rev-parse --revs-only --no-flags --default $ref_stash "$@") &&
 	w_tree=$(git rev-parse --verify "$s:") &&
-	b_tree=$(git rev-parse --verify "$s^:") ||
+	b_tree=$(git rev-parse --verify "$s^1:") &&
+	i_tree=$(git rev-parse --verify "$s^2:") ||
 		die "$*: no valid stashed state found"
 
-	test -z "$unstash_index" || {
+	unstashed_index_tree=
+	if test -n "$unstash_index" && test "$b_tree" != "$i_tree"
+	then
 		git diff --binary $s^2^..$s^2 | git apply --cached
 		test $? -ne 0 &&
 			die 'Conflicts in index. Try without --index.'
 		unstashed_index_tree=$(git-write-tree) ||
 			die 'Could not save index tree'
 		git reset
-	}
+	fi
 
 	eval "
 		GITHEAD_$w_tree='Stashed changes' &&
@@ -157,13 +162,19 @@ apply_stash () {
 		git read-tree --reset $c_tree &&
 		git update-index --add --stdin <"$a" ||
 			die "Cannot unstage modified files"
-		git-status
 		rm -f "$a"
-		test -z "$unstash_index" || git read-tree $unstashed_index_tree
+		if test -n "$unstashed_index_tree"
+		then
+			git read-tree "$unstashed_index_tree"
+		fi
+		git status || :
 	else
 		# Merge conflict; keep the exit status from merge-recursive
 		status=$?
-		test -z "$unstash_index" || echo 'Index was not unstashed.' >&2
+		if test -n "$unstash_index"
+		then
+			echo >&2 'Index was not unstashed.'
+		fi
 		exit $status
 	fi
 }
-- 
1.5.3.rc3.16.g9f04-dirty
Junio C Hamano· Jul 28, 2007, 07:02 UTC · re: Junio C Hamano · lore

[PATCH 2/2] git-stash apply --index: optimize postprocessing

Originally, "apply --index" codepath was bolted on to the "update working tree files and index, but then revert the changes we make to the index except for added files so that we do not forget about them" codepath, almost as an afterthought. Because "apply --index" first prepares the final index state upfront, "revert except the added paths" postprocessing does not have to be done.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 git-stash.sh |   13 +++++++------
 1 files changed, 7 insertions(+), 6 deletions(-)
Show changes to git-stash.sh +7 −6
diff --git a/git-stash.sh b/git-stash.sh
index 873e7be..30425ce 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -157,15 +157,16 @@ apply_stash () {
 	if git-merge-recursive $b_tree -- $c_tree $w_tree
 	then
 		# No conflict
-		a="$TMP-added" &&
-		git diff --cached --name-only --diff-filter=A $c_tree >"$a" &&
-		git read-tree --reset $c_tree &&
-		git update-index --add --stdin <"$a" ||
-			die "Cannot unstage modified files"
-		rm -f "$a"
 		if test -n "$unstashed_index_tree"
 		then
 			git read-tree "$unstashed_index_tree"
+		else
+			a="$TMP-added" &&
+			git diff --cached --name-only --diff-filter=A $c_tree >"$a" &&
+			git read-tree --reset $c_tree &&
+			git update-index --add --stdin <"$a" ||
+				die "Cannot unstage modified files"
+			rm -f "$a"
 		fi
 		git status || :
 	else
-- 
1.5.3.rc3.16.g9f04-dirty
しらいしななこ· Jul 28, 2007, 09:06 UTC · re: Junio C Hamano · lore

Re: [PATCH 0/2] git-stash last-minute fixes

Quoting Junio C Hamano <gitster@pobox.com> writes:
Show 10 quoted lines
> Here are a small patch series to git-stash.
>
>  [1/2] Fix git-stash apply --index
>
>  This fixes two rather embarrasing bugs in "apply --index".
>
>  [2/2] git-stash apply --index: optimize postprocessing
>
>  This builds on top of the previous one to avoid unnecessary
>  index manipulations that is later wiped by a read-tree.

Thank you for the patch. I have not used the --index things myself and did not notice these breakages.

-- 
Nanako Shiraishi
http://ivory.ap.teacup.com/nanako3/

----------------------------------------------------------------------
Get a free email address with REAL anti-spam protection.
http://www.bluebottle.com/tag/1

← back to recent threads