Coverage for pydantic/_internal/_git.py: 100.00%
7 statements
« prev ^ index » next coverage.py v7.8.1, created at 2025-05-22 20:36 +0000
« prev ^ index » next coverage.py v7.8.1, created at 2025-05-22 20:36 +0000
1"""Git utilities, adopted from mypy's git utilities (https://github.com/python/mypy/blob/master/mypy/git.py)."""
3from __future__ import annotations 1abcdefghijklmnopqrstuvwxyzABCDEFGHI
5import subprocess 1abcdefghijklmnopqrstuvwxyzABCDEFGHI
6from pathlib import Path 1abcdefghijklmnopqrstuvwxyzABCDEFGHI
9def is_git_repo(dir: Path) -> bool: 1abcdefghijklmnopqrstuvwxyzABCDEFGHI
10 """Is the given directory version-controlled with git?"""
11 return dir.joinpath('.git').exists() 1abcdefghijklmnopqrstuvwxyzABCDEFGHI
14def have_git() -> bool: # pragma: no cover 1abcdefghijklmnopqrstuvwxyzABCDEFGHI
15 """Can we run the git executable?"""
16 try: 1abcdefghijklmnopqrstuvwxyzABCDEFGHI
17 subprocess.check_output(['git', '--help']) 1abcdefghijklmnopqrstuvwxyzABCDEFGHI
18 return True 1abcdefghijklmnopqrstuvwxyzABCDEFGHI
19 except subprocess.CalledProcessError:
20 return False
21 except OSError:
22 return False
25def git_revision(dir: Path) -> str: 1abcdefghijklmnopqrstuvwxyzABCDEFGHI
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() 1abcdefghijklmnopqrstuvwxyzABCDEFGHI