3 test_description
='test git worktree repair'
5 TEST_PASSES_SANITIZE_LEAK
=true
8 test_expect_success setup
'
12 test_expect_success
'skip missing worktree' '
13 test_when_finished "git worktree prune" &&
14 git worktree add --detach missing &&
16 git worktree repair >out 2>err &&
17 test_must_be_empty out &&
18 test_must_be_empty err
21 test_expect_success
'worktree path not directory' '
22 test_when_finished "git worktree prune" &&
23 git worktree add --detach notdir &&
26 test_must_fail git worktree repair >out 2>err &&
27 test_must_be_empty out &&
28 test_grep "not a directory" err
31 test_expect_success
"don't clobber .git repo" '
32 test_when_finished "rm -rf repo && git worktree prune" &&
33 git worktree add --detach repo &&
35 test_create_repo repo &&
36 test_must_fail git worktree repair >out 2>err &&
37 test_must_be_empty out &&
38 test_grep ".git is not a file" err
41 test_corrupt_gitfile
() {
45 test_when_finished
'rm -rf corrupt && git worktree prune' &&
46 git worktree add
--detach corrupt
&&
47 git
-C corrupt rev-parse
--absolute-git-dir >expect
&&
49 git
-C "$repairdir" worktree repair
2>err
&&
50 test_grep
"$problem" err
&&
51 git
-C corrupt rev-parse
--absolute-git-dir >actual
&&
52 test_cmp expect actual
55 test_expect_success
'repair missing .git file' '
56 test_corrupt_gitfile "rm -f corrupt/.git" ".git file broken"
59 test_expect_success
'repair bogus .git file' '
60 test_corrupt_gitfile "echo \"gitdir: /nowhere\" >corrupt/.git" \
64 test_expect_success
'repair incorrect .git file' '
65 test_when_finished "rm -rf other && git worktree prune" &&
66 test_create_repo other &&
67 other=$(git -C other rev-parse --absolute-git-dir) &&
68 test_corrupt_gitfile "echo \"gitdir: $other\" >corrupt/.git" \
72 test_expect_success
'repair .git file from main/.git' '
73 test_corrupt_gitfile "rm -f corrupt/.git" ".git file broken" .git
76 test_expect_success
'repair .git file from linked worktree' '
77 test_when_finished "rm -rf other && git worktree prune" &&
78 git worktree add --detach other &&
79 test_corrupt_gitfile "rm -f corrupt/.git" ".git file broken" other
82 test_expect_success
'repair .git file from bare.git' '
83 test_when_finished "rm -rf bare.git corrupt && git worktree prune" &&
84 git clone --bare . bare.git &&
85 git -C bare.git worktree add --detach ../corrupt &&
86 git -C corrupt rev-parse --absolute-git-dir >expect &&
88 git -C bare.git worktree repair &&
89 git -C corrupt rev-parse --absolute-git-dir >actual &&
90 test_cmp expect actual
93 test_expect_success
'invalid worktree path' '
94 test_must_fail git worktree repair /notvalid >out 2>err &&
95 test_must_be_empty out &&
96 test_grep "not a valid path" err
99 test_expect_success
'repo not found; .git not file' '
100 test_when_finished "rm -rf not-a-worktree" &&
101 test_create_repo not-a-worktree &&
102 test_must_fail git worktree repair not-a-worktree >out 2>err &&
103 test_must_be_empty out &&
104 test_grep ".git is not a file" err
107 test_expect_success
'repo not found; .git not referencing repo' '
108 test_when_finished "rm -rf side not-a-repo && git worktree prune" &&
109 git worktree add --detach side &&
110 sed s,\.git/worktrees/side$,not-a-repo, side/.git >side/.newgit &&
111 mv side/.newgit side/.git &&
113 test_must_fail git worktree repair side 2>err &&
114 test_grep ".git file does not reference a repository" err
117 test_expect_success
'repo not found; .git file broken' '
118 test_when_finished "rm -rf orig moved && git worktree prune" &&
119 git worktree add --detach orig &&
120 echo /invalid >orig/.git &&
122 test_must_fail git worktree repair moved >out 2>err &&
123 test_must_be_empty out &&
124 test_grep ".git file broken" err
127 test_expect_success
'repair broken gitdir' '
128 test_when_finished "rm -rf orig moved && git worktree prune" &&
129 git worktree add --detach orig &&
130 sed s,orig/\.git$,moved/.git, .git/worktrees/orig/gitdir >expect &&
131 rm .git/worktrees/orig/gitdir &&
133 git worktree repair moved 2>err &&
134 test_cmp expect .git/worktrees/orig/gitdir &&
135 test_grep "gitdir unreadable" err
138 test_expect_success
'repair incorrect gitdir' '
139 test_when_finished "rm -rf orig moved && git worktree prune" &&
140 git worktree add --detach orig &&
141 sed s,orig/\.git$,moved/.git, .git/worktrees/orig/gitdir >expect &&
143 git worktree repair moved 2>err &&
144 test_cmp expect .git/worktrees/orig/gitdir &&
145 test_grep "gitdir incorrect" err
148 test_expect_success
'repair gitdir (implicit) from linked worktree' '
149 test_when_finished "rm -rf orig moved && git worktree prune" &&
150 git worktree add --detach orig &&
151 sed s,orig/\.git$,moved/.git, .git/worktrees/orig/gitdir >expect &&
153 git -C moved worktree repair 2>err &&
154 test_cmp expect .git/worktrees/orig/gitdir &&
155 test_grep "gitdir incorrect" err
158 test_expect_success
'unable to repair gitdir (implicit) from main worktree' '
159 test_when_finished "rm -rf orig moved && git worktree prune" &&
160 git worktree add --detach orig &&
161 cat .git/worktrees/orig/gitdir >expect &&
163 git worktree repair 2>err &&
164 test_cmp expect .git/worktrees/orig/gitdir &&
165 test_must_be_empty err
168 test_expect_success
'repair multiple gitdir files' '
169 test_when_finished "rm -rf orig1 orig2 moved1 moved2 &&
170 git worktree prune" &&
171 git worktree add --detach orig1 &&
172 git worktree add --detach orig2 &&
173 sed s,orig1/\.git$,moved1/.git, .git/worktrees/orig1/gitdir >expect1 &&
174 sed s,orig2/\.git$,moved2/.git, .git/worktrees/orig2/gitdir >expect2 &&
177 git worktree repair moved1 moved2 2>err &&
178 test_cmp expect1 .git/worktrees/orig1/gitdir &&
179 test_cmp expect2 .git/worktrees/orig2/gitdir &&
180 test_grep "gitdir incorrect:.*orig1/gitdir$" err &&
181 test_grep "gitdir incorrect:.*orig2/gitdir$" err
184 test_expect_success
'repair moved main and linked worktrees' '
185 test_when_finished "rm -rf main side mainmoved sidemoved" &&
186 test_create_repo main &&
187 test_commit -C main init &&
188 git -C main worktree add --detach ../side &&
189 sed "s,side/\.git$,sidemoved/.git," \
190 main/.git/worktrees/side/gitdir >expect-gitdir &&
191 sed "s,main/.git/worktrees/side$,mainmoved/.git/worktrees/side," \
192 side/.git >expect-gitfile &&
195 git -C mainmoved worktree repair ../sidemoved &&
196 test_cmp expect-gitdir mainmoved/.git/worktrees/side/gitdir &&
197 test_cmp expect-gitfile sidemoved/.git
200 test_expect_success
'repair copied main and linked worktrees' '
201 test_when_finished "rm -rf orig dup" &&
203 git -C orig init main &&
204 test_commit -C orig/main nothing &&
205 git -C orig/main worktree add ../linked &&
206 cp orig/main/.git/worktrees/linked/gitdir orig/main.expect &&
207 cp orig/linked/.git orig/linked.expect &&
209 sed "s,orig/linked/\.git$,dup/linked/.git," orig/main.expect >dup/main.expect &&
210 sed "s,orig/main/\.git/worktrees/linked$,dup/main/.git/worktrees/linked," \
211 orig/linked.expect >dup/linked.expect &&
212 git -C dup/main worktree repair ../linked &&
213 test_cmp orig/main.expect orig/main/.git/worktrees/linked/gitdir &&
214 test_cmp orig/linked.expect orig/linked/.git &&
215 test_cmp dup/main.expect dup/main/.git/worktrees/linked/gitdir &&
216 test_cmp dup/linked.expect dup/linked/.git