lib.py 575 B

12345678910111213141516171819
  1. from __future__ import annotations
  2. from functools import cache
  3. # Simplified from pre-commit @ fb0ccf3546a9cb34ec3692e403270feb6d6033a2
  4. @cache
  5. def gitroot() -> str:
  6. from os.path import abspath
  7. from subprocess import CalledProcessError, run
  8. try:
  9. proc = run(("git", "rev-parse", "--show-cdup"), check=True, capture_output=True)
  10. root = abspath(proc.stdout.decode().strip())
  11. except CalledProcessError:
  12. raise SystemExit(
  13. "git failed. Is it installed, and are you in a Git repository " "directory?",
  14. )
  15. return root