| 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2011 David Caldwell |
| 4 | # |
| 5 | |
| 6 | test_description='Test git stash --include-untracked' |
| 7 | |
| 8 | . ./test-lib.sh |
| 9 | |
| 10 | test_expect_success 'stash save --include-untracked some dirty working directory' ' |
| 11 | echo 1 >file && |
| 12 | git add file && |
| 13 | test_tick && |
| 14 | git commit -m initial && |
| 15 | echo 2 >file && |
| 16 | git add file && |
| 17 | echo 3 >file && |
| 18 | test_tick && |
| 19 | echo 1 >file2 && |
| 20 | echo 1 >HEAD && |
| 21 | mkdir untracked && |
| 22 | echo untracked >untracked/untracked && |
| 23 | git stash --include-untracked && |
| 24 | git diff-files --quiet && |
| 25 | git diff-index --cached --quiet HEAD |
| 26 | ' |
| 27 | |
| 28 | test_expect_success 'stash save --include-untracked cleaned the untracked files' ' |
| 29 | cat >expect <<-EOF && |
| 30 | ?? actual |
| 31 | ?? expect |
| 32 | EOF |
| 33 | |
| 34 | git status --porcelain >actual && |
| 35 | test_cmp expect actual |
| 36 | ' |
| 37 | |
| 38 | test_expect_success 'stash save --include-untracked stashed the untracked files' ' |
| 39 | one_blob=$(echo 1 | git hash-object --stdin) && |
| 40 | tracked=$(git rev-parse --short "$one_blob") && |
| 41 | untracked_blob=$(echo untracked | git hash-object --stdin) && |
| 42 | untracked=$(git rev-parse --short "$untracked_blob") && |
| 43 | cat >expect.diff <<-EOF && |
| 44 | diff --git a/HEAD b/HEAD |
| 45 | new file mode 100644 |
| 46 | index 0000000..$tracked |
| 47 | --- /dev/null |
| 48 | +++ b/HEAD |
| 49 | @@ -0,0 +1 @@ |
| 50 | +1 |
| 51 | diff --git a/file2 b/file2 |
| 52 | new file mode 100644 |
| 53 | index 0000000..$tracked |
| 54 | --- /dev/null |
| 55 | +++ b/file2 |
| 56 | @@ -0,0 +1 @@ |
| 57 | +1 |
| 58 | diff --git a/untracked/untracked b/untracked/untracked |
| 59 | new file mode 100644 |
| 60 | index 0000000..$untracked |
| 61 | --- /dev/null |
| 62 | +++ b/untracked/untracked |
| 63 | @@ -0,0 +1 @@ |
| 64 | +untracked |
| 65 | EOF |
| 66 | cat >expect.lstree <<-EOF && |
| 67 | HEAD |
| 68 | file2 |
| 69 | untracked |
| 70 | EOF |
| 71 | |
| 72 | test_path_is_missing file2 && |
| 73 | test_path_is_missing untracked && |
| 74 | test_path_is_missing HEAD && |
| 75 | git diff HEAD stash^3 -- HEAD file2 untracked >actual && |
| 76 | test_cmp expect.diff actual && |
| 77 | git ls-tree --name-only stash^3: >actual && |
| 78 | test_cmp expect.lstree actual |
| 79 | ' |
| 80 | test_expect_success 'stash save --patch --include-untracked fails' ' |
| 81 | test_must_fail git stash --patch --include-untracked |
| 82 | ' |
| 83 | |
| 84 | test_expect_success 'stash save --patch --all fails' ' |
| 85 | test_must_fail git stash --patch --all |
| 86 | ' |
| 87 | |
| 88 | test_expect_success 'clean up untracked/untracked file to prepare for next tests' ' |
| 89 | git clean --force --quiet |
| 90 | ' |
| 91 | |
| 92 | test_expect_success 'stash pop after save --include-untracked leaves files untracked again' ' |
| 93 | cat >expect <<-EOF && |
| 94 | M file |
| 95 | ?? HEAD |
| 96 | ?? actual |
| 97 | ?? expect |
| 98 | ?? file2 |
| 99 | ?? untracked/ |
| 100 | EOF |
| 101 | |
| 102 | git stash pop && |
| 103 | git status --porcelain >actual && |
| 104 | test_cmp expect actual && |
| 105 | echo 1 >expect_file2 && |
| 106 | test_cmp expect_file2 file2 && |
| 107 | echo untracked >untracked_expect && |
| 108 | test_cmp untracked_expect untracked/untracked |
| 109 | ' |
| 110 | |
| 111 | test_expect_success 'clean up untracked/ directory to prepare for next tests' ' |
| 112 | git clean --force --quiet -d |
| 113 | ' |
| 114 | |
| 115 | test_expect_success 'stash save -u dirty index' ' |
| 116 | echo 4 >file3 && |
| 117 | git add file3 && |
| 118 | test_tick && |
| 119 | git stash -u |
| 120 | ' |
| 121 | |
| 122 | test_expect_success 'stash save --include-untracked dirty index got stashed' ' |
| 123 | four_blob=$(echo 4 | git hash-object --stdin) && |
| 124 | blob=$(git rev-parse --short "$four_blob") && |
| 125 | cat >expect <<-EOF && |
| 126 | diff --git a/file3 b/file3 |
| 127 | new file mode 100644 |
| 128 | index 0000000..$blob |
| 129 | --- /dev/null |
| 130 | +++ b/file3 |
| 131 | @@ -0,0 +1 @@ |
| 132 | +4 |
| 133 | EOF |
| 134 | |
| 135 | git stash pop --index && |
| 136 | test_when_finished "git reset" && |
| 137 | git diff --cached >actual && |
| 138 | test_cmp expect actual |
| 139 | ' |
| 140 | |
| 141 | # Must direct output somewhere where it won't be considered an untracked file |
| 142 | test_expect_success 'stash save --include-untracked -q is quiet' ' |
| 143 | echo 1 >file5 && |
| 144 | git stash save --include-untracked --quiet >.git/stash-output.out 2>&1 && |
| 145 | test_line_count = 0 .git/stash-output.out && |
| 146 | rm -f .git/stash-output.out |
| 147 | ' |
| 148 | |
| 149 | test_expect_success 'stash save --include-untracked removed files' ' |
| 150 | rm -f file && |
| 151 | git stash save --include-untracked && |
| 152 | echo 1 >expect && |
| 153 | test_when_finished "rm -f expect" && |
| 154 | test_cmp expect file |
| 155 | ' |
| 156 | |
| 157 | test_expect_success 'stash save --include-untracked removed files got stashed' ' |
| 158 | git stash pop && |
| 159 | test_path_is_missing file |
| 160 | ' |
| 161 | |
| 162 | test_expect_success 'stash save --include-untracked respects .gitignore' ' |
| 163 | cat >.gitignore <<-EOF && |
| 164 | .gitignore |
| 165 | ignored |
| 166 | ignored.d/ |
| 167 | EOF |
| 168 | |
| 169 | echo ignored >ignored && |
| 170 | mkdir ignored.d && |
| 171 | echo ignored >ignored.d/untracked && |
| 172 | git stash -u && |
| 173 | test_file_not_empty ignored && |
| 174 | test_file_not_empty ignored.d/untracked && |
| 175 | test_file_not_empty .gitignore |
| 176 | ' |
| 177 | |
| 178 | test_expect_success 'stash save -u can stash with only untracked files different' ' |
| 179 | echo 4 >file4 && |
| 180 | git stash -u && |
| 181 | test_path_is_missing file4 |
| 182 | ' |
| 183 | |
| 184 | test_expect_success 'stash save --all does not respect .gitignore' ' |
| 185 | git stash -a && |
| 186 | test_path_is_missing ignored && |
| 187 | test_path_is_missing ignored.d && |
| 188 | test_path_is_missing .gitignore |
| 189 | ' |
| 190 | |
| 191 | test_expect_success 'stash save --all is stash poppable' ' |
| 192 | git stash pop && |
| 193 | test_file_not_empty ignored && |
| 194 | test_file_not_empty ignored.d/untracked && |
| 195 | test_file_not_empty .gitignore |
| 196 | ' |
| 197 | |
| 198 | test_expect_success 'stash push --include-untracked with pathspec' ' |
| 199 | >foo && |
| 200 | >bar && |
| 201 | git stash push --include-untracked -- foo && |
| 202 | test_path_is_file bar && |
| 203 | test_path_is_missing foo && |
| 204 | git stash pop && |
| 205 | test_path_is_file bar && |
| 206 | test_path_is_file foo |
| 207 | ' |
| 208 | |
| 209 | test_expect_success 'stash push with $IFS character' ' |
| 210 | >"foo bar" && |
| 211 | >foo && |
| 212 | >bar && |
| 213 | git add foo* && |
| 214 | git stash push --include-untracked -- "foo b*" && |
| 215 | test_path_is_missing "foo bar" && |
| 216 | test_path_is_file foo && |
| 217 | test_path_is_file bar && |
| 218 | git stash pop && |
| 219 | test_path_is_file "foo bar" && |
| 220 | test_path_is_file foo && |
| 221 | test_path_is_file bar |
| 222 | ' |
| 223 | |
| 224 | test_expect_success 'stash previously ignored file' ' |
| 225 | cat >.gitignore <<-EOF && |
| 226 | ignored |
| 227 | ignored.d/* |
| 228 | EOF |
| 229 | |
| 230 | git reset HEAD && |
| 231 | git add .gitignore && |
| 232 | git commit -m "Add .gitignore" && |
| 233 | >ignored.d/foo && |
| 234 | echo "!ignored.d/foo" >>.gitignore && |
| 235 | git stash save --include-untracked && |
| 236 | test_path_is_missing ignored.d/foo && |
| 237 | git stash pop && |
| 238 | test_path_is_file ignored.d/foo |
| 239 | ' |
| 240 | |
| 241 | test_expect_success 'stash -u -- <untracked> doesnt print error' ' |
| 242 | >untracked && |
| 243 | git stash push -u -- untracked 2>actual && |
| 244 | test_path_is_missing untracked && |
| 245 | test_line_count = 0 actual |
| 246 | ' |
| 247 | |
| 248 | test_expect_success 'stash -u -- <untracked> leaves rest of working tree in place' ' |
| 249 | >tracked && |
| 250 | git add tracked && |
| 251 | >untracked && |
| 252 | git stash push -u -- untracked && |
| 253 | test_path_is_missing untracked && |
| 254 | test_path_is_file tracked |
| 255 | ' |
| 256 | |
| 257 | test_expect_success 'stash -u -- <tracked> <untracked> clears changes in both' ' |
| 258 | >tracked && |
| 259 | git add tracked && |
| 260 | >untracked && |
| 261 | git stash push -u -- tracked untracked && |
| 262 | test_path_is_missing tracked && |
| 263 | test_path_is_missing untracked |
| 264 | ' |
| 265 | |
| 266 | test_expect_success 'stash --all -- <ignored> stashes ignored file' ' |
| 267 | >ignored.d/bar && |
| 268 | git stash push --all -- ignored.d/bar && |
| 269 | test_path_is_missing ignored.d/bar |
| 270 | ' |
| 271 | |
| 272 | test_expect_success 'stash --all -- <tracked> <ignored> clears changes in both' ' |
| 273 | >tracked && |
| 274 | git add tracked && |
| 275 | >ignored.d/bar && |
| 276 | git stash push --all -- tracked ignored.d/bar && |
| 277 | test_path_is_missing tracked && |
| 278 | test_path_is_missing ignored.d/bar |
| 279 | ' |
| 280 | |
| 281 | test_expect_success 'stash -u -- <ignored> leaves ignored file alone' ' |
| 282 | >ignored.d/bar && |
| 283 | git stash push -u -- ignored.d/bar && |
| 284 | test_path_is_file ignored.d/bar |
| 285 | ' |
| 286 | |
| 287 | test_expect_success 'stash -u -- <non-existent> shows no changes when there are none' ' |
| 288 | git stash push -u -- non-existent >actual && |
| 289 | echo "No local changes to save" >expect && |
| 290 | test_cmp expect actual |
| 291 | ' |
| 292 | |
| 293 | test_expect_success 'stash -u with globs' ' |
| 294 | >untracked.txt && |
| 295 | git stash -u -- ":(glob)**/*.txt" && |
| 296 | test_path_is_missing untracked.txt |
| 297 | ' |
| 298 | |
| 299 | test_expect_success 'stash show --include-untracked shows untracked files' ' |
| 300 | git reset --hard && |
| 301 | git clean -xf && |
| 302 | >untracked && |
| 303 | >tracked && |
| 304 | git add tracked && |
| 305 | empty_blob_oid=$(git rev-parse --short :tracked) && |
| 306 | git stash -u && |
| 307 | |
| 308 | cat >expect <<-EOF && |
| 309 | tracked | 0 |
| 310 | untracked | 0 |
| 311 | 2 files changed, 0 insertions(+), 0 deletions(-) |
| 312 | EOF |
| 313 | git stash show --include-untracked >actual && |
| 314 | test_cmp expect actual && |
| 315 | git stash show -u >actual && |
| 316 | test_cmp expect actual && |
| 317 | git stash show --no-include-untracked --include-untracked >actual && |
| 318 | test_cmp expect actual && |
| 319 | git stash show --only-untracked --include-untracked >actual && |
| 320 | test_cmp expect actual && |
| 321 | git -c stash.showIncludeUntracked=true stash show >actual && |
| 322 | test_cmp expect actual && |
| 323 | |
| 324 | cat >expect <<-EOF && |
| 325 | diff --git a/tracked b/tracked |
| 326 | new file mode 100644 |
| 327 | index 0000000..$empty_blob_oid |
| 328 | diff --git a/untracked b/untracked |
| 329 | new file mode 100644 |
| 330 | index 0000000..$empty_blob_oid |
| 331 | EOF |
| 332 | git stash show -p --include-untracked >actual && |
| 333 | test_cmp expect actual && |
| 334 | git stash show --include-untracked -p >actual && |
| 335 | test_cmp expect actual && |
| 336 | git -c stash.showIncludeUntracked=true stash show -p >actual && |
| 337 | test_cmp expect actual |
| 338 | ' |
| 339 | |
| 340 | test_expect_success 'stash show --only-untracked only shows untracked files' ' |
| 341 | git reset --hard && |
| 342 | git clean -xf && |
| 343 | >untracked && |
| 344 | >tracked && |
| 345 | git add tracked && |
| 346 | empty_blob_oid=$(git rev-parse --short :tracked) && |
| 347 | git stash -u && |
| 348 | |
| 349 | cat >expect <<-EOF && |
| 350 | untracked | 0 |
| 351 | 1 file changed, 0 insertions(+), 0 deletions(-) |
| 352 | EOF |
| 353 | git stash show --only-untracked >actual && |
| 354 | test_cmp expect actual && |
| 355 | git stash show --no-include-untracked --only-untracked >actual && |
| 356 | test_cmp expect actual && |
| 357 | git stash show --include-untracked --only-untracked >actual && |
| 358 | test_cmp expect actual && |
| 359 | |
| 360 | cat >expect <<-EOF && |
| 361 | diff --git a/untracked b/untracked |
| 362 | new file mode 100644 |
| 363 | index 0000000..$empty_blob_oid |
| 364 | EOF |
| 365 | git stash show -p --only-untracked >actual && |
| 366 | test_cmp expect actual && |
| 367 | git stash show --only-untracked -p >actual && |
| 368 | test_cmp expect actual |
| 369 | ' |
| 370 | |
| 371 | test_expect_success 'stash show --no-include-untracked cancels --{include,only}-untracked' ' |
| 372 | git reset --hard && |
| 373 | git clean -xf && |
| 374 | >untracked && |
| 375 | >tracked && |
| 376 | git add tracked && |
| 377 | git stash -u && |
| 378 | |
| 379 | cat >expect <<-EOF && |
| 380 | tracked | 0 |
| 381 | 1 file changed, 0 insertions(+), 0 deletions(-) |
| 382 | EOF |
| 383 | git stash show --only-untracked --no-include-untracked >actual && |
| 384 | test_cmp expect actual && |
| 385 | git stash show --include-untracked --no-include-untracked >actual && |
| 386 | test_cmp expect actual |
| 387 | ' |
| 388 | |
| 389 | test_expect_success 'stash show --include-untracked errors on duplicate files' ' |
| 390 | git reset --hard && |
| 391 | git clean -xf && |
| 392 | >tracked && |
| 393 | git add tracked && |
| 394 | tree=$(git write-tree) && |
| 395 | i_commit=$(git commit-tree -p HEAD -m "index on any-branch" "$tree") && |
| 396 | test_when_finished "rm -f untracked_index" && |
| 397 | u_commit=$( |
| 398 | GIT_INDEX_FILE="untracked_index" && |
| 399 | export GIT_INDEX_FILE && |
| 400 | git update-index --add tracked && |
| 401 | u_tree=$(git write-tree) && |
| 402 | git commit-tree -m "untracked files on any-branch" "$u_tree" |
| 403 | ) && |
| 404 | w_commit=$(git commit-tree -p HEAD -p "$i_commit" -p "$u_commit" -m "WIP on any-branch" "$tree") && |
| 405 | test_must_fail git stash show --include-untracked "$w_commit" 2>err && |
| 406 | test_grep "worktree and untracked commit have duplicate entries: tracked" err |
| 407 | ' |
| 408 | |
| 409 | test_expect_success 'stash show --{include,only}-untracked on stashes without untracked entries' ' |
| 410 | git reset --hard && |
| 411 | git clean -xf && |
| 412 | >tracked && |
| 413 | git add tracked && |
| 414 | git stash && |
| 415 | |
| 416 | git stash show >expect && |
| 417 | git stash show --include-untracked >actual && |
| 418 | test_cmp expect actual && |
| 419 | |
| 420 | git stash show --only-untracked >actual && |
| 421 | test_must_be_empty actual |
| 422 | ' |
| 423 | |
| 424 | test_expect_success 'stash -u ignores sub-repository' ' |
| 425 | test_when_finished "rm -rf sub-repo" && |
| 426 | git init sub-repo && |
| 427 | git stash -u |
| 428 | ' |
| 429 | |
| 430 | test_done |