chainlint: let here-doc and multi-line string commence on same line
[gitweb.git] / t / t3701-add-interactive.sh
index f818c532dac640d72300a76ea5476f32a36c7dad..609fbfdc317137e420fab48e394f0095b0a1df69 100755 (executable)
@@ -46,13 +46,13 @@ test_expect_success 'setup expected' '
 '
 
 test_expect_success 'diff works (initial)' '
-       (echo d; echo 1) | git add -i >output &&
+       test_write_lines d 1 | git add -i >output &&
        sed -ne "/new file/,/content/p" <output >diff &&
        diff_cmp expected diff
 '
 test_expect_success 'revert works (initial)' '
        git add file &&
-       (echo r; echo 1) | git add -i &&
+       test_write_lines r 1 | git add -i &&
        git ls-files >output &&
        ! grep . output
 '
@@ -83,13 +83,13 @@ test_expect_success 'setup expected' '
 '
 
 test_expect_success 'diff works (commit)' '
-       (echo d; echo 1) | git add -i >output &&
+       test_write_lines d 1 | git add -i >output &&
        sed -ne "/^index/,/content/p" <output >diff &&
        diff_cmp expected diff
 '
 test_expect_success 'revert works (commit)' '
        git add file &&
-       (echo r; echo 1) | git add -i &&
+       test_write_lines r 1 | git add -i &&
        git add -i </dev/null >output &&
        grep "unchanged *+3/-0 file" output
 '
@@ -102,7 +102,7 @@ test_expect_success 'setup expected' '
 
 test_expect_success 'dummy edit works' '
        test_set_editor : &&
-       (echo e; echo a) | git add -p &&
+       test_write_lines e a | git add -p &&
        git diff > diff &&
        diff_cmp expected diff
 '
@@ -127,7 +127,7 @@ test_expect_success 'setup fake editor' '
 
 test_expect_success 'bad edit rejected' '
        git reset &&
-       (echo e; echo n; echo d) | git add -p >output &&
+       test_write_lines e n d | git add -p >output &&
        grep "hunk does not apply" output
 '
 
@@ -140,7 +140,7 @@ test_expect_success 'setup patch' '
 
 test_expect_success 'garbage edit rejected' '
        git reset &&
-       (echo e; echo n; echo d) | git add -p >output &&
+       test_write_lines e n d | git add -p >output &&
        grep "hunk does not apply" output
 '
 
@@ -170,7 +170,50 @@ test_expect_success 'setup expected' '
 '
 
 test_expect_success 'real edit works' '
-       (echo e; echo n; echo d) | git add -p &&
+       test_write_lines e n d | git add -p &&
+       git diff >output &&
+       diff_cmp expected output
+'
+
+test_expect_success 'setup file' '
+       test_write_lines a "" b "" c >file &&
+       git add file &&
+       test_write_lines a "" d "" c >file
+'
+
+test_expect_success 'setup patch' '
+       SP=" " &&
+       NULL="" &&
+       cat >patch <<-EOF
+       @@ -1,4 +1,4 @@
+        a
+       $NULL
+       -b
+       +f
+       $SP
+       c
+       EOF
+'
+
+test_expect_success 'setup expected' '
+       cat >expected <<-EOF
+       diff --git a/file b/file
+       index b5dd6c9..f910ae9 100644
+       --- a/file
+       +++ b/file
+       @@ -1,5 +1,5 @@
+        a
+       $SP
+       -f
+       +d
+       $SP
+        c
+       EOF
+'
+
+test_expect_success 'edit can strip spaces from empty context lines' '
+       test_write_lines e n q | git add -p 2>error &&
+       test_must_be_empty error &&
        git diff >output &&
        diff_cmp expected output
 '
@@ -237,14 +280,15 @@ test_expect_success 'setup patch' '
         baseline
         content
        +lastline
+       \ No newline at end of file
        EOF
 '
 
-# Expected output, similar to the patch but w/ diff at the top
+# Expected output, diff is similar to the patch but w/ diff at the top
 test_expect_success 'setup expected' '
-       cat >expected <<-\EOF
-       diff --git a/file b/file
-       index b6f2c08..61b9053 100755
+       echo diff --git a/file b/file >expected &&
+       cat patch |sed "/^index/s/ 100644/ 100755/" >>expected &&
+       cat >expected-output <<-\EOF
        --- a/file
        +++ b/file
        @@ -1,2 +1,4 @@
@@ -252,16 +296,30 @@ test_expect_success 'setup expected' '
         baseline
         content
        +lastline
+       \ No newline at end of file
+       @@ -1,2 +1,3 @@
+       +firstline
+        baseline
+        content
+       @@ -1,2 +2,3 @@
+        baseline
+        content
+       +lastline
+       \ No newline at end of file
        EOF
 '
 
 # Test splitting the first patch, then adding both
-test_expect_success 'add first line works' '
+test_expect_success C_LOCALE_OUTPUT 'add first line works' '
        git commit -am "clear local changes" &&
        git apply patch &&
-       (echo s; echo y; echo y) | git add -p file &&
-       git diff --cached > diff &&
-       diff_cmp expected diff
+       printf "%s\n" s y y | git add -p file 2>error |
+               sed -n -e "s/^Stage this hunk[^@]*\(@@ .*\)/\1/" \
+                      -e "/^[-+@ \\\\]"/p  >output &&
+       test_must_be_empty error &&
+       git diff --cached >diff &&
+       diff_cmp expected diff &&
+       test_cmp expected-output output
 '
 
 test_expect_success 'setup expected' '
@@ -382,6 +440,26 @@ test_expect_success TTY 'diffs can be colorized' '
        grep "$(printf "\\033")" output
 '
 
+test_expect_success TTY 'diffFilter filters diff' '
+       git reset --hard &&
+
+       echo content >test &&
+       test_config interactive.diffFilter "sed s/^/foo:/" &&
+       printf y | test_terminal git add -p >output 2>&1 &&
+
+       # avoid depending on the exact coloring or content of the prompts,
+       # and just make sure we saw our diff prefixed
+       grep foo:.*content output
+'
+
+test_expect_success TTY 'detect bogus diffFilter output' '
+       git reset --hard &&
+
+       echo content >test &&
+       test_config interactive.diffFilter "echo too-short" &&
+       printf y | test_must_fail test_terminal git add -p
+'
+
 test_expect_success 'patch-mode via -i prompts for files' '
        git reset --hard &&
 
@@ -483,4 +561,82 @@ test_expect_success 'add -p works even with color.ui=always' '
        test_cmp expect actual
 '
 
+test_expect_success 'setup different kinds of dirty submodules' '
+       test_create_repo for-submodules &&
+       (
+               cd for-submodules &&
+               test_commit initial &&
+               test_create_repo dirty-head &&
+               (
+                       cd dirty-head &&
+                       test_commit initial
+               ) &&
+               cp -R dirty-head dirty-otherwise &&
+               cp -R dirty-head dirty-both-ways &&
+               git add dirty-head &&
+               git add dirty-otherwise dirty-both-ways &&
+               git commit -m initial &&
+
+               cd dirty-head &&
+               test_commit updated &&
+               cd ../dirty-both-ways &&
+               test_commit updated &&
+               echo dirty >>initial &&
+               : >untracked &&
+               cd ../dirty-otherwise &&
+               echo dirty >>initial &&
+               : >untracked
+       ) &&
+       git -C for-submodules diff-files --name-only >actual &&
+       cat >expected <<-\EOF &&
+       dirty-both-ways
+       dirty-head
+       dirty-otherwise
+       EOF
+       test_cmp expected actual &&
+       git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
+       cat >expected <<-\EOF &&
+       dirty-both-ways
+       dirty-head
+       EOF
+       test_cmp expected actual
+'
+
+test_expect_success 'status ignores dirty submodules (except HEAD)' '
+       git -C for-submodules add -i </dev/null >output &&
+       grep dirty-head output &&
+       grep dirty-both-ways output &&
+       ! grep dirty-otherwise output
+'
+
+test_expect_success 'set up pathological context' '
+       git reset --hard &&
+       test_write_lines a a a a a a a a a a a >a &&
+       git add a &&
+       git commit -m a &&
+       test_write_lines c b a a a a a a a b a a a a >a &&
+       test_write_lines     a a a a a a a b a a a a >expected-1 &&
+       test_write_lines   b a a a a a a a b a a a a >expected-2 &&
+       # check editing can cope with missing header and deleted context lines
+       # as well as changes to other lines
+       test_write_lines +b " a" >patch
+'
+
+test_expect_success 'add -p works with pathological context lines' '
+       git reset &&
+       printf "%s\n" n y |
+       git add -p &&
+       git cat-file blob :a >actual &&
+       test_cmp expected-1 actual
+'
+
+test_expect_success 'add -p patch editing works with pathological context lines' '
+       git reset &&
+       # n q q below is in case edit fails
+       printf "%s\n" e y    n q q |
+       git add -p &&
+       git cat-file blob :a >actual &&
+       test_cmp expected-2 actual
+'
+
 test_done