_git.py 784 B

123456789101112131415161718192021222324252627
  1. """Git utilities, adopted from mypy's git utilities (https://github.com/python/mypy/blob/master/mypy/git.py)."""
  2. from __future__ import annotations
  3. import os
  4. import subprocess
  5. def is_git_repo(dir: str) -> bool:
  6. """Is the given directory version-controlled with git?"""
  7. return os.path.exists(os.path.join(dir, '.git'))
  8. def have_git() -> bool:
  9. """Can we run the git executable?"""
  10. try:
  11. subprocess.check_output(['git', '--help'])
  12. return True
  13. except subprocess.CalledProcessError:
  14. return False
  15. except OSError:
  16. return False
  17. def git_revision(dir: str) -> str:
  18. """Get the SHA-1 of the HEAD of a git repository."""
  19. return subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], cwd=dir).decode('utf-8').strip()