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