22
33from __future__ import annotations
44
5+ import collections .abc
56import dataclasses
67import datetime
78import typing as t
8- from collections .abc import Callable , Mapping
99
10- from libvcs import BaseSync , SyncPolicy , SyncResult , SyncTarget
10+ from libvcs import BaseSync , RecoveryToken , SyncPolicy , SyncResult , SyncTarget
1111from libvcs ._internal .shortcuts import create_project
1212from libvcs ._internal .types import VCSLiteral
1313from libvcs .sync .git import GitOptions , GitRemote
1414from libvcs .sync .hg import HgOptions
1515from libvcs .sync .svn import SvnOptions
1616
17+ from vcspull import exc
18+ from vcspull ._internal .private_path import PrivatePath
1719from vcspull .validator import validate_working_copy
1820
1921
20- @dataclasses .dataclass (frozen = True )
22+ @dataclasses .dataclass (frozen = True , slots = True )
2123class SyncExecution :
22- """Keep the backend instance and its complete synchronization result."""
24+ """Keep the backend instance and its complete synchronization result.
25+
26+ Attributes
27+ ----------
28+ project : BaseSync
29+ Constructed libvcs synchronization backend for the repository.
30+ result : SyncResult
31+ Completed synchronization outcome, including recovery metadata.
32+ """
2333
2434 project : BaseSync
2535 result : SyncResult
2636
2737
2838def create_sync_project (
29- repo : Mapping [str , t .Any ],
39+ repo : collections . abc . Mapping [str , t .Any ],
3040 * ,
3141 vcs : VCSLiteral ,
32- progress_callback : Callable [[str , datetime .datetime ], None ] | None = None ,
42+ progress_callback : collections .abc .Callable [[str , datetime .datetime ], None ]
43+ | None = None ,
3344) -> BaseSync :
3445 """Construct a typed backend without accessing or changing its checkout."""
3546 options_type = {"git" : GitOptions , "hg" : HgOptions , "svn" : SvnOptions }[vcs ]
@@ -52,7 +63,7 @@ def create_sync_project(
5263
5364
5465def checkout_settings (
55- config : Mapping [str , t .Any ] | None , * , worktree : bool = False
66+ config : collections . abc . Mapping [str , t .Any ] | None , * , worktree : bool = False
5667) -> tuple [SyncTarget | None , SyncPolicy ]:
5768 """Translate a validated checkout without collapsing its selector kind.
5869
@@ -91,3 +102,52 @@ def sync_result_data(result: SyncResult) -> dict[str, t.Any]:
91102 {"step" : error .step , "message" : error .message } for error in result .errors
92103 ],
93104 }
105+
106+
107+ def format_recovery_lines (
108+ primary : SyncResult | None ,
109+ retained : collections .abc .Sequence [SyncResult ] = (),
110+ ) -> list [str ]:
111+ """Render deduplicated recovery, conflict, and inspection lines.
112+
113+ >>> token = RecoveryToken("tok-1", "git", "/tmp/store/tok-1")
114+ >>> res = SyncResult(
115+ ... recovery=token,
116+ ... update_state="completed",
117+ ... preservation_state="restored",
118+ ... )
119+ >>> format_recovery_lines(res)
120+ ['Update: completed; local changes: restored', 'Recovery retained (git): tok-1 ...']
121+ """
122+ lines : list [str ] = []
123+ if primary is not None :
124+ lines .extend (
125+ f"Recovery inspection: { error .message } "
126+ for error in primary .errors
127+ if error .step == "recovery-inspection"
128+ )
129+ seen : set [RecoveryToken ] = set ()
130+ for result in (primary , * retained ):
131+ if result is None or result .recovery is None or result .recovery in seen :
132+ continue
133+ token = result .recovery
134+ seen .add (token )
135+ lines .append (
136+ f"Update: { result .update_state } ; local changes: { result .preservation_state } "
137+ )
138+ lines .extend (
139+ f"Conflict: { conflict .path } ({ conflict .reason } )"
140+ for conflict in result .conflicts
141+ )
142+ lines .append (
143+ f"Recovery retained ({ token .backend } ): { token .id } "
144+ f"at { PrivatePath (token .location )} "
145+ )
146+ return lines
147+
148+
149+ def require_discard_authorization (policy : SyncPolicy , * , allow_discard : bool ) -> None :
150+ """Require explicit invocation consent before constructing a discard backend."""
151+ if policy .dirty == "discard" and not allow_discard :
152+ message = "dirty: discard requires --yes before synchronization"
153+ raise exc .VCSPullException (message )
0 commit comments