Git Utilities¶
Git operations for version control integration in CI/CD.
git
¶
Git utilities for repository operations.
This module provides functions for working with Git repositories, including URL parsing, cloning, and commit hash retrieval.
Classes¶
GitUrl
¶
Bases: ValidatedStr
Validated string representing a Git repository URL.
Supports various Git URL formats including HTTPS, SSH, and git protocols. Can extract repository name and optional ref (branch/tag/commit).
Attributes:
| Name | Type | Description |
|---|---|---|
regex_pattern |
Pattern
|
Compiled regex pattern for URL validation. |
Functions¶
is_repo_url
¶
Check if a URL is a valid Git repository URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL to validate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the URL is a valid Git repository URL, False otherwise. |
Source code in src/aibs_informatics_cdk_lib/common/git.py
is_local_repo
¶
Check if a path is a local Git repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_path
|
Union[str, Path]
|
The file system path to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the path is a local Git repository, False otherwise. |
Source code in src/aibs_informatics_cdk_lib/common/git.py
get_commit_hash
¶
Get the HEAD commit hash of a Git repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_url_or_path
|
Union[str, Path]
|
The repository URL or local path. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
The commit hash of the HEAD reference. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input is neither a valid URL nor a local repository. |
Source code in src/aibs_informatics_cdk_lib/common/git.py
get_repo_url_components
¶
Extract base URL and ref from a Git repository URL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_url
|
str
|
The full repository URL. |
required |
Returns:
| Type | Description |
|---|---|
tuple[str, str | None]
|
Tuple of (base_url, ref) where ref may be None. |
Source code in src/aibs_informatics_cdk_lib/common/git.py
get_commit_hash_from_url
¶
Get the commit hash from a remote Git repository URL.
Uses git ls-remote to fetch the commit hash without cloning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_url
|
str
|
The repository URL. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The commit hash of the HEAD or specified branch. |
Raises:
| Type | Description |
|---|---|
CalledProcessError
|
If git ls-remote fails. |
Source code in src/aibs_informatics_cdk_lib/common/git.py
get_commit_hash_from_local
¶
Get the HEAD commit hash from a local Git repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_path
|
Union[str, Path]
|
Path to the local repository. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The commit hash of HEAD. |
Raises:
| Type | Description |
|---|---|
CalledProcessError
|
If git rev-parse fails. |
Source code in src/aibs_informatics_cdk_lib/common/git.py
get_repo_name
¶
Get the repository name from a URL or local path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_url_or_path
|
Union[str, Path]
|
The repository URL or local path. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The repository name. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input is neither a valid URL nor a local repository. |
CalledProcessError
|
If git commands fail. |
Source code in src/aibs_informatics_cdk_lib/common/git.py
construct_repo_path
¶
Construct a deterministic path for a cloned repository.
The path includes the repository name and commit hash to ensure unique paths for different versions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_url
|
str
|
The repository URL. |
required |
target_dir
|
Optional[Union[str, Path]]
|
Base directory for the path. Defaults to system temp directory. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path where the repository should be cloned. |
Source code in src/aibs_informatics_cdk_lib/common/git.py
clone_repo
¶
clone_repo(
repo_url: str,
target_dir: str | Path | None = None,
skip_if_exists: bool = True,
) -> Path
Clone a Git repository into a target directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_url
|
str
|
The URL of the Git repository. |
required |
target_dir
|
Optional[Union[str, Path]]
|
Target directory to store repo under. The repo will be written to a subdirectory. Defaults to temp directory. |
None
|
skip_if_exists
|
bool
|
Skip cloning if the target directory already exists and the commit hash matches. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the cloned repository. |