gastownhall/beads · error

unexpected git rev-parse output

Error message

unexpected git rev-parse output

What it means

resolveGitHooksDir shells out to 'git rev-parse' expecting exactly two lines of output: the repo root and the git common dir. If the trimmed output has fewer than two lines, the function cannot locate the hooks directory and returns this error. It indicates git did not answer in the expected format, usually because the target is not inside a git worktree.

Source

Thrown at cmd/bd/doctor/hooks_migration.go:297

		if strings.Contains(lower, "bd") || strings.Contains(lower, "beads") {
			return true
		}
	}

	return false
}

func resolveGitHooksDir(path string) (repoRoot string, hooksDir string, err error) {
	cmd := exec.Command("git", "rev-parse", "--show-toplevel", "--git-common-dir")
	cmd.Dir = path
	out, err := cmd.Output()
	if err != nil {
		return "", "", err
	}

	lines := strings.Split(strings.TrimSpace(string(out)), "\n")
	if len(lines) < 2 {
		return "", "", fmt.Errorf("unexpected git rev-parse output")
	}

	repoRoot = strings.TrimSpace(lines[0])
	gitCommonDir := strings.TrimSpace(lines[1])
	if !filepath.IsAbs(gitCommonDir) {
		gitCommonDir = filepath.Join(repoRoot, gitCommonDir)
	}

	hooksDir = filepath.Join(gitCommonDir, "hooks")

	hooksPathCmd := exec.Command("git", "config", "--get", "core.hooksPath")
	hooksPathCmd.Dir = repoRoot
	if hooksPathOut, hooksPathErr := hooksPathCmd.Output(); hooksPathErr == nil {
		hooksPath := strings.TrimSpace(string(hooksPathOut))
		if hooksPath != "" {
			hooksPath = expandHookPathTilde(hooksPath)
			if !filepath.IsAbs(hooksPath) {
				hooksPath = filepath.Join(repoRoot, hooksPath)

View on GitHub (pinned to 71377f2769)

Solutions

  1. Run the command from inside a git worktree (git rev-parse --is-inside-work-tree should say true).
  2. Check git is installed and reasonably current: git --version; upgrade if very old.
  3. Inspect unset or wrong GIT_DIR / GIT_WORK_TREE environment variables and remove them if unintended.
  4. Verify the repo is not bare/corrupt: ls .git and run git status to confirm it is healthy.

Example fix

// before
$ cd ~/not-a-repo && bd doctor hooks --plan .
Error: unexpected git rev-parse output
// after
$ cd /path/to/repo && bd doctor hooks --plan .
✓ migration plan built
Defensive patterns

Strategy: validation

Validate before calling

out, err := exec.Command("git", "-C", path, "rev-parse", "--is-inside-work-tree").Output()
if err != nil || strings.TrimSpace(string(out)) != "true" {
    return fmt.Errorf("%s is not inside a git worktree", path)
}

Try / catch

plan, err := doctor.PlanHookMigration(path)
if err != nil && strings.Contains(err.Error(), "unexpected git rev-parse output") {
    // not a git repo (or broken .git): surface a friendly message
    return fmt.Errorf("%s is not a usable git repository", path)
}

Prevention

When it happens

Trigger: git rev-parse --git-common-dir --show-toplevel (as invoked by resolveGitHooksDir) prints fewer than 2 newline-separated lines — typically because the path is not a git repository, so rev-parse fails or prints one line / nothing.

Common situations: Running bd doctor hook migration outside any git repo; inside a bare repo where --show-toplevel is empty; a broken .git directory; an ancient git version emitting different output; GIT_DIR/GIT_WORK_TREE env vars pointing somewhere invalid.

Related errors


AI-assisted analysis of gastownhall/beads@71377f2769 (2026-08-30). Data as JSON: /api/errors/f93e5c9d9eecc1a5. Report an issue: GitHub.