Skip to content

Commit

Permalink
Adding short commit to version_info (#8145)
Browse files Browse the repository at this point in the history
Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com>
  • Loading branch information
sydney-runkle and hramezani committed Nov 16, 2023
1 parent f42dc2f commit 575dcd9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
26 changes: 26 additions & 0 deletions pydantic/_internal/_git.py
@@ -0,0 +1,26 @@
"""Git utilities, adopted from mypy's git utilities (https://github.com/python/mypy/blob/master/mypy/git.py)."""
from __future__ import annotations

import os
import subprocess


def is_git_repo(dir: str) -> bool:
"""Is the given directory version-controlled with git?"""
return os.path.exists(os.path.join(dir, '.git'))


def have_git() -> bool:
"""Can we run the git executable?"""
try:
subprocess.check_output(['git', '--help'])
return True
except subprocess.CalledProcessError:
return False
except OSError:
return False


def git_revision(dir: str) -> str:
"""Get the SHA-1 of the HEAD of a git repository."""
return subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD'], cwd=dir).decode('utf-8').strip()
9 changes: 9 additions & 0 deletions pydantic/version.py
Expand Up @@ -18,12 +18,15 @@ def version_short() -> str:
def version_info() -> str:
"""Return complete version information for Pydantic and its dependencies."""
import importlib.metadata as importlib_metadata
import os
import platform
import sys
from pathlib import Path

import pydantic_core._pydantic_core as pdc

from ._internal import _git as git

# get data about packages that are closely related to pydantic, use pydantic or often conflict with pydantic
package_names = {
'email-validator',
Expand All @@ -41,6 +44,11 @@ def version_info() -> str:
if name in package_names:
related_packages.append(f'{name}-{dist.version}')

pydantic_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
most_recent_commit = (
git.git_revision(pydantic_dir) if git.is_git_repo(pydantic_dir) and git.have_git() else 'unknown'
)

info = {
'pydantic version': VERSION,
'pydantic-core version': pdc.__version__,
Expand All @@ -49,6 +57,7 @@ def version_info() -> str:
'python version': sys.version,
'platform': platform.platform(),
'related packages': ' '.join(related_packages),
'commit': most_recent_commit,
}
return '\n'.join('{:>30} {}'.format(k + ':', str(v).replace('\n', ' ')) for k, v in info.items())

Expand Down
16 changes: 13 additions & 3 deletions tests/test_version.py
@@ -1,4 +1,3 @@
import re
from unittest.mock import patch

import pytest
Expand All @@ -9,9 +8,20 @@


def test_version_info():
version_info_fields = [
'pydantic version',
'pydantic-core version',
'pydantic-core build',
'install path',
'python version',
'platform',
'related packages',
'commit',
]

s = version_info()
assert re.match(' *pydantic version: ', s)
assert s.count('\n') == 6
assert all([f'{field}:' in s for field in version_info_fields])
assert s.count('\n') == 7


def test_standard_version():
Expand Down

0 comments on commit 575dcd9

Please sign in to comment.