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
//! Reflog store trait.
//!
//! # Architecture
//! This module defines the abstract contract for managing reference logs (reflogs).
//! Reflogs act as an append-only audit trail, recording every mutation to a reference
//! (e.g., commits, resets, checkouts). This history is crucial for recovering from
//! accidental operations and for garbage collection pruning.
//!
//! # Design Rationale: Strict Append-Only Semantics
//! The trait exposes only `append` and `entries` methods. There is no `delete` or
//! `update` operation for individual entries. This enforces the append-only nature
//! of reflogs at the type level, preventing consumers from accidentally rewriting
//! audit history.
use crateVctrlError;
use crate;
/// Trait for managing reflogs.
///
/// # Why this exists
/// Provides a unified interface for recording and retrieving the history of
/// reference updates. By abstracting this into a trait, the crate allows the core
/// engine to track state changes without being tied to the standard `.git/logs`
/// filesystem layout. Consumers can inject in-memory reflogs for testing or
/// database-backed reflogs for enterprise persistence.
///
/// # How it works
/// The store maintains a mapping between reference names and a chronological list
/// of [`ReflogEntry`] items. The `append` method requires `&mut self` to enforce
/// exclusive access, ensuring that concurrent updates to the same reference's
/// reflog do not interleave and corrupt the history file. The `entries` method
/// takes `&self`, allowing safe, concurrent reads of the audit trail.
///
/// # Design Rationale: `Vec` over Iterators
/// Unlike [`RefStore::list_refs`](crate::traits::core::ref_store::RefStore::list_refs),
/// which returns an iterator to handle millions of refs, `entries` returns a `Vec`.
/// Reflogs are bounded in size (e.g., Git defaults to 90 days or 250 entries). The
/// memory footprint of loading a single reference's reflog is strictly bounded,
/// making a `Vec` more ergonomic and efficient than a streaming iterator.
///
/// # Examples
///
/// Implementing the trait for a mock in-memory store:
///
/// ```
/// # use libvctrl_handler::traits::core::reflog::ReflogStore;
/// # use libvctrl_handler::{Hash, ReflogEntry, VctrlError};
/// # use std::collections::HashMap;
/// #
/// #[derive(Default)]
/// struct MockReflogStore {
/// logs: HashMap<String, Vec<ReflogEntry>>,
/// }
///
/// impl ReflogStore for MockReflogStore {
/// type RefName = String;
///
/// fn append(
/// &mut self,
/// reference: &Self::RefName,
/// old_hash: Option<Hash>,
/// new_hash: Option<Hash>,
/// reason: &str,
/// timestamp: i64,
/// timezone_offset: i16,
/// ) -> Result<(), VctrlError> {
/// let entry = ReflogEntry::new(
/// old_hash,
/// new_hash,
/// reason.to_string(),
/// timestamp,
/// timezone_offset,
/// )?;
/// self.logs.entry(reference.clone()).or_default().push(entry);
/// Ok(())
/// }
///
/// fn entries(&self, reference: &Self::RefName) -> Result<Vec<ReflogEntry>, VctrlError> {
/// Ok(self.logs.get(reference).cloned().unwrap_or_default())
/// }
/// }
///
/// let mut store = MockReflogStore::default();
/// let hash = Hash::from_bytes(&[0_u8; 64])?;
/// store.append(&"refs/heads/main".to_string(), None, Some(hash), "initial commit", 0, 0)?;
/// assert_eq!(store.entries(&"refs/heads/main".to_string())?.len(), 1);
/// # Ok::<(), VctrlError>(())
/// ```