]> git.ipfire.org Git - thirdparty/git.git/blobdiff - t/t3701-add-interactive.sh
The 19th batch
[thirdparty/git.git] / t / t3701-add-interactive.sh
index bc55255b0a8da397ad14212035da9c4b9cb8d7d8..718438ffc7d2a8d3482544378749b0d4dfe6313b 100755 (executable)
@@ -4,9 +4,12 @@ test_description='add -i basic tests'
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 
+TEST_PASSES_SANITIZE_LEAK=true
 . ./test-lib.sh
 . "$TEST_DIRECTORY"/lib-terminal.sh
 
 . ./test-lib.sh
 . "$TEST_DIRECTORY"/lib-terminal.sh
 
+SP=" "
+
 diff_cmp () {
        for x
        do
 diff_cmp () {
        for x
        do
@@ -40,19 +43,17 @@ force_color () {
        )
 }
 
        )
 }
 
-test_expect_success 'warn about add.interactive.useBuiltin' '
-       cat >expect <<-\EOF &&
-       warning: the add.interactive.useBuiltin setting has been removed!
-       See its entry in '\''git help config'\'' for details.
-       No changes.
+test_expect_success 'unknown command' '
+       test_when_finished "git reset --hard; rm -f command" &&
+       echo W >command &&
+       git add -N command &&
+       git diff command >expect &&
+       cat >>expect <<-EOF &&
+       (1/1) Stage addition [y,n,q,a,d,e,p,?]? Unknown command ${SQ}W${SQ} (use ${SQ}?${SQ} for help)
+       (1/1) Stage addition [y,n,q,a,d,e,p,?]?$SP
        EOF
        EOF
-
-       for v in = =true =false
-       do
-               git -c "add.interactive.useBuiltin$v" add -p >out 2>actual &&
-               test_must_be_empty out &&
-               test_cmp expect actual || return 1
-       done
+       git add -p -- command <command >actual 2>&1 &&
+       test_cmp expect actual
 '
 
 test_expect_success 'setup (initial)' '
 '
 
 test_expect_success 'setup (initial)' '
@@ -144,6 +145,14 @@ test_expect_success 'revert works (commit)' '
        grep "unchanged *+3/-0 file" output
 '
 
        grep "unchanged *+3/-0 file" output
 '
 
+test_expect_success 'reject multi-key input' '
+       saved=$(git hash-object -w file) &&
+       test_when_finished "git cat-file blob $saved >file" &&
+       echo an extra line >>file &&
+       test_write_lines aa | git add -p >actual &&
+       test_grep "is expected, got ${SQ}aa${SQ}" actual
+'
+
 test_expect_success 'setup expected' '
        cat >expected <<-\EOF
        EOF
 test_expect_success 'setup expected' '
        cat >expected <<-\EOF
        EOF
@@ -231,7 +240,6 @@ test_expect_success 'setup file' '
 '
 
 test_expect_success 'setup patch' '
 '
 
 test_expect_success 'setup patch' '
-       SP=" " &&
        NULL="" &&
        cat >patch <<-EOF
        @@ -1,4 +1,4 @@
        NULL="" &&
        cat >patch <<-EOF
        @@ -1,4 +1,4 @@
@@ -334,13 +342,13 @@ test_expect_success 'different prompts for mode change/deleted' '
 
 test_expect_success 'correct message when there is nothing to do' '
        git reset --hard &&
 
 test_expect_success 'correct message when there is nothing to do' '
        git reset --hard &&
-       git add -p 2>err &&
-       test_grep "No changes" err &&
+       git add -p >out &&
+       test_grep "No changes" out &&
        printf "\\0123" >binary &&
        git add binary &&
        printf "\\0abc" >binary &&
        printf "\\0123" >binary &&
        git add binary &&
        printf "\\0abc" >binary &&
-       git add -p 2>err &&
-       test_grep "Only binary files changed" err
+       git add -p >out &&
+       test_grep "Only binary files changed" out
 '
 
 test_expect_success 'setup again' '
 '
 
 test_expect_success 'setup again' '
@@ -511,7 +519,7 @@ test_expect_success 'split hunk setup' '
        test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
 '
 
        test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
 '
 
-test_expect_success 'goto hunk' '
+test_expect_success 'goto hunk 1 with "g 1"' '
        test_when_finished "git reset" &&
        tr _ " " >expect <<-EOF &&
        (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? + 1:  -1,2 +1,3          +15
        test_when_finished "git reset" &&
        tr _ " " >expect <<-EOF &&
        (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? + 1:  -1,2 +1,3          +15
@@ -527,7 +535,20 @@ test_expect_success 'goto hunk' '
        test_cmp expect actual.trimmed
 '
 
        test_cmp expect actual.trimmed
 '
 
-test_expect_success 'navigate to hunk via regex' '
+test_expect_success 'goto hunk 1 with "g1"' '
+       test_when_finished "git reset" &&
+       tr _ " " >expect <<-EOF &&
+       _10
+       +15
+       _20
+       (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
+       EOF
+       test_write_lines s y g1 | git add -p >actual &&
+       tail -n 4 <actual >actual.trimmed &&
+       test_cmp expect actual.trimmed
+'
+
+test_expect_success 'navigate to hunk via regex /pattern' '
        test_when_finished "git reset" &&
        tr _ " " >expect <<-EOF &&
        (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? @@ -1,2 +1,3 @@
        test_when_finished "git reset" &&
        tr _ " " >expect <<-EOF &&
        (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? @@ -1,2 +1,3 @@
@@ -541,6 +562,67 @@ test_expect_success 'navigate to hunk via regex' '
        test_cmp expect actual.trimmed
 '
 
        test_cmp expect actual.trimmed
 '
 
+test_expect_success 'navigate to hunk via regex / pattern' '
+       test_when_finished "git reset" &&
+       tr _ " " >expect <<-EOF &&
+       _10
+       +15
+       _20
+       (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
+       EOF
+       test_write_lines s y / 1,2 | git add -p >actual &&
+       tail -n 4 <actual >actual.trimmed &&
+       test_cmp expect actual.trimmed
+'
+
+test_expect_success 'print again the hunk' '
+       test_when_finished "git reset" &&
+       tr _ " " >expect <<-EOF &&
+       +15
+        20
+       (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? @@ -1,2 +1,3 @@
+        10
+       +15
+        20
+       (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
+       EOF
+       test_write_lines s y g 1 p | git add -p >actual &&
+       tail -n 7 <actual >actual.trimmed &&
+       test_cmp expect actual.trimmed
+'
+
+test_expect_success TTY 'print again the hunk (PAGER)' '
+       test_when_finished "git reset" &&
+       cat >expect <<-EOF &&
+       <GREEN>+<RESET><GREEN>15<RESET>
+        20<RESET>
+       <BOLD;BLUE>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>PAGER <CYAN>@@ -1,2 +1,3 @@<RESET>
+       PAGER  10<RESET>
+       PAGER <GREEN>+<RESET><GREEN>15<RESET>
+       PAGER  20<RESET>
+       <BOLD;BLUE>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>
+       EOF
+       test_write_lines s y g 1 P |
+       (
+               GIT_PAGER="sed s/^/PAGER\ /" &&
+               export GIT_PAGER &&
+               test_terminal git add -p >actual
+       ) &&
+       tail -n 7 <actual | test_decode_color >actual.trimmed &&
+       test_cmp expect actual.trimmed
+'
+
+test_expect_success TTY 'P handles SIGPIPE when writing to pager' '
+       test_when_finished "rm -f huge_file; git reset" &&
+       printf "\n%2500000s" Y >huge_file &&
+       git add -N huge_file &&
+       test_write_lines P q | (
+               GIT_PAGER="head -n 1" &&
+               export GIT_PAGER &&
+               test_terminal git add -p
+       )
+'
+
 test_expect_success 'split hunk "add -p (edit)"' '
        # Split, say Edit and do nothing.  Then:
        #
 test_expect_success 'split hunk "add -p (edit)"' '
        # Split, say Edit and do nothing.  Then:
        #
@@ -1130,4 +1212,23 @@ test_expect_success 'reset -p with unmerged files' '
        test_must_be_empty staged
 '
 
        test_must_be_empty staged
 '
 
+test_expect_success 'hunk splitting works with diff.suppressBlankEmpty' '
+       test_config diff.suppressBlankEmpty true &&
+       write_script fake-editor.sh <<-\EOF &&
+       tr F G <"$1" >"$1.tmp" &&
+       mv "$1.tmp" "$1"
+       EOF
+
+       test_write_lines a b "" c d  "" e f "" >file &&
+       git add file &&
+       test_write_lines A b "" c D  "" e F "" >file &&
+       (
+               test_set_editor "$(pwd)/fake-editor.sh" &&
+               test_write_lines s n y e q | git add -p file
+       ) &&
+       git cat-file blob :file >actual &&
+       test_write_lines a b "" c D "" e G "" >expect &&
+       test_cmp expect actual
+'
+
 test_done
 test_done