ReviewOS

also looking at this

stacks/gitlint

fix: support linked Git worktrees

#551
Merged chris wants to merge agent/worktree-git-paths into main
4 files +122 -13
src/hooks.tsmodified+30-10
Changes to src/hooks.ts
@@ -1,13 +1,14 @@
1import { execSync } from 'node:child_process'
1import { execFileSync } from 'node:child_process'
22import fs from 'node:fs'
33import path from 'node:path'
4import process from 'node:process'
45
56/**
67 * Find the Git repository root directory
78 */
8function findGitRoot(): string | null {
9function findGitRoot(cwd: string): string | null {
910 try {
10 const gitRoot = execSync('git rev-parse --show-toplevel', { encoding: 'utf8' }).trim()
11 const gitRoot = execFileSync('git', ['rev-parse', '--show-toplevel'], { cwd, encoding: 'utf8' }).trim()
1112 return gitRoot
1213 }
1314 // eslint-disable-next-line unused-imports/no-unused-vars
@@ -16,18 +17,32 @@ function findGitRoot(): string | null {
1617 }
1718}
1819
20/** Resolve the effective hooks directory for main checkouts and linked worktrees. */
21export function resolveHooksDirectory(cwd: string = process.cwd()): string | null {
22 try {
23 const hooksPath = execFileSync('git', ['rev-parse', '--git-path', 'hooks'], {
24 cwd,
25 encoding: 'utf8',
26 }).trim()
27 return path.resolve(cwd, hooksPath)
28 }
29 catch {
30 return null
31 }
32}
33
1934/**
2035 * Install Git hooks for the current repository
2136 */
22export function installGitHooks(force = false): boolean {
23 const gitRoot = findGitRoot()
37export function installGitHooks(force = false, cwd: string = process.cwd()): boolean {
38 const gitRoot = findGitRoot(cwd)
2439 if (!gitRoot) {
2540 console.error('Not a git repository')
2641 return false
2742 }
2843
29 const hooksDir = path.join(gitRoot, '.git', 'hooks')
30 if (!fs.existsSync(hooksDir)) {
44 const hooksDir = resolveHooksDirectory(cwd)
45 if (!hooksDir || !fs.existsSync(hooksDir)) {
3146 console.error(`Git hooks directory not found: ${hooksDir}`)
3247 return false
3348 }
@@ -63,14 +78,19 @@ gitlint --edit "$1"
6378/**
6479 * Uninstall Git hooks for the current repository
6580 */
66export function uninstallGitHooks(): boolean {
67 const gitRoot = findGitRoot()
81export function uninstallGitHooks(cwd: string = process.cwd()): boolean {
82 const gitRoot = findGitRoot(cwd)
6883 if (!gitRoot) {
6984 console.error('Not a git repository')
7085 return false
7186 }
7287
73 const commitMsgHookPath = path.join(gitRoot, '.git', 'hooks', 'commit-msg')
88 const hooksDir = resolveHooksDirectory(cwd)
89 if (!hooksDir) {
90 console.error('Git hooks directory could not be resolved')
91 return false
92 }
93 const commitMsgHookPath = path.join(hooksDir, 'commit-msg')
7494 if (!fs.existsSync(commitMsgHookPath)) {
7595 console.error('No commit-msg hook found')
7696 return true