Skip to contentSkip to conversation

stash drops the stash even if creating the branch fails because it already exists

PATCH New Tomas Carnecky 2 participants 6 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 stash drops the stash even if creating the branch fails because it already exists

This bug was disovered by someone on IRC when he tried to 'git stash branch <branch> <stash>' while <branch> already existed. In that case the stash is dropped even though it isn't applied on any branch, so the stash is effectively lost. I think that shouldn't happen, so here is a test.

---
 t/t3903-stash.sh |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
t/t3903-stash.sh +11 -0
index 9ed2396..0f6b2e4 100755
@@ -545,4 +545,15 @@ test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
 	git stash drop
 '
 
+test_expect_failure 'stash branch should not drop the stash if the branch exists' '
+	git stash clear &&
+	echo foo > file &&
+	git add file &&
+	git commit -m initial &&
+	echo bar > file &&
+	git stash &&
+	test_must_fail git stash branch master stash@{0} &&
+	git rev-parse stash@{0} --
+'
+
 test_done
Signature · 1 line
1.7.3.3.gd2416
#1 lore raw

cover stash: test and fix git stash branch regression

This series fixes another (sorry!) regression I introduced with my detached-stash series.

I have included Tomas' test, so that my tweak to the test script applies cleanly.

Jon Seymour (1):
stash: fix git stash branch regression when branch creation fails

Tomas Carnecky (1):
stash drops the stash even if creating the branch fails because it
already exists

 git-stash.sh     |    6 +++---
 t/t3903-stash.sh |   11 +++++++++++
 2 files changed, 14 insertions(+), 3 deletions(-)
Signature · 1 line
1.7.3.4.g787b.dirty
#2 lore raw

PATCH 1/2 stash drops the stash even if creating the branch fails because it already exists

In reply to Tomas Carnecky

From: Tomas Carnecky <[email protected]>

This bug was disovered by someone on IRC when he tried to 'git stash branch <branch> <stash>' while <branch> already existed. In that case the stash is dropped even though it isn't applied on any branch, so the stash is effectively lost. I think that shouldn't happen, so here is a test.

---
 t/t3903-stash.sh |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
t/t3903-stash.sh +11 -0
index 9ed2396..0f6b2e4 100755
@@ -545,4 +545,15 @@ test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
 	git stash drop
 '
 
+test_expect_failure 'stash branch should not drop the stash if the branch exists' '
+	git stash clear &&
+	echo foo > file &&
+	git add file &&
+	git commit -m initial &&
+	echo bar > file &&
+	git stash &&
+	test_must_fail git stash branch master stash@{0} &&
+	git rev-parse stash@{0} --
+'
+
 test_done
Signature · 1 line
1.7.3.4.g787b.dirty
#3 lore raw
Quoted 6 lines · Jon Seymour

From: Tomas Carnecky <[email protected]>

This bug was disovered by someone on IRC when he tried to 'git stash branch <branch> <stash>' while <branch> already existed. In that case the stash is dropped even though it isn't applied on any branch, so the stash is effectively lost. I think that shouldn't happen, so here is a test.

This line was missing from my original patch, sorry about that:

Quoted 24 lines
---
 t/t3903-stash.sh |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
t/t3903-stash.sh +11 -0
index 9ed2396..0f6b2e4 100755
@@ -545,4 +545,15 @@ test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
 	git stash drop
 '
 
+test_expect_failure 'stash branch should not drop the stash if the branch exists' '
+	git stash clear &&
+	echo foo > file &&
+	git add file &&
+	git commit -m initial &&
+	echo bar > file &&
+	git stash &&
+	test_must_fail git stash branch master stash@{0} &&
+	git rev-parse stash@{0} --
+'
+
 test_done
#5 lore raw

PATCH 2/2 stash: fix git stash branch regression when branch creation fails

In reply to Tomas Carnecky

Tomas Carnecky reported a regression in the behaviour of git stash branch, when the branch creation fails.

This patch fixes that regression by restoring the pre-condition for dropping that previously existed.

This patch assumes Tomas Carnecky's patch has already been applied.

---
 git-stash.sh     |    6 +++---
 t/t3903-stash.sh |    2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
git-stash.sh +3 -3
index 23a9ab5..5fb1245 100755
@@ -432,9 +432,9 @@ apply_to_branch () {
 	assert_stash_like "$@"
 
 	git checkout -b $branch $REV^ &&
-	apply_stash "$@"
-
-	test -z "$IS_STASH_REF" || drop_stash "$@"
+	apply_stash "$@" && {
+		test -z "$IS_STASH_REF" || drop_stash "$@"
+	}
 }
 
 PARSE_CACHE='--not-parsed'
t/t3903-stash.sh +1 -1
index 0f6b2e4..336e244 100755
@@ -545,7 +545,7 @@ test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
 	git stash drop
 '
 
-test_expect_failure 'stash branch should not drop the stash if the branch exists' '
+test_expect_success 'stash branch should not drop the stash if the branch exists' '
 	git stash clear &&
 	echo foo > file &&
 	git add file &&
Signature · 1 line
1.7.3.4.g787b.dirty
#4 lore raw
In reply to Tomas Carnecky

Junio,

Let me know if you want me to re-roll this series with Tomas' sign-off. I can also add my simplification of the Brian's fix and another fix I have made to have git stash save/create fail early in case the index contains merge conflicts.

jon.

Quoted 42 lines · Tomas Carnecky
Quoted 6 lines · Jon Seymour

From: Tomas Carnecky <[email protected]>

This bug was disovered by someone on IRC when he tried to 'git stash branch <branch> <stash>' while <branch> already existed. In that case the stash is dropped even though it isn't applied on any branch, so the stash is effectively lost. I think that shouldn't happen, so here is a test.

This line was missing from my original patch, sorry about that:

Quoted 23 lines

---
 t/t3903-stash.sh |   11 +++++++++++

 1 files changed, 11 insertions(+), 0 deletions(-)
t/t3903-stash.sh +0 -0
index 9ed2396..0f6b2e4 100755
@@ -545,4 +545,15 @@ test_expect_success 'invalid ref of the form stash@{n}, n >= N' '

      git stash drop
 '

t/t3903-stash.sh +11 -0
+test_expect_failure 'stash branch should not drop the stash if the branch exists' '
+     git stash clear &&
+     echo foo > file &&
+     git add file &&
+     git commit -m initial &&
+     echo bar > file &&
+     git stash &&
+     test_must_fail git stash branch master stash@{0} &&
+     git rev-parse stash@{0} --
+'
+
Quoted 1 line

 test_done

--
To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html