]>
Commit | Line | Data |
---|---|---|
4f353658 TR |
1 | #!/bin/sh |
2 | ||
3 | test_description='git checkout --patch' | |
4 | ||
5 | . ./lib-patch-mode.sh | |
6 | ||
ed922dca | 7 | test_expect_success 'setup' ' |
4f353658 TR |
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 | ||
ed922dca | 21 | test_expect_success 'saying "n" does nothing' ' |
4f353658 | 22 | set_and_save_state dir/foo work head && |
0590ff26 | 23 | test_write_lines n n | git checkout -p && |
4f353658 TR |
24 | verify_saved_state bar && |
25 | verify_saved_state dir/foo | |
26 | ' | |
27 | ||
ed922dca | 28 | test_expect_success 'git checkout -p' ' |
0590ff26 | 29 | test_write_lines n y | git checkout -p && |
4f353658 TR |
30 | verify_saved_state bar && |
31 | verify_state dir/foo head head | |
32 | ' | |
33 | ||
ed922dca | 34 | test_expect_success 'git checkout -p with staged changes' ' |
a48fcd83 | 35 | set_state dir/foo work index && |
0590ff26 | 36 | test_write_lines n y | git checkout -p && |
4f353658 TR |
37 | verify_saved_state bar && |
38 | verify_state dir/foo index index | |
39 | ' | |
40 | ||
5a8ed3fe GT |
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 | ' | |
4f353658 | 50 | |
5a8ed3fe GT |
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 | ' | |
4f353658 | 57 | |
5a8ed3fe GT |
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 | |
4f353658 | 67 | |
ed922dca | 68 | test_expect_success 'git checkout -p HEAD^...' ' |
5602b500 DL |
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 | ||
ed922dca | 75 | test_expect_success 'git checkout -p HEAD^' ' |
4f353658 | 76 | # the third n is to get out in case it mistakenly does not apply |
0590ff26 | 77 | test_write_lines n y n | git checkout -p HEAD^ && |
4f353658 TR |
78 | verify_saved_state bar && |
79 | verify_state dir/foo parent parent | |
80 | ' | |
81 | ||
ed922dca | 82 | test_expect_success 'git checkout -p handles deletion' ' |
e1327ed5 JK |
83 | set_state dir/foo work index && |
84 | rm dir/foo && | |
0590ff26 | 85 | test_write_lines n y | git checkout -p && |
e1327ed5 JK |
86 | verify_saved_state bar && |
87 | verify_state dir/foo index index | |
88 | ' | |
89 | ||
4f353658 TR |
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 | ||
ed922dca | 95 | test_expect_success 'path limiting works: dir' ' |
4f353658 | 96 | set_state dir/foo work head && |
0590ff26 | 97 | test_write_lines y n | git checkout -p dir && |
4f353658 TR |
98 | verify_saved_state bar && |
99 | verify_state dir/foo head head | |
100 | ' | |
101 | ||
ed922dca | 102 | test_expect_success 'path limiting works: -- dir' ' |
4f353658 | 103 | set_state dir/foo work head && |
0590ff26 | 104 | test_write_lines y n | git checkout -p -- dir && |
4f353658 TR |
105 | verify_saved_state bar && |
106 | verify_state dir/foo head head | |
107 | ' | |
108 | ||
ed922dca | 109 | test_expect_success 'path limiting works: HEAD^ -- dir' ' |
4f353658 | 110 | # the third n is to get out in case it mistakenly does not apply |
0590ff26 | 111 | test_write_lines y n n | git checkout -p HEAD^ -- dir && |
4f353658 TR |
112 | verify_saved_state bar && |
113 | verify_state dir/foo parent parent | |
114 | ' | |
115 | ||
ed922dca | 116 | test_expect_success 'path limiting works: foo inside dir' ' |
4f353658 TR |
117 | set_state dir/foo work head && |
118 | # the third n is to get out in case it mistakenly does not apply | |
0590ff26 | 119 | test_write_lines y n n | (cd dir && git checkout -p foo) && |
4f353658 TR |
120 | verify_saved_state bar && |
121 | verify_state dir/foo head head | |
122 | ' | |
123 | ||
ed922dca | 124 | test_expect_success 'none of this moved HEAD' ' |
4f353658 TR |
125 | verify_saved_head |
126 | ' | |
127 | ||
ed922dca | 128 | test_expect_success 'empty tree can be handled' ' |
5c29f19c JS |
129 | test_when_finished "git reset --hard" && |
130 | git checkout -p $(test_oid empty_tree) -- | |
131 | ' | |
132 | ||
4f353658 | 133 | test_done |