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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//! Reference store trait.
//!
//! # Architecture
//! This module defines the abstract contract for managing Git references (branches,
//! tags, HEAD). In Git's architecture, the object database is strictly immutable,
//! while references provide the mutable pointers that track the current state of
//! branches and tags. By isolating reference management into a dedicated trait,
//! the crate decouples state mutations from content storage.
//!
//! # Design Rationale: Lazy Iteration
//! The [`RefStore::list_refs`] method returns a custom associated iterator type
//! (`type RefsIterator`) rather than a `Vec<String>`. This is a critical architectural
//! decision for scalability. Repositories like the Linux kernel contain millions of
//! references. Returning a `Vec` would require loading all names into memory
//! simultaneously, risking out-of-memory (OOM) errors. By returning an iterator,
//! backends can stream reference names lazily from disk or a database cursor,
//! maintaining a constant memory footprint.
use crateVctrlError;
use crateHash;
/// A trait for managing Git references (branches, tags, etc.).
///
/// # Why this exists
/// Provides a unified, type-safe interface for mutating and querying repository
/// state. Git references map human-readable names (e.g., `refs/heads/main`) to
/// cryptographic hashes. This trait enforces that structure, allowing the core
/// engine to orchestrate branch updates, tag creation, and HEAD detachments
/// without being tied to a specific filesystem layout or database backend.
///
/// # How it works
/// The store maintains a mapping between reference names and [`Hash`] values.
/// Write operations (`set_ref`, `delete_ref`) require `&mut self`, enforcing
/// exclusive access at the Rust type level. This mimics Git's `.lock` files,
/// preventing race conditions where two concurrent processes try to update the
/// same branch. Read operations (`get_ref`, `list_refs`) take `&self`, allowing
/// highly concurrent parallel reads across multiple threads.
///
/// # Design Rationale: Thread Safety
/// The trait requires `Send + Sync`. Reference resolution is one of the most
/// frequent operations in Git (e.g., during revision walks or merge analysis).
/// By enforcing thread safety, the engine can parallelize operations that
/// require resolving multiple refs without requiring external locking mechanisms.
///
/// # Examples
///
/// Implementing the trait for a mock in-memory store:
///
/// ```
/// # use libvctrl_handler::traits::core::ref_store::RefStore;
/// # use libvctrl_handler::{Hash, VctrlError};
/// # use std::collections::HashMap;
/// #
/// #[derive(Default)]
/// struct MockRefStore {
/// refs: HashMap<String, Hash>,
/// }
///
/// impl RefStore for MockRefStore {
/// type RefsIterator = std::vec::IntoIter<Result<String, VctrlError>>;
///
/// fn set_ref(&mut self, name: &str, hash: &Hash) -> Result<(), VctrlError> {
/// self.refs.insert(name.to_string(), *hash);
/// Ok(())
/// }
///
/// fn get_ref(&self, name: &str) -> Result<Hash, VctrlError> {
/// self.refs
/// .get(name)
/// .copied()
/// .ok_or_else(|| VctrlError::RefNotFound(name.to_string()))
/// }
///
/// fn delete_ref(&mut self, name: &str) -> Result<(), VctrlError> {
/// self.refs.remove(name);
/// Ok(())
/// }
///
/// fn list_refs(&self) -> Result<Self::RefsIterator, VctrlError> {
/// let refs: Vec<_> = self.refs.keys().map(|k| Ok(k.clone())).collect();
/// Ok(refs.into_iter())
/// }
/// }
///
/// let mut store = MockRefStore::default();
/// let hash = Hash::from_bytes(&[0_u8; 64])?;
/// store.set_ref("refs/heads/main", &hash)?;
/// assert_eq!(store.get_ref("refs/heads/main")?, hash);
/// # Ok::<(), VctrlError>(())
/// ```