| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='git checkout --patch' |
| 4 | |
| 5 | . ./lib-patch-mode.sh |
| 6 | |
| 7 | test_expect_success 'setup' ' |
| 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 | |
| 21 | test_expect_success 'saying "n" does nothing' ' |
| 22 | set_and_save_state dir/foo work head && |
| 23 | test_write_lines n n | git checkout -p && |
| 24 | verify_saved_state bar && |
| 25 | verify_saved_state dir/foo |
| 26 | ' |
| 27 | |
| 28 | test_expect_success 'git checkout -p' ' |
| 29 | test_write_lines n y | git checkout -p && |
| 30 | verify_saved_state bar && |
| 31 | verify_state dir/foo head head |
| 32 | ' |
| 33 | |
| 34 | test_expect_success 'git checkout -p with staged changes' ' |
| 35 | set_state dir/foo work index && |
| 36 | test_write_lines n y | git checkout -p && |
| 37 | verify_saved_state bar && |
| 38 | verify_state dir/foo index index |
| 39 | ' |
| 40 | |
| 41 | for opt in "HEAD" "@" |
| 42 | do |
| 43 | test_expect_success "git checkout -p $opt with NO staged changes: abort" ' |
| 44 | set_and_save_state dir/foo work head && |
| 45 | test_write_lines n y n | git checkout -p $opt >output && |
| 46 | verify_saved_state bar && |
| 47 | verify_saved_state dir/foo && |
| 48 | test_grep "Discard" output |
| 49 | ' |
| 50 | |
| 51 | test_expect_success "git checkout -p $opt with NO staged changes: apply" ' |
| 52 | test_write_lines n y y | git checkout -p $opt >output && |
| 53 | verify_saved_state bar && |
| 54 | verify_state dir/foo head head && |
| 55 | test_grep "Discard" output |
| 56 | ' |
| 57 | |
| 58 | test_expect_success "git checkout -p $opt with change already staged" ' |
| 59 | set_state dir/foo index index && |
| 60 | # the third n is to get out in case it mistakenly does not apply |
| 61 | test_write_lines n y n | git checkout -p $opt >output && |
| 62 | verify_saved_state bar && |
| 63 | verify_state dir/foo head head && |
| 64 | test_grep "Discard" output |
| 65 | ' |
| 66 | done |
| 67 | |
| 68 | test_expect_success 'git checkout -p HEAD^...' ' |
| 69 | # the third n is to get out in case it mistakenly does not apply |
| 70 | test_write_lines n y n | git checkout -p HEAD^... && |
| 71 | verify_saved_state bar && |
| 72 | verify_state dir/foo parent parent |
| 73 | ' |
| 74 | |
| 75 | test_expect_success 'git checkout -p HEAD^' ' |
| 76 | # the third n is to get out in case it mistakenly does not apply |
| 77 | test_write_lines n y n | git checkout -p HEAD^ && |
| 78 | verify_saved_state bar && |
| 79 | verify_state dir/foo parent parent |
| 80 | ' |
| 81 | |
| 82 | test_expect_success 'git checkout -p handles deletion' ' |
| 83 | set_state dir/foo work index && |
| 84 | rm dir/foo && |
| 85 | test_write_lines n y | git checkout -p && |
| 86 | verify_saved_state bar && |
| 87 | verify_state dir/foo index index |
| 88 | ' |
| 89 | |
| 90 | # The idea in the rest is that bar sorts first, so we always say 'y' |
| 91 | # first and if the path limiter fails it'll apply to bar instead of |
| 92 | # dir/foo. There's always an extra 'n' to reject edits to dir/foo in |
| 93 | # the failure case (and thus get out of the loop). |
| 94 | |
| 95 | test_expect_success 'path limiting works: dir' ' |
| 96 | set_state dir/foo work head && |
| 97 | test_write_lines y n | git checkout -p dir && |
| 98 | verify_saved_state bar && |
| 99 | verify_state dir/foo head head |
| 100 | ' |
| 101 | |
| 102 | test_expect_success 'path limiting works: -- dir' ' |
| 103 | set_state dir/foo work head && |
| 104 | test_write_lines y n | git checkout -p -- dir && |
| 105 | verify_saved_state bar && |
| 106 | verify_state dir/foo head head |
| 107 | ' |
| 108 | |
| 109 | test_expect_success 'path limiting works: HEAD^ -- dir' ' |
| 110 | # the third n is to get out in case it mistakenly does not apply |
| 111 | test_write_lines y n n | git checkout -p HEAD^ -- dir && |
| 112 | verify_saved_state bar && |
| 113 | verify_state dir/foo parent parent |
| 114 | ' |
| 115 | |
| 116 | test_expect_success 'path limiting works: foo inside dir' ' |
| 117 | set_state dir/foo work head && |
| 118 | # the third n is to get out in case it mistakenly does not apply |
| 119 | test_write_lines y n n | (cd dir && git checkout -p foo) && |
| 120 | verify_saved_state bar && |
| 121 | verify_state dir/foo head head |
| 122 | ' |
| 123 | |
| 124 | test_expect_success 'none of this moved HEAD' ' |
| 125 | verify_saved_head |
| 126 | ' |
| 127 | |
| 128 | test_expect_success 'empty tree can be handled' ' |
| 129 | test_when_finished "git reset --hard" && |
| 130 | git checkout -p $(test_oid empty_tree) -- |
| 131 | ' |
| 132 | |
| 133 | test_done |