Coverage for pydantic/_internal/_git.py: 100.00%

7 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-06-04 10:05 +0000

1"""Git utilities, adopted from mypy's git utilities (https://github.com/python/mypy/blob/master/mypy/git.py).""" 

2 

3from __future__ import annotations 1abcdefghijklmnopqrstuvwxyzABCDEFGHI

4 

5import subprocess 1abcdefghijklmnopqrstuvwxyzABCDEFGHI

6from pathlib import Path 1abcdefghijklmnopqrstuvwxyzABCDEFGHI

7 

8 

9def is_git_repo(dir: Path) -> bool: 1abcdefghijklmnopqrstuvwxyzABCDEFGHI

10 """Is the given directory version-controlled with git?""" 

11 return dir.joinpath('.git').exists() 1abcdefghijklmnopqrstuvwxyzABCDEFGHI

12 

13 

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 

23 

24 

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