class documentation

class WorkTree:

Constructor: WorkTree(repo, path)

View In Hierarchy

Working tree operations for a Git repository.

This class provides methods for working with the working tree, such as staging files, committing changes, and resetting the index.

Method __init__ Initialize a WorkTree for the given repository.
Method commit Create a new commit.
Method configure_for_cone_mode Ensure the repository is configured for cone-mode sparse-checkout.
Method get_sparse_checkout_patterns Return a list of sparse-checkout patterns from info/sparse-checkout.
Method infer_cone_mode Return True if 'core.sparseCheckoutCone' is set to 'true' in config, else False.
Method reset_index Reset the index back to a specific tree.
Method set_cone_mode_patterns Write the given cone-mode directory patterns into info/sparse-checkout.
Method set_sparse_checkout_patterns Write the given sparse-checkout patterns into info/sparse-checkout.
Method stage Stage a set of paths.
Method unstage Unstage specific file in the index.
Instance Variable path Undocumented
Method _sparse_checkout_file_path Return the path of the sparse-checkout file in this repo's control dir.
Instance Variable _repo Undocumented
def __init__(self, repo: Repo, path: str | bytes | os.PathLike[str]):

Initialize a WorkTree for the given repository.

Parameters
repo:RepoThe repository this working tree belongs to
path:str | bytes | os.PathLike[str]Path to the working tree directory
def commit(self, message: str | bytes | Callable[[Any, Commit], bytes] | None = None, committer: bytes | None = None, author: bytes | None = None, commit_timestamp: float | None = None, commit_timezone: int | None = None, author_timestamp: float | None = None, author_timezone: int | None = None, tree: ObjectID | None = None, encoding: bytes | None = None, ref: Ref | None = Ref(b'HEAD'), merge_heads: Sequence[ObjectID] | None = None, no_verify: bool = False, sign: bool | None = None, signoff: bool | None = None) -> ObjectID:

Create a new commit.

If not specified, committer and author default to get_user_identity(..., 'COMMITTER') and get_user_identity(..., 'AUTHOR') respectively.

Parameters
message:str | bytes | Callable[[Any, Commit], bytes] | NoneCommit message (bytes or callable that takes (repo, commit) and returns bytes)
committer:bytes | NoneCommitter fullname
author:bytes | NoneAuthor fullname
commit_timestamp:float | NoneCommit timestamp (defaults to now)
commit_timezone:int | NoneCommit timestamp timezone (defaults to GMT)
author_timestamp:float | NoneAuthor timestamp (defaults to commit timestamp)
author_timezone:int | NoneAuthor timestamp timezone (defaults to commit timestamp timezone)
tree:ObjectID | NoneSHA1 of the tree root to use (if not specified the current index will be committed).
encoding:bytes | NoneEncoding
ref:Ref | NoneOptional ref to commit to (defaults to current branch). If None, creates a dangling commit without updating any ref.
merge_heads:Sequence[ObjectID] | NoneMerge heads (defaults to .git/MERGE_HEAD)
no_verify:boolSkip pre-commit and commit-msg hooks
sign:bool | NoneGPG Sign the commit (bool, defaults to False, pass True to use default GPG key, pass a str containing Key ID to use a specific GPG key)
signoff:bool | NoneAdd Signed-off-by line (DCO) to commit message. If None, uses format.signoff config.
Returns
ObjectIDNew commit SHA1
def configure_for_cone_mode(self):

Ensure the repository is configured for cone-mode sparse-checkout.

def get_sparse_checkout_patterns(self) -> list[str]:

Return a list of sparse-checkout patterns from info/sparse-checkout.

Returns
list[str]A list of patterns. Returns an empty list if the file is missing.
def infer_cone_mode(self) -> bool:

Return True if 'core.sparseCheckoutCone' is set to 'true' in config, else False.

def reset_index(self, tree: ObjectID | None = None):

Reset the index back to a specific tree.

Parameters
tree:ObjectID | NoneTree SHA to reset to, None for current HEAD tree.
def set_cone_mode_patterns(self, dirs: Sequence[str] | None = None):

Write the given cone-mode directory patterns into info/sparse-checkout.

For each directory to include, add an inclusion line that "undoes" the prior !/*/ 'exclude' that re-includes that directory and everything under it. Never add the same line twice.

def set_sparse_checkout_patterns(self, patterns: Sequence[str]):

Write the given sparse-checkout patterns into info/sparse-checkout.

Creates the info/ directory if it does not exist.

Parameters
patterns:Sequence[str]A list of gitignore-style patterns to store.
def stage(self, fs_paths: str | bytes | os.PathLike[str] | Iterable[str | bytes | os.PathLike[str]]):

Stage a set of paths.

Parameters
fs_paths:str | bytes | os.PathLike[str] | Iterable[str | bytes | os.PathLike[str]]List of paths, relative to the repository path
def unstage(self, fs_paths: Sequence[str]):

Unstage specific file in the index.

Parameters
fs_paths:Sequence[str]a list of files to unstage, relative to the repository path.
path: str =

Undocumented

def _sparse_checkout_file_path(self) -> str:

Return the path of the sparse-checkout file in this repo's control dir.

_repo =

Undocumented