]> git.ipfire.org Git - thirdparty/git.git/blobdiff - t/t3701-add-interactive.sh
Merge branch 'en/ort-perf-batch-9'
[thirdparty/git.git] / t / t3701-add-interactive.sh
index 5bae6e50f1f3e76c1615bf5e3031e99f92d619b9..207714655f283971f791b4ab2426aa0d7b910080 100755 (executable)
@@ -1,6 +1,9 @@
 #!/bin/sh
 
 test_description='add -i basic tests'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
 . ./test-lib.sh
 . "$TEST_DIRECTORY"/lib-terminal.sh
 
@@ -31,7 +34,16 @@ diff_cmp () {
 # indicates a dumb terminal, so we set that variable, too.
 
 force_color () {
-       env GIT_PAGER_IN_USE=true TERM=vt100 "$@"
+       # The first element of $@ may be a shell function, as a result POSIX
+       # does not guarantee that "one-shot assignment" will not persist after
+       # the function call. Thus, we prevent these variables from escaping
+       # this function's context with this subshell.
+       (
+               GIT_PAGER_IN_USE=true &&
+               TERM=vt100 &&
+               export GIT_PAGER_IN_USE TERM &&
+               "$@"
+       )
 }
 
 test_expect_success 'setup (initial)' '
@@ -358,7 +370,7 @@ test_expect_success 'setup expected' '
 '
 
 # Test splitting the first patch, then adding both
-test_expect_success C_LOCALE_OUTPUT 'add first line works' '
+test_expect_success 'add first line works' '
        git commit -am "clear local changes" &&
        git apply patch &&
        printf "%s\n" s y y | git add -p file 2>error |
@@ -412,6 +424,25 @@ test_expect_success 'deleting an empty file' '
        diff_cmp expected diff
 '
 
+test_expect_success 'adding an empty file' '
+       git init added &&
+       (
+               cd added &&
+               test_commit initial &&
+               >empty &&
+               git add empty &&
+               test_tick &&
+               git commit -m empty &&
+               git tag added-file &&
+               git reset --hard HEAD^ &&
+               test_path_is_missing empty &&
+
+               echo y | git checkout -p added-file -- >actual &&
+               test_path_is_file empty &&
+               test_i18ngrep "Apply addition to index and worktree" actual
+       )
+'
+
 test_expect_success 'split hunk setup' '
        git reset --hard &&
        test_write_lines 10 20 30 40 50 60 >test &&
@@ -521,8 +552,8 @@ test_expect_success 'patch mode ignores unmerged entries' '
        test_commit non-conflict &&
        git checkout -b side &&
        test_commit side conflict.t &&
-       git checkout master &&
-       test_commit master conflict.t &&
+       git checkout main &&
+       test_commit main conflict.t &&
        test_must_fail git merge side &&
        echo changed >non-conflict.t &&
        echo y | git add -p >output &&
@@ -541,18 +572,110 @@ test_expect_success 'patch mode ignores unmerged entries' '
        diff_cmp expected diff
 '
 
+test_expect_success 'index is refreshed after applying patch' '
+       git reset --hard &&
+       echo content >test &&
+       printf y | git add -p &&
+       git diff-files --exit-code
+'
+
 test_expect_success 'diffs can be colorized' '
        git reset --hard &&
 
        echo content >test &&
        printf y >y &&
        force_color git add -p >output 2>&1 <y &&
+       git diff-files --exit-code &&
 
        # We do not want to depend on the exact coloring scheme
        # git uses for diffs, so just check that we saw some kind of color.
        grep "$(printf "\\033")" output
 '
 
+test_expect_success 'colors can be overridden' '
+       git reset --hard &&
+       test_when_finished "git rm -f color-test" &&
+       test_write_lines context old more-context >color-test &&
+       git add color-test &&
+       test_write_lines context new more-context another-one >color-test &&
+
+       echo trigger an error message >input &&
+       force_color git \
+               -c color.interactive.error=blue \
+               add -i 2>err.raw <input &&
+       test_decode_color <err.raw >err &&
+       grep "<BLUE>Huh (trigger)?<RESET>" err &&
+
+       test_write_lines help quit >input &&
+       force_color git \
+               -c color.interactive.header=red \
+               -c color.interactive.help=green \
+               -c color.interactive.prompt=yellow \
+               add -i >actual.raw <input &&
+       test_decode_color <actual.raw >actual &&
+       cat >expect <<-\EOF &&
+       <RED>           staged     unstaged path<RESET>
+         1:        +3/-0        +2/-1 color-test
+
+       <RED>*** Commands ***<RESET>
+         1: <YELLOW>s<RESET>tatus        2: <YELLOW>u<RESET>pdate        3: <YELLOW>r<RESET>evert        4: <YELLOW>a<RESET>dd untracked
+         5: <YELLOW>p<RESET>atch         6: <YELLOW>d<RESET>iff          7: <YELLOW>q<RESET>uit          8: <YELLOW>h<RESET>elp
+       <YELLOW>What now<RESET>> <GREEN>status        - show paths with changes<RESET>
+       <GREEN>update        - add working tree state to the staged set of changes<RESET>
+       <GREEN>revert        - revert staged set of changes back to the HEAD version<RESET>
+       <GREEN>patch         - pick hunks and update selectively<RESET>
+       <GREEN>diff          - view diff between HEAD and index<RESET>
+       <GREEN>add untracked - add contents of untracked files to the staged set of changes<RESET>
+       <RED>*** Commands ***<RESET>
+         1: <YELLOW>s<RESET>tatus        2: <YELLOW>u<RESET>pdate        3: <YELLOW>r<RESET>evert        4: <YELLOW>a<RESET>dd untracked
+         5: <YELLOW>p<RESET>atch         6: <YELLOW>d<RESET>iff          7: <YELLOW>q<RESET>uit          8: <YELLOW>h<RESET>elp
+       <YELLOW>What now<RESET>> Bye.
+       EOF
+       test_cmp expect actual &&
+
+       : exercise recolor_hunk by editing and then look at the hunk again &&
+       test_write_lines s e K q >input &&
+       force_color git \
+               -c color.interactive.prompt=yellow \
+               -c color.diff.meta=italic \
+               -c color.diff.frag=magenta \
+               -c color.diff.context=cyan \
+               -c color.diff.old=bold \
+               -c color.diff.new=blue \
+               -c core.editor=touch \
+               add -p >actual.raw <input &&
+       test_decode_color <actual.raw >actual.decoded &&
+       sed "s/index [0-9a-f]*\\.\\.[0-9a-f]* 100644/<INDEX-LINE>/" <actual.decoded >actual &&
+       cat >expect <<-\EOF &&
+       <ITALIC>diff --git a/color-test b/color-test<RESET>
+       <ITALIC><INDEX-LINE><RESET>
+       <ITALIC>--- a/color-test<RESET>
+       <ITALIC>+++ b/color-test<RESET>
+       <MAGENTA>@@ -1,3 +1,4 @@<RESET>
+       <CYAN> context<RESET>
+       <BOLD>-old<RESET>
+       <BLUE>+<RESET><BLUE>new<RESET>
+       <CYAN> more-context<RESET>
+       <BLUE>+<RESET><BLUE>another-one<RESET>
+       <YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
+       <MAGENTA>@@ -1,3 +1,3 @@<RESET>
+       <CYAN> context<RESET>
+       <BOLD>-old<RESET>
+       <BLUE>+<RESET><BLUE>new<RESET>
+       <CYAN> more-context<RESET>
+       <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
+       <CYAN> more-context<RESET>
+       <BLUE>+<RESET><BLUE>another-one<RESET>
+       <YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
+       <CYAN> context<RESET>
+       <BOLD>-old<RESET>
+       <BLUE>+new<RESET>
+       <CYAN> more-context<RESET>
+       <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? <RESET>
+       EOF
+       test_cmp expect actual
+'
+
 test_expect_success 'colorized diffs respect diff.wsErrorHighlight' '
        git reset --hard &&
 
@@ -585,7 +708,7 @@ test_expect_success 'detect bogus diffFilter output' '
        echo content >test &&
        test_config interactive.diffFilter "sed 1d" &&
        printf y >y &&
-       test_must_fail force_color git add -p <y
+       force_color test_must_fail git add -p <y
 '
 
 test_expect_success 'diff.algorithm is passed to `git diff-files`' '
@@ -729,6 +852,12 @@ test_expect_success 'setup different kinds of dirty submodules' '
        cat >expected <<-\EOF &&
        dirty-both-ways
        dirty-head
+       EOF
+       test_cmp expected actual &&
+       git -C for-submodules diff-files --name-only --ignore-submodules=none >actual &&
+       cat >expected <<-\EOF &&
+       dirty-both-ways
+       dirty-head
        dirty-otherwise
        EOF
        test_cmp expected actual &&
@@ -780,12 +909,50 @@ test_expect_success 'add -p patch editing works with pathological context lines'
 test_expect_success 'checkout -p works with pathological context lines' '
        test_write_lines a a a a a a >a &&
        git add a &&
-       test_write_lines a b a b a b a b a b a > a&&
+       test_write_lines a b a b a b a b a b a >&&
        test_write_lines s n n y q | git checkout -p &&
        test_write_lines a b a b a a b a b a >expect &&
        test_cmp expect a
 '
 
+# This should be called from a subshell as it sets a temporary editor
+setup_new_file() {
+       write_script new-file-editor.sh <<-\EOF &&
+       sed /^#/d "$1" >patch &&
+       sed /^+c/d patch >"$1"
+       EOF
+       test_set_editor "$(pwd)/new-file-editor.sh" &&
+       test_write_lines a b c d e f >new-file &&
+       test_write_lines a b d e f >new-file-expect &&
+       test_write_lines "@@ -0,0 +1,6 @@" +a +b +c +d +e +f >patch-expect
+}
+
+test_expect_success 'add -N followed by add -p patch editing' '
+       git reset --hard &&
+       (
+               setup_new_file &&
+               git add -N new-file &&
+               test_write_lines e n q | git add -p &&
+               git cat-file blob :new-file >actual &&
+               test_cmp new-file-expect actual &&
+               test_cmp patch-expect patch
+       )
+'
+
+test_expect_success 'checkout -p patch editing of added file' '
+       git reset --hard &&
+       (
+               setup_new_file &&
+               git add new-file &&
+               git commit -m "add new file" &&
+               git rm new-file &&
+               git commit -m "remove new file" &&
+               test_write_lines e n q | git checkout -p HEAD^ &&
+               test_cmp new-file-expect new-file &&
+               test_cmp patch-expect patch
+       )
+'
+
 test_expect_success 'show help from add--helper' '
        git reset --hard &&
        cat >expect <<-EOF &&
@@ -807,7 +974,7 @@ test_expect_success 'show help from add--helper' '
        EOF
        test_write_lines h | force_color git add -i >actual.colored &&
        test_decode_color <actual.colored >actual &&
-       test_i18ncmp expect actual
+       test_cmp expect actual
 '
 
 test_done