summaryrefslogtreecommitdiff
path: root/t/t3701-add-interactive.sh
diff options
context:
space:
mode:
Diffstat (limited to 't/t3701-add-interactive.sh')
-rwxr-xr-xt/t3701-add-interactive.sh386
1 files changed, 353 insertions, 33 deletions
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index b3d8bb7..bc55255 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -1,15 +1,12 @@
#!/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
-if ! test_have_prereq PERL
-then
- skip_all='skipping add -i tests, perl not available'
- test_done
-fi
-
diff_cmp () {
for x
do
@@ -31,9 +28,33 @@ 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 '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.
+ 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
+'
+
test_expect_success 'setup (initial)' '
echo content >file &&
git add file &&
@@ -91,6 +112,15 @@ test_expect_success 'status works (commit)' '
grep "+1/-0 *+2/-0 file" output
'
+test_expect_success 'update can stage deletions' '
+ >to-delete &&
+ git add to-delete &&
+ rm to-delete &&
+ test_write_lines u t "" | git add -i &&
+ git ls-files to-delete >output &&
+ test_must_be_empty output
+'
+
test_expect_success 'setup expected' '
cat >expected <<-\EOF
index 180b47c..b6f2c08 100644
@@ -275,9 +305,11 @@ test_expect_success FILEMODE 'stage mode and hunk' '
echo content >>file &&
chmod +x file &&
printf "y\\ny\\n" | git add -p &&
- git diff --cached file | grep "new mode" &&
- git diff --cached file | grep "+content" &&
- test -z "$(git diff file)"
+ git diff --cached file >out &&
+ grep "new mode" out &&
+ grep "+content" out &&
+ git diff file >out &&
+ test_must_be_empty out
'
# end of tests disabled when filemode is not usable
@@ -293,9 +325,9 @@ test_expect_success 'different prompts for mode change/deleted' '
git -c core.filemode=true add -p >actual &&
sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
cat >expect <<-\EOF &&
- (1/1) Stage deletion [y,n,q,a,d,?]?
- (1/2) Stage mode change [y,n,q,a,d,j,J,g,/,?]?
- (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]?
+ (1/1) Stage deletion [y,n,q,a,d,p,?]?
+ (1/2) Stage mode change [y,n,q,a,d,j,J,g,/,p,?]?
+ (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]?
EOF
test_cmp expect actual.filtered
'
@@ -303,18 +335,20 @@ test_expect_success 'different prompts for mode change/deleted' '
test_expect_success 'correct message when there is nothing to do' '
git reset --hard &&
git add -p 2>err &&
- test_i18ngrep "No changes" err &&
+ test_grep "No changes" err &&
printf "\\0123" >binary &&
git add binary &&
printf "\\0abc" >binary &&
git add -p 2>err &&
- test_i18ngrep "Only binary files changed" err
+ test_grep "Only binary files changed" err
'
test_expect_success 'setup again' '
git reset --hard &&
test_chmod +x file &&
- echo content >>file
+ echo content >>file &&
+ test_write_lines A B C D>file2 &&
+ git add file2
'
# Write the patch file with a new line at the top and bottom
@@ -329,13 +363,27 @@ test_expect_success 'setup patch' '
content
+lastline
\ No newline at end of file
+ diff --git a/file2 b/file2
+ index 8422d40..35b930a 100644
+ --- a/file2
+ +++ b/file2
+ @@ -1,4 +1,5 @@
+ -A
+ +Z
+ B
+ +Y
+ C
+ -D
+ +X
EOF
'
# Expected output, diff is similar to the patch but w/ diff at the top
test_expect_success 'setup expected' '
echo diff --git a/file b/file >expected &&
- cat patch |sed "/^index/s/ 100644/ 100755/" >>expected &&
+ sed -e "/^index 180b47c/s/ 100644/ 100755/" \
+ -e /1,5/s//1,4/ \
+ -e /Y/d patch >>expected &&
cat >expected-output <<-\EOF
--- a/file
+++ b/file
@@ -354,16 +402,38 @@ test_expect_success 'setup expected' '
content
+lastline
\ No newline at end of file
+ --- a/file2
+ +++ b/file2
+ @@ -1,4 +1,5 @@
+ -A
+ +Z
+ B
+ +Y
+ C
+ -D
+ +X
+ @@ -1,2 +1,2 @@
+ -A
+ +Z
+ B
+ @@ -2,2 +2,3 @@
+ B
+ +Y
+ C
+ @@ -3,2 +4,2 @@
+ C
+ -D
+ +X
EOF
'
# 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 |
- sed -n -e "s/^([1-2]\/[1-2]) Stage this hunk[^@]*\(@@ .*\)/\1/" \
- -e "/^[-+@ \\\\]"/p >output &&
+ test_write_lines s y y s y n y | git add -p 2>error >raw-output &&
+ sed -n -e "s/^([1-9]\/[1-9]) Stage this hunk[^@]*\(@@ .*\)/\1/" \
+ -e "/^[-+@ \\\\]"/p raw-output >output &&
test_must_be_empty error &&
git diff --cached >diff &&
diff_cmp expected diff &&
@@ -412,6 +482,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_grep "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 &&
@@ -425,13 +514,13 @@ test_expect_success 'split hunk setup' '
test_expect_success 'goto hunk' '
test_when_finished "git reset" &&
tr _ " " >expect <<-EOF &&
- (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? + 1: -1,2 +1,3 +15
+ (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? + 1: -1,2 +1,3 +15
_ 2: -2,4 +3,8 +21
go to which hunk? @@ -1,2 +1,3 @@
_10
+15
_20
- (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?_
+ (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
EOF
test_write_lines s y g 1 | git add -p >actual &&
tail -n 7 <actual >actual.trimmed &&
@@ -441,11 +530,11 @@ test_expect_success 'goto hunk' '
test_expect_success 'navigate to hunk via regex' '
test_when_finished "git reset" &&
tr _ " " >expect <<-EOF &&
- (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? @@ -1,2 +1,3 @@
+ (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? @@ -1,2 +1,3 @@
_10
+15
_20
- (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?_
+ (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 5 <actual >actual.trimmed &&
@@ -469,7 +558,7 @@ test_expect_success 'split hunk "add -p (edit)"' '
! grep "^+15" actual
'
-test_expect_failure 'split hunk "add -p (no, yes, edit)"' '
+test_expect_success 'split hunk "add -p (no, yes, edit)"' '
test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
git reset &&
# test sequence is s(plit), n(o), y(es), e(dit)
@@ -493,7 +582,7 @@ test_expect_success 'split hunk with incomplete line at end' '
test_must_fail git grep --cached before
'
-test_expect_failure 'edit, adding lines to the first hunk' '
+test_expect_success 'edit, adding lines to the first hunk' '
test_write_lines 10 11 20 30 40 50 51 60 >test &&
git reset &&
tr _ " " >patch <<-EOF &&
@@ -521,8 +610,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 +630,148 @@ 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,p,?]? <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,p,?]? <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,p,?]? <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,p,?]? <RESET>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'brackets appear without color' '
+ git reset --hard &&
+ test_when_finished "git rm -f bracket-test" &&
+ test_write_lines context old more-context >bracket-test &&
+ git add bracket-test &&
+ test_write_lines context new more-context another-one >bracket-test &&
+
+ test_write_lines quit >input &&
+ git add -i >actual <input &&
+
+ sed "s/^|//" >expect <<-\EOF &&
+ | staged unstaged path
+ | 1: +3/-0 +2/-1 bracket-test
+ |
+ |*** Commands ***
+ | 1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
+ | 5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
+ |What now> Bye.
+ EOF
+
+ test_cmp expect actual
+'
+
+test_expect_success 'colors can be skipped with color.ui=false' '
+ 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 &&
+
+ test_write_lines help quit >input &&
+ force_color git \
+ -c color.ui=false \
+ add -i >actual.raw <input &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp actual.raw actual
+'
+
test_expect_success 'colorized diffs respect diff.wsErrorHighlight' '
git reset --hard &&
@@ -583,9 +802,33 @@ test_expect_success 'detect bogus diffFilter output' '
git reset --hard &&
echo content >test &&
- test_config interactive.diffFilter "sed 1d" &&
+ test_config interactive.diffFilter "sed 6d" &&
printf y >y &&
- test_must_fail force_color git add -p <y
+ force_color test_must_fail git add -p <y >output 2>&1 &&
+ grep "mismatched output" output
+'
+
+test_expect_success 'handle iffy colored hunk headers' '
+ git reset --hard &&
+
+ echo content >test &&
+ printf n >n &&
+ force_color git -c interactive.diffFilter="sed s/.*@@.*/XX/" \
+ add -p >output 2>&1 <n &&
+ grep "^XX$" output
+'
+
+test_expect_success 'handle very large filtered diff' '
+ git reset --hard &&
+ # The specific number here is not important, but it must
+ # be large enough that the output of "git diff --color"
+ # fills up the pipe buffer. 10,000 results in ~200k of
+ # colored output.
+ test_seq 10000 >test &&
+ test_config interactive.diffFilter cat &&
+ printf y >y &&
+ force_color git add -p >output 2>&1 <y &&
+ git diff-files --exit-code -- test
'
test_expect_success 'diff.algorithm is passed to `git diff-files`' '
@@ -595,7 +838,7 @@ test_expect_success 'diff.algorithm is passed to `git diff-files`' '
git add file &&
echo changed >file &&
test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
- test_i18ngrep "error: option diff-algorithm accepts " err
+ test_grep "error: option diff-algorithm accepts " err
'
test_expect_success 'patch-mode via -i prompts for files' '
@@ -729,6 +972,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 &&
@@ -747,6 +996,18 @@ test_expect_success 'status ignores dirty submodules (except HEAD)' '
! grep dirty-otherwise output
'
+test_expect_success 'handle submodules' '
+ echo 123 >>for-submodules/dirty-otherwise/initial.t &&
+
+ force_color git -C for-submodules add -p dirty-otherwise >output 2>&1 &&
+ grep "No changes" output &&
+
+ force_color git -C for-submodules add -p dirty-head >output 2>&1 <y &&
+ git -C for-submodules ls-files --stage dirty-head >actual &&
+ rev="$(git -C for-submodules/dirty-head rev-parse HEAD)" &&
+ grep "$rev" actual
+'
+
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 &&
@@ -786,6 +1047,44 @@ test_expect_success 'checkout -p works with pathological context lines' '
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 +1106,28 @@ 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_expect_success 'reset -p with unmerged files' '
+ test_when_finished "git checkout --force main" &&
+ test_commit one conflict &&
+ git checkout -B side HEAD^ &&
+ test_commit two conflict &&
+ test_must_fail git merge one &&
+
+ # this is a noop with only an unmerged entry
+ git reset -p &&
+
+ # add files that sort before and after unmerged entry
+ echo a >a &&
+ echo z >z &&
+ git add a z &&
+
+ # confirm that we can reset those files
+ printf "%s\n" y y | git reset -p &&
+ git diff-index --cached --diff-filter=u HEAD >staged &&
+ test_must_be_empty staged
'
test_done