lib.py 999 B

12345678910111213141516171819202122232425262728293031
  1. from __future__ import annotations
  2. import sys
  3. from typing import Any, Callable
  4. # Unbounded function cache.
  5. # lru_cache(maxsize=None) has a fast path since it doesn't care about bounds.
  6. # functools.cache is introduced in python 3.9, but is literally just what the shim is.
  7. if sys.version_info >= (3, 9):
  8. from functools import cache
  9. else:
  10. from functools import lru_cache as _lru_cache
  11. def cache(func: Callable[..., Any]) -> Any:
  12. return _lru_cache(maxsize=None)(func)
  13. # Simplified from pre-commit @ fb0ccf3546a9cb34ec3692e403270feb6d6033a2
  14. @cache
  15. def gitroot() -> str:
  16. from os.path import abspath
  17. from subprocess import CalledProcessError, run
  18. try:
  19. proc = run(("git", "rev-parse", "--show-cdup"), check=True, capture_output=True)
  20. root = abspath(proc.stdout.decode().strip())
  21. except CalledProcessError:
  22. raise SystemExit(
  23. "git failed. Is it installed, and are you in a Git repository " "directory?",
  24. )
  25. return root