fix: support git worktrees in hooks installation

Use `git rev-parse --git-dir` instead of hardcoded `.git` path to find
the actual git directory. In worktrees, `.git` is a file containing a
gitdir pointer, not a directory.

Changes:
- Add getGitDir() helper in hooks.go
- Update installHooks(), uninstallHooks(), CheckGitHooks() to use it
- Update hooksInstalled(), detectExistingHooks(), installGitHooks() in init.go
- Update checkHooksQuick() in doctor.go
- Update GitHooks() in doctor/fix/hooks.go
- Update tests to use real git repos via `git init`

Fixes bd-63l

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-11-29 23:19:57 -08:00
parent fa9285a663
commit 0b13a0df3c
7 changed files with 127 additions and 58 deletions

View File

@@ -13,6 +13,18 @@ import (
"github.com/spf13/cobra"
)
// getGitDir returns the actual .git directory path.
// In a normal repo, this is ".git". In a worktree, .git is a file
// containing "gitdir: /path/to/actual/git/dir", so we use git rev-parse.
func getGitDir() (string, error) {
cmd := exec.Command("git", "rev-parse", "--git-dir")
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("not a git repository: %w", err)
}
return strings.TrimSpace(string(output)), nil
}
//go:embed templates/hooks/*
var hooksFS embed.FS
@@ -46,13 +58,23 @@ func CheckGitHooks() []HookStatus {
hooks := []string{"pre-commit", "post-merge", "pre-push", "post-checkout"}
statuses := make([]HookStatus, 0, len(hooks))
// Get actual git directory (handles worktrees)
gitDir, err := getGitDir()
if err != nil {
// Not a git repo - return all hooks as not installed
for _, hookName := range hooks {
statuses = append(statuses, HookStatus{Name: hookName, Installed: false})
}
return statuses
}
for _, hookName := range hooks {
status := HookStatus{
Name: hookName,
}
// Check if hook exists
hookPath := filepath.Join(".git", "hooks", hookName)
hookPath := filepath.Join(gitDir, "hooks", hookName)
version, err := getHookVersion(hookPath)
if err != nil {
// Hook doesn't exist or couldn't be read
@@ -276,10 +298,10 @@ var hooksListCmd = &cobra.Command{
}
func installHooks(embeddedHooks map[string]string, force bool, shared bool) error {
// Check if .git directory exists
gitDir := ".git"
if _, err := os.Stat(gitDir); os.IsNotExist(err) {
return fmt.Errorf("not a git repository (no .git directory found)")
// Get actual git directory (handles worktrees where .git is a file)
gitDir, err := getGitDir()
if err != nil {
return err
}
var hooksDir string
@@ -338,7 +360,12 @@ func configureSharedHooksPath() error {
}
func uninstallHooks() error {
hooksDir := filepath.Join(".git", "hooks")
// Get actual git directory (handles worktrees)
gitDir, err := getGitDir()
if err != nil {
return err
}
hooksDir := filepath.Join(gitDir, "hooks")
hookNames := []string{"pre-commit", "post-merge", "pre-push", "post-checkout"}
for _, hookName := range hookNames {