From f2f3fc95478c21687646a71f934c524a45e3ec03 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Wed, 22 Apr 2015 15:30:52 -0400 Subject: t3903: stop hard-coding commit sha1s When testing the diff output of "git stash list", we look for the stash's subject of "WIP on master: $sha1", even though it's not relevant to the diff output. This makes the test brittle to refactoring, as any changes to earlier tests may impact the commit sha1. Since we don't care about the commit subject here, we can simply ask stash not to print it. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index 1e29962..6da4856 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -695,8 +695,8 @@ test_expect_success 'setup stash with index and worktree changes' ' ' test_expect_success 'stash list implies --first-parent -m' ' - cat >expect <<-\EOF && - stash@{0}: WIP on master: b27a2bc subdir + cat >expect <<-EOF && + stash@{0} diff --git a/file b/file index 257cc56..d26b33d 100644 @@ -706,13 +706,13 @@ test_expect_success 'stash list implies --first-parent -m' ' -foo +working EOF - git stash list -p >actual && + git stash list --format=%gd -p >actual && test_cmp expect actual ' test_expect_success 'stash list --cc shows combined diff' ' cat >expect <<-\EOF && - stash@{0}: WIP on master: b27a2bc subdir + stash@{0} diff --cc file index 257cc56,9015a7a..d26b33d @@ -723,7 +723,7 @@ test_expect_success 'stash list --cc shows combined diff' ' -index ++working EOF - git stash list -p --cc >actual && + git stash list --format=%gd -p --cc >actual && test_cmp expect actual ' -- cgit v0.10.2-6-g49f6 From 88bab59c5bf8de2a1577c3134f4dff1e225409e2 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Wed, 22 Apr 2015 15:30:58 -0400 Subject: t3903: avoid applying onto dirty index One of the tests in t3903 wants to make sure that applying a stash that touches only "file" can still happen even if there are working tree changes to "other-file". To do so, it adds "other-file" to the index (since otherwise it is an untracked file, voiding the purpose of the test). But as we are about to refactor the dirty-index handling, and as this test does not actually care about having a dirty index (only a dirty working tree), let's bump the tracking of "other-file" into the setup phase, so we can have _just_ a dirty working tree here. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index 6da4856..f179c93 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -10,6 +10,8 @@ test_description='Test git stash' test_expect_success 'stash some dirty working directory' ' echo 1 > file && git add file && + echo unrelated >other-file && + git add other-file && test_tick && git commit -m initial && echo 2 > file && @@ -45,8 +47,6 @@ test_expect_success 'applying bogus stash does nothing' ' test_expect_success 'apply does not need clean working directory' ' echo 4 >other-file && - git add other-file && - echo 5 >other-file && git stash apply && echo 3 >expect && test_cmp expect file -- cgit v0.10.2-6-g49f6 From ed178ef13a26136d86ff4e33bb7b1afb5033f908 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Wed, 22 Apr 2015 15:31:02 -0400 Subject: stash: require a clean index to apply If you have staged contents in your index and run "stash apply", we may hit a conflict and put new entries into the index. Recovering to your original state is difficult at that point, because tools like "git reset --keep" will blow away anything staged. We can make this safer by refusing to apply when there are staged changes. It's possible we could provide better tooling here, as "git stash apply" should be writing only conflicts to the index (so we know that any stage-0 entries are potentially precious). But it is the odd duck; most "mergy" commands will update the index for cleanly merged entries, and it is not worth updating our tooling to support this use case which is unlikely to be of interest (besides which, we would still need to block a dirty index for "stash apply --index", since that case _would_ be ambiguous). Signed-off-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/git-stash.sh b/git-stash.sh index d4cf818..cc28368 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -442,6 +442,8 @@ apply_stash () { assert_stash_like "$@" git update-index -q --refresh || die "$(gettext "unable to refresh index")" + git diff-index --cached --quiet --ignore-submodules HEAD -- || + die "$(gettext "Cannot apply stash: Your index contains uncommitted changes.")" # current index state c_tree=$(git write-tree) || diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index f179c93..0746eee 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -45,6 +45,13 @@ test_expect_success 'applying bogus stash does nothing' ' test_cmp expect file ' +test_expect_success 'apply requires a clean index' ' + test_when_finished "git reset --hard" && + echo changed >other-file && + git add other-file && + test_must_fail git stash apply +' + test_expect_success 'apply does not need clean working directory' ' echo 4 >other-file && git stash apply && -- cgit v0.10.2-6-g49f6