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