Commit d51f9ae
committed
tests: add a safety net to prevent git from operating on the wrong repo
The GIT_CEILING_DIRECTORIES fix in the preceding commit should be
sufficient to prevent test-spawned git processes from discovering the
enclosing repository. However, when that protection was put to the test
by running `rm -rf node_modules/ build/ && npm ci && npm run build &&
npm test` from a worktree, the test commits still ended up on the real
worktree's HEAD for reasons that could not be fully diagnosed. Since the
consequences of this happening are severe (the primary worktree was
marked as `core.bare = true` and test commits, notes refs, and fake
upstream refs all landed on the real repository), an additional layer of
defense is warranted.
Introduce a `validateWorkDir()` function in `lib/git.ts` that is called
from every function that spawns a git process (`git()`, `revParse()`,
`revListCount()`, `gitConfig()`, `gitConfigForEach()`,
`gitCommandExists()`). When the `GIT_WORK_DIR_PREFIX` environment
variable is set, the function verifies that the resolved `workDir` falls
inside the expected directory, and throws otherwise. For `git init
<path>` and `git --git-dir=<path>` invocations, the target path from
the arguments is validated instead of the cwd, since those commands
legitimately operate on a path specified as an argument rather than
through the `workDir` option.
In `testCreateRepo()`, the guard is suspended during the setup phase
(which needs to run `git init` and `git config --global` without a test
repo `workDir`), then activated by setting `GIT_WORK_DIR_PREFIX` to the
`.test-dir/` path before returning. This ensures that all subsequent
`git()` calls in the test process must use a `workDir` inside the test
directory, or fail loudly.
Assisted-by: Claude Opus 4.6
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>1 parent 0fda24f commit d51f9ae
2 files changed
Lines changed: 71 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
19 | 32 | | |
20 | 33 | | |
21 | 34 | | |
| |||
43 | 56 | | |
44 | 57 | | |
45 | 58 | | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
46 | 97 | | |
47 | 98 | | |
48 | 99 | | |
| |||
158 | 209 | | |
159 | 210 | | |
160 | 211 | | |
161 | | - | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
162 | 215 | | |
163 | 216 | | |
164 | 217 | | |
| |||
171 | 224 | | |
172 | 225 | | |
173 | 226 | | |
| 227 | + | |
174 | 228 | | |
175 | 229 | | |
176 | 230 | | |
| |||
196 | 250 | | |
197 | 251 | | |
198 | 252 | | |
199 | | - | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
200 | 256 | | |
201 | 257 | | |
202 | 258 | | |
| |||
208 | 264 | | |
209 | 265 | | |
210 | 266 | | |
211 | | - | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
212 | 270 | | |
213 | 271 | | |
214 | 272 | | |
215 | 273 | | |
216 | | - | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
217 | 277 | | |
218 | 278 | | |
219 | 279 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
148 | 148 | | |
149 | 149 | | |
150 | 150 | | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
151 | 154 | | |
152 | 155 | | |
153 | 156 | | |
| |||
191 | 194 | | |
192 | 195 | | |
193 | 196 | | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
194 | 201 | | |
195 | 202 | | |
196 | 203 | | |
| |||
0 commit comments