hbutils.system.git

This module provides functionality for checking and retrieving information about Git and Git LFS installations.

It includes functions to:

  1. Check if Git is installed and get its version information.

  2. Check if Git LFS is installed and get its version information.

  3. Cache the results of these checks for improved performance.

The module uses subprocess to run Git commands and parse their output, providing detailed information about the Git and Git LFS installations on the system.

This is the main entry point for the git system utilities, which re-exports all functionality from the info module.

Example::
>>> from hbutils.system.git import git_info
>>> info = git_info()
>>> if info['installed']:
...     print(f"Git version: {info['version']}")
...     if info['lfs']['installed']:
...         print(f"Git LFS version: {info['lfs']['version']}")

git_info

hbutils.system.git.git_info(git_path: str | None = None)[source]

Get information about Git and Git LFS installations.

This function attempts to locate the Git executable and retrieve information about the Git and Git LFS installations. It first tries to use the provided git_path, then checks the system PATH, and finally falls back to None if Git is not found.

The function normalizes the git_path to ensure consistent caching behavior across different path representations of the same executable.

Parameters:

git_path (Optional[str]) – Optional path to the Git executable. If not provided, the function will attempt to find Git in the system PATH using shutil.which().

Returns:

A dictionary containing information about Git and Git LFS installations. The dictionary structure includes:

  • ’exec’ (str or None): Path to Git executable

  • ’installed’ (bool): Whether Git is installed

  • ’version_info’ (str or None): Full version string from ‘git –version’

  • ’version’ (str or None): Parsed Git version number

  • ’lfs’ (dict): Git LFS information (only present if Git is installed)

    • ’installed’ (bool): Whether Git LFS is installed

    • ’version_info’ (str): Full version string from ‘git lfs version’

    • ’version’ (str or None): Parsed Git LFS version number

Return type:

dict

Warns UserWarning:

If Git version information cannot be parsed or if Git LFS version information is unrecognizable.

Example::
>>> info = git_info()
>>> print(info['installed'])  # Check if Git is installed
True
>>> print(info['version'])    # Get Git version
'2.34.1'
>>> print(info['lfs']['installed'])  # Check if Git LFS is installed
True
Example with custom git path::
>>> info = git_info('/usr/local/bin/git')
>>> if info['installed']:
...     print(f"Using Git at: {info['exec']}")
Using Git at: /usr/local/bin/git

The returned dictionary has the following structure:

{
    'exec': str,          # Path to Git executable
    'installed': bool,    # Whether Git is installed
    'version_info': str,  # Full version string from 'git --version'
    'version': str,       # Parsed Git version
    'lfs': {              # Information about Git LFS
        'installed': bool,    # Whether Git LFS is installed
        'version_info': str,  # Full version string from 'git lfs version'
        'version': str        # Parsed Git LFS version
    }
}