Skip to content

Bug | Any git error other than "not a git repository" makes every command crash with a TypeError #356

Closed

Description

@diego-ojeda-binbash

What?

get_root_path() (leverage/path.py) runs git rev-parse --show-toplevel. When git fails, it only turns the error into NotARepositoryError if stderr contains fatal: not a git repository. Any other git error makes it return None silently, and the caller crashes:

  File "leverage/leverage.py", line 35, in leverage
    state.config = conf.load()
  File "leverage/conf.py", line 33, in load
    root_path = Path(get_root_path())
TypeError: argument should be a str or an os.PathLike object where __fspath__ returns a str, not 'NoneType'

Every command except --help goes through conf.load(), so the whole CLI is unusable, with a traceback that doesn't mention git.

When does git fail with another message?

  • fatal: detected dubious ownership in repository at ...: the repository belongs to another user (Docker volumes, WSL, shared folders, CI runners). Git refuses to work there until safe.directory is set.
  • git with a non-English locale: the not a git repository message is translated, so even the "outside a repository" case returns None.

Repro

A fake git that answers like the dubious ownership case:

mkdir -p /tmp/fakegit && cat > /tmp/fakegit/git <<'SH'
#!/bin/sh
echo "fatal: detected dubious ownership in repository at '/work/project'" >&2
exit 128
SH
chmod +x /tmp/fakegit/git
cd /tmp && PATH=/tmp/fakegit:$PATH leverage tofu plan   # TypeError traceback

Proposal

  • Any CalledProcessError from git has to end in an error, never None.
  • Decide what each case shows:
    • Outside a repository: keep NotARepositoryError. Detect it from the exit code (128) plus git rev-parse --is-inside-work-tree, or force LC_ALL=C for the call, instead of matching the English message.
    • Any other error: a clear message with git's own stderr (e.g. the safe.directory hint), instead of treating it as "not a repository", which would hide the cause.
  • Add a test with a fake git on PATH, like the real-git tests in tests/test_root_path.py.

Found while rewriting tests/test_root_path.py (#346, hygiene).

🤖 Generated with Claude Code

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions