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
252
253
254
255
256
257
258
259
260
261
262
263
//! Blame computation trait.
//!
//! # Architecture
//! This module provides the contracts for attributing lines in a file to specific commits.
//! Blame computation is fundamentally different from standard diffing; it requires traversing
//! history in reverse and tracking line movements across revisions. By isolating this into
//! a dedicated trait, the crate allows consumers to plug in different blame algorithms
//! (e.g., linear history vs. merge-aware) without altering the core engine.
//!
//! # Design Rationale: Immutability and Validation
//! The [`BlameEntry`] struct is constructed via a fallible constructor (`new`). This ensures
//! that invalid states—such as a line range starting at 0 or having a length of 0—cannot
//! exist at runtime. Once constructed, the entry is immutable, guaranteeing that the blame
//! history remains tamper-proof.
use crateVctrlError;
use crateHash;
/// A single line range in a file attributed to a commit.
///
/// # Why this exists
/// Represents the atomic unit of blame data. Instead of attributing an entire file to a single
/// commit, Git blame operates on line ranges. This struct encapsulates the mapping between a
/// specific range of lines in a file and the commit that last modified them.
///
/// # How it works
/// The struct holds a reference to the committing [`Hash`], the 1-based line number range,
/// the file path, and an optional commit summary. The `commit_id` is stored as a copied `Hash`
/// (which is a fixed 64-byte array) rather than a reference, to simplify lifetime management
/// when returning vectors of blame entries from background threads.
///
/// # Examples
///
/// ```
/// # use libvctrl_handler::traits::core::blame::BlameEntry;
/// # use libvctrl_handler::Hash;
/// # let hash = Hash::from_bytes(&[0_u8; 64]).unwrap();
/// let entry = BlameEntry::new(
/// hash,
/// 10,
/// 5,
/// "src/main.rs".to_string(),
/// Some("Initial commit".to_string()),
/// );
/// assert!(entry.is_ok());
/// ```
/// Trait for computing blame information for files.
///
/// # Why this exists
/// Defines the abstract contract for attributing file lines to commits. By using a trait,
/// the crate decouples the blame algorithm from the repository backend. This allows for
/// different implementations (e.g., a simple linear walker vs. a complex graph traversal
/// that handles merges).
///
/// # Design Rationale: `Send + Sync`
/// The trait requires `Send + Sync` because blame computation is highly parallelizable.
/// File-level blame operations are independent of one another. Implementors can safely
/// distribute `&self` across multiple threads to compute blame for different files
/// concurrently, leveraging multi-core processors without data races.
///
/// # Examples
///
/// Implementing the trait for a mock repository:
///
/// ```
/// # use libvctrl_handler::traits::core::blame::{Blame, BlameEntry};
/// # use libvctrl_handler::{Hash, VctrlError};
/// #
/// struct MockRepo;
///
/// impl Blame for MockRepo {
/// fn blame_file(&self, _path: &str) -> Result<Vec<BlameEntry>, VctrlError> {
/// # let hash = Hash::from_bytes(&[0_u8; 64]).unwrap();
/// let entry = BlameEntry::new(hash, 1, 10, "file.txt".into(), None)?;
/// Ok(vec![entry])
/// }
/// }
///
/// let repo = MockRepo;
/// let entries = repo.blame_file("file.txt").unwrap();
/// assert_eq!(entries.len(), 1);
/// ```