1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//! Remote repository trait.
//!
//! # Architecture
//! This module defines the abstract contract for interacting with remote repositories.
//! It abstracts the complex orchestration of network protocols (e.g., HTTP, SSH, Git)
//! into a unified interface. By using this trait, the core engine can execute fetch
//! and push operations without being coupled to the underlying transport mechanism
//! or wire protocol.
//!
//! # Design Rationale: Associated Types vs. Generics
//! The trait uses associated types (`type RefSpec`, `type RemoteRef`) rather than
//! generic parameters. This design ties the data representations directly to the
//! specific `Remote` implementation. An HTTP backend might parse refspecs into
//! structured objects, while a custom binary protocol might use raw byte slices.
//! This prevents type mismatches at compile time and simplifies the API by removing
//! the need for verbose generic annotations at every call site.
use crateVctrlError;
/// Trait for interacting with remote repositories.
///
/// # Why this exists
/// Provides a high-level interface for synchronizing state between a local
/// repository and a remote endpoint. It encapsulates the logic for discovering
/// remote references, fetching missing objects, and pushing local history.
/// Abstracting this into a trait allows the crate to support multiple remote
/// backends (e.g., standard Git, custom distributed ledgers) seamlessly.
///
/// # How it works
/// The trait defines three core operations:
/// - `list_refs`: Queries the remote for its current reference state.
/// - `fetch`: Downloads objects specified by refspecs and updates local remote-tracking branches.
/// - `push`: Uploads local objects and updates remote references.
///
/// # Design Rationale: Mutability Split
/// `list_refs` takes `&self` because it is a pure query operation that does not
/// alter the local or remote state; multiple threads can safely list refs concurrently.
/// Conversely, `fetch` and `push` take `&mut self`. These operations fundamentally
/// mutate state (updating local object stores or remote refs) and often require
/// sequential, exclusive access to network streams and internal buffers to prevent
/// data corruption or race conditions.
///
/// # Examples
///
/// Implementing the trait for a mock remote backend:
///
/// ```
/// # use libvctrl_handler::traits::core::remote::Remote;
/// # use libvctrl_handler::VctrlError;
/// #
/// #[derive(Default)]
/// struct MockRemote {
/// refs: Vec<String>,
/// }
///
/// impl Remote for MockRemote {
/// type RefSpec = String;
/// type RemoteRef = String;
///
/// fn list_refs(&self) -> Result<Vec<Self::RemoteRef>, VctrlError> {
/// Ok(self.refs.clone())
/// }
///
/// fn fetch(&mut self, _refspecs: &[Self::RefSpec]) -> Result<(), VctrlError> {
/// // Mock fetch: no-op
/// Ok(())
/// }
///
/// fn push(&mut self, _refspecs: &[Self::RefSpec]) -> Result<(), VctrlError> {
/// // Mock push: no-op
/// Ok(())
/// }
/// }
///
/// let remote = MockRemote::default();
/// assert!(remote.list_refs().is_ok());
/// # Ok::<(), VctrlError>(())
/// ```