t / t3905-stash-include-untracked.shon commit Merge branch 'jk/ref-array-push' (b3d6c48)
   1#!/bin/sh
   2#
   3# Copyright (c) 2011 David Caldwell
   4#
   5
   6test_description='Test git stash --include-untracked'
   7
   8. ./test-lib.sh
   9
  10test_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
  28cat > expect <<EOF
  29?? actual
  30?? expect
  31EOF
  32
  33test_expect_success 'stash save --include-untracked cleaned the untracked files' '
  34        git status --porcelain >actual &&
  35        test_cmp expect actual
  36'
  37
  38cat > expect.diff <<EOF
  39diff --git a/HEAD b/HEAD
  40new file mode 100644
  41index 0000000..d00491f
  42--- /dev/null
  43+++ b/HEAD
  44@@ -0,0 +1 @@
  45+1
  46diff --git a/file2 b/file2
  47new file mode 100644
  48index 0000000..d00491f
  49--- /dev/null
  50+++ b/file2
  51@@ -0,0 +1 @@
  52+1
  53diff --git a/untracked/untracked b/untracked/untracked
  54new file mode 100644
  55index 0000000..5a72eb2
  56--- /dev/null
  57+++ b/untracked/untracked
  58@@ -0,0 +1 @@
  59+untracked
  60EOF
  61cat > expect.lstree <<EOF
  62HEAD
  63file2
  64untracked
  65EOF
  66
  67test_expect_success 'stash save --include-untracked stashed the untracked files' '
  68        test_path_is_missing file2 &&
  69        test_path_is_missing untracked &&
  70        test_path_is_missing HEAD &&
  71        git diff HEAD stash^3 -- HEAD file2 untracked >actual &&
  72        test_cmp expect.diff actual &&
  73        git ls-tree --name-only stash^3: >actual &&
  74        test_cmp expect.lstree actual
  75'
  76test_expect_success 'stash save --patch --include-untracked fails' '
  77        test_must_fail git stash --patch --include-untracked
  78'
  79
  80test_expect_success 'stash save --patch --all fails' '
  81        test_must_fail git stash --patch --all
  82'
  83
  84git clean --force --quiet
  85
  86cat > expect <<EOF
  87 M file
  88?? HEAD
  89?? actual
  90?? expect
  91?? file2
  92?? untracked/
  93EOF
  94
  95test_expect_success 'stash pop after save --include-untracked leaves files untracked again' '
  96        git stash pop &&
  97        git status --porcelain >actual &&
  98        test_cmp expect actual &&
  99        test "1" = "$(cat file2)" &&
 100        test untracked = "$(cat untracked/untracked)"
 101'
 102
 103git clean --force --quiet -d
 104
 105test_expect_success 'stash save -u dirty index' '
 106        echo 4 > file3 &&
 107        git add file3 &&
 108        test_tick &&
 109        git stash -u
 110'
 111
 112cat > expect <<EOF
 113diff --git a/file3 b/file3
 114new file mode 100644
 115index 0000000..b8626c4
 116--- /dev/null
 117+++ b/file3
 118@@ -0,0 +1 @@
 119+4
 120EOF
 121
 122test_expect_success 'stash save --include-untracked dirty index got stashed' '
 123        git stash pop --index &&
 124        git diff --cached >actual &&
 125        test_cmp expect actual
 126'
 127
 128git reset > /dev/null
 129
 130# Must direct output somewhere where it won't be considered an untracked file
 131test_expect_success 'stash save --include-untracked -q is quiet' '
 132        echo 1 > file5 &&
 133        git stash save --include-untracked --quiet > .git/stash-output.out 2>&1 &&
 134        test_line_count = 0 .git/stash-output.out &&
 135        rm -f .git/stash-output.out
 136'
 137
 138test_expect_success 'stash save --include-untracked removed files' '
 139        rm -f file &&
 140        git stash save --include-untracked &&
 141        echo 1 > expect &&
 142        test_cmp file expect
 143'
 144
 145rm -f expect
 146
 147test_expect_success 'stash save --include-untracked removed files got stashed' '
 148        git stash pop &&
 149        test_path_is_missing file
 150'
 151
 152cat > .gitignore <<EOF
 153.gitignore
 154ignored
 155ignored.d/
 156EOF
 157
 158test_expect_success 'stash save --include-untracked respects .gitignore' '
 159        echo ignored > ignored &&
 160        mkdir ignored.d &&
 161        echo ignored >ignored.d/untracked &&
 162        git stash -u &&
 163        test -s ignored &&
 164        test -s ignored.d/untracked &&
 165        test -s .gitignore
 166'
 167
 168test_expect_success 'stash save -u can stash with only untracked files different' '
 169        echo 4 > file4 &&
 170        git stash -u &&
 171        test_path_is_missing file4
 172'
 173
 174test_expect_success 'stash save --all does not respect .gitignore' '
 175        git stash -a &&
 176        test_path_is_missing ignored &&
 177        test_path_is_missing ignored.d &&
 178        test_path_is_missing .gitignore
 179'
 180
 181test_expect_success 'stash save --all is stash poppable' '
 182        git stash pop &&
 183        test -s ignored &&
 184        test -s ignored.d/untracked &&
 185        test -s .gitignore
 186'
 187
 188test_expect_success 'stash push --include-untracked with pathspec' '
 189        >foo &&
 190        >bar &&
 191        git stash push --include-untracked -- foo &&
 192        test_path_is_file bar &&
 193        test_path_is_missing foo &&
 194        git stash pop &&
 195        test_path_is_file bar &&
 196        test_path_is_file foo
 197'
 198
 199test_expect_success 'stash push with $IFS character' '
 200        >"foo bar" &&
 201        >foo &&
 202        >bar &&
 203        git add foo* &&
 204        git stash push --include-untracked -- "foo b*" &&
 205        test_path_is_missing "foo bar" &&
 206        test_path_is_file foo &&
 207        test_path_is_file bar &&
 208        git stash pop &&
 209        test_path_is_file "foo bar" &&
 210        test_path_is_file foo &&
 211        test_path_is_file bar
 212'
 213
 214cat > .gitignore <<EOF
 215ignored
 216ignored.d/*
 217EOF
 218
 219test_expect_success 'stash previously ignored file' '
 220        git reset HEAD &&
 221        git add .gitignore &&
 222        git commit -m "Add .gitignore" &&
 223        >ignored.d/foo &&
 224        echo "!ignored.d/foo" >> .gitignore &&
 225        git stash save --include-untracked &&
 226        test_path_is_missing ignored.d/foo &&
 227        git stash pop &&
 228        test_path_is_file ignored.d/foo
 229'
 230
 231test_expect_success 'stash -u -- <untracked> doesnt print error' '
 232        >untracked &&
 233        git stash push -u -- untracked 2>actual &&
 234        test_path_is_missing untracked &&
 235        test_line_count = 0 actual
 236'
 237
 238test_expect_success 'stash -u -- <untracked> leaves rest of working tree in place' '
 239        >tracked &&
 240        git add tracked &&
 241        >untracked &&
 242        git stash push -u -- untracked &&
 243        test_path_is_missing untracked &&
 244        test_path_is_file tracked
 245'
 246
 247test_expect_success 'stash -u -- <tracked> <untracked> clears changes in both' '
 248        >tracked &&
 249        git add tracked &&
 250        >untracked &&
 251        git stash push -u -- tracked untracked &&
 252        test_path_is_missing tracked &&
 253        test_path_is_missing untracked
 254'
 255
 256test_expect_success 'stash --all -- <ignored> stashes ignored file' '
 257        >ignored.d/bar &&
 258        git stash push --all -- ignored.d/bar &&
 259        test_path_is_missing ignored.d/bar
 260'
 261
 262test_expect_success 'stash --all -- <tracked> <ignored> clears changes in both' '
 263        >tracked &&
 264        git add tracked &&
 265        >ignored.d/bar &&
 266        git stash push --all -- tracked ignored.d/bar &&
 267        test_path_is_missing tracked &&
 268        test_path_is_missing ignored.d/bar
 269'
 270
 271test_expect_success 'stash -u -- <ignored> leaves ignored file alone' '
 272        >ignored.d/bar &&
 273        git stash push -u -- ignored.d/bar &&
 274        test_path_is_file ignored.d/bar
 275'
 276
 277test_expect_success 'stash -u -- <non-existant> shows no changes when there are none' '
 278        git stash push -u -- non-existant >actual &&
 279        echo "No local changes to save" >expect &&
 280        test_i18ncmp expect actual
 281'
 282
 283test_done