22
33from __future__ import annotations
44
5+ import collections .abc
56import dataclasses
67import enum
78import logging
89import pathlib
910import subprocess
1011import typing as t
11- from collections .abc import Mapping
1212
1313from libvcs import (
1414 SyncPolicy ,
@@ -50,97 +50,113 @@ class WorktreeAction(enum.Enum):
5050 """Operation failed (ref not found, permission, etc.)."""
5151
5252
53- @dataclasses .dataclass
53+ @dataclasses .dataclass ( slots = True )
5454class WorktreeCheck :
55- """A single check performed during worktree planning."""
55+ """A single check performed during worktree planning.
5656
57- name : str
58- """Check name: 'validate_config', 'ref_exists', 'worktree_exists', 'is_dirty'."""
57+ Attributes
58+ ----------
59+ name : str
60+ Check name (``'validate_config'``, ``'ref_exists'``,
61+ ``'worktree_exists'``, or ``'is_dirty'``).
62+ passed : bool
63+ Whether the check passed.
64+ detail : str
65+ Human-readable description of the check result.
66+ exception : exc.WorktreeError | None
67+ Typed exception if the check failed.
68+ """
5969
70+ name : str
6071 passed : bool
61- """Whether the check passed."""
62-
6372 detail : str
64- """Human-readable description of the check result."""
65-
6673 exception : exc .WorktreeError | None = None
67- """Typed exception if the check failed."""
6874
6975
70- @dataclasses .dataclass
76+ @dataclasses .dataclass ( slots = True )
7177class WorktreePlanEntry :
72- """Planning information for a single worktree operation."""
78+ """Planning and execution record for a single worktree operation.
7379
74- worktree_path : pathlib .Path
75- """Absolute path where the worktree will be/is located."""
80+ Attributes
81+ ----------
82+ worktree_path : pathlib.Path
83+ Absolute path where the worktree is or will be located.
84+ ref_type : str
85+ Configured selector kind (``'tag'``, ``'branch'``, ``'commit'``, or ``'rev'``).
86+ ref_value : str
87+ Configured tag name, branch name, commit SHA, or revision expression.
88+ action : WorktreeAction
89+ Planned or executed worktree action.
90+ detail : str | None
91+ Human-readable explanation of the action.
92+ error : str | None
93+ Error summary when ``action`` is ``ERROR``.
94+ exists : bool | None
95+ Whether the worktree existed before synchronization, or ``None`` if unknown.
96+ is_dirty : bool | None
97+ Whether the worktree had uncommitted changes, or ``None`` if unknown.
98+ current_ref : str | None
99+ Observed HEAD commit SHA when the worktree exists.
100+ position : WorkingCopyPosition | None
101+ Observed native position before synchronization.
102+ target_position : WorkingCopyPosition | None
103+ Configured target resolved from available native metadata.
104+ drifted : bool | None
105+ Whether observed and configured revisions differ, or ``None`` if unresolved.
106+ result : SyncResult | None
107+ Complete native synchronization outcome, including recovery metadata.
108+ status : {"timed_out", "interrupted"} | None
109+ Watchdog termination state when execution did not finish normally.
110+ retained_recoveries : tuple[SyncResult, ...]
111+ Retained recovery records discovered after worker termination.
112+ checks : list[WorktreeCheck]
113+ Ordered audit trail of checks recorded for the entry.
114+ """
76115
116+ worktree_path : pathlib .Path
77117 ref_type : str
78- """Type of reference: 'tag', 'branch', or 'commit'."""
79-
80118 ref_value : str
81- """The actual tag name, branch name, or commit SHA."""
82-
83119 action : WorktreeAction
84- """What action will be/was taken."""
85-
86120 detail : str | None = None
87- """Human-readable explanation of the action."""
88-
89121 error : str | None = None
90- """Error message if action is ERROR."""
91-
92122 exists : bool | None = False
93- """Whether the worktree currently exists."""
94-
95123 is_dirty : bool | None = False
96- """Whether the worktree has uncommitted changes."""
97-
98124 current_ref : str | None = None
99- """Current HEAD reference if worktree exists."""
100-
101125 position : WorkingCopyPosition | None = None
102- """Observed native position before synchronization."""
103-
104126 target_position : WorkingCopyPosition | None = None
105- """Configured target resolved from available native metadata."""
106-
107127 drifted : bool | None = None
108- """Whether the observed and configured OIDs differ; None if unresolved."""
109-
110128 result : SyncResult | None = None
111- """Complete native outcome, including retained recovery identity."""
112-
113129 status : t .Literal ["timed_out" , "interrupted" ] | None = None
114- """Forced termination leaves the native operation outcome uncertain."""
115-
116130 retained_recoveries : tuple [SyncResult , ...] = ()
117- """Records discovered after the mutation worker stopped."""
118-
119131 checks : list [WorktreeCheck ] = dataclasses .field (default_factory = list )
120- """Ordered audit trail of checks performed during planning."""
121132
122133
123- @dataclasses .dataclass
134+ @dataclasses .dataclass ( slots = True )
124135class WorktreeSyncResult :
125- """Result of a worktree sync operation."""
136+ """Aggregated result of synchronizing a repository's worktrees.
126137
127- entries : list [WorktreePlanEntry ] = dataclasses .field (default_factory = list )
128- """List of worktree plan entries."""
138+ Attributes
139+ ----------
140+ entries : list[WorktreePlanEntry]
141+ Evaluated or executed worktree entries in configuration order.
142+ created : int
143+ Number of worktrees created (or planned for creation in dry-run).
144+ updated : int
145+ Number of worktrees updated.
146+ unchanged : int
147+ Number of worktrees left unchanged.
148+ blocked : int
149+ Number of worktrees blocked by dirty policy.
150+ errors : int
151+ Number of worktrees that encountered errors.
152+ """
129153
154+ entries : list [WorktreePlanEntry ] = dataclasses .field (default_factory = list )
130155 created : int = 0
131- """Number of worktrees created."""
132-
133156 updated : int = 0
134- """Number of worktrees updated."""
135-
136157 unchanged : int = 0
137- """Number of worktrees left unchanged."""
138-
139158 blocked : int = 0
140- """Number of worktrees blocked due to dirty state."""
141-
142159 errors : int = 0
143- """Number of worktrees that encountered errors."""
144160
145161
146162class WorktreeInterrupted (KeyboardInterrupt ):
@@ -176,7 +192,10 @@ def worktree_entry_data(entry: WorktreePlanEntry) -> dict[str, t.Any]:
176192
177193
178194def _worktree_entry_from_data (
179- data : Mapping [str , t .Any ], path : pathlib .Path , ref_type : str , ref_value : str
195+ data : collections .abc .Mapping [str , t .Any ],
196+ path : pathlib .Path ,
197+ ref_type : str ,
198+ ref_value : str ,
180199) -> WorktreePlanEntry :
181200 """Validate the worker's metadata before it can count as a completed action."""
182201 fields = dict (data )
@@ -253,7 +272,7 @@ def _sync_worktree_process(
253272 workspace_root : pathlib .Path ,
254273 * ,
255274 allow_discard : bool ,
256- repo_config : Mapping [str , t .Any ] | None ,
275+ repo_config : collections . abc . Mapping [str , t .Any ] | None ,
257276 timeout : float ,
258277) -> WorktreePlanEntry :
259278 """Stop one owned worktree process before reporting results or recovery."""
@@ -623,7 +642,7 @@ def _resolve_worktree_path(
623642def _worktree_project (
624643 repo_path : pathlib .Path ,
625644 worktree_path : pathlib .Path ,
626- repo_config : Mapping [str , t .Any ] | None ,
645+ repo_config : collections . abc . Mapping [str , t .Any ] | None ,
627646) -> GitSync :
628647 values = dict (repo_config or {"url" : str (repo_path )})
629648 values ["path" ] = worktree_path
@@ -646,7 +665,7 @@ def plan_worktree_sync(
646665 worktrees_config : list [WorktreeConfigDict ],
647666 workspace_root : pathlib .Path ,
648667 * ,
649- repo_config : Mapping [str , t .Any ] | None = None ,
668+ repo_config : collections . abc . Mapping [str , t .Any ] | None = None ,
650669) -> list [WorktreePlanEntry ]:
651670 """Inspect local worktree targets and policy without fetching or changing refs.
652671
@@ -784,7 +803,7 @@ def sync_worktree(
784803 * ,
785804 dry_run : bool = False ,
786805 allow_discard : bool = False ,
787- repo_config : Mapping [str , t .Any ] | None = None ,
806+ repo_config : collections . abc . Mapping [str , t .Any ] | None = None ,
788807) -> WorktreePlanEntry :
789808 """Synchronize a linked checkout and retain its complete native outcome.
790809
@@ -855,6 +874,12 @@ def sync_worktree(
855874 )
856875 entry .detail = entry .error
857876 else :
877+ entry .checks = [
878+ WorktreeCheck ("ref_exists" , True , "target resolved during sync" )
879+ if check .name == "ref_exists" and not check .passed
880+ else check
881+ for check in entry .checks
882+ ]
858883 entry .target_position = (
859884 project .resolve_target (target )
860885 if exists
@@ -935,7 +960,7 @@ def sync_all_worktrees(
935960 * ,
936961 dry_run : bool = False ,
937962 allow_discard : bool = False ,
938- repo_config : Mapping [str , t .Any ] | None = None ,
963+ repo_config : collections . abc . Mapping [str , t .Any ] | None = None ,
939964 timeout : float | None = None ,
940965) -> WorktreeSyncResult :
941966 """Synchronize all worktrees for a repository.
0 commit comments