]> git.ipfire.org Git - thirdparty/git.git/blob - t/t2071-restore-patch.sh
Merge branch 'jk/doc-remote-helpers-markup-fix'
[thirdparty/git.git] / t / t2071-restore-patch.sh
1 #!/bin/sh
2
3 test_description='git restore --patch'
4
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./lib-patch-mode.sh
7
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
18 '
19
20 test_expect_success 'restore -p without pathspec is fine' '
21 echo q >cmd &&
22 git restore -p <cmd
23 '
24
25 # note: bar sorts before dir/foo, so the first 'n' is always to skip 'bar'
26
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
32 '
33
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
39 '
40
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
46 '
47
48 for opt in "HEAD" "@"
49 do
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
57 '
58 done
59
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
66 '
67
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
74 '
75
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
82 '
83
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
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
94 '
95
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
101 '
102
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
109 '
110
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
117 '
118
119 test_expect_success 'none of this moved HEAD' '
120 verify_saved_head
121 '
122
123 test_done