3 test_description
='restore basic functionality'
7 test_expect_success
'setup' '
9 echo first-and-a-half >>first.t &&
14 echo untracked >untracked &&
15 echo ignored >ignored &&
16 echo /ignored >.gitignore &&
17 git add one two .gitignore &&
18 git update-ref refs/heads/one master
21 test_expect_success
'restore without pathspec is not ok' '
22 test_must_fail git restore &&
23 test_must_fail git restore --source=first
26 test_expect_success
'restore a file, ignoring branch of same name' '
33 test_expect_success
'restore a file on worktree from another ref' '
34 test_when_finished git reset --hard &&
35 git cat-file blob first:./first.t >expected &&
36 git restore --source=first first.t &&
37 test_cmp expected first.t &&
38 git cat-file blob HEAD:./first.t >expected &&
39 git show :first.t >actual &&
40 test_cmp expected actual
43 test_expect_success
'restore a file in the index from another ref' '
44 test_when_finished git reset --hard &&
45 git cat-file blob first:./first.t >expected &&
46 git restore --source=first --staged first.t &&
47 git show :first.t >actual &&
48 test_cmp expected actual &&
49 git cat-file blob HEAD:./first.t >expected &&
50 test_cmp expected first.t
53 test_expect_success
'restore a file in both the index and worktree from another ref' '
54 test_when_finished git reset --hard &&
55 git cat-file blob first:./first.t >expected &&
56 git restore --source=first --staged --worktree first.t &&
57 git show :first.t >actual &&
58 test_cmp expected actual &&
59 test_cmp expected first.t
62 test_expect_success
'restore --staged uses HEAD as source' '
63 test_when_finished git reset --hard &&
64 git cat-file blob :./first.t >expected &&
65 echo index-dirty >>first.t &&
67 git restore --staged first.t &&
68 git cat-file blob :./first.t >actual &&
69 test_cmp expected actual
72 test_expect_success
'restore --worktree --staged uses HEAD as source' '
73 test_when_finished git reset --hard &&
74 git show HEAD:./first.t >expected &&
75 echo dirty >>first.t &&
77 git restore --worktree --staged first.t &&
78 git show :./first.t >actual &&
79 test_cmp expected actual &&
80 test_cmp expected first.t
83 test_expect_success
'restore --ignore-unmerged ignores unmerged entries' '
89 git add unmerged common &&
90 git commit -m common &&
91 git switch -c first &&
92 echo first >unmerged &&
93 git commit -am first &&
94 git switch -c second master &&
95 echo second >unmerged &&
96 git commit -am second &&
97 test_must_fail git merge first &&
99 echo dirty >>common &&
100 test_must_fail git restore . &&
102 git restore --ignore-unmerged --quiet . >output 2>&1 &&
103 git diff common >diff-output &&
104 test_must_be_empty output &&
105 test_must_be_empty diff-output
109 test_expect_success
'restore --staged adds deleted intent-to-add file back to index' '
110 echo "nonempty" >nonempty &&
112 git add nonempty empty &&
113 git commit -m "create files to be deleted" &&
114 git rm --cached nonempty empty &&
115 git add -N nonempty empty &&
116 git restore --staged nonempty empty &&
117 git diff --cached --exit-code
120 test_expect_success
'restore --staged invalidates cache tree for deletions' '
121 test_when_finished git reset --hard &&
126 # It is important to commit and then reset here, so that the index
127 # contains a valid cache-tree for the "both" tree.
128 git commit -m both &&
129 git reset --soft HEAD^ &&
131 git restore --staged new1 &&
132 git commit -m "just new2" &&
133 git rev-parse HEAD:new2 &&
134 test_must_fail git rev-parse HEAD:new1