mislav/hub · error

unable to determine git working directory

Error message

unable to determine git working directory

What it means

WorkdirName() runs `git rev-parse --show-toplevel` and throws when the command's first output line is empty, meaning no top-level working directory could be determined. Note the underlying err is still returned alongside this sentinel, and rev-parse also fails outside a repository.

Source

Thrown at git/git.go:74

		gitDir, err = filepath.Abs(gitDir)
		if err != nil {
			return "", err
		}

		gitDir = filepath.Clean(gitDir)
	}

	cachedDir = gitDir
	return gitDir, nil
}

func WorkdirName() (string, error) {
	toplevelCmd := gitCmd("rev-parse", "--show-toplevel")
	toplevelCmd.Stderr = nil
	output, err := toplevelCmd.Output()
	dir := firstLine(output)
	if dir == "" {
		return "", fmt.Errorf("unable to determine git working directory")
	}
	return dir, err
}

func HasFile(segments ...string) bool {
	// The blessed way to resolve paths within git dir since Git 2.5.0
	pathCmd := gitCmd("rev-parse", "-q", "--git-path", filepath.Join(segments...))
	pathCmd.Stderr = nil
	if output, err := pathCmd.Output(); err == nil {
		if lines := outputLines(output); len(lines) == 1 {
			if _, err := os.Stat(lines[0]); err == nil {
				return true
			}
		}
	}

	// Fallback for older git versions
	dir, err := Dir()

View on GitHub (pinned to 5c547ed804)

Solutions

  1. Run from inside a regular (non-bare) git work tree
  2. Do not run the tool from inside the .git directory itself
  3. Verify with `git rev-parse --show-toplevel` manually
  4. Check the accompanying err return for the underlying cause

Example fix

// before
dir, err := git.WorkdirName()
// after
dir, err := git.WorkdirName()
if err != nil {
    return fmt.Errorf("run from inside a git work tree: %w", err)
}
if dir == "" {
    return fmt.Errorf("empty workdir, not a git work tree")
}
Defensive patterns

Strategy: validation

Validate before calling

out, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
if err != nil || len(strings.TrimSpace(string(out))) == 0 {
    return fmt.Errorf("not inside a git work tree; cd into a checked-out repository")
}

Try / catch

dir, err := git.WorkdirName()
if err != nil || dir == "" {
    return fmt.Errorf("unable to determine working directory; run inside a non-bare git work tree")
}

Prevention

When it happens

Trigger: Calling git.WorkdirName() (via create, createIssue, pullRequest, transformRemoteArgs) outside a git repository, or in odd contexts where `--show-toplevel` prints nothing (e.g. bare repo, or in a .git directory itself where toplevel is unavailable).

Common situations: Running inside a bare repository; executing from within the .git directory; running in a non-repo directory; Windows/UTF-8 locale quirks historically made --show-toplevel fail on non-ASCII paths.

Related errors


AI-assisted analysis of mislav/hub@5c547ed804 (2026-09-01). Data as JSON: /api/errors/9afbbd433b99df64. Report an issue: GitHub.