3 test_description
='restore basic functionality'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 TEST_PASSES_SANITIZE_LEAK
=true
11 test_expect_success
'setup' '
13 echo first-and-a-half >>first.t &&
18 echo untracked >untracked &&
19 echo ignored >ignored &&
20 echo /ignored >.gitignore &&
21 git add one two .gitignore &&
22 git update-ref refs/heads/one main
25 test_expect_success
'restore without pathspec is not ok' '
26 test_must_fail git restore &&
27 test_must_fail git restore --source=first
30 test_expect_success
'restore a file, ignoring branch of same name' '
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
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
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
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 &&
71 git restore --staged first.t &&
72 git cat-file blob :./first.t >actual &&
73 test_cmp expected actual
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 &&
81 git restore --worktree --staged first.t &&
82 git show :./first.t >actual &&
83 test_cmp expected actual &&
84 test_cmp expected first.t
87 test_expect_success
'restore --ignore-unmerged ignores unmerged entries' '
93 git add unmerged common &&
94 git commit -m common &&
95 git switch -c first &&
96 echo first >unmerged &&
97 git commit -am first &&
98 git switch -c second main &&
99 echo second >unmerged &&
100 git commit -am second &&
101 test_must_fail git merge first &&
103 echo dirty >>common &&
104 test_must_fail git restore . &&
106 git restore --ignore-unmerged --quiet . >output 2>&1 &&
107 git diff common >diff-output &&
108 test_must_be_empty output &&
109 test_must_be_empty diff-output
113 test_expect_success
'restore --staged adds deleted intent-to-add file back to index' '
114 echo "nonempty" >nonempty &&
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
124 test_expect_success
'restore --staged invalidates cache tree for deletions' '
125 test_when_finished git reset --hard &&
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^ &&
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
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) &&
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 &&
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) &&
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 &&
173 git restore --worktree --merge file &&
174 cat >expect <<-\EOF &&
184 test_expect_success
'restore --merge to unresolve after (mistaken) resolution' '
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) &&
189 echo "100644 $O 1 file" &&
190 echo "100644 $A 2 file" &&
191 echo "100644 $B 3 file"
192 } | git update-index --index-info &&
194 git restore --worktree --merge file &&
195 cat >expect <<-\EOF &&
205 test_expect_success
'restore with merge options are incompatible with certain options' '
208 "--staged --theirs" \
210 "--source=HEAD --ours" \
211 "--source=HEAD --theirs" \
212 "--source=HEAD --merge" \
213 "--staged --conflict=diff3" \
214 "--staged --worktree --ours" \
215 "--staged --worktree --theirs" \
216 "--staged --worktree --merge" \
217 "--staged --worktree --conflict=zdiff3"
219 test_must_fail git restore $opts . 2>err &&
220 grep "cannot be used" err || return