git_commands
Functions for using git.
Classes¶
Functions¶
apply_patch
¶
apply_patch(repo: Repo, diff: str) -> None
Apply a patch to a destination directory.
A git 3 way merge is the best bet at applying patches.
PARAMETER | DESCRIPTION |
---|---|
repo
|
The git repo to apply the patch to
TYPE:
|
diff
|
The previously calculated diff
TYPE:
|
branch_exists
¶
branch_exists(repo: Repo, branch_name: str) -> bool
checkout_branch
¶
checkout_branch(
repo: Repo,
branch_name: str,
remote_name: str = "origin",
) -> None
Checkout a local or remote branch.
checkout_ref
¶
checkout_ref(repo: Repo, ref: str) -> None
Checkout a ref.
PARAMETER | DESCRIPTION |
---|---|
repo
|
The repository to check out
TYPE:
|
ref
|
The ref to check out
TYPE:
|
clone
¶
clone(
repo_url: ParsedURL, dest_path: Optional[Path] = None
) -> Repo
get_repo
¶
get_repo(
project_dir: Union[str, Path],
search_parent_directories: bool = False,
ensure_clean: bool = False,
) -> Repo
Get the git Repo object for a directory.
PARAMETER | DESCRIPTION |
---|---|
project_dir
|
The directory containing the .git folder |
search_parent_directories
|
if
TYPE:
|
ensure_clean
|
if
TYPE:
|
RAISES | DESCRIPTION |
---|---|
GitError
|
If the directory is not a git repo |
GitError
|
If the directory git repository is dirty |
RETURNS | DESCRIPTION |
---|---|
Repo
|
The GitPython Repo object |
remote_branch_exists
¶
remote_branch_exists(
repo: Repo,
branch_name: str,
remote_name: str = "origin",
) -> bool
Does the branch exist in the remote repo?
PARAMETER | DESCRIPTION |
---|---|
repo
|
The repository to check
TYPE:
|
branch_name
|
The name of the branch to check for
TYPE:
|
remote_name
|
The name of the remote reference. Defaults to
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
|
temp_git_worktree_dir
¶
temp_git_worktree_dir(
repo_path: Path,
worktree_path: Optional[Path] = None,
branch: Optional[str] = None,
commit: Optional[str] = None,
) -> Iterator[Path]
Context Manager for a temporary working directory of a branch in a git repo.
Inspired by https://github.com/thomasjahoda/cookiecutter_project_upgrader/blob/master/ cookiecutter_project_upgrader/logic.py
Logic for checking out a branch or commit:
- If a commit is provided, use that
- If a branch is provided, and it is not the current branch, use that
- If a branch is provided, and it is the current branch, use the current commit
- If neither a branch nor a commit is provided, use the current branch and commit
PARAMETER | DESCRIPTION |
---|---|
repo_path
|
The path to the template git repo
TYPE:
|
worktree_path
|
The path put the worktree in. Defaults to a temporary directory. |
branch
|
The branch to check out |
commit
|
The optional commit to check out |
YIELDS | DESCRIPTION |
---|---|
Path
|
The worktree_path
TYPE::
|
RAISES | DESCRIPTION |
---|---|
GitError
|
If the worktree could not be created |