Coverage for pydantic / _internal / _git.py: 100.00%
7 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-11-21 11:56 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-11-21 11:56 +0000
1"""Git utilities, adopted from mypy's git utilities (https://github.com/python/mypy/blob/master/mypy/git.py)."""
3from __future__ import annotations 1abcdefghijklmnopqr
5import subprocess 1abcdefghijklmnopqr
6from pathlib import Path 1abcdefghijklmnopqr
9def is_git_repo(dir: Path) -> bool: 1abcdefghijklmnopqr
10 """Is the given directory version-controlled with git?"""
11 return dir.joinpath('.git').exists() 1abcdefghijklmnopqr
14def have_git() -> bool: # pragma: no cover 1abcdefghijklmnopqr
15 """Can we run the git executable?"""
16 try: 1abcdefghijklmnopqr
17 subprocess.check_output(['git', '--help']) 1abcdefghijklmnopqr
18 return True 1abcdefghijklmnopqr
19 except subprocess.CalledProcessError:
20 return False
21 except OSError:
22 return False
25def git_revision(dir: Path) -> str: 1abcdefghijklmnopqr
26 """Get the SHA-1 of the HEAD of a git repository."""
27 return subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], cwd=dir).decode('utf-8').strip() 1abcdefghijklmnopqr