module documentation

Working tree operations for Git repositories.

Class WorkTree Working tree operations for a Git repository.
Class WorkTreeContainer Container for managing multiple working trees.
Class WorkTreeInfo Information about a single worktree.
Function add_worktree Add a new worktree to the repository.
Function list_worktrees List all worktrees for the given repository.
Function lock_worktree Lock a worktree to prevent it from being pruned.
Function move_worktree Move a worktree to a new location.
Function prune_worktrees Prune worktree administrative files for missing worktrees.
Function read_worktree_lock_reason Read the lock reason for a worktree.
Function remove_worktree Remove a worktree.
Function repair_worktree Repair worktree administrative files.
Function temporary_worktree Create a temporary worktree that is automatically cleaned up.
Function unlock_worktree Unlock a worktree.
Function _compute_gitdir_path Compute the gitdir path and enable extension if needed.
Function _find_worktree_id Find the worktree identifier for the given path.
Function _should_use_relative_paths Determine whether to use relative paths for gitdir references.
def add_worktree(repo: Repo, path: str | bytes | os.PathLike[str], branch: str | bytes | None = None, commit: ObjectID | None = None, force: bool = False, detach: bool = False, exist_ok: bool = False, relative_paths: bool | None = None) -> Repo:

Add a new worktree to the repository.

Parameters
repo:RepoThe main repository
path:str | bytes | os.PathLike[str]Path where the new worktree should be created
branch:str | bytes | NoneBranch to checkout in the new worktree (creates if doesn't exist)
commit:ObjectID | NoneSpecific commit to checkout (results in detached HEAD)
force:boolForce creation even if branch is already checked out elsewhere
detach:boolDetach HEAD in the new worktree
exist_ok:boolIf True, do not raise an error if the directory already exists
relative_paths:bool | NoneIf True, use relative paths for gitdir references. If None, check worktree.useRelativePaths config (defaults to False)
Returns
RepoThe newly created worktree repository
Raises
ValueErrorIf the path already exists (and exist_ok is False) or branch is already checked out
def list_worktrees(repo: Repo) -> list[WorkTreeInfo]:

List all worktrees for the given repository.

Parameters
repo:RepoThe repository to list worktrees for
Returns
list[WorkTreeInfo]A list of WorkTreeInfo objects
def lock_worktree(repo: Repo, path: str | bytes | os.PathLike[str], reason: str | None = None):

Lock a worktree to prevent it from being pruned.

Parameters
repo:RepoThe main repository
path:str | bytes | os.PathLike[str]Path to the worktree to lock
reason:str | NoneOptional reason for locking
def move_worktree(repo: Repo, old_path: str | bytes | os.PathLike[str], new_path: str | bytes | os.PathLike[str], relative_paths: bool | None = None):

Move a worktree to a new location.

Parameters
repo:RepoThe main repository
old_path:str | bytes | os.PathLike[str]Current path of the worktree
new_path:str | bytes | os.PathLike[str]New path for the worktree
relative_paths:bool | NoneIf True, use relative paths for gitdir references. If None, check worktree.useRelativePaths config or preserve existing format
Raises
ValueErrorIf the worktree doesn't exist or new path already exists
def prune_worktrees(repo: Repo, expire: int | None = None, dry_run: bool = False) -> list[str]:

Prune worktree administrative files for missing worktrees.

Parameters
repo:RepoThe main repository
expire:int | NoneOnly prune worktrees older than this many seconds
dry_run:boolDon't actually remove anything, just report what would be removed
Returns
list[str]List of pruned worktree identifiers
def read_worktree_lock_reason(worktree_path: str) -> str | None:

Read the lock reason for a worktree.

Parameters
worktree_path:strPath to the worktree's administrative directory
Returns
str | NoneThe lock reason if the worktree is locked, None otherwise
def remove_worktree(repo: Repo, path: str | bytes | os.PathLike[str], force: bool = False):

Remove a worktree.

Parameters
repo:RepoThe main repository
path:str | bytes | os.PathLike[str]Path to the worktree to remove
force:boolForce removal even if there are local changes
Raises
ValueErrorIf the worktree doesn't exist, has local changes, or is locked
def repair_worktree(repo: Repo, paths: Sequence[str | bytes | os.PathLike[str]] | None = None, relative_paths: bool | None = None) -> list[str]:

Repair worktree administrative files.

This repairs the connection between worktrees and the main repository when they have been moved or become corrupted.

Parameters
repo:RepoThe main repository
paths:Sequence[str | bytes | os.PathLike[str]] | NoneOptional list of worktree paths to repair. If None, repairs connections from the main repository to all linked worktrees.
relative_paths:bool | NoneIf True, use relative paths for gitdir references. If None, check worktree.useRelativePaths config or preserve existing format
Returns
list[str]List of repaired worktree paths
Raises
ValueErrorIf a specified path is not a valid worktree
def temporary_worktree(repo: Repo, prefix: str = 'tmp-worktree-') -> Iterator[Repo]:

Create a temporary worktree that is automatically cleaned up.

Parameters
repo:RepoDulwich repository object
prefix:strPrefix for the temporary directory name
Returns
Iterator[Repo]Undocumented
Yields
Worktree object
def unlock_worktree(repo: Repo, path: str | bytes | os.PathLike[str]):

Unlock a worktree.

Parameters
repo:RepoThe main repository
path:str | bytes | os.PathLike[str]Path to the worktree to unlock
def _compute_gitdir_path(repo: Repo, gitdir_file: str, worktree_control_dir: str, use_relative: bool) -> str:

Compute the gitdir path and enable extension if needed.

Parameters
repo:RepoThe repository
gitdir_file:strAbsolute path to the .git file
worktree_control_dir:strAbsolute path to the worktree control directory
use_relative:boolWhether to use relative paths
Returns
strThe path to write (relative or absolute)
def _find_worktree_id(repo: Repo, path: str | bytes | os.PathLike[str]) -> str:

Find the worktree identifier for the given path.

Parameters
repo:RepoThe main repository
path:str | bytes | os.PathLike[str]Path to the worktree
Returns
strThe worktree identifier
Raises
ValueErrorIf the worktree is not found
def _should_use_relative_paths(repo: Repo, relative_paths: bool | None, existing_path: bytes | None = None) -> bool:

Determine whether to use relative paths for gitdir references.

Parameters
repo:RepoThe repository
relative_paths:bool | NoneExplicit preference (True/False) or None to check config
existing_path:bytes | NoneOptional existing path to check format (for preserving format)
Returns
boolTrue if relative paths should be used, False otherwise