Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mercury/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ encoding_rs = { workspace = true }
rayon = { workspace = true }
reqwest = { workspace = true }
ring = { workspace = true }
serde_json.workspace = true

[dev-dependencies]
tokio = { workspace = true, features = ["full"] }
Expand Down
13 changes: 9 additions & 4 deletions mercury/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ impl SHA1 {
let h = sha1::Sha1::digest(data);
SHA1::from_bytes(h.as_slice())
}

/// Create a Hash from the object type and data
/// This function is used to create a SHA1 hash from the object type and data.
/// It constructs a byte vector that includes the object type, the size of the data,
/// and the data itself, and then computes the SHA1 hash of this byte vector.
///
/// Hash compute <- {Object Type}+{ }+{Object Size(before compress)}+{\x00}+{Object Content(before compress)}
pub fn from_type_and_data(object_type: ObjectType, data: &[u8]) -> SHA1 {
let mut d: Vec<u8> = Vec::new();
d.extend(object_type.to_data().unwrap());
Expand Down Expand Up @@ -213,7 +218,7 @@ mod tests {
Ok(hash) => {
assert_eq!(hash.to_string(), "8ab686eafeb1f44702738c8b0f24f2567c36da6d");
}
Err(e) => println!("Error: {}", e),
Err(e) => println!("Error: {e}"),
}
}

Expand All @@ -225,7 +230,7 @@ mod tests {
Ok(hash) => {
assert_eq!(hash.to_string(), "8ab686eafeb1f44702738c8b0f24f2567c36da6d");
}
Err(e) => println!("Error: {}", e),
Err(e) => println!("Error: {e}"),
}
}

Expand All @@ -243,7 +248,7 @@ mod tests {
]
);
}
Err(e) => println!("Error: {}", e),
Err(e) => println!("Error: {e}"),
}
}
}
4 changes: 2 additions & 2 deletions mercury/src/internal/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ mod tests {
let index = Index::from_file("../tests/data/index/index-760").unwrap();
assert_eq!(index.size(), 760);
for (_, entry) in index.entries.iter() {
println!("{}", entry);
println!("{entry}");
}
}

Expand All @@ -499,6 +499,6 @@ mod tests {
let hash = SHA1::from_bytes(&[0; 20]);
let workdir = Path::new("../");
let entry = IndexEntry::new_from_file(file, hash, workdir).unwrap();
println!("{}", entry);
println!("{entry}");
}
}
42 changes: 2 additions & 40 deletions mercury/src/internal/model/commit.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,15 @@
use std::str::FromStr;


use callisto::{git_commit, mega_commit};
use common::utils::generate_id;

use crate::{
hash::SHA1,
internal::{
object::{commit::Commit, signature::Signature, ObjectTrait},
object::{commit::Commit, ObjectTrait},
pack::entry::Entry,
},
};

impl From<mega_commit::Model> for Commit {
fn from(value: mega_commit::Model) -> Self {
Commit {
id: SHA1::from_str(&value.commit_id).unwrap(),
tree_id: SHA1::from_str(&value.tree).unwrap(),
parent_commit_ids: value
.parents_id
.as_array()
.unwrap()
.iter()
.map(|id| SHA1::from_str(id.as_str().unwrap()).unwrap())
.collect(),
author: Signature::from_data(value.author.unwrap().into()).unwrap(),
committer: Signature::from_data(value.committer.unwrap().into()).unwrap(),
message: value.content.unwrap(),
}
}
}

impl From<git_commit::Model> for Commit {
fn from(value: git_commit::Model) -> Self {
Commit {
id: SHA1::from_str(&value.commit_id).unwrap(),
tree_id: SHA1::from_str(&value.tree).unwrap(),
parent_commit_ids: value
.parents_id
.as_array()
.unwrap()
.iter()
.map(|id| SHA1::from_str(id.as_str().unwrap()).unwrap())
.collect(),
author: Signature::from_data(value.author.unwrap().into()).unwrap(),
committer: Signature::from_data(value.committer.unwrap().into()).unwrap(),
message: value.content.unwrap(),
}
}
}

impl From<Commit> for mega_commit::Model {
fn from(value: Commit) -> Self {
Expand Down
8 changes: 5 additions & 3 deletions mercury/src/internal/object/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ impl ObjectTrait for Blob {
}

impl Blob {

/// Create a new Blob object from the given content string.
/// - This is a convenience method for creating a Blob from a string.
/// - It converts the string to bytes and then calls `from_content_bytes`.
pub fn from_content(content: &str) -> Self {
// let blob_content = Cursor::new(utils::compress_zlib(content.as_bytes()).unwrap());
// let mut buf = ReadBoxed::new(blob_content, ObjectType::Blob, content.len());
// Blob::from_buf_read(&mut buf, content.len())
let content = content.as_bytes().to_vec();
Blob::from_content_bytes(content)
}
Expand All @@ -94,6 +95,7 @@ impl Blob {
/// - some file content can't be represented as a string (UTF-8), so we need to use bytes.
pub fn from_content_bytes(content: Vec<u8>) -> Self {
Blob {
// Calculate the SHA1 hash from the type and content
id: SHA1::from_type_and_data(ObjectType::Blob, &content),
data: content,
}
Expand Down
66 changes: 66 additions & 0 deletions mercury/src/internal/object/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use crate::internal::object::signature::Signature;
use crate::internal::object::ObjectTrait;
use crate::internal::object::ObjectType;
use bstr::ByteSlice;
use callisto::git_commit;
use callisto::mega_commit;
use serde::Deserialize;
use serde::Serialize;

Expand All @@ -34,6 +36,7 @@ use serde::Serialize;
/// - The author and committer fields contain the name, email address, timestamp and timezone.
/// - The message field contains the commit message, which maybe include signed or DCO.
#[derive(Eq, Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Commit {
pub id: SHA1,
pub tree_id: SHA1,
Expand All @@ -48,6 +51,8 @@ impl PartialEq for Commit {
}
}



impl Display for Commit {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
writeln!(f, "tree: {}", self.tree_id)?;
Expand Down Expand Up @@ -76,11 +81,24 @@ impl Commit {
committer,
message: message.to_string(),
};
// Calculate the hash of the commit object
// The hash is calculated from the type and data of the commit object
let hash = SHA1::from_type_and_data(ObjectType::Commit, &commit.to_data().unwrap());
commit.id = hash;
commit
}

/// Creates a new commit object from a tree ID and a list of parent commit IDs.
/// This function generates the author and committer signatures using the current time
/// and a fixed email address.
/// It also sets the commit message to the provided string.
/// # Arguments
/// - `tree_id`: The SHA1 hash of the tree object that this commit points to.
/// - `parent_commit_ids`: A vector of SHA1 hashes of the parent commits.
/// - `message`: A string containing the commit message.
/// # Returns
/// A new `Commit` object with the specified tree ID, parent commit IDs, and commit message.
/// The author and committer signatures are generated using the current time and a fixed email address.
pub fn from_tree_id(tree_id: SHA1, parent_commit_ids: Vec<SHA1>, message: &str) -> Commit {
let author = Signature::from_data(
format!(
Expand Down Expand Up @@ -214,3 +232,51 @@ impl ObjectTrait for Commit {
Ok(data)
}
}
fn commit_from_model(
commit_id: &str,
tree: &str,
parents_id: &serde_json::Value,
author: Option<String>,
committer: Option<String>,
message: Option<String>,
) -> Commit {
Commit {
id: SHA1::from_str(commit_id).unwrap(),
tree_id: SHA1::from_str(tree).unwrap(),
parent_commit_ids: parents_id
.as_array()
.unwrap()
.iter()
.map(|id| SHA1::from_str(id.as_str().unwrap()).unwrap())
.collect(),
author: Signature::from_data(author.unwrap().into()).unwrap(),
committer: Signature::from_data(committer.unwrap().into()).unwrap(),
message: message.unwrap(),
}
}

impl From<mega_commit::Model> for Commit {
fn from(value: mega_commit::Model) -> Self {
commit_from_model(
&value.commit_id,
&value.tree,
&value.parents_id,
value.author,
value.committer,
value.content,
)
}
}

impl From<git_commit::Model> for Commit {
fn from(value: git_commit::Model) -> Self {
commit_from_model(
&value.commit_id,
&value.tree,
&value.parents_id,
value.author,
value.committer,
value.content,
)
}
}
6 changes: 3 additions & 3 deletions mercury/src/internal/pack/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ mod tests {
#[tokio::test]
async fn test_pack_check_header() {
let res = crate::test_utils::setup_lfs_file().await;
println!("{:?}", res);
println!("{res:?}");
let source = res
.get("git-2d187177923cd618a75da6c6db45bb89d92bd504.pack")
.unwrap();
Expand Down Expand Up @@ -733,7 +733,7 @@ mod tests {
assert_eq!(bytes_read, compressed_size);
assert_eq!(decompressed_data, data);
}
Err(e) => panic!("Decompression failed: {:?}", e),
Err(e) => panic!("Decompression failed: {e:?}"),
}
}

Expand Down Expand Up @@ -803,7 +803,7 @@ mod tests {
});
if let Err(e) = rt {
fs::remove_dir_all(tmp).unwrap();
panic!("Error: {:?}", e);
panic!("Error: {e:?}");
}
} // it will be stuck on dropping `Pack` on Windows if `mem_size` is None, so we need `mimalloc`

Expand Down
4 changes: 2 additions & 2 deletions mercury/src/internal/pack/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,10 @@ mod tests {
let value = 16389;

let data = encode_offset(value);
println!("{:?}", data);
println!("{data:?}");
let mut reader = Cursor::new(data);
let (result, _) = read_offset_encoding(&mut reader).unwrap();
println!("result: {}", result);
println!("result: {result}" );
assert_eq!(result, value as u64);
}
}
Loading