Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='git checkout --patch' |
| 4 | |
| 5 | . ./lib-patch-mode.sh |
| 6 | |
Johannes Schindelin | ed922dc | 2021-11-30 14:14:14 +0000 | [diff] [blame] | 7 | test_expect_success 'setup' ' |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 8 | mkdir dir && |
| 9 | echo parent > dir/foo && |
| 10 | echo dummy > bar && |
| 11 | git add bar dir/foo && |
| 12 | git commit -m initial && |
| 13 | test_tick && |
| 14 | test_commit second dir/foo head && |
| 15 | set_and_save_state bar bar_work bar_index && |
| 16 | save_head |
| 17 | ' |
| 18 | |
| 19 | # note: bar sorts before dir/foo, so the first 'n' is always to skip 'bar' |
| 20 | |
Johannes Schindelin | ed922dc | 2021-11-30 14:14:14 +0000 | [diff] [blame] | 21 | test_expect_success 'saying "n" does nothing' ' |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 22 | set_and_save_state dir/foo work head && |
Eric Sunshine | 0590ff2 | 2018-07-01 20:23:42 -0400 | [diff] [blame] | 23 | test_write_lines n n | git checkout -p && |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 24 | verify_saved_state bar && |
| 25 | verify_saved_state dir/foo |
| 26 | ' |
| 27 | |
Johannes Schindelin | ed922dc | 2021-11-30 14:14:14 +0000 | [diff] [blame] | 28 | test_expect_success 'git checkout -p' ' |
Eric Sunshine | 0590ff2 | 2018-07-01 20:23:42 -0400 | [diff] [blame] | 29 | test_write_lines n y | git checkout -p && |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 30 | verify_saved_state bar && |
| 31 | verify_state dir/foo head head |
| 32 | ' |
| 33 | |
Johannes Schindelin | ed922dc | 2021-11-30 14:14:14 +0000 | [diff] [blame] | 34 | test_expect_success 'git checkout -p with staged changes' ' |
Jonathan Nieder | a48fcd8 | 2010-10-30 20:46:54 -0500 | [diff] [blame] | 35 | set_state dir/foo work index && |
Eric Sunshine | 0590ff2 | 2018-07-01 20:23:42 -0400 | [diff] [blame] | 36 | test_write_lines n y | git checkout -p && |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 37 | verify_saved_state bar && |
| 38 | verify_state dir/foo index index |
| 39 | ' |
| 40 | |
Johannes Schindelin | ed922dc | 2021-11-30 14:14:14 +0000 | [diff] [blame] | 41 | test_expect_success 'git checkout -p HEAD with NO staged changes: abort' ' |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 42 | set_and_save_state dir/foo work head && |
Eric Sunshine | 0590ff2 | 2018-07-01 20:23:42 -0400 | [diff] [blame] | 43 | test_write_lines n y n | git checkout -p HEAD && |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 44 | verify_saved_state bar && |
| 45 | verify_saved_state dir/foo |
| 46 | ' |
| 47 | |
Johannes Schindelin | ed922dc | 2021-11-30 14:14:14 +0000 | [diff] [blame] | 48 | test_expect_success 'git checkout -p HEAD with NO staged changes: apply' ' |
Eric Sunshine | 0590ff2 | 2018-07-01 20:23:42 -0400 | [diff] [blame] | 49 | test_write_lines n y y | git checkout -p HEAD && |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 50 | verify_saved_state bar && |
| 51 | verify_state dir/foo head head |
| 52 | ' |
| 53 | |
Johannes Schindelin | ed922dc | 2021-11-30 14:14:14 +0000 | [diff] [blame] | 54 | test_expect_success 'git checkout -p HEAD with change already staged' ' |
Jonathan Nieder | a814615 | 2010-09-07 03:22:53 -0500 | [diff] [blame] | 55 | set_state dir/foo index index && |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 56 | # the third n is to get out in case it mistakenly does not apply |
Eric Sunshine | 0590ff2 | 2018-07-01 20:23:42 -0400 | [diff] [blame] | 57 | test_write_lines n y n | git checkout -p HEAD && |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 58 | verify_saved_state bar && |
| 59 | verify_state dir/foo head head |
| 60 | ' |
| 61 | |
Johannes Schindelin | ed922dc | 2021-11-30 14:14:14 +0000 | [diff] [blame] | 62 | test_expect_success 'git checkout -p HEAD^...' ' |
Denton Liu | 5602b50 | 2020-10-07 00:56:15 -0700 | [diff] [blame] | 63 | # the third n is to get out in case it mistakenly does not apply |
| 64 | test_write_lines n y n | git checkout -p HEAD^... && |
| 65 | verify_saved_state bar && |
| 66 | verify_state dir/foo parent parent |
| 67 | ' |
| 68 | |
Johannes Schindelin | ed922dc | 2021-11-30 14:14:14 +0000 | [diff] [blame] | 69 | test_expect_success 'git checkout -p HEAD^' ' |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 70 | # the third n is to get out in case it mistakenly does not apply |
Eric Sunshine | 0590ff2 | 2018-07-01 20:23:42 -0400 | [diff] [blame] | 71 | test_write_lines n y n | git checkout -p HEAD^ && |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 72 | verify_saved_state bar && |
| 73 | verify_state dir/foo parent parent |
| 74 | ' |
| 75 | |
Johannes Schindelin | ed922dc | 2021-11-30 14:14:14 +0000 | [diff] [blame] | 76 | test_expect_success 'git checkout -p handles deletion' ' |
Jeff King | e1327ed | 2010-02-22 20:05:44 -0500 | [diff] [blame] | 77 | set_state dir/foo work index && |
| 78 | rm dir/foo && |
Eric Sunshine | 0590ff2 | 2018-07-01 20:23:42 -0400 | [diff] [blame] | 79 | test_write_lines n y | git checkout -p && |
Jeff King | e1327ed | 2010-02-22 20:05:44 -0500 | [diff] [blame] | 80 | verify_saved_state bar && |
| 81 | verify_state dir/foo index index |
| 82 | ' |
| 83 | |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 84 | # The idea in the rest is that bar sorts first, so we always say 'y' |
| 85 | # first and if the path limiter fails it'll apply to bar instead of |
| 86 | # dir/foo. There's always an extra 'n' to reject edits to dir/foo in |
| 87 | # the failure case (and thus get out of the loop). |
| 88 | |
Johannes Schindelin | ed922dc | 2021-11-30 14:14:14 +0000 | [diff] [blame] | 89 | test_expect_success 'path limiting works: dir' ' |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 90 | set_state dir/foo work head && |
Eric Sunshine | 0590ff2 | 2018-07-01 20:23:42 -0400 | [diff] [blame] | 91 | test_write_lines y n | git checkout -p dir && |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 92 | verify_saved_state bar && |
| 93 | verify_state dir/foo head head |
| 94 | ' |
| 95 | |
Johannes Schindelin | ed922dc | 2021-11-30 14:14:14 +0000 | [diff] [blame] | 96 | test_expect_success 'path limiting works: -- dir' ' |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 97 | set_state dir/foo work head && |
Eric Sunshine | 0590ff2 | 2018-07-01 20:23:42 -0400 | [diff] [blame] | 98 | test_write_lines y n | git checkout -p -- dir && |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 99 | verify_saved_state bar && |
| 100 | verify_state dir/foo head head |
| 101 | ' |
| 102 | |
Johannes Schindelin | ed922dc | 2021-11-30 14:14:14 +0000 | [diff] [blame] | 103 | test_expect_success 'path limiting works: HEAD^ -- dir' ' |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 104 | # the third n is to get out in case it mistakenly does not apply |
Eric Sunshine | 0590ff2 | 2018-07-01 20:23:42 -0400 | [diff] [blame] | 105 | test_write_lines y n n | git checkout -p HEAD^ -- dir && |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 106 | verify_saved_state bar && |
| 107 | verify_state dir/foo parent parent |
| 108 | ' |
| 109 | |
Johannes Schindelin | ed922dc | 2021-11-30 14:14:14 +0000 | [diff] [blame] | 110 | test_expect_success 'path limiting works: foo inside dir' ' |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 111 | set_state dir/foo work head && |
| 112 | # the third n is to get out in case it mistakenly does not apply |
Eric Sunshine | 0590ff2 | 2018-07-01 20:23:42 -0400 | [diff] [blame] | 113 | test_write_lines y n n | (cd dir && git checkout -p foo) && |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 114 | verify_saved_state bar && |
| 115 | verify_state dir/foo head head |
| 116 | ' |
| 117 | |
Johannes Schindelin | ed922dc | 2021-11-30 14:14:14 +0000 | [diff] [blame] | 118 | test_expect_success 'none of this moved HEAD' ' |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 119 | verify_saved_head |
| 120 | ' |
| 121 | |
Johannes Schindelin | ed922dc | 2021-11-30 14:14:14 +0000 | [diff] [blame] | 122 | test_expect_success 'empty tree can be handled' ' |
Johannes Schindelin | 5c29f19 | 2020-12-19 14:55:59 +0000 | [diff] [blame] | 123 | test_when_finished "git reset --hard" && |
| 124 | git checkout -p $(test_oid empty_tree) -- |
| 125 | ' |
| 126 | |
Thomas Rast | 4f35365 | 2009-08-15 13:48:30 +0200 | [diff] [blame] | 127 | test_done |