|
| 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 | + git branch branch1 other-branch && |
| 35 | + echo dirty >file0 && |
| 36 | + git switch --autostash branch1 >actual 2>&1 && |
| 37 | + test_grep "Created autostash" actual && |
| 38 | + test_grep "Applied autostash" actual && |
| 39 | + echo dirty >expected && |
| 40 | + test_cmp expected file0 && |
| 41 | + git switch main |
| 42 | +' |
| 43 | + |
| 44 | +test_expect_success 'checkout --autostash on dirty worktree' ' |
| 45 | + git branch branch2 other-branch && |
| 46 | + echo dirty >file0 && |
| 47 | + git checkout --autostash branch2 >actual 2>&1 && |
| 48 | + test_grep "Created autostash" actual && |
| 49 | + test_grep "Applied autostash" actual && |
| 50 | + echo dirty >expected && |
| 51 | + test_cmp expected file0 && |
| 52 | + git checkout main |
| 53 | +' |
| 54 | + |
| 55 | +test_expect_success 'switch: checkout.autostash config' ' |
| 56 | + git branch branch3 other-branch && |
| 57 | + echo dirty >file0 && |
| 58 | + test_config checkout.autostash true && |
| 59 | + git switch branch3 >actual 2>&1 && |
| 60 | + test_grep "Created autostash" actual && |
| 61 | + test_grep "Applied autostash" actual && |
| 62 | + echo dirty >expected && |
| 63 | + test_cmp expected file0 && |
| 64 | + git switch main |
| 65 | +' |
| 66 | + |
| 67 | +test_expect_success 'checkout: checkout.autostash config' ' |
| 68 | + git branch branch4 other-branch && |
| 69 | + echo dirty >file0 && |
| 70 | + test_config checkout.autostash true && |
| 71 | + git checkout branch4 >actual 2>&1 && |
| 72 | + test_grep "Created autostash" actual && |
| 73 | + test_grep "Applied autostash" actual && |
| 74 | + echo dirty >expected && |
| 75 | + test_cmp expected file0 && |
| 76 | + git checkout main |
| 77 | +' |
| 78 | + |
| 79 | +test_expect_success '--no-autostash overrides checkout.autostash' ' |
| 80 | + git branch branch5 other-branch && |
| 81 | + echo dirty >file1 && |
| 82 | + test_config checkout.autostash true && |
| 83 | + test_must_fail git switch --no-autostash branch5 2>stderr && |
| 84 | + test_grep ! "Created autostash" stderr && |
| 85 | + git checkout -- file1 |
| 86 | +' |
| 87 | + |
| 88 | +test_expect_success '--autostash overrides checkout.autostash=false' ' |
| 89 | + git branch branch6 other-branch && |
| 90 | + echo dirty >file0 && |
| 91 | + test_config checkout.autostash false && |
| 92 | + git switch --autostash branch6 >actual 2>&1 && |
| 93 | + test_grep "Created autostash" actual && |
| 94 | + test_grep "Applied autostash" actual && |
| 95 | + echo dirty >expected && |
| 96 | + test_cmp expected file0 && |
| 97 | + git switch main |
| 98 | +' |
| 99 | + |
| 100 | +test_expect_success 'autostash with dirty index' ' |
| 101 | + git branch branch7 other-branch && |
| 102 | + echo dirty-index >file0 && |
| 103 | + git add file0 && |
| 104 | + git switch --autostash branch7 >actual 2>&1 && |
| 105 | + test_grep "Created autostash" actual && |
| 106 | + test_grep "Applied autostash" actual && |
| 107 | + echo dirty-index >expected && |
| 108 | + test_cmp expected file0 && |
| 109 | + git checkout -- file0 && |
| 110 | + git switch main |
| 111 | +' |
| 112 | + |
| 113 | +test_expect_success 'autostash bypasses conflicting local changes' ' |
| 114 | + git branch branch8 other-branch && |
| 115 | + echo dirty >file1 && |
| 116 | + test_must_fail git switch branch8 2>stderr && |
| 117 | + test_grep "Your local changes" stderr && |
| 118 | + git switch --autostash branch8 >actual 2>&1 && |
| 119 | + test_grep "Created autostash" actual && |
| 120 | + test_grep "Applying autostash resulted in conflicts" actual && |
| 121 | + test_grep "Your changes are safe in the stash" actual && |
| 122 | + git stash drop && |
| 123 | + git reset --hard && |
| 124 | + git switch main |
| 125 | +' |
| 126 | + |
| 127 | +test_expect_success 'autostash is a no-op with clean worktree' ' |
| 128 | + git branch branch9 other-branch && |
| 129 | + git switch --autostash branch9 >actual 2>&1 && |
| 130 | + test_grep ! "Created autostash" actual && |
| 131 | + git switch main |
| 132 | +' |
| 133 | + |
| 134 | +test_expect_success '--autostash with --merge stashes and switches' ' |
| 135 | + git branch branch10 other-branch && |
| 136 | + echo dirty >file0 && |
| 137 | + git switch --autostash --merge branch10 >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 conflicting changes' ' |
| 146 | + git branch branch11 other-branch && |
| 147 | + echo staged-change >file1 && |
| 148 | + git add file1 && |
| 149 | + git switch --autostash branch11 >actual 2>&1 && |
| 150 | + test_grep "Created autostash" actual && |
| 151 | + test_grep "Applying autostash resulted in conflicts" actual && |
| 152 | + test_grep "Your changes are safe in the stash" actual && |
| 153 | + git stash drop && |
| 154 | + git reset --hard && |
| 155 | + git switch main |
| 156 | +' |
| 157 | + |
| 158 | +test_expect_success '--autostash with --force preserves dirty changes' ' |
| 159 | + git branch branch12 other-branch && |
| 160 | + echo dirty-force >file1 && |
| 161 | + git switch --autostash --force branch12 >actual 2>&1 && |
| 162 | + test_grep "Created autostash" actual && |
| 163 | + test_grep "Applying autostash resulted in conflicts" actual && |
| 164 | + test_grep "Your changes are safe in the stash" actual && |
| 165 | + git stash drop && |
| 166 | + git reset --hard && |
| 167 | + git switch main |
| 168 | +' |
| 169 | + |
| 170 | +test_expect_success '--autostash with new branch creation' ' |
| 171 | + echo dirty >file0 && |
| 172 | + git switch --autostash -c branch13 >actual 2>&1 && |
| 173 | + test_grep "Created autostash" actual && |
| 174 | + test_grep "Applied autostash" actual && |
| 175 | + echo dirty >expected && |
| 176 | + test_cmp expected file0 && |
| 177 | + git switch main && |
| 178 | + git branch -D branch13 |
| 179 | +' |
| 180 | + |
| 181 | +test_done |
0 commit comments