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 #[serde(skip)]
166 pub conflict_labels: Merge<String>,
167 pub change_id: ChangeId,
168 pub description: String,
169 pub author: Signature,
170 pub committer: Signature,
171 #[serde(skip)] pub secure_sig: Option<SecureSig>,
173}
174
175#[derive(Debug, PartialEq, Eq, Clone)]
177pub struct CopyRecord {
178 pub target: RepoPathBuf,
180 pub target_commit: CommitId,
182 pub source: RepoPathBuf,
188 pub source_file: FileId,
189 pub source_commit: CommitId,
200}
201
202#[derive(ContentHash, Debug, PartialEq, Eq, Clone, PartialOrd, Ord)]
205pub struct CopyHistory {
206 pub current_path: RepoPathBuf,
208 pub parents: Vec<CopyId>,
213 pub salt: Vec<u8>,
218}
219
220#[derive(Debug, Error)]
222#[error(transparent)]
223pub struct BackendInitError(pub Box<dyn std::error::Error + Send + Sync>);
224
225#[derive(Debug, Error)]
227#[error(transparent)]
228pub struct BackendLoadError(pub Box<dyn std::error::Error + Send + Sync>);
229
230#[derive(Debug, Error)]
232pub enum BackendError {
233 #[error(
234 "Invalid hash length for object of type {object_type} (expected {expected} bytes, got \
235 {actual} bytes): {hash}"
236 )]
237 InvalidHashLength {
238 expected: usize,
239 actual: usize,
240 object_type: String,
241 hash: String,
242 },
243 #[error("Invalid UTF-8 for object {hash} of type {object_type}")]
244 InvalidUtf8 {
245 object_type: String,
246 hash: String,
247 source: std::str::Utf8Error,
248 },
249 #[error("Object {hash} of type {object_type} not found")]
250 ObjectNotFound {
251 object_type: String,
252 hash: String,
253 source: Box<dyn std::error::Error + Send + Sync>,
254 },
255 #[error("Error when reading object {hash} of type {object_type}")]
256 ReadObject {
257 object_type: String,
258 hash: String,
259 source: Box<dyn std::error::Error + Send + Sync>,
260 },
261 #[error("Access denied to read object {hash} of type {object_type}")]
262 ReadAccessDenied {
263 object_type: String,
264 hash: String,
265 source: Box<dyn std::error::Error + Send + Sync>,
266 },
267 #[error(
268 "Error when reading file content for file {path} with id {id}",
269 path = path.as_internal_file_string()
270 )]
271 ReadFile {
272 path: RepoPathBuf,
273 id: FileId,
274 source: Box<dyn std::error::Error + Send + Sync>,
275 },
276 #[error("Could not write object of type {object_type}")]
277 WriteObject {
278 object_type: &'static str,
279 source: Box<dyn std::error::Error + Send + Sync>,
280 },
281 #[error(transparent)]
282 Other(Box<dyn std::error::Error + Send + Sync>),
283 #[error("{0}")]
286 Unsupported(String),
287}
288
289pub type BackendResult<T> = Result<T, BackendError>;
290
291#[derive(ContentHash, Debug, PartialEq, Eq, Clone, Hash)]
292pub enum TreeValue {
293 File {
296 id: FileId,
297 executable: bool,
298 copy_id: CopyId,
299 },
300 Symlink(SymlinkId),
301 Tree(TreeId),
302 GitSubmodule(CommitId),
303}
304
305impl TreeValue {
306 pub fn hex(&self) -> String {
307 match self {
308 Self::File { id, .. } => id.hex(),
309 Self::Symlink(id) => id.hex(),
310 Self::Tree(id) => id.hex(),
311 Self::GitSubmodule(id) => id.hex(),
312 }
313 }
314}
315
316#[derive(Debug, PartialEq, Eq, Clone)]
317pub struct TreeEntry<'a> {
318 name: &'a RepoPathComponent,
319 value: &'a TreeValue,
320}
321
322impl<'a> TreeEntry<'a> {
323 pub fn new(name: &'a RepoPathComponent, value: &'a TreeValue) -> Self {
324 Self { name, value }
325 }
326
327 pub fn name(&self) -> &'a RepoPathComponent {
328 self.name
329 }
330
331 pub fn value(&self) -> &'a TreeValue {
332 self.value
333 }
334}
335
336pub struct TreeEntriesNonRecursiveIterator<'a> {
337 iter: slice::Iter<'a, (RepoPathComponentBuf, TreeValue)>,
338}
339
340impl<'a> Iterator for TreeEntriesNonRecursiveIterator<'a> {
341 type Item = TreeEntry<'a>;
342
343 fn next(&mut self) -> Option<Self::Item> {
344 self.iter
345 .next()
346 .map(|(name, value)| TreeEntry { name, value })
347 }
348}
349
350#[derive(ContentHash, Default, PartialEq, Eq, Debug, Clone)]
351pub struct Tree {
352 entries: Vec<(RepoPathComponentBuf, TreeValue)>,
353}
354
355impl Tree {
356 pub fn from_sorted_entries(entries: Vec<(RepoPathComponentBuf, TreeValue)>) -> Self {
357 debug_assert!(entries.is_sorted_by(|(a, _), (b, _)| a < b));
358 Self { entries }
359 }
360
361 pub fn is_empty(&self) -> bool {
362 self.entries.is_empty()
363 }
364
365 pub fn names(&self) -> impl Iterator<Item = &RepoPathComponent> {
366 self.entries.iter().map(|(name, _)| name.as_ref())
367 }
368
369 pub fn entries(&self) -> TreeEntriesNonRecursiveIterator<'_> {
370 TreeEntriesNonRecursiveIterator {
371 iter: self.entries.iter(),
372 }
373 }
374
375 pub fn entry(&self, name: &RepoPathComponent) -> Option<TreeEntry<'_>> {
376 let index = self
377 .entries
378 .binary_search_by_key(&name, |(name, _)| name)
379 .ok()?;
380 let (name, value) = &self.entries[index];
381 Some(TreeEntry { name, value })
382 }
383
384 pub fn value(&self, name: &RepoPathComponent) -> Option<&TreeValue> {
385 self.entry(name).map(|entry| entry.value)
386 }
387}
388
389pub fn make_root_commit(root_change_id: ChangeId, empty_tree_id: TreeId) -> Commit {
390 let timestamp = Timestamp {
391 timestamp: MillisSinceEpoch(0),
392 tz_offset: 0,
393 };
394 let signature = Signature {
395 name: String::new(),
396 email: String::new(),
397 timestamp,
398 };
399 Commit {
400 parents: vec![],
401 predecessors: vec![],
402 root_tree: Merge::resolved(empty_tree_id),
403 conflict_labels: Merge::resolved(String::new()),
404 change_id: root_change_id,
405 description: String::new(),
406 author: signature.clone(),
407 committer: signature,
408 secure_sig: None,
409 }
410}
411
412#[async_trait]
414pub trait Backend: Any + Send + Sync + Debug {
415 fn name(&self) -> &str;
418
419 fn commit_id_length(&self) -> usize;
421
422 fn change_id_length(&self) -> usize;
424
425 fn root_commit_id(&self) -> &CommitId;
426
427 fn root_change_id(&self) -> &ChangeId;
428
429 fn empty_tree_id(&self) -> &TreeId;
430
431 fn concurrency(&self) -> usize;
439
440 async fn read_file(
441 &self,
442 path: &RepoPath,
443 id: &FileId,
444 ) -> BackendResult<Pin<Box<dyn AsyncRead + Send>>>;
445
446 async fn write_file(
447 &self,
448 path: &RepoPath,
449 contents: &mut (dyn AsyncRead + Send + Unpin),
450 ) -> BackendResult<FileId>;
451
452 async fn read_symlink(&self, path: &RepoPath, id: &SymlinkId) -> BackendResult<String>;
453
454 async fn write_symlink(&self, path: &RepoPath, target: &str) -> BackendResult<SymlinkId>;
455
456 async fn read_copy(&self, id: &CopyId) -> BackendResult<CopyHistory>;
461
462 async fn write_copy(&self, copy: &CopyHistory) -> BackendResult<CopyId>;
467
468 async fn get_related_copies(&self, copy_id: &CopyId) -> BackendResult<Vec<CopyHistory>>;
478
479 async fn read_tree(&self, path: &RepoPath, id: &TreeId) -> BackendResult<Tree>;
480
481 async fn write_tree(&self, path: &RepoPath, contents: &Tree) -> BackendResult<TreeId>;
482
483 async fn read_commit(&self, id: &CommitId) -> BackendResult<Commit>;
484
485 async fn write_commit(
499 &self,
500 contents: Commit,
501 sign_with: Option<&mut SigningFn>,
502 ) -> BackendResult<(CommitId, Commit)>;
503
504 fn get_copy_records(
517 &self,
518 paths: Option<&[RepoPathBuf]>,
519 root: &CommitId,
520 head: &CommitId,
521 ) -> BackendResult<BoxStream<'_, BackendResult<CopyRecord>>>;
522
523 fn gc(&self, index: &dyn Index, keep_newer: SystemTime) -> BackendResult<()>;
529}
530
531impl dyn Backend {
532 pub fn downcast_ref<T: Backend>(&self) -> Option<&T> {
534 (self as &dyn Any).downcast_ref()
535 }
536}