t8*: adjust the references to the default branch name "main"
[git] / t / t2071-restore-patch.sh
1 #!/bin/sh
2
3 test_description='git restore --patch'
4
5 . ./lib-patch-mode.sh
6
7 test_expect_success PERL '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 test_expect_success PERL 'restore -p without pathspec is fine' '
20         echo q >cmd &&
21         git restore -p <cmd
22 '
23
24 # note: bar sorts before dir/foo, so the first 'n' is always to skip 'bar'
25
26 test_expect_success PERL 'saying "n" does nothing' '
27         set_and_save_state dir/foo work head &&
28         test_write_lines n n | git restore -p &&
29         verify_saved_state bar &&
30         verify_saved_state dir/foo
31 '
32
33 test_expect_success PERL 'git restore -p' '
34         set_and_save_state dir/foo work head &&
35         test_write_lines n y | git restore -p &&
36         verify_saved_state bar &&
37         verify_state dir/foo head head
38 '
39
40 test_expect_success PERL 'git restore -p with staged changes' '
41         set_state dir/foo work index &&
42         test_write_lines n y | git restore -p &&
43         verify_saved_state bar &&
44         verify_state dir/foo index index
45 '
46
47 test_expect_success PERL 'git restore -p --source=HEAD' '
48         set_state dir/foo work index &&
49         # the third n is to get out in case it mistakenly does not apply
50         test_write_lines n y n | git restore -p --source=HEAD &&
51         verify_saved_state bar &&
52         verify_state dir/foo head index
53 '
54
55 test_expect_success PERL 'git restore -p --source=HEAD^' '
56         set_state dir/foo work index &&
57         # the third n is to get out in case it mistakenly does not apply
58         test_write_lines n y n | git restore -p --source=HEAD^ &&
59         verify_saved_state bar &&
60         verify_state dir/foo parent index
61 '
62
63 test_expect_success PERL 'git restore -p --source=HEAD^...' '
64         set_state dir/foo work index &&
65         # the third n is to get out in case it mistakenly does not apply
66         test_write_lines n y n | git restore -p --source=HEAD^... &&
67         verify_saved_state bar &&
68         verify_state dir/foo parent index
69 '
70
71 test_expect_success PERL 'git restore -p handles deletion' '
72         set_state dir/foo work index &&
73         rm dir/foo &&
74         test_write_lines n y | git restore -p &&
75         verify_saved_state bar &&
76         verify_state dir/foo index index
77 '
78
79 # The idea in the rest is that bar sorts first, so we always say 'y'
80 # first and if the path limiter fails it'll apply to bar instead of
81 # dir/foo.  There's always an extra 'n' to reject edits to dir/foo in
82 # the failure case (and thus get out of the loop).
83
84 test_expect_success PERL 'path limiting works: dir' '
85         set_state dir/foo work head &&
86         test_write_lines y n | git restore -p dir &&
87         verify_saved_state bar &&
88         verify_state dir/foo head head
89 '
90
91 test_expect_success PERL 'path limiting works: -- dir' '
92         set_state dir/foo work head &&
93         test_write_lines y n | git restore -p -- dir &&
94         verify_saved_state bar &&
95         verify_state dir/foo head head
96 '
97
98 test_expect_success PERL 'path limiting works: HEAD^ -- dir' '
99         set_state dir/foo work head &&
100         # the third n is to get out in case it mistakenly does not apply
101         test_write_lines y n n | git restore -p --source=HEAD^ -- dir &&
102         verify_saved_state bar &&
103         verify_state dir/foo parent head
104 '
105
106 test_expect_success PERL 'path limiting works: foo inside dir' '
107         set_state dir/foo work head &&
108         # the third n is to get out in case it mistakenly does not apply
109         test_write_lines y n n | (cd dir && git restore -p foo) &&
110         verify_saved_state bar &&
111         verify_state dir/foo head head
112 '
113
114 test_expect_success PERL 'none of this moved HEAD' '
115         verify_saved_head
116 '
117
118 test_done