t / t3701-add-interactive.shon commit t7400: clarify submodule update tests (ce8b54d)
   1#!/bin/sh
   2
   3test_description='add -i basic tests'
   4. ./test-lib.sh
   5
   6if ! test_have_prereq PERL; then
   7        say 'skipping git add -i tests, perl not available'
   8        test_done
   9fi
  10
  11test_expect_success 'setup (initial)' '
  12        echo content >file &&
  13        git add file &&
  14        echo more >>file &&
  15        echo lines >>file
  16'
  17test_expect_success 'status works (initial)' '
  18        git add -i </dev/null >output &&
  19        grep "+1/-0 *+2/-0 file" output
  20'
  21cat >expected <<EOF
  22new file mode 100644
  23index 0000000..d95f3ad
  24--- /dev/null
  25+++ b/file
  26@@ -0,0 +1 @@
  27+content
  28EOF
  29test_expect_success 'diff works (initial)' '
  30        (echo d; echo 1) | git add -i >output &&
  31        sed -ne "/new file/,/content/p" <output >diff &&
  32        test_cmp expected diff
  33'
  34test_expect_success 'revert works (initial)' '
  35        git add file &&
  36        (echo r; echo 1) | git add -i &&
  37        git ls-files >output &&
  38        ! grep . output
  39'
  40
  41test_expect_success 'setup (commit)' '
  42        echo baseline >file &&
  43        git add file &&
  44        git commit -m commit &&
  45        echo content >>file &&
  46        git add file &&
  47        echo more >>file &&
  48        echo lines >>file
  49'
  50test_expect_success 'status works (commit)' '
  51        git add -i </dev/null >output &&
  52        grep "+1/-0 *+2/-0 file" output
  53'
  54cat >expected <<EOF
  55index 180b47c..b6f2c08 100644
  56--- a/file
  57+++ b/file
  58@@ -1 +1,2 @@
  59 baseline
  60+content
  61EOF
  62test_expect_success 'diff works (commit)' '
  63        (echo d; echo 1) | git add -i >output &&
  64        sed -ne "/^index/,/content/p" <output >diff &&
  65        test_cmp expected diff
  66'
  67test_expect_success 'revert works (commit)' '
  68        git add file &&
  69        (echo r; echo 1) | git add -i &&
  70        git add -i </dev/null >output &&
  71        grep "unchanged *+3/-0 file" output
  72'
  73
  74cat >expected <<EOF
  75EOF
  76cat >fake_editor.sh <<EOF
  77EOF
  78chmod a+x fake_editor.sh
  79test_set_editor "$(pwd)/fake_editor.sh"
  80test_expect_success 'dummy edit works' '
  81        (echo e; echo a) | git add -p &&
  82        git diff > diff &&
  83        test_cmp expected diff
  84'
  85
  86cat >patch <<EOF
  87@@ -1,1 +1,4 @@
  88 this
  89+patch
  90-doesn't
  91 apply
  92EOF
  93echo "#!$SHELL_PATH" >fake_editor.sh
  94cat >>fake_editor.sh <<\EOF
  95mv -f "$1" oldpatch &&
  96mv -f patch "$1"
  97EOF
  98chmod a+x fake_editor.sh
  99test_set_editor "$(pwd)/fake_editor.sh"
 100test_expect_success 'bad edit rejected' '
 101        git reset &&
 102        (echo e; echo n; echo d) | git add -p >output &&
 103        grep "hunk does not apply" output
 104'
 105
 106cat >patch <<EOF
 107this patch
 108is garbage
 109EOF
 110test_expect_success 'garbage edit rejected' '
 111        git reset &&
 112        (echo e; echo n; echo d) | git add -p >output &&
 113        grep "hunk does not apply" output
 114'
 115
 116cat >patch <<EOF
 117@@ -1,0 +1,0 @@
 118 baseline
 119+content
 120+newcontent
 121+lines
 122EOF
 123cat >expected <<EOF
 124diff --git a/file b/file
 125index b5dd6c9..f910ae9 100644
 126--- a/file
 127+++ b/file
 128@@ -1,4 +1,4 @@
 129 baseline
 130 content
 131-newcontent
 132+more
 133 lines
 134EOF
 135test_expect_success 'real edit works' '
 136        (echo e; echo n; echo d) | git add -p &&
 137        git diff >output &&
 138        test_cmp expected output
 139'
 140
 141test_expect_success 'skip files similarly as commit -a' '
 142        git reset &&
 143        echo file >.gitignore &&
 144        echo changed >file &&
 145        echo y | git add -p file &&
 146        git diff >output &&
 147        git reset &&
 148        git commit -am commit &&
 149        git diff >expected &&
 150        test_cmp expected output &&
 151        git reset --hard HEAD^
 152'
 153rm -f .gitignore
 154
 155if test "$(git config --bool core.filemode)" = false
 156then
 157        say 'skipping filemode tests (filesystem does not properly support modes)'
 158else
 159        test_set_prereq FILEMODE
 160fi
 161
 162test_expect_success FILEMODE 'patch does not affect mode' '
 163        git reset --hard &&
 164        echo content >>file &&
 165        chmod +x file &&
 166        printf "n\\ny\\n" | git add -p &&
 167        git show :file | grep content &&
 168        git diff file | grep "new mode"
 169'
 170
 171test_expect_success FILEMODE 'stage mode but not hunk' '
 172        git reset --hard &&
 173        echo content >>file &&
 174        chmod +x file &&
 175        printf "y\\nn\\n" | git add -p &&
 176        git diff --cached file | grep "new mode" &&
 177        git diff          file | grep "+content"
 178'
 179
 180
 181test_expect_success FILEMODE 'stage mode and hunk' '
 182        git reset --hard &&
 183        echo content >>file &&
 184        chmod +x file &&
 185        printf "y\\ny\\n" | git add -p &&
 186        git diff --cached file | grep "new mode" &&
 187        git diff --cached file | grep "+content" &&
 188        test -z "$(git diff file)"
 189'
 190
 191# end of tests disabled when filemode is not usable
 192
 193test_expect_success 'setup again' '
 194        git reset --hard &&
 195        test_chmod +x file &&
 196        echo content >>file
 197'
 198
 199# Write the patch file with a new line at the top and bottom
 200cat >patch <<EOF
 201index 180b47c..b6f2c08 100644
 202--- a/file
 203+++ b/file
 204@@ -1,2 +1,4 @@
 205+firstline
 206 baseline
 207 content
 208+lastline
 209EOF
 210# Expected output, similar to the patch but w/ diff at the top
 211cat >expected <<EOF
 212diff --git a/file b/file
 213index b6f2c08..61b9053 100755
 214--- a/file
 215+++ b/file
 216@@ -1,2 +1,4 @@
 217+firstline
 218 baseline
 219 content
 220+lastline
 221EOF
 222# Test splitting the first patch, then adding both
 223test_expect_success 'add first line works' '
 224        git commit -am "clear local changes" &&
 225        git apply patch &&
 226        (echo s; echo y; echo y) | git add -p file &&
 227        git diff --cached > diff &&
 228        test_cmp expected diff
 229'
 230
 231cat >expected <<EOF
 232diff --git a/non-empty b/non-empty
 233deleted file mode 100644
 234index d95f3ad..0000000
 235--- a/non-empty
 236+++ /dev/null
 237@@ -1 +0,0 @@
 238-content
 239EOF
 240test_expect_success 'deleting a non-empty file' '
 241        git reset --hard &&
 242        echo content >non-empty &&
 243        git add non-empty &&
 244        git commit -m non-empty &&
 245        rm non-empty &&
 246        echo y | git add -p non-empty &&
 247        git diff --cached >diff &&
 248        test_cmp expected diff
 249'
 250
 251cat >expected <<EOF
 252diff --git a/empty b/empty
 253deleted file mode 100644
 254index e69de29..0000000
 255EOF
 256
 257test_expect_success 'deleting an empty file' '
 258        git reset --hard &&
 259        > empty &&
 260        git add empty &&
 261        git commit -m empty &&
 262        rm empty &&
 263        echo y | git add -p empty &&
 264        git diff --cached >diff &&
 265        test_cmp expected diff
 266'
 267
 268test_done