module documentation
Working tree operations for Git repositories.
| Class | |
Working tree operations for a Git repository. |
| Class | |
Container for managing multiple working trees. |
| Class | |
Information about a single worktree. |
| Function | add |
Add a new worktree to the repository. |
| Function | list |
List all worktrees for the given repository. |
| Function | lock |
Lock a worktree to prevent it from being pruned. |
| Function | move |
Move a worktree to a new location. |
| Function | prune |
Prune worktree administrative files for missing worktrees. |
| Function | read |
Read the lock reason for a worktree. |
| Function | remove |
Remove a worktree. |
| Function | repair |
Repair worktree administrative files. |
| Function | temporary |
Create a temporary worktree that is automatically cleaned up. |
| Function | unlock |
Unlock a worktree. |
| Function | _compute |
Compute the gitdir path and enable extension if needed. |
| Function | _find |
Find the worktree identifier for the given path. |
| Function | _should |
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:Repo | The main repository |
path:str | bytes | os.PathLike[ | Path where the new worktree should be created |
branch:str | bytes | None | Branch to checkout in the new worktree (creates if doesn't exist) |
commit:ObjectID | None | Specific commit to checkout (results in detached HEAD) |
force:bool | Force creation even if branch is already checked out elsewhere |
detach:bool | Detach HEAD in the new worktree |
existbool | If True, do not raise an error if the directory already exists |
relativebool | None | If True, use relative paths for gitdir references. If None, check worktree.useRelativePaths config (defaults to False) |
| Returns | |
Repo | The newly created worktree repository |
| Raises | |
ValueError | If the path already exists (and exist_ok is False) or branch is already checked out |
List all worktrees for the given repository.
| Parameters | |
repo:Repo | The repository to list worktrees for |
| Returns | |
list[ | A list of WorkTreeInfo objects |
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:Repo | The main repository |
oldstr | bytes | os.PathLike[ | Current path of the worktree |
newstr | bytes | os.PathLike[ | New path for the worktree |
relativebool | None | If True, use relative paths for gitdir references. If None, check worktree.useRelativePaths config or preserve existing format |
| Raises | |
ValueError | If the worktree doesn't exist or new path already exists |
Remove a worktree.
| Parameters | |
repo:Repo | The main repository |
path:str | bytes | os.PathLike[ | Path to the worktree to remove |
force:bool | Force removal even if there are local changes |
| Raises | |
ValueError | If 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:Repo | The main repository |
paths:Sequence[ | Optional list of worktree paths to repair. If None, repairs connections from the main repository to all linked worktrees. |
relativebool | None | If True, use relative paths for gitdir references. If None, check worktree.useRelativePaths config or preserve existing format |
| Returns | |
list[ | List of repaired worktree paths |
| Raises | |
ValueError | If a specified path is not a valid worktree |
Unlock a worktree.
| Parameters | |
repo:Repo | The main repository |
path:str | bytes | os.PathLike[ | 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:Repo | The repository |
gitdirstr | Absolute path to the .git file |
worktreestr | Absolute path to the worktree control directory |
usebool | Whether to use relative paths |
| Returns | |
str | The path to write (relative or absolute) |
Find the worktree identifier for the given path.
| Parameters | |
repo:Repo | The main repository |
path:str | bytes | os.PathLike[ | Path to the worktree |
| Returns | |
str | The worktree identifier |
| Raises | |
ValueError | If 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:Repo | The repository |
relativebool | None | Explicit preference (True/False) or None to check config |
existingbytes | None | Optional existing path to check format (for preserving format) |
| Returns | |
bool | True if relative paths should be used, False otherwise |