Imported Upstream version 2.27.0
[platform/upstream/git.git] / t / t2070-restore.sh
1 #!/bin/sh
2
3 test_description='restore basic functionality'
4
5 . ./test-lib.sh
6
7 test_expect_success 'setup' '
8         test_commit first &&
9         echo first-and-a-half >>first.t &&
10         git add first.t &&
11         test_commit second &&
12         echo one >one &&
13         echo two >two &&
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
19 '
20
21 test_expect_success 'restore without pathspec is not ok' '
22         test_must_fail git restore &&
23         test_must_fail git restore --source=first
24 '
25
26 test_expect_success 'restore a file, ignoring branch of same name' '
27         cat one >expected &&
28         echo dirty >>one &&
29         git restore one &&
30         test_cmp expected one
31 '
32
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
41 '
42
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
51 '
52
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
60 '
61
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 &&
66         git add first.t &&
67         git restore --staged first.t &&
68         git cat-file blob :./first.t >actual &&
69         test_cmp expected actual
70 '
71
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 &&
76         git add 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
81 '
82
83 test_expect_success 'restore --ignore-unmerged ignores unmerged entries' '
84         git init unmerged &&
85         (
86                 cd unmerged &&
87                 echo one >unmerged &&
88                 echo one >common &&
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 &&
98
99                 echo dirty >>common &&
100                 test_must_fail git restore . &&
101
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
106         )
107 '
108
109 test_expect_success 'restore --staged adds deleted intent-to-add file back to index' '
110         echo "nonempty" >nonempty &&
111         >empty &&
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
118 '
119
120 test_expect_success 'restore --staged invalidates cache tree for deletions' '
121         test_when_finished git reset --hard &&
122         >new1 &&
123         >new2 &&
124         git add new1 new2 &&
125
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^ &&
130
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
135 '
136
137 test_done