Skip to content

git_commands

Functions for using git.

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(repo: Repo, branch_name: str) -> bool

Does the branch exist in the repo?

Parameters:

repo

The repository to check

TYPE: Repo

branch_name

The name of the branch to check for

TYPE: str

Returns:

bool

True if the branch exists

checkout_branch(
    repo: Repo,
    branch_name: str,
    remote_name: str = "origin",
) -> None

Checkout a local or remote branch.

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(
    repo_url: str, dest_path: Optional[Path] = None
) -> Repo

Clone a repo.

Parameters:

repo_url

Repo URL or local path.

TYPE: str

dest_path

The path to clone to.

TYPE: Optional[Path]

DEFAULT: None

Returns:

Repo

The repository.

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

TYPE: Union[str, Path]

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 dirty

TYPE: bool

DEFAULT: False

Raises:

GitError

If the directory is not a git repo

GitError

If the directory git repository is dirty

Returns:

Repo

The GitPython Repo object

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(
    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.

TYPE: Optional[Path]

DEFAULT: None

branch

The branch to check out

TYPE: Optional[str]

DEFAULT: None

commit

The optional commit to check out

TYPE: Optional[str]

DEFAULT: None

YIELDS DESCRIPTION
Path

The worktree_path

Raises:

GitError

If the worktree could not be created