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