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

15 statements  

« prev     ^ index     » next       coverage.py v7.5.4, created at 2024-07-03 19:29 +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 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

4 

5import os 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

6import subprocess 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

7 

8 

9def is_git_repo(dir: str) -> bool: 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

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

11 return os.path.exists(os.path.join(dir, '.git')) 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

12 

13 

14def have_git() -> bool: 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

15 """Can we run the git executable?""" 

16 try: 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

17 subprocess.check_output(['git', '--help']) 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

18 return True 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

19 except subprocess.CalledProcessError: 

20 return False 

21 except OSError: 

22 return False 

23 

24 

25def git_revision(dir: str) -> str: 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL

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() 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL