Git 2.45
[git/gitster.git] / t / t2071-restore-patch.sh
blob42d552211915754e14ddb79d869fc9ba8f517fce
1 #!/bin/sh
3 test_description='git restore --patch'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./lib-patch-mode.sh
8 test_expect_success 'setup' '
9 mkdir dir &&
10 echo parent >dir/foo &&
11 echo dummy >bar &&
12 git add bar dir/foo &&
13 git commit -m initial &&
14 test_tick &&
15 test_commit second dir/foo head &&
16 set_and_save_state bar bar_work bar_index &&
17 save_head
20 test_expect_success 'restore -p without pathspec is fine' '
21 echo q >cmd &&
22 git restore -p <cmd
25 # note: bar sorts before dir/foo, so the first 'n' is always to skip 'bar'
27 test_expect_success 'saying "n" does nothing' '
28 set_and_save_state dir/foo work head &&
29 test_write_lines n n | git restore -p &&
30 verify_saved_state bar &&
31 verify_saved_state dir/foo
34 test_expect_success 'git restore -p' '
35 set_and_save_state dir/foo work head &&
36 test_write_lines n y | git restore -p &&
37 verify_saved_state bar &&
38 verify_state dir/foo head head
41 test_expect_success 'git restore -p with staged changes' '
42 set_state dir/foo work index &&
43 test_write_lines n y | git restore -p &&
44 verify_saved_state bar &&
45 verify_state dir/foo index index
48 for opt in "HEAD" "@"
50 test_expect_success "git restore -p --source=$opt" '
51 set_state dir/foo work index &&
52 # the third n is to get out in case it mistakenly does not apply
53 test_write_lines n y n | git restore -p --source=$opt >output &&
54 verify_saved_state bar &&
55 verify_state dir/foo head index &&
56 test_grep "Discard" output
58 done
60 test_expect_success 'git restore -p --source=HEAD^' '
61 set_state dir/foo work index &&
62 # the third n is to get out in case it mistakenly does not apply
63 test_write_lines n y n | git restore -p --source=HEAD^ &&
64 verify_saved_state bar &&
65 verify_state dir/foo parent index
68 test_expect_success 'git restore -p --source=HEAD^...' '
69 set_state dir/foo work index &&
70 # the third n is to get out in case it mistakenly does not apply
71 test_write_lines n y n | git restore -p --source=HEAD^... &&
72 verify_saved_state bar &&
73 verify_state dir/foo parent index
76 test_expect_success 'git restore -p handles deletion' '
77 set_state dir/foo work index &&
78 rm dir/foo &&
79 test_write_lines n y | git restore -p &&
80 verify_saved_state bar &&
81 verify_state dir/foo index index
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).
89 test_expect_success 'path limiting works: dir' '
90 set_state dir/foo work head &&
91 test_write_lines y n | git restore -p dir &&
92 verify_saved_state bar &&
93 verify_state dir/foo head head
96 test_expect_success 'path limiting works: -- dir' '
97 set_state dir/foo work head &&
98 test_write_lines y n | git restore -p -- dir &&
99 verify_saved_state bar &&
100 verify_state dir/foo head head
103 test_expect_success 'path limiting works: HEAD^ -- dir' '
104 set_state dir/foo work head &&
105 # the third n is to get out in case it mistakenly does not apply
106 test_write_lines y n n | git restore -p --source=HEAD^ -- dir &&
107 verify_saved_state bar &&
108 verify_state dir/foo parent head
111 test_expect_success 'path limiting works: foo inside dir' '
112 set_state dir/foo work head &&
113 # the third n is to get out in case it mistakenly does not apply
114 test_write_lines y n n | (cd dir && git restore -p foo) &&
115 verify_saved_state bar &&
116 verify_state dir/foo head head
119 test_expect_success 'none of this moved HEAD' '
120 verify_saved_head
123 test_done