|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +test_description='checkout/switch --autostash tests' |
| 4 | + |
| 5 | +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 6 | +export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 7 | + |
| 8 | +. ./test-lib.sh |
| 9 | + |
| 10 | +test_expect_success 'setup' ' |
| 11 | + echo file0content >file0 && |
| 12 | + echo file1content >file1 && |
| 13 | + git add . && |
| 14 | + test_tick && |
| 15 | + git commit -m "initial commit" && |
| 16 | + git branch other-branch && |
| 17 | + echo file1main >file1 && |
| 18 | + git add . && |
| 19 | + test_tick && |
| 20 | + git commit -m "modify file1 on main" && |
| 21 | + git checkout other-branch && |
| 22 | + echo file1other >file1 && |
| 23 | + git add . && |
| 24 | + test_tick && |
| 25 | + git commit -m "modify file1 on other-branch" && |
| 26 | + echo file2content >file2 && |
| 27 | + git add . && |
| 28 | + test_tick && |
| 29 | + git commit -m "add file2 on other-branch" && |
| 30 | + git checkout main |
| 31 | +' |
| 32 | + |
| 33 | +test_expect_success 'switch --autostash on dirty worktree' ' |
| 34 | + echo dirty >file0 && |
| 35 | + git switch --autostash other-branch >actual 2>&1 && |
| 36 | + test_grep "Created autostash" actual && |
| 37 | + test_grep "Applied autostash" actual && |
| 38 | + echo dirty >expected && |
| 39 | + test_cmp expected file0 && |
| 40 | + git switch main |
| 41 | +' |
| 42 | + |
| 43 | +test_expect_success 'checkout --autostash on dirty worktree' ' |
| 44 | + echo dirty >file0 && |
| 45 | + git checkout --autostash other-branch >actual 2>&1 && |
| 46 | + test_grep "Created autostash" actual && |
| 47 | + test_grep "Applied autostash" actual && |
| 48 | + echo dirty >expected && |
| 49 | + test_cmp expected file0 && |
| 50 | + git checkout main |
| 51 | +' |
| 52 | + |
| 53 | +test_expect_success 'switch: checkout.autostash config' ' |
| 54 | + echo dirty >file0 && |
| 55 | + test_config checkout.autostash true && |
| 56 | + git switch other-branch >actual 2>&1 && |
| 57 | + test_grep "Created autostash" actual && |
| 58 | + test_grep "Applied autostash" actual && |
| 59 | + echo dirty >expected && |
| 60 | + test_cmp expected file0 && |
| 61 | + git switch main |
| 62 | +' |
| 63 | + |
| 64 | +test_expect_success 'checkout: checkout.autostash config' ' |
| 65 | + echo dirty >file0 && |
| 66 | + test_config checkout.autostash true && |
| 67 | + git checkout other-branch >actual 2>&1 && |
| 68 | + test_grep "Created autostash" actual && |
| 69 | + test_grep "Applied autostash" actual && |
| 70 | + echo dirty >expected && |
| 71 | + test_cmp expected file0 && |
| 72 | + git checkout main |
| 73 | +' |
| 74 | + |
| 75 | +test_expect_success '--no-autostash overrides checkout.autostash' ' |
| 76 | + echo dirty >file1 && |
| 77 | + test_config checkout.autostash true && |
| 78 | + test_must_fail git switch --no-autostash other-branch 2>stderr && |
| 79 | + test_grep ! "Created autostash" stderr && |
| 80 | + git checkout -- file1 |
| 81 | +' |
| 82 | + |
| 83 | +test_expect_success '--autostash overrides checkout.autostash=false' ' |
| 84 | + echo dirty >file0 && |
| 85 | + test_config checkout.autostash false && |
| 86 | + git switch --autostash other-branch >actual 2>&1 && |
| 87 | + test_grep "Created autostash" actual && |
| 88 | + test_grep "Applied autostash" actual && |
| 89 | + echo dirty >expected && |
| 90 | + test_cmp expected file0 && |
| 91 | + git switch main |
| 92 | +' |
| 93 | + |
| 94 | +test_expect_success 'autostash with dirty index' ' |
| 95 | + echo dirty-index >file0 && |
| 96 | + git add file0 && |
| 97 | + git switch --autostash other-branch >actual 2>&1 && |
| 98 | + test_grep "Created autostash" actual && |
| 99 | + test_grep "Applied autostash" actual && |
| 100 | + echo dirty-index >expected && |
| 101 | + test_cmp expected file0 && |
| 102 | + git checkout -- file0 && |
| 103 | + git switch main |
| 104 | +' |
| 105 | + |
| 106 | +test_expect_success 'autostash bypasses local changes would be overwritten error' ' |
| 107 | + echo dirty >file1 && |
| 108 | + test_must_fail git switch other-branch 2>stderr && |
| 109 | + test_grep "Your local changes" stderr && |
| 110 | + git switch --autostash other-branch >actual 2>&1 && |
| 111 | + test_grep "Created autostash" actual && |
| 112 | + test_grep "Applying autostash resulted in conflicts" actual && |
| 113 | + git stash drop && |
| 114 | + git reset --hard && |
| 115 | + git switch main |
| 116 | +' |
| 117 | + |
| 118 | +test_expect_success 'autostash with conflicting stash apply saves to stash list' ' |
| 119 | + echo dirty-conflicting >file1 && |
| 120 | + git switch --autostash other-branch >actual 2>&1 && |
| 121 | + test_grep "Created autostash" actual && |
| 122 | + test_grep "Applying autostash resulted in conflicts" actual && |
| 123 | + test_grep "Your changes are safe in the stash" actual && |
| 124 | + git stash drop && |
| 125 | + git reset --hard && |
| 126 | + git switch main |
| 127 | +' |
| 128 | + |
| 129 | +test_expect_success 'autostash is a no-op with clean worktree' ' |
| 130 | + git switch --autostash other-branch >actual 2>&1 && |
| 131 | + test_grep ! "Created autostash" actual && |
| 132 | + git switch main |
| 133 | +' |
| 134 | + |
| 135 | +test_expect_success '--autostash with --merge stashes and switches' ' |
| 136 | + echo dirty >file0 && |
| 137 | + git switch --autostash --merge other-branch >actual 2>&1 && |
| 138 | + test_grep "Created autostash" actual && |
| 139 | + test_grep "Applied autostash" actual && |
| 140 | + echo dirty >expected && |
| 141 | + test_cmp expected file0 && |
| 142 | + git switch main |
| 143 | +' |
| 144 | + |
| 145 | +test_expect_success 'autostash with staged changes switches successfully' ' |
| 146 | + echo staged-change >file1 && |
| 147 | + git add file1 && |
| 148 | + git switch --autostash other-branch >actual 2>&1 && |
| 149 | + test_grep "Created autostash" actual && |
| 150 | + git stash drop 2>/dev/null && |
| 151 | + git reset --hard && |
| 152 | + git switch main |
| 153 | +' |
| 154 | + |
| 155 | +test_done |
0 commit comments