1#![expect(missing_docs)]
16
17use std::any::Any;
18use std::fmt::Debug;
19use std::pin::Pin;
20use std::slice;
21use std::time::SystemTime;
22
23use async_trait::async_trait;
24use chrono::TimeZone as _;
25use futures::stream::BoxStream;
26use thiserror::Error;
27use tokio::io::AsyncRead;
28
29use crate::content_hash::ContentHash;
30use crate::hex_util;
31use crate::index::Index;
32use crate::merge::Merge;
33use crate::object_id::ObjectId as _;
34use crate::object_id::id_type;
35use crate::repo_path::RepoPath;
36use crate::repo_path::RepoPathBuf;
37use crate::repo_path::RepoPathComponent;
38use crate::repo_path::RepoPathComponentBuf;
39use crate::signing::SignResult;
40
41id_type!(
42 pub CommitId { hex() }
45);
46id_type!(
47 pub ChangeId { reverse_hex() }
50);
51id_type!(pub TreeId { hex() });
52id_type!(pub FileId { hex() });
53id_type!(pub SymlinkId { hex() });
54id_type!(pub CopyId { hex() });
55
56impl ChangeId {
57 pub fn try_from_reverse_hex(hex: impl AsRef<[u8]>) -> Option<Self> {
59 hex_util::decode_reverse_hex(hex).map(Self)
60 }
61
62 pub fn reverse_hex(&self) -> String {
65 hex_util::encode_reverse_hex(&self.0)
66 }
67}
68
69impl CopyId {
70 pub fn placeholder() -> Self {
74 Self::new(vec![])
75 }
76}
77
78#[derive(Debug, Error)]
79#[error("Out-of-range date")]
80pub struct TimestampOutOfRange;
81
82#[derive(ContentHash, Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord)]
83pub struct MillisSinceEpoch(pub i64);
84
85#[derive(ContentHash, Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord)]
86pub struct Timestamp {
87 pub timestamp: MillisSinceEpoch,
88 pub tz_offset: i32,
90}
91
92impl Timestamp {
93 pub fn now() -> Self {
94 Self::from_datetime(chrono::offset::Local::now())
95 }
96
97 pub fn from_datetime<Tz: chrono::TimeZone<Offset = chrono::offset::FixedOffset>>(
98 datetime: chrono::DateTime<Tz>,
99 ) -> Self {
100 Self {
101 timestamp: MillisSinceEpoch(datetime.timestamp_millis()),
102 tz_offset: datetime.offset().local_minus_utc() / 60,
103 }
104 }
105
106 pub fn to_datetime(
107 &self,
108 ) -> Result<chrono::DateTime<chrono::FixedOffset>, TimestampOutOfRange> {
109 let utc = match chrono::Utc.timestamp_opt(
110 self.timestamp.0.div_euclid(1000),
111 (self.timestamp.0.rem_euclid(1000)) as u32 * 1000000,
112 ) {
113 chrono::LocalResult::None => {
114 return Err(TimestampOutOfRange);
115 }
116 chrono::LocalResult::Single(x) => x,
117 chrono::LocalResult::Ambiguous(y, _z) => y,
118 };
119
120 Ok(utc.with_timezone(
121 &chrono::FixedOffset::east_opt(self.tz_offset * 60)
122 .unwrap_or_else(|| chrono::FixedOffset::east_opt(0).unwrap()),
123 ))
124 }
125}
126
127impl serde::Serialize for Timestamp {
128 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
129 where
130 S: serde::Serializer,
131 {
132 let t = self.to_datetime().map_err(serde::ser::Error::custom)?;
134 t.serialize(serializer)
135 }
136}
137
138#[derive(ContentHash, Debug, PartialEq, Eq, Clone, serde::Serialize)]
140pub struct Signature {
141 pub name: String,
142 pub email: String,
143 pub timestamp: Timestamp,
144}
145
146#[derive(ContentHash, Debug, PartialEq, Eq, Clone)]
148pub struct SecureSig {
149 pub data: Vec<u8>,
150 pub sig: Vec<u8>,
151}
152
153pub type SigningFn<'a> = dyn FnMut(&[u8]) -> SignResult<Vec<u8>> + Send + 'a;
154
155#[derive(ContentHash, Debug, PartialEq, Eq, Clone, serde::Serialize)]
156pub struct Commit {
157 pub parents: Vec<CommitId>,
158 #[serde(skip)] pub predecessors: Vec<CommitId>,
162 #[serde(skip)] pub root_tree: Merge<TreeId>,
164 pub change_id: ChangeId,
165 pub description: String,
166 pub author: Signature,
167 pub committer: Signature,
168 #[serde(skip)] pub secure_sig: Option<SecureSig>,
170}
171
172#[derive(Debug, PartialEq, Eq, Clone)]
174pub struct CopyRecord {
175 pub target: RepoPathBuf,
177 pub target_commit: CommitId,
179 pub source: RepoPathBuf,
185 pub source_file: FileId,
186 pub source_commit: CommitId,
197}
198
199#[derive(ContentHash, Debug, PartialEq, Eq, Clone, PartialOrd, Ord)]
202pub struct CopyHistory {
203 pub current_path: RepoPathBuf,
205 pub parents: Vec<CopyId>,
210 pub salt: Vec<u8>,
215}
216
217#[derive(Debug, Error)]
219#[error(transparent)]
220pub struct BackendInitError(pub Box<dyn std::error::Error + Send + Sync>);
221
222#[derive(Debug, Error)]
224#[error(transparent)]
225pub struct BackendLoadError(pub Box<dyn std::error::Error + Send + Sync>);
226
227#[derive(Debug, Error)]
229pub enum BackendError {
230 #[error(
231 "Invalid hash length for object of type {object_type} (expected {expected} bytes, got \
232 {actual} bytes): {hash}"
233 )]
234 InvalidHashLength {
235 expected: usize,
236 actual: usize,
237 object_type: String,
238 hash: String,
239 },
240 #[error("Invalid UTF-8 for object {hash} of type {object_type}")]
241 InvalidUtf8 {
242 object_type: String,
243 hash: String,
244 source: std::str::Utf8Error,
245 },
246 #[error("Object {hash} of type {object_type} not found")]
247 ObjectNotFound {
248 object_type: String,
249 hash: String,
250 source: Box<dyn std::error::Error + Send + Sync>,
251 },
252 #[error("Error when reading object {hash} of type {object_type}")]
253 ReadObject {
254 object_type: String,
255 hash: String,
256 source: Box<dyn std::error::Error + Send + Sync>,
257 },
258 #[error("Access denied to read object {hash} of type {object_type}")]
259 ReadAccessDenied {
260 object_type: String,
261 hash: String,
262 source: Box<dyn std::error::Error + Send + Sync>,
263 },
264 #[error(
265 "Error when reading file content for file {path} with id {id}",
266 path = path.as_internal_file_string()
267 )]
268 ReadFile {
269 path: RepoPathBuf,
270 id: FileId,
271 source: Box<dyn std::error::Error + Send + Sync>,
272 },
273 #[error("Could not write object of type {object_type}")]
274 WriteObject {
275 object_type: &'static str,
276 source: Box<dyn std::error::Error + Send + Sync>,
277 },
278 #[error(transparent)]
279 Other(Box<dyn std::error::Error + Send + Sync>),
280 #[error("{0}")]
283 Unsupported(String),
284}
285
286pub type BackendResult<T> = Result<T, BackendError>;
287
288#[derive(ContentHash, Debug, PartialEq, Eq, Clone, Hash)]
289pub enum TreeValue {
290 File {
293 id: FileId,
294 executable: bool,
295 copy_id: CopyId,
296 },
297 Symlink(SymlinkId),
298 Tree(TreeId),
299 GitSubmodule(CommitId),
300}
301
302impl TreeValue {
303 pub fn hex(&self) -> String {
304 match self {
305 Self::File { id, .. } => id.hex(),
306 Self::Symlink(id) => id.hex(),
307 Self::Tree(id) => id.hex(),
308 Self::GitSubmodule(id) => id.hex(),
309 }
310 }
311}
312
313#[derive(Debug, PartialEq, Eq, Clone)]
314pub struct TreeEntry<'a> {
315 name: &'a RepoPathComponent,
316 value: &'a TreeValue,
317}
318
319impl<'a> TreeEntry<'a> {
320 pub fn new(name: &'a RepoPathComponent, value: &'a TreeValue) -> Self {
321 Self { name, value }
322 }
323
324 pub fn name(&self) -> &'a RepoPathComponent {
325 self.name
326 }
327
328 pub fn value(&self) -> &'a TreeValue {
329 self.value
330 }
331}
332
333pub struct TreeEntriesNonRecursiveIterator<'a> {
334 iter: slice::Iter<'a, (RepoPathComponentBuf, TreeValue)>,
335}
336
337impl<'a> Iterator for TreeEntriesNonRecursiveIterator<'a> {
338 type Item = TreeEntry<'a>;
339
340 fn next(&mut self) -> Option<Self::Item> {
341 self.iter
342 .next()
343 .map(|(name, value)| TreeEntry { name, value })
344 }
345}
346
347#[derive(ContentHash, Default, PartialEq, Eq, Debug, Clone)]
348pub struct Tree {
349 entries: Vec<(RepoPathComponentBuf, TreeValue)>,
350}
351
352impl Tree {
353 pub fn from_sorted_entries(entries: Vec<(RepoPathComponentBuf, TreeValue)>) -> Self {
354 debug_assert!(entries.is_sorted_by(|(a, _), (b, _)| a < b));
355 Self { entries }
356 }
357
358 pub fn is_empty(&self) -> bool {
359 self.entries.is_empty()
360 }
361
362 pub fn names(&self) -> impl Iterator<Item = &RepoPathComponent> {
363 self.entries.iter().map(|(name, _)| name.as_ref())
364 }
365
366 pub fn entries(&self) -> TreeEntriesNonRecursiveIterator<'_> {
367 TreeEntriesNonRecursiveIterator {
368 iter: self.entries.iter(),
369 }
370 }
371
372 pub fn entry(&self, name: &RepoPathComponent) -> Option<TreeEntry<'_>> {
373 let index = self
374 .entries
375 .binary_search_by_key(&name, |(name, _)| name)
376 .ok()?;
377 let (name, value) = &self.entries[index];
378 Some(TreeEntry { name, value })
379 }
380
381 pub fn value(&self, name: &RepoPathComponent) -> Option<&TreeValue> {
382 self.entry(name).map(|entry| entry.value)
383 }
384}
385
386pub fn make_root_commit(root_change_id: ChangeId, empty_tree_id: TreeId) -> Commit {
387 let timestamp = Timestamp {
388 timestamp: MillisSinceEpoch(0),
389 tz_offset: 0,
390 };
391 let signature = Signature {
392 name: String::new(),
393 email: String::new(),
394 timestamp,
395 };
396 Commit {
397 parents: vec![],
398 predecessors: vec![],
399 root_tree: Merge::resolved(empty_tree_id),
400 change_id: root_change_id,
401 description: String::new(),
402 author: signature.clone(),
403 committer: signature,
404 secure_sig: None,
405 }
406}
407
408#[async_trait]
410pub trait Backend: Any + Send + Sync + Debug {
411 fn name(&self) -> &str;
414
415 fn commit_id_length(&self) -> usize;
417
418 fn change_id_length(&self) -> usize;
420
421 fn root_commit_id(&self) -> &CommitId;
422
423 fn root_change_id(&self) -> &ChangeId;
424
425 fn empty_tree_id(&self) -> &TreeId;
426
427 fn concurrency(&self) -> usize;
435
436 async fn read_file(
437 &self,
438 path: &RepoPath,
439 id: &FileId,
440 ) -> BackendResult<Pin<Box<dyn AsyncRead + Send>>>;
441
442 async fn write_file(
443 &self,
444 path: &RepoPath,
445 contents: &mut (dyn AsyncRead + Send + Unpin),
446 ) -> BackendResult<FileId>;
447
448 async fn read_symlink(&self, path: &RepoPath, id: &SymlinkId) -> BackendResult<String>;
449
450 async fn write_symlink(&self, path: &RepoPath, target: &str) -> BackendResult<SymlinkId>;
451
452 async fn read_copy(&self, id: &CopyId) -> BackendResult<CopyHistory>;
457
458 async fn write_copy(&self, copy: &CopyHistory) -> BackendResult<CopyId>;
463
464 async fn get_related_copies(&self, copy_id: &CopyId) -> BackendResult<Vec<CopyHistory>>;
474
475 async fn read_tree(&self, path: &RepoPath, id: &TreeId) -> BackendResult<Tree>;
476
477 async fn write_tree(&self, path: &RepoPath, contents: &Tree) -> BackendResult<TreeId>;
478
479 async fn read_commit(&self, id: &CommitId) -> BackendResult<Commit>;
480
481 async fn write_commit(
495 &self,
496 contents: Commit,
497 sign_with: Option<&mut SigningFn>,
498 ) -> BackendResult<(CommitId, Commit)>;
499
500 fn get_copy_records(
513 &self,
514 paths: Option<&[RepoPathBuf]>,
515 root: &CommitId,
516 head: &CommitId,
517 ) -> BackendResult<BoxStream<'_, BackendResult<CopyRecord>>>;
518
519 fn gc(&self, index: &dyn Index, keep_newer: SystemTime) -> BackendResult<()>;
525}
526
527impl dyn Backend {
528 pub fn downcast_ref<T: Backend>(&self) -> Option<&T> {
530 (self as &dyn Any).downcast_ref()
531 }
532}