Skip to content

Commit cc4c0bf

Browse files
committed
worktree(fix,docs): Forward list options, dry-run exit, and SIGINT stderr
1 parent 5a134c7 commit cc4c0bf

7 files changed

Lines changed: 268 additions & 97 deletions

File tree

‎docs/cli/worktree/index.md‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,32 @@ Each worktree entry emits:
110110

111111
```json
112112
{
113+
"reason": "worktree",
113114
"worktree_path": "~/code/myproject-v2.0",
114115
"ref_type": "tag",
115116
"ref_value": "v2.0.0",
116117
"action": "unchanged",
117118
"exists": true,
118119
"is_dirty": false,
119120
"detail": "already at configured target",
120-
"error": null
121+
"error": null,
122+
"position": {
123+
"revision": "3b8a1f2c9d0e4b5a6c7d8e9f0a1b2c3d4e5f6a7b",
124+
"ref_name": "3b8a1f2c9d0e4b5a6c7d8e9f0a1b2c3d4e5f6a7b",
125+
"ref_kind": "commit",
126+
"follows": false,
127+
"mixed": false,
128+
"switched": false
129+
},
130+
"target_position": {
131+
"revision": "3b8a1f2c9d0e4b5a6c7d8e9f0a1b2c3d4e5f6a7b",
132+
"ref_name": "v2.0.0",
133+
"ref_kind": "tag",
134+
"follows": false,
135+
"mixed": false,
136+
"switched": false
137+
},
138+
"drifted": false
121139
}
122140
```
123141

@@ -144,11 +162,11 @@ Configure `sync.dirty: preserve` to retain and restore them, or configure
144162
{ref}`cli-worktree-sync` for recovery output and drift policy. Worktree
145163
pruning still refuses dirty checkouts.
146164

147-
**Unknown refs stop creation.** If a configured tag, branch, or commit
148-
cannot be resolved, the entry is reported as an error (`✗`). Preview and
149-
list operations only inspect local metadata. Creating a missing worktree
150-
requires a locally available target; existing worktrees can fetch during
151-
follow synchronization.
165+
**Unknown refs stop creation.** `vcspull worktree list` and `--dry-run`
166+
inspect local metadata only, reporting unfetched refs as errors (`✗`) without
167+
contacting remotes. `vcspull worktree sync` fetches configured remotes before
168+
creating missing worktrees or updating branches, and reports an error (`✗`) if
169+
the target still cannot be resolved.
152170

153171
**Locks guard against removal.** Worktrees created with `lock: true` or
154172
`lock_reason` are locked via `git worktree lock`, preventing accidental

‎docs/cli/worktree/sync.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,51 @@ $ vcspull worktree sync 'django*'
106106
$ vcspull worktree sync --json '*'
107107
```
108108

109+
Each synchronized entry includes native position, update/preservation state,
110+
recovery metadata, and ordered errors:
111+
112+
```json
113+
[
114+
{
115+
"reason": "worktree",
116+
"worktree_path": "~/code/myproject-dev",
117+
"ref_type": "branch",
118+
"ref_value": "develop",
119+
"action": "update",
120+
"exists": true,
121+
"is_dirty": false,
122+
"detail": "worktree updated",
123+
"error": null,
124+
"position": {
125+
"revision": "1111111111111111111111111111111111111111",
126+
"ref_name": "develop",
127+
"ref_kind": "branch",
128+
"follows": true,
129+
"mixed": false,
130+
"switched": false
131+
},
132+
"target_position": {
133+
"revision": "2222222222222222222222222222222222222222",
134+
"ref_name": "develop",
135+
"ref_kind": "branch",
136+
"follows": true,
137+
"mixed": false,
138+
"switched": false
139+
},
140+
"drifted": true,
141+
"update_state": "completed",
142+
"preservation_state": "not-needed",
143+
"recovery": null,
144+
"conflicts": [],
145+
"errors": []
146+
}
147+
]
148+
```
149+
109150
## NDJSON output
110151

152+
Stream one JSON object per line as each worktree completes:
153+
111154
```console
112155
$ vcspull worktree sync --ndjson '*'
113156
```

‎src/vcspull/_internal/sync_process.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class SyncOutcome:
4343
Deserialized synchronization result from the worker, if reported.
4444
retained_recoveries : tuple[SyncResult, ...]
4545
Retained recovery records discovered by post-failure inspection.
46+
worktree : dict[str, t.Any] | None
47+
Serialized worktree plan entry returned by a ``"worktree"`` worker.
4648
"""
4749

4850
status: t.Literal["synced", "failed", "timed_out"]
@@ -316,7 +318,7 @@ def run_sync_process(
316318
timeout: float,
317319
is_human: bool,
318320
yes: bool = False,
319-
worktree: Mapping[str, t.Any] | None = None,
321+
worktree: collections.abc.Mapping[str, t.Any] | None = None,
320322
) -> SyncOutcome:
321323
"""Run one owned mutation process; inspect retained records after a timeout."""
322324
if os.name != "posix":

‎src/vcspull/_internal/worktree_sync.py‎

Lines changed: 87 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
from __future__ import annotations
44

5+
import collections.abc
56
import dataclasses
67
import enum
78
import logging
89
import pathlib
910
import subprocess
1011
import typing as t
11-
from collections.abc import Mapping
1212

1313
from 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)
5454
class 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)
7177
class 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)
124135
class 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

146162
class WorktreeInterrupted(KeyboardInterrupt):
@@ -176,7 +192,10 @@ def worktree_entry_data(entry: WorktreePlanEntry) -> dict[str, t.Any]:
176192

177193

178194
def _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(
623642
def _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

Comments
 (0)