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.
Parameters:
repo
-
The git repo to apply the patch to
TYPE:
Repo
diff
-
The previously calculated diff
TYPE:
str
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.
Parameters:
repo
-
The repository to check out
TYPE:
Repo
ref
-
The ref to check out
TYPE:
str
clone
¶
clone(
repo_url: str, 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.
Parameters:
project_dir
-
The directory containing the .git folder
search_parent_directories
-
if
True
, all parent directories will be searched for a valid repo as well.TYPE:
bool
DEFAULT:
False
ensure_clean
-
if
True
, raise an error if the repo is dirtyTYPE:
bool
DEFAULT:
False
Raises:
Returns:
-
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?
Parameters:
repo
-
The repository to check
TYPE:
Repo
branch_name
-
The name of the branch to check for
TYPE:
str
remote_name
-
The name of the remote reference. Defaults to
origin
TYPE:
str
DEFAULT:
'origin'
Returns:
-
bool
-
True
if the branch exists in the remote repository
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
Parameters:
repo_path
-
The path to the template git repo
TYPE:
Path
worktree_path
-
The path put the worktree in. Defaults to a temporary directory.
DEFAULT:
None
branch
-
The branch to check out
DEFAULT:
None
commit
-
The optional commit to check out
DEFAULT:
None
YIELDS | DESCRIPTION |
---|---|
Path
|
The worktree_path |
Raises:
-
GitError
-
If the worktree could not be created