Browse Source
* tr/reset-checkout-patch: stash: simplify defaulting to "save" and reject unknown options Make test case number unique tests: disable interactive hunk selection tests if perl is not available DWIM 'git stash save -p' for 'git stash -p' Implement 'git stash save --patch' Implement 'git checkout --patch' Implement 'git reset --patch' builtin-add: refactor the meat of interactive_add() Add a small patch-mode testing library git-apply--interactive: Refactor patch mode code Make 'git stash -k' a short form for 'git stash save --keep-index'maint

14 changed files with 674 additions and 73 deletions
@ -0,0 +1,41 @@ |
|||||||
|
. ./test-lib.sh |
||||||
|
|
||||||
|
if ! test_have_prereq PERL; then |
||||||
|
say 'skipping --patch tests, perl not available' |
||||||
|
test_done |
||||||
|
fi |
||||||
|
|
||||||
|
set_state () { |
||||||
|
echo "$3" > "$1" && |
||||||
|
git add "$1" && |
||||||
|
echo "$2" > "$1" |
||||||
|
} |
||||||
|
|
||||||
|
save_state () { |
||||||
|
noslash="$(echo "$1" | tr / _)" && |
||||||
|
cat "$1" > _worktree_"$noslash" && |
||||||
|
git show :"$1" > _index_"$noslash" |
||||||
|
} |
||||||
|
|
||||||
|
set_and_save_state () { |
||||||
|
set_state "$@" && |
||||||
|
save_state "$1" |
||||||
|
} |
||||||
|
|
||||||
|
verify_state () { |
||||||
|
test "$(cat "$1")" = "$2" && |
||||||
|
test "$(git show :"$1")" = "$3" |
||||||
|
} |
||||||
|
|
||||||
|
verify_saved_state () { |
||||||
|
noslash="$(echo "$1" | tr / _)" && |
||||||
|
verify_state "$1" "$(cat _worktree_"$noslash")" "$(cat _index_"$noslash")" |
||||||
|
} |
||||||
|
|
||||||
|
save_head () { |
||||||
|
git rev-parse HEAD > _head |
||||||
|
} |
||||||
|
|
||||||
|
verify_saved_head () { |
||||||
|
test "$(cat _head)" = "$(git rev-parse HEAD)" |
||||||
|
} |
@ -0,0 +1,107 @@ |
|||||||
|
#!/bin/sh |
||||||
|
|
||||||
|
test_description='git checkout --patch' |
||||||
|
|
||||||
|
. ./lib-patch-mode.sh |
||||||
|
|
||||||
|
test_expect_success 'setup' ' |
||||||
|
mkdir dir && |
||||||
|
echo parent > dir/foo && |
||||||
|
echo dummy > bar && |
||||||
|
git add bar dir/foo && |
||||||
|
git commit -m initial && |
||||||
|
test_tick && |
||||||
|
test_commit second dir/foo head && |
||||||
|
set_and_save_state bar bar_work bar_index && |
||||||
|
save_head |
||||||
|
' |
||||||
|
|
||||||
|
# note: bar sorts before dir/foo, so the first 'n' is always to skip 'bar' |
||||||
|
|
||||||
|
test_expect_success 'saying "n" does nothing' ' |
||||||
|
set_and_save_state dir/foo work head && |
||||||
|
(echo n; echo n) | git checkout -p && |
||||||
|
verify_saved_state bar && |
||||||
|
verify_saved_state dir/foo |
||||||
|
' |
||||||
|
|
||||||
|
test_expect_success 'git checkout -p' ' |
||||||
|
(echo n; echo y) | git checkout -p && |
||||||
|
verify_saved_state bar && |
||||||
|
verify_state dir/foo head head |
||||||
|
' |
||||||
|
|
||||||
|
test_expect_success 'git checkout -p with staged changes' ' |
||||||
|
set_state dir/foo work index |
||||||
|
(echo n; echo y) | git checkout -p && |
||||||
|
verify_saved_state bar && |
||||||
|
verify_state dir/foo index index |
||||||
|
' |
||||||
|
|
||||||
|
test_expect_success 'git checkout -p HEAD with NO staged changes: abort' ' |
||||||
|
set_and_save_state dir/foo work head && |
||||||
|
(echo n; echo y; echo n) | git checkout -p HEAD && |
||||||
|
verify_saved_state bar && |
||||||
|
verify_saved_state dir/foo |
||||||
|
' |
||||||
|
|
||||||
|
test_expect_success 'git checkout -p HEAD with NO staged changes: apply' ' |
||||||
|
(echo n; echo y; echo y) | git checkout -p HEAD && |
||||||
|
verify_saved_state bar && |
||||||
|
verify_state dir/foo head head |
||||||
|
' |
||||||
|
|
||||||
|
test_expect_success 'git checkout -p HEAD with change already staged' ' |
||||||
|
set_state dir/foo index index |
||||||
|
# the third n is to get out in case it mistakenly does not apply |
||||||
|
(echo n; echo y; echo n) | git checkout -p HEAD && |
||||||
|
verify_saved_state bar && |
||||||
|
verify_state dir/foo head head |
||||||
|
' |
||||||
|
|
||||||
|
test_expect_success 'git checkout -p HEAD^' ' |
||||||
|
# the third n is to get out in case it mistakenly does not apply |
||||||
|
(echo n; echo y; echo n) | git checkout -p HEAD^ && |
||||||
|
verify_saved_state bar && |
||||||
|
verify_state dir/foo parent parent |
||||||
|
' |
||||||
|
|
||||||
|
# The idea in the rest is that bar sorts first, so we always say 'y' |
||||||
|
# first and if the path limiter fails it'll apply to bar instead of |
||||||
|
# dir/foo. There's always an extra 'n' to reject edits to dir/foo in |
||||||
|
# the failure case (and thus get out of the loop). |
||||||
|
|
||||||
|
test_expect_success 'path limiting works: dir' ' |
||||||
|
set_state dir/foo work head && |
||||||
|
(echo y; echo n) | git checkout -p dir && |
||||||
|
verify_saved_state bar && |
||||||
|
verify_state dir/foo head head |
||||||
|
' |
||||||
|
|
||||||
|
test_expect_success 'path limiting works: -- dir' ' |
||||||
|
set_state dir/foo work head && |
||||||
|
(echo y; echo n) | git checkout -p -- dir && |
||||||
|
verify_saved_state bar && |
||||||
|
verify_state dir/foo head head |
||||||
|
' |
||||||
|
|
||||||
|
test_expect_success 'path limiting works: HEAD^ -- dir' ' |
||||||
|
# the third n is to get out in case it mistakenly does not apply |
||||||
|
(echo y; echo n; echo n) | git checkout -p HEAD^ -- dir && |
||||||
|
verify_saved_state bar && |
||||||
|
verify_state dir/foo parent parent |
||||||
|
' |
||||||
|
|
||||||
|
test_expect_success 'path limiting works: foo inside dir' ' |
||||||
|
set_state dir/foo work head && |
||||||
|
# the third n is to get out in case it mistakenly does not apply |
||||||
|
(echo y; echo n; echo n) | (cd dir && git checkout -p foo) && |
||||||
|
verify_saved_state bar && |
||||||
|
verify_state dir/foo head head |
||||||
|
' |
||||||
|
|
||||||
|
test_expect_success 'none of this moved HEAD' ' |
||||||
|
verify_saved_head |
||||||
|
' |
||||||
|
|
||||||
|
test_done |
@ -0,0 +1,55 @@ |
|||||||
|
#!/bin/sh |
||||||
|
|
||||||
|
test_description='git checkout --patch' |
||||||
|
. ./lib-patch-mode.sh |
||||||
|
|
||||||
|
test_expect_success 'setup' ' |
||||||
|
mkdir dir && |
||||||
|
echo parent > dir/foo && |
||||||
|
echo dummy > bar && |
||||||
|
git add bar dir/foo && |
||||||
|
git commit -m initial && |
||||||
|
test_tick && |
||||||
|
test_commit second dir/foo head && |
||||||
|
echo index > dir/foo && |
||||||
|
git add dir/foo && |
||||||
|
set_and_save_state bar bar_work bar_index && |
||||||
|
save_head |
||||||
|
' |
||||||
|
|
||||||
|
# note: bar sorts before dir, so the first 'n' is always to skip 'bar' |
||||||
|
|
||||||
|
test_expect_success 'saying "n" does nothing' ' |
||||||
|
set_state dir/foo work index |
||||||
|
(echo n; echo n) | test_must_fail git stash save -p && |
||||||
|
verify_state dir/foo work index && |
||||||
|
verify_saved_state bar |
||||||
|
' |
||||||
|
|
||||||
|
test_expect_success 'git stash -p' ' |
||||||
|
(echo n; echo y) | git stash save -p && |
||||||
|
verify_state dir/foo head index && |
||||||
|
verify_saved_state bar && |
||||||
|
git reset --hard && |
||||||
|
git stash apply && |
||||||
|
verify_state dir/foo work head && |
||||||
|
verify_state bar dummy dummy |
||||||
|
' |
||||||
|
|
||||||
|
test_expect_success 'git stash -p --no-keep-index' ' |
||||||
|
set_state dir/foo work index && |
||||||
|
set_state bar bar_work bar_index && |
||||||
|
(echo n; echo y) | git stash save -p --no-keep-index && |
||||||
|
verify_state dir/foo head head && |
||||||
|
verify_state bar bar_work dummy && |
||||||
|
git reset --hard && |
||||||
|
git stash apply --index && |
||||||
|
verify_state dir/foo work index && |
||||||
|
verify_state bar dummy bar_index |
||||||
|
' |
||||||
|
|
||||||
|
test_expect_success 'none of this moved HEAD' ' |
||||||
|
verify_saved_head |
||||||
|
' |
||||||
|
|
||||||
|
test_done |
@ -0,0 +1,69 @@ |
|||||||
|
#!/bin/sh |
||||||
|
|
||||||
|
test_description='git reset --patch' |
||||||
|
. ./lib-patch-mode.sh |
||||||
|
|
||||||
|
test_expect_success 'setup' ' |
||||||
|
mkdir dir && |
||||||
|
echo parent > dir/foo && |
||||||
|
echo dummy > bar && |
||||||
|
git add dir && |
||||||
|
git commit -m initial && |
||||||
|
test_tick && |
||||||
|
test_commit second dir/foo head && |
||||||
|
set_and_save_state bar bar_work bar_index && |
||||||
|
save_head |
||||||
|
' |
||||||
|
|
||||||
|
# note: bar sorts before foo, so the first 'n' is always to skip 'bar' |
||||||
|
|
||||||
|
test_expect_success 'saying "n" does nothing' ' |
||||||
|
set_and_save_state dir/foo work work |
||||||
|
(echo n; echo n) | git reset -p && |
||||||
|
verify_saved_state dir/foo && |
||||||
|
verify_saved_state bar |
||||||
|
' |
||||||
|
|
||||||
|
test_expect_success 'git reset -p' ' |
||||||
|
(echo n; echo y) | git reset -p && |
||||||
|
verify_state dir/foo work head && |
||||||
|
verify_saved_state bar |
||||||
|
' |
||||||
|
|
||||||
|
test_expect_success 'git reset -p HEAD^' ' |
||||||
|
(echo n; echo y) | git reset -p HEAD^ && |
||||||
|
verify_state dir/foo work parent && |
||||||
|
verify_saved_state bar |
||||||
|
' |
||||||
|
|
||||||
|
# The idea in the rest is that bar sorts first, so we always say 'y' |
||||||
|
# first and if the path limiter fails it'll apply to bar instead of |
||||||
|
# dir/foo. There's always an extra 'n' to reject edits to dir/foo in |
||||||
|
# the failure case (and thus get out of the loop). |
||||||
|
|
||||||
|
test_expect_success 'git reset -p dir' ' |
||||||
|
set_state dir/foo work work |
||||||
|
(echo y; echo n) | git reset -p dir && |
||||||
|
verify_state dir/foo work head && |
||||||
|
verify_saved_state bar |
||||||
|
' |
||||||
|
|
||||||
|
test_expect_success 'git reset -p -- foo (inside dir)' ' |
||||||
|
set_state dir/foo work work |
||||||
|
(echo y; echo n) | (cd dir && git reset -p -- foo) && |
||||||
|
verify_state dir/foo work head && |
||||||
|
verify_saved_state bar |
||||||
|
' |
||||||
|
|
||||||
|
test_expect_success 'git reset -p HEAD^ -- dir' ' |
||||||
|
(echo y; echo n) | git reset -p HEAD^ -- dir && |
||||||
|
verify_state dir/foo work parent && |
||||||
|
verify_saved_state bar |
||||||
|
' |
||||||
|
|
||||||
|
test_expect_success 'none of this moved HEAD' ' |
||||||
|
verify_saved_head |
||||||
|
' |
||||||
|
|
||||||
|
|
||||||
|
test_done |
Loading…
Reference in new issue