Skip to content

Commit 558cf62

Browse files
[mercury]: Add comments (#1232)
* [mercury]: Add comments Signed-off-by: Xiaoyang Han <lux1an@qq.com> * [mercury]: Add comments Signed-off-by: Xiaoyang Han <lux1an@qq.com> * [refact]: for scorpio Signed-off-by: Han Xiaoyang <lux1an@qq.com> --------- Signed-off-by: Xiaoyang Han <lux1an@qq.com> Signed-off-by: Han Xiaoyang <lux1an@qq.com>
1 parent 0842b70 commit 558cf62

14 files changed

Lines changed: 383 additions & 362 deletions

File tree

mercury/src/hash.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
use std::{fmt::Display, io};
77

8+
use bincode::{Encode, Decode};
89
use colored::Colorize;
910
use serde::{Deserialize, Serialize};
1011
use sha1::Digest;
@@ -28,6 +29,7 @@ use crate::internal::object::types::ObjectType;
2829
///
2930
#[derive(
3031
Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default, Deserialize, Serialize,
32+
Encode, Decode
3133
)]
3234
pub struct SHA1(pub [u8; 20]);
3335

mercury/src/internal/object/blob.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use crate::internal::object::ObjectTrait;
3636

3737
/// **The Blob Object**
3838
#[derive(Eq, Debug, Clone)]
39+
#[non_exhaustive]
3940
pub struct Blob {
4041
pub id: SHA1,
4142
pub data: Vec<u8>,
@@ -113,4 +114,5 @@ mod tests {
113114
"5dd01c177f5d7d1be5346a5bc18a569a7410c2ef"
114115
);
115116
}
117+
116118
}

mercury/src/internal/object/commit.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::hash::SHA1;
1919
use crate::internal::object::signature::Signature;
2020
use crate::internal::object::ObjectTrait;
2121
use crate::internal::object::ObjectType;
22+
use bincode::{Decode, Encode};
2223
use bstr::ByteSlice;
2324
use callisto::git_commit;
2425
use callisto::mega_commit;
@@ -35,7 +36,7 @@ use serde::Serialize;
3536
/// history of a repository with a single commit object at its root.
3637
/// - The author and committer fields contain the name, email address, timestamp and timezone.
3738
/// - The message field contains the commit message, which maybe include signed or DCO.
38-
#[derive(Eq, Debug, Clone, Serialize, Deserialize)]
39+
#[derive(Eq, Debug, Clone, Serialize, Deserialize, Decode, Encode)]
3940
#[non_exhaustive]
4041
pub struct Commit {
4142
pub id: SHA1,
@@ -119,6 +120,8 @@ impl Commit {
119120
Commit::new(author, committer, tree_id, parent_commit_ids, message)
120121
}
121122

123+
/// Formats the commit message by extracting the first line of the message.
124+
/// If the message contains a PGP signature, it will return the first line after the signature.
122125
pub fn format_message(&self) -> String {
123126
let mut has_signature = false;
124127
for line in self.message.lines() {
@@ -148,21 +151,26 @@ impl ObjectTrait for Commit {
148151
// Find the tree id and remove it from the data
149152
let tree_end = commit.find_byte(0x0a).unwrap();
150153
let tree_id: SHA1 = SHA1::from_str(
151-
String::from_utf8(commit[5..tree_end].to_owned())
154+
String::from_utf8(commit[5..tree_end].to_owned()) // 5 is the length of "tree "
152155
.unwrap()
153156
.as_str(),
154157
)
155158
.unwrap();
156-
let binding = commit[tree_end + 1..].to_vec();
159+
let binding = commit[tree_end + 1..].to_vec(); // Move past the tree id
157160
commit = &binding;
158161

159162
// Find the parent commit ids and remove them from the data
160163
let author_begin = commit.find("author").unwrap();
164+
// Find all parent commit ids
165+
// The parent commit ids are all the lines that start with "parent "
166+
// We can use find_iter to find all occurrences of "parent "
167+
// and then extract the SHA1 hashes from them.
161168
let parent_commit_ids: Vec<SHA1> = commit[..author_begin]
162169
.find_iter("parent")
163170
.map(|parent| {
164171
let parent_end = commit[parent..].find_byte(0x0a).unwrap();
165172
SHA1::from_str(
173+
// 7 is the length of "parent "
166174
String::from_utf8(commit[parent + 7..parent + parent_end].to_owned())
167175
.unwrap()
168176
.as_str(),
@@ -174,8 +182,10 @@ impl ObjectTrait for Commit {
174182
commit = &binding;
175183

176184
// Find the author and committer and remove them from the data
185+
// 0x0a is the newline character
177186
let author =
178-
Signature::from_data(commit[..commit.find_byte(0x0a).unwrap()].to_vec()).unwrap();
187+
Signature::from_data(commit[..commit.find_byte(0x0a).unwrap()].to_vec()).unwrap();
188+
179189
let binding = commit[commit.find_byte(0x0a).unwrap() + 1..].to_vec();
180190
commit = &binding;
181191
let committer =

mercury/src/internal/object/signature.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//!
1212
use std::{fmt::Display, str::FromStr};
1313

14+
use bincode::{Decode, Encode};
1415
use bstr::ByteSlice;
1516
use chrono::Offset;
1617
use serde::{Deserialize, Serialize};
@@ -30,7 +31,7 @@ use crate::errors::GitError;
3031
/// ```
3132
///
3233
/// So, we design a `SignatureType` enum to indicate the signature type.
33-
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
34+
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize, Decode, Encode)]
3435
pub enum SignatureType {
3536
Author,
3637
Committer,
@@ -75,7 +76,7 @@ impl SignatureType {
7576
}
7677
}
7778

78-
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
79+
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize,Decode,Encode)]
7980
pub struct Signature {
8081
pub signature_type: SignatureType,
8182
pub name: String,

mercury/src/internal/object/tag.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use crate::internal::object::ObjectType;
4949

5050
/// The tag object is used to Annotated tag
5151
#[derive(Eq, Debug, Clone)]
52+
#[non_exhaustive]
5253
pub struct Tag {
5354
pub id: SHA1,
5455
pub object_hash: SHA1,
@@ -123,7 +124,7 @@ impl ObjectTrait for Tag {
123124
let tagger = Signature::from_data(tagger_data).unwrap();
124125
data = &data[data.find_byte(0x0a).unwrap() + 1..];
125126

126-
let message = unsafe {
127+
let message = unsafe { // There may be non-UTF-8 characters, so we use `to_str_unchecked` for conversion.
127128
data[data.find_byte(0x0a).unwrap()..]
128129
.to_vec()
129130
.to_str_unchecked()

mercury/src/internal/object/tree.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use crate::errors::GitError;
1818
use crate::hash::SHA1;
1919
use crate::internal::object::ObjectTrait;
2020
use crate::internal::object::ObjectType;
21+
use bincode::{Encode, Decode};
2122
use colored::Colorize;
2223
use encoding_rs::GBK;
2324
use serde::Deserialize;
@@ -28,7 +29,7 @@ use std::fmt::Display;
2829
/// that entry. The mode is a three-digit octal number that encodes both the permissions and the
2930
/// type of the object. The first digit specifies the object type, and the remaining two digits
3031
/// specify the file mode or permissions.
31-
#[derive(PartialEq, Eq, Debug, Clone, Copy, Serialize, Deserialize, Hash)]
32+
#[derive(PartialEq, Eq, Debug, Clone, Copy, Serialize, Deserialize, Hash, Encode, Decode)]
3233
pub enum TreeItemMode {
3334
Blob,
3435
BlobExecutable,
@@ -129,7 +130,7 @@ impl TreeItemMode {
129130
/// 100644 hello-world\0<blob object ID>
130131
/// 040000 data\0<tree object ID>
131132
/// ```
132-
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize, Hash)]
133+
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize, Hash, Encode, Decode)]
133134
pub struct TreeItem {
134135
pub mode: TreeItemMode,
135136
pub id: SHA1,
@@ -220,7 +221,8 @@ impl TreeItem {
220221

221222
/// A tree object is a Git object that represents a directory. It contains a list of entries, one
222223
/// for each file or directory in the tree.
223-
#[derive(Eq, Debug, Clone, Serialize, Deserialize)]
224+
#[derive(Eq, Debug, Clone, Serialize, Deserialize, Encode, Decode)]
225+
#[non_exhaustive]
224226
pub struct Tree {
225227
pub id: SHA1,
226228
pub tree_items: Vec<TreeItem>,

mercury/src/internal/pack/cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl Caches {
9595
let hash_str = hash._to_string();
9696
path.push(&hash_str[..2]); // use first 2 chars as the directory
9797
self.path_prefixes[hash.as_ref()[0] as usize].call_once(|| {
98-
// 检查目录是否存在,只有在不存在时才创建
98+
// Check if the directory exists, if not, create it
9999
if !path.exists() {
100100
fs::create_dir_all(&path).unwrap();
101101
}

mercury/src/internal/pack/encode.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ impl PackEncoder {
153153
return self.inner_encode(entry_rx, true).await;
154154
}
155155

156+
/// Delta selection heuristics are based on:
157+
/// https://github.com/git/git/blob/master/Documentation/technical/pack-heuristics.adoc
156158
async fn inner_encode(
157159
&mut self,
158160
mut entry_rx: mpsc::Receiver<Entry>,

scorpio/Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ reqwest = { version = "0.12.7", features = ["json","blocking"] }
1212
serde = { version = "1.0.210", features = ["derive"] }
1313
fuse-backend-rs = { version = "0.12.0", features = ["fusedev","async-io"]}
1414
tokio = { version = "1.40.0", features = ["full"] }
15-
vm-memory = { version = "0.15.0", features = ["backend-mmap", "backend-bitmap"] }
16-
axum = { version = "0.7.7",features=["macros"]}
15+
axum = { version = "0.8.4",features=["macros"]}
1716
rfuse3 = { version = "0.0.2" ,features = ["tokio-runtime","unprivileged"]}
18-
futures-util = { version = "0.3.30", features = ["sink"] }
1917
syn = { version = "2.0.98", features = ["full", "extra-traits"] }
2018
clap = { version = "4.0", features = ["derive"] }
2119

@@ -30,11 +28,10 @@ once_cell = "1.19.0"
3028
arc-swap = "1.7.1"
3129
env_logger = "0.11.5"
3230
sled = "0.34.7"
33-
bincode = "1.3.3"
31+
bincode = { workspace = true , features = ["serde"] }
3432
async-recursion = "1.1.1"
3533
bytes = "1.7.2"
3634
futures = "0.3.31"
37-
vmm-sys-util = "0.11"
3835
quote = "1.0.38"
3936
proc-macro2 = "1.0.93"
4037
uuid = "1.14.0"

scorpio/src/dicfuse/tree_store.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::util::{config, GPath};
2+
use bincode::{Decode, Encode};
23
use rfuse3::raw::reply::ReplyEntry;
34
use rfuse3::FileType;
45
use serde::{Deserialize, Serialize};
@@ -14,7 +15,7 @@ pub struct TreeStorage {
1415
db: Db,
1516
}
1617

17-
#[derive(Serialize, Deserialize, Clone)]
18+
#[derive(Serialize, Deserialize, Clone,Encode,Decode)]
1819
pub struct StorageItem {
1920
inode: u64,
2021
parent: u64,
@@ -80,12 +81,12 @@ impl TreeStorage {
8081
children: Vec::new(),
8182
hash: item.hash,
8283
};
83-
84+
let config = bincode::config::standard();
8485
// Insert an item into db and update the parent item's children list.
8586
self.db
8687
.insert(
8788
inode.to_be_bytes(),
88-
bincode::serialize(&storage_item).map_err(Error::other)?,
89+
bincode::encode_to_vec(&storage_item,config).map_err(Error::other)?,
8990
)
9091
.map_err(Error::other)?;
9192

@@ -97,7 +98,7 @@ impl TreeStorage {
9798
self.db
9899
.insert(
99100
parent.to_be_bytes(),
100-
bincode::serialize(&parent_item).map_err(Error::other)?,
101+
bincode::encode_to_vec(&parent_item,config).map_err(Error::other)?,
101102
)
102103
.map_err(Error::other)?;
103104
}
@@ -122,10 +123,12 @@ impl TreeStorage {
122123
if storage_item.parent != 0 {
123124
let mut parent_item: StorageItem = self.get_storage_item(storage_item.parent)?;
124125
parent_item.children.retain(|&x| x != inode);
126+
let config = bincode::config::standard();
127+
125128
self.db
126129
.insert(
127130
storage_item.parent.to_be_bytes(),
128-
bincode::serialize(&parent_item).map_err(Error::other)?,
131+
bincode::encode_to_vec(&parent_item, config).map_err(Error::other)?,
129132
)
130133
.map_err(Error::other)?;
131134
}
@@ -140,10 +143,11 @@ impl TreeStorage {
140143
pub fn append_child(&self, parent: u64, inode: u64) -> io::Result<()> {
141144
let mut st = self.get_storage_item(parent)?;
142145
st.children.push(inode);
146+
let config = bincode::config::standard();
143147
self.db
144148
.insert(
145149
parent.to_be_bytes(),
146-
bincode::serialize(&st).map_err(Error::other)?,
150+
bincode::encode_to_vec(&st, config).map_err(Error::other)?,
147151
)
148152
.map_err(Error::other)?;
149153
Ok(())
@@ -163,7 +167,8 @@ impl TreeStorage {
163167
pub fn get_storage_item(&self, inode: u64) -> io::Result<StorageItem> {
164168
match self.db.get(inode.to_be_bytes())? {
165169
Some(value) => {
166-
let item: StorageItem = bincode::deserialize(&value).map_err(Error::other)?;
170+
let config = bincode::config::standard();
171+
let (item ,_) = bincode::decode_from_slice(&value,config).map_err(Error::other)?;
167172
Ok(item)
168173
}
169174
None => Err(Error::new(ErrorKind::NotFound, "Item not found")),
@@ -184,10 +189,11 @@ impl TreeStorage {
184189
pub fn update_item_hash(&self, inode: u64, hash: String) -> io::Result<()> {
185190
let mut item = self.get_storage_item(inode)?;
186191
item.hash = hash;
192+
let config = bincode::config::standard();
187193
self.db
188194
.insert(
189195
inode.to_be_bytes(),
190-
bincode::serialize(&item).map_err(Error::other)?,
196+
bincode::encode_to_vec(&item,config).map_err(Error::other)?,
191197
)
192198
.map_err(Error::other)?;
193199
Ok(())

0 commit comments

Comments
 (0)