( projectRoot: string, hooks: GitHookName[] = DEFAULT_SYNC_HOOKS, )
| 226 | * it, and any user-authored hook content is preserved. |
| 227 | */ |
| 228 | export function installGitSyncHook( |
| 229 | projectRoot: string, |
| 230 | hooks: GitHookName[] = DEFAULT_SYNC_HOOKS, |
| 231 | ): GitHookResult { |
| 232 | // The block names its root on a comment line; a line break in the path |
| 233 | // would end that comment and run the rest of the path as a command. |
| 234 | const root = rootKey(projectRoot); |
| 235 | if (/[\r\n]/.test(root)) { |
| 236 | return { installed: [], hooksDir: null, skipped: 'the project path contains a line break' }; |
| 237 | } |
| 238 | |
| 239 | const hooksDir = gitHooksDir(projectRoot); |
| 240 | if (!hooksDir) { |
| 241 | return { installed: [], hooksDir: null, skipped: 'not a git repository' }; |
| 242 | } |
| 243 | |
| 244 | try { |
| 245 | fs.mkdirSync(hooksDir, { recursive: true }); |
| 246 | } catch { |
| 247 | return { installed: [], hooksDir, skipped: 'could not access the git hooks directory' }; |
| 248 | } |
| 249 | |
| 250 | const block = markerBlock(root); |
| 251 | const owns = ownerFor(projectRoot); |
| 252 | const installed: GitHookName[] = []; |
| 253 | const unsupported: GitHookName[] = []; |
| 254 | |
| 255 | for (const hook of hooks) { |
| 256 | const file = path.join(hooksDir, hook); |
| 257 | let content = `#!/bin/sh\n${block}\n`; |
| 258 | |
| 259 | if (fs.existsSync(file)) { |
| 260 | const existing = fs.readFileSync(file, 'utf8'); |
| 261 | if (!isShellHook(existing)) { |
| 262 | unsupported.push(hook); |
| 263 | continue; |
| 264 | } |
| 265 | // Replace this root's prior block; go in right after the shebang so a |
| 266 | // user hook that ends in `exit` or `exec` can't skip it. |
| 267 | const base = stripMarkerBlocks(existing, owns).replace(/\s*$/, ''); |
| 268 | if (!isEffectivelyEmpty(base)) { |
| 269 | const lines = base.split('\n'); |
| 270 | const shebang = lines[0]!.startsWith('#!') ? lines.shift()! : null; |
| 271 | const rest = lines.join('\n').replace(/^\s*\n/, ''); |
| 272 | content = `${shebang ? `${shebang}\n` : ''}${block}\n\n${rest}\n`; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | fs.writeFileSync(file, content); |
| 277 | chmodExecutable(file); |
| 278 | installed.push(hook); |
| 279 | } |
| 280 | |
| 281 | return unsupported.length > 0 ? { installed, hooksDir, unsupported } : { installed, hooksDir }; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Remove the CodeGraph sync hooks. Strips only our marker block; deletes the |
no test coverage detected