Worktree sync - vcspull._internal.worktree_sync¶
Warning
Be careful with these! Internal APIs are not covered by version policies. They can break or be removed between minor versions!
If you need an internal API stabilized please file an issue.
Core worktree synchronization logic for vcspull.
- class vcspull._internal.worktree_sync.WorktreeAction[source]¶
Bases:
EnumActions that can be taken on a worktree during sync.
- CREATE = 'create'¶
Worktree doesn’t exist, will be created.
- UPDATE = 'update'¶
Branch worktree exists, will pull latest.
- UNCHANGED = 'unchanged'¶
Tag/commit worktree exists, already at target.
- BLOCKED = 'blocked'¶
Worktree has uncommitted changes (safety).
- ERROR = 'error'¶
Operation failed (ref not found, permission, etc.).
- class vcspull._internal.worktree_sync.WorktreeCheck[source]¶
Bases:
objectA single check performed during worktree planning.
-
exception:
WorktreeError|None= None¶ Typed exception if the check failed.
-
exception:
- class vcspull._internal.worktree_sync.WorktreePlanEntry[source]¶
Bases:
objectPlanning information for a single worktree operation.
-
action:
WorktreeAction¶ What action will be/was taken.
-
checks:
list[WorktreeCheck]¶ Ordered audit trail of checks performed during planning.
-
action:
- class vcspull._internal.worktree_sync.WorktreeSyncResult[source]¶
Bases:
objectResult of a worktree sync operation.
-
entries:
list[WorktreePlanEntry]¶ List of worktree plan entries.
-
entries:
- vcspull._internal.worktree_sync._get_ref_type_and_value(wt_config)[source]¶
Extract the reference type and value from worktree config.
- Return type:
- Returns:
Tuple of (ref_type, ref_value) or None if invalid config.
- Return type:
- Parameters:
wt_config (WorktreeConfigDict)
Examples
>>> _get_ref_type_and_value({"dir": "../v1", "tag": "v1.0.0"}) ('tag', 'v1.0.0') >>> _get_ref_type_and_value({"dir": "../dev", "branch": "develop"}) ('branch', 'develop') >>> _get_ref_type_and_value({"dir": "../abc", "commit": "abc123"}) ('commit', 'abc123') >>> _get_ref_type_and_value({"dir": "../empty"}) is None True >>> multi = {"dir": "../multi", "tag": "v1", "branch": "main"} >>> _get_ref_type_and_value(multi) is None True >>> _get_ref_type_and_value({"dir": "../wt", "tag": ""}) is None True
- vcspull._internal.worktree_sync.validate_worktree_config(wt_config)[source]¶
Validate a worktree configuration dictionary.
- Return type:
- Parameters:
wt_config (WorktreeConfigDict) – The worktree configuration to validate.
- Raises:
WorktreeConfigError – If the configuration is invalid.
Examples
>>> validate_worktree_config({"dir": "../v1", "tag": "v1.0.0"}) >>> validate_worktree_config({"dir": "../dev", "branch": "develop"}) >>> validate_worktree_config({"tag": "v1.0.0"}) # Missing dir Traceback (most recent call last): ... vcspull.exc.WorktreeConfigError: Worktree config missing required 'dir' field >>> validate_worktree_config({"dir": "../proj"}) # No ref Traceback (most recent call last): ... vcspull.exc.WorktreeConfigError: Worktree config must specify one of: ...
- vcspull._internal.worktree_sync._is_worktree_dirty(worktree_path)[source]¶
Check if a worktree has uncommitted changes.
Returns
Trueif dirty or if the check fails (fail-safe). This prevents destructive operations when the dirty state is unknown.- Return type:
- Parameters:
worktree_path (pathlib.Path) – Path to the worktree directory.
- Returns:
True if the worktree has uncommitted changes or if the check fails.
- Return type:
Examples
>>> import pathlib >>> _is_worktree_dirty(pathlib.Path("/nonexistent/path")) True
- vcspull._internal.worktree_sync._ref_exists(repo_path, ref, ref_type)[source]¶
Check if a reference exists in the repository.
- Return type:
- Parameters:
repo_path (pathlib.Path) – Path to the main repository.
ref (str) – The reference to check.
ref_type (str) – Type of reference: ‘tag’, ‘branch’, or ‘commit’.
- Returns:
True if the reference exists.
- Return type:
Examples
>>> import pathlib >>> _ref_exists(pathlib.Path("/nonexistent/repo"), "v1.0.0", "tag") False >>> _ref_exists(pathlib.Path("/nonexistent/repo"), "main", "branch") False
- vcspull._internal.worktree_sync._get_worktree_head(worktree_path)[source]¶
Get the current HEAD reference of a worktree.
- Return type:
- Parameters:
worktree_path (pathlib.Path) – Path to the worktree.
- Returns:
The HEAD reference or None if unable to determine.
- Return type:
str | None
Examples
>>> import pathlib >>> _get_worktree_head(pathlib.Path("/nonexistent/worktree")) is None True
- vcspull._internal.worktree_sync._worktree_exists(repo_path, worktree_path)[source]¶
Check if a worktree is registered in the repository.
- Return type:
- Parameters:
repo_path (pathlib.Path) – Path to the main repository.
worktree_path (pathlib.Path) – Path to check for worktree.
- Returns:
True if the worktree exists and is registered.
- Return type:
Examples
>>> import pathlib >>> _worktree_exists(pathlib.Path("/repo"), pathlib.Path("/nonexistent")) False >>> repo_dir = tmp_path / "repo" >>> repo_dir.mkdir() >>> _worktree_exists(repo_dir, tmp_path / "missing") False
- vcspull._internal.worktree_sync._resolve_worktree_path(wt_config, workspace_root)[source]¶
Resolve the worktree path from config.
- Return type:
- Parameters:
wt_config (WorktreeConfigDict) – Worktree configuration.
workspace_root (pathlib.Path) – The workspace root directory.
- Returns:
Absolute path for the worktree.
- Return type:
Examples
>>> import pathlib >>> workspace = pathlib.Path("/home/user/code") >>> wt = {"dir": "../sibling", "tag": "v1.0.0"} >>> _resolve_worktree_path(wt, workspace) PosixPath('/home/user/sibling') >>> wt_abs = {"dir": "/tmp/worktree", "tag": "v1.0.0"} >>> _resolve_worktree_path(wt_abs, workspace) PosixPath('/tmp/worktree')
- vcspull._internal.worktree_sync.plan_worktree_sync(repo_path, worktrees_config, workspace_root)[source]¶
Plan worktree sync operations without executing them.
- Return type:
- Parameters:
repo_path (pathlib.Path) – Path to the main repository.
worktrees_config (list[WorktreeConfigDict]) – List of worktree configurations.
workspace_root (pathlib.Path) – The workspace root directory for resolving relative paths.
- Returns:
List of planned operations.
- Return type:
Examples
>>> import pathlib >>> entries = plan_worktree_sync( ... pathlib.Path("/nonexistent/repo"), ... [{"dir": "../wt", "tag": "v1.0.0"}], ... pathlib.Path("/nonexistent"), ... ) >>> len(entries) 1 >>> entries[0].action == WorktreeAction.ERROR True
- vcspull._internal.worktree_sync.sync_worktree(repo_path, wt_config, workspace_root, *, dry_run=False)[source]¶
Synchronize a single worktree.
- Return type:
- Parameters:
repo_path (pathlib.Path) – Path to the main repository.
wt_config (WorktreeConfigDict) – Worktree configuration.
workspace_root (pathlib.Path) – The workspace root directory.
dry_run (bool) – If True, only plan without executing.
- Returns:
Result of the sync operation.
- Return type:
Examples
>>> import pathlib >>> entry = sync_worktree( ... pathlib.Path("/nonexistent/repo"), ... {"dir": "../wt", "tag": "v1.0.0"}, ... pathlib.Path("/nonexistent"), ... dry_run=True, ... ) >>> entry.action == WorktreeAction.ERROR True
- vcspull._internal.worktree_sync._create_worktree(repo_path, worktree_path, ref_type, ref_value, wt_config)[source]¶
Create a new worktree.
- Return type:
- Parameters:
repo_path (pathlib.Path) – Path to the main repository.
worktree_path (pathlib.Path) – Path for the new worktree.
ref_type (str) – Type of reference: ‘tag’, ‘branch’, or ‘commit’.
ref_value (str) – The reference value.
wt_config (WorktreeConfigDict) – Full worktree configuration.
- Raises:
subprocess.CalledProcessError – If the git command fails (e.g., ref not found).
FileNotFoundError – If the repository path does not exist.
Examples
This function requires a valid git repository. When called with an invalid path, it raises FileNotFoundError:
>>> import pathlib >>> _create_worktree( ... pathlib.Path("/nonexistent"), ... pathlib.Path("/tmp/wt"), ... "tag", ... "v1.0.0", ... {"dir": "../wt", "tag": "v1.0.0"}, ... ) Traceback (most recent call last): ... FileNotFoundError: ...
- vcspull._internal.worktree_sync._update_worktree(worktree_path, branch)[source]¶
Update a branch worktree by pulling latest changes.
Verifies the worktree is on the expected branch before pulling. If the worktree is on a different branch, checks out the expected branch first.
- Return type:
- Parameters:
worktree_path (pathlib.Path) – Path to the worktree.
branch (str) – The expected branch name.
- Raises:
subprocess.CalledProcessError – If the git command fails.
FileNotFoundError – If the worktree path does not exist.
Examples
This function requires a valid git worktree. When called with an invalid path, it raises FileNotFoundError:
>>> import pathlib >>> _update_worktree(pathlib.Path("/nonexistent"), "main") Traceback (most recent call last): ... FileNotFoundError: ...
- vcspull._internal.worktree_sync.sync_all_worktrees(repo_path, worktrees_config, workspace_root, *, dry_run=False)[source]¶
Synchronize all worktrees for a repository.
- Return type:
- Parameters:
repo_path (pathlib.Path) – Path to the main repository.
worktrees_config (list[WorktreeConfigDict]) – List of worktree configurations.
workspace_root (pathlib.Path) – The workspace root directory.
dry_run (bool) – If True, only plan without executing.
- Returns:
Summary of all sync operations.
- Return type:
Examples
>>> import pathlib >>> result = sync_all_worktrees( ... pathlib.Path("/nonexistent/repo"), ... [{"dir": "../wt", "tag": "v1.0.0"}], ... pathlib.Path("/nonexistent"), ... dry_run=True, ... ) >>> result.errors 1 >>> len(result.entries) 1
- vcspull._internal.worktree_sync.list_existing_worktrees(repo_path)[source]¶
List all existing worktrees for a repository.
- Return type:
- Parameters:
repo_path (pathlib.Path) – Path to the main repository.
- Returns:
List of worktree paths.
- Return type:
Examples
>>> import pathlib >>> list_existing_worktrees(pathlib.Path("/nonexistent/repo")) []
- vcspull._internal.worktree_sync.prune_worktrees(repo_path, config_worktrees, workspace_root, *, dry_run=False)[source]¶
Remove worktrees that are not in the configuration.
- Return type:
- Parameters:
repo_path (pathlib.Path) – Path to the main repository.
config_worktrees (list[WorktreeConfigDict]) – List of configured worktrees.
workspace_root (pathlib.Path) – The workspace root directory.
dry_run (bool) – If True, only report what would be pruned.
- Returns:
List of worktree paths that were (or would be) pruned.
- Return type:
Examples
>>> import pathlib >>> prune_worktrees( ... pathlib.Path("/nonexistent/repo"), ... [], ... pathlib.Path("/nonexistent"), ... dry_run=True, ... ) []