]> git.ipfire.org Git - thirdparty/git.git/blame - t/t2070-restore.sh
Merge branch 'jc/bisect-doc' into maint-2.43
[thirdparty/git.git] / t / t2070-restore.sh
CommitLineData
4df3ec63
NTND
1#!/bin/sh
2
3test_description='restore basic functionality'
4
883b98ef 5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
6export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
4df3ec63
NTND
8. ./test-lib.sh
9
10test_expect_success 'setup' '
11 test_commit first &&
12 echo first-and-a-half >>first.t &&
13 git add first.t &&
14 test_commit second &&
15 echo one >one &&
16 echo two >two &&
17 echo untracked >untracked &&
18 echo ignored >ignored &&
19 echo /ignored >.gitignore &&
20 git add one two .gitignore &&
883b98ef 21 git update-ref refs/heads/one main
4df3ec63
NTND
22'
23
24test_expect_success 'restore without pathspec is not ok' '
25 test_must_fail git restore &&
26 test_must_fail git restore --source=first
27'
28
29test_expect_success 'restore a file, ignoring branch of same name' '
30 cat one >expected &&
31 echo dirty >>one &&
32 git restore one &&
33 test_cmp expected one
34'
35
36test_expect_success 'restore a file on worktree from another ref' '
37 test_when_finished git reset --hard &&
38 git cat-file blob first:./first.t >expected &&
39 git restore --source=first first.t &&
40 test_cmp expected first.t &&
41 git cat-file blob HEAD:./first.t >expected &&
42 git show :first.t >actual &&
43 test_cmp expected actual
44'
45
46test_expect_success 'restore a file in the index from another ref' '
47 test_when_finished git reset --hard &&
48 git cat-file blob first:./first.t >expected &&
49 git restore --source=first --staged first.t &&
50 git show :first.t >actual &&
51 test_cmp expected actual &&
52 git cat-file blob HEAD:./first.t >expected &&
53 test_cmp expected first.t
54'
55
56test_expect_success 'restore a file in both the index and worktree from another ref' '
57 test_when_finished git reset --hard &&
58 git cat-file blob first:./first.t >expected &&
59 git restore --source=first --staged --worktree first.t &&
60 git show :first.t >actual &&
61 test_cmp expected actual &&
62 test_cmp expected first.t
63'
64
65test_expect_success 'restore --staged uses HEAD as source' '
66 test_when_finished git reset --hard &&
67 git cat-file blob :./first.t >expected &&
68 echo index-dirty >>first.t &&
69 git add first.t &&
70 git restore --staged first.t &&
71 git cat-file blob :./first.t >actual &&
72 test_cmp expected actual
73'
74
088018e3
ES
75test_expect_success 'restore --worktree --staged uses HEAD as source' '
76 test_when_finished git reset --hard &&
77 git show HEAD:./first.t >expected &&
78 echo dirty >>first.t &&
79 git add first.t &&
80 git restore --worktree --staged first.t &&
81 git show :./first.t >actual &&
82 test_cmp expected actual &&
83 test_cmp expected first.t
84'
85
4df3ec63
NTND
86test_expect_success 'restore --ignore-unmerged ignores unmerged entries' '
87 git init unmerged &&
88 (
89 cd unmerged &&
90 echo one >unmerged &&
91 echo one >common &&
92 git add unmerged common &&
93 git commit -m common &&
94 git switch -c first &&
95 echo first >unmerged &&
96 git commit -am first &&
883b98ef 97 git switch -c second main &&
4df3ec63
NTND
98 echo second >unmerged &&
99 git commit -am second &&
100 test_must_fail git merge first &&
101
102 echo dirty >>common &&
103 test_must_fail git restore . &&
104
105 git restore --ignore-unmerged --quiet . >output 2>&1 &&
106 git diff common >diff-output &&
bcba4065
NTND
107 test_must_be_empty output &&
108 test_must_be_empty diff-output
4df3ec63
NTND
109 )
110'
111
620c09e1
VN
112test_expect_success 'restore --staged adds deleted intent-to-add file back to index' '
113 echo "nonempty" >nonempty &&
114 >empty &&
115 git add nonempty empty &&
116 git commit -m "create files to be deleted" &&
117 git rm --cached nonempty empty &&
118 git add -N nonempty empty &&
119 git restore --staged nonempty empty &&
120 git diff --cached --exit-code
121'
122
e701bab3
JK
123test_expect_success 'restore --staged invalidates cache tree for deletions' '
124 test_when_finished git reset --hard &&
125 >new1 &&
126 >new2 &&
127 git add new1 new2 &&
128
129 # It is important to commit and then reset here, so that the index
130 # contains a valid cache-tree for the "both" tree.
131 git commit -m both &&
132 git reset --soft HEAD^ &&
133
134 git restore --staged new1 &&
135 git commit -m "just new2" &&
136 git rev-parse HEAD:new2 &&
137 test_must_fail git rev-parse HEAD:new1
138'
139
ed3789f2
JH
140test_expect_success 'restore --merge to unresolve' '
141 O=$(echo original | git hash-object -w --stdin) &&
142 A=$(echo ourside | git hash-object -w --stdin) &&
143 B=$(echo theirside | git hash-object -w --stdin) &&
144 {
145 echo "100644 $O 1 file" &&
146 echo "100644 $A 2 file" &&
147 echo "100644 $B 3 file"
148 } | git update-index --index-info &&
149 echo nothing >file &&
150 git restore --worktree --merge file &&
151 cat >expect <<-\EOF &&
152 <<<<<<< ours
153 ourside
154 =======
155 theirside
156 >>>>>>> theirs
157 EOF
158 test_cmp expect file
159'
160
161test_expect_success 'restore --merge to unresolve after (mistaken) resolution' '
162 O=$(echo original | git hash-object -w --stdin) &&
163 A=$(echo ourside | git hash-object -w --stdin) &&
164 B=$(echo theirside | git hash-object -w --stdin) &&
165 {
166 echo "100644 $O 1 file" &&
167 echo "100644 $A 2 file" &&
168 echo "100644 $B 3 file"
169 } | git update-index --index-info &&
170 echo nothing >file &&
171 git add file &&
172 git restore --worktree --merge file &&
173 cat >expect <<-\EOF &&
174 <<<<<<< ours
175 ourside
176 =======
177 theirside
178 >>>>>>> theirs
179 EOF
180 test_cmp expect file
181'
182
5bdedac3 183test_expect_success 'restore --merge to unresolve after (mistaken) resolution' '
ed3789f2
JH
184 O=$(echo original | git hash-object -w --stdin) &&
185 A=$(echo ourside | git hash-object -w --stdin) &&
186 B=$(echo theirside | git hash-object -w --stdin) &&
187 {
188 echo "100644 $O 1 file" &&
189 echo "100644 $A 2 file" &&
190 echo "100644 $B 3 file"
191 } | git update-index --index-info &&
192 git rm -f file &&
193 git restore --worktree --merge file &&
194 cat >expect <<-\EOF &&
195 <<<<<<< ours
196 ourside
197 =======
198 theirside
199 >>>>>>> theirs
200 EOF
201 test_cmp expect file
202'
203
54f98fee 204test_expect_success 'restore with merge options are incompatible with certain options' '
ee8a8882
AK
205 for opts in \
206 "--staged --ours" \
207 "--staged --theirs" \
208 "--staged --merge" \
54f98fee
JH
209 "--source=HEAD --ours" \
210 "--source=HEAD --theirs" \
211 "--source=HEAD --merge" \
ee8a8882
AK
212 "--staged --conflict=diff3" \
213 "--staged --worktree --ours" \
214 "--staged --worktree --theirs" \
215 "--staged --worktree --merge" \
216 "--staged --worktree --conflict=zdiff3"
217 do
218 test_must_fail git restore $opts . 2>err &&
54f98fee 219 grep "cannot be used" err || return
ee8a8882
AK
220 done
221'
222
4df3ec63 223test_done