3 test_description='restore basic functionality'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 test_expect_success 'setup' '
12 echo first-and-a-half >>first.t &&
17 echo untracked >untracked &&
18 echo ignored >ignored &&
19 echo /ignored >.gitignore &&
20 git add one two .gitignore &&
21 git update-ref refs/heads/one main
24 test_expect_success 'restore without pathspec is not ok' '
25 test_must_fail git restore &&
26 test_must_fail git restore --source=first
29 test_expect_success 'restore a file, ignoring branch of same name' '
36 test_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
46 test_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
56 test_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
65 test_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 &&
70 git restore --staged first.t &&
71 git cat-file blob :./first.t >actual &&
72 test_cmp expected actual
75 test_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 &&
80 git restore --worktree --staged first.t &&
81 git show :./first.t >actual &&
82 test_cmp expected actual &&
83 test_cmp expected first.t
86 test_expect_success 'restore --ignore-unmerged ignores unmerged entries' '
92 git add unmerged common &&
93 git commit -m common &&
94 git switch -c first &&
95 echo first >unmerged &&
96 git commit -am first &&
97 git switch -c second main &&
98 echo second >unmerged &&
99 git commit -am second &&
100 test_must_fail git merge first &&
102 echo dirty >>common &&
103 test_must_fail git restore . &&
105 git restore --ignore-unmerged --quiet . >output 2>&1 &&
106 git diff common >diff-output &&
107 test_must_be_empty output &&
108 test_must_be_empty diff-output
112 test_expect_success 'restore --staged adds deleted intent-to-add file back to index' '
113 echo "nonempty" >nonempty &&
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
123 test_expect_success 'restore --staged invalidates cache tree for deletions' '
124 test_when_finished git reset --hard &&
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^ &&
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