git¶
Git-related utilities.
git ¶
Git-related utilities.
This module provides functions for interacting with git repositories, including running git commands, cloning snippet repositories, and staging modified files for commit.
run_cmd(args, cwd=None, check=True) ¶
Run a shell command and return its stdout, stripped of whitespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args | list[str] | Command and arguments as a list of strings. | required |
cwd | Path | None | Working directory for the command. Defaults to current directory. | None |
check | bool | If True, raise CalledProcessError on non-zero exit. Defaults to True. | True |
Returns:
| Type | Description |
|---|---|
str | The command's stdout with leading/trailing whitespace removed. |
Raises:
| Type | Description |
|---|---|
CalledProcessError | If check is True and the command fails. |
Source code in pre_commit_snippet/git.py
get_repo_root() ¶
Get the root directory of the current git repository.
Returns:
| Type | Description |
|---|---|
Path | Path to the repository root. |
Raises:
| Type | Description |
|---|---|
CalledProcessError | If not in a git repository. |
Source code in pre_commit_snippet/git.py
clone_snippet_repo(source, dest) ¶
Clone a snippet repository to a destination directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source | SnippetSource | The snippet source configuration. | required |
dest | Path | Destination directory for the clone. | required |
Returns:
| Type | Description |
|---|---|
Path | Path to the snippet root (dest / subdir). |
Raises:
| Type | Description |
|---|---|
CalledProcessError | If the clone fails. |
FileNotFoundError | If the snippet subdirectory doesn't exist. |
Source code in pre_commit_snippet/git.py
cleanup_temp_dir(tmp_dir) ¶
Remove a temporary directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tmp_dir | Path | The directory to remove. | required |
stage_files(files, repo_root) ¶
Stage files for commit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
files | list[str] | List of file paths relative to repo root. | required |
repo_root | Path | Root of the git repository. | required |