From d0c51ebea6268dc5211dea5e0645bc4cf61127e5 Mon Sep 17 00:00:00 2001 From: "benjamin.747" Date: Wed, 22 Nov 2023 10:34:35 +0800 Subject: [PATCH] add dependency import rules and format code --- craft/src/vault/command.rs | 28 +++++++--- craft/src/vault/crypt.rs | 8 +-- craft/src/vault/mod.rs | 19 +++++-- craft/src/vault/pgp_key.rs | 46 +++++++--------- docs/development.md | 54 ++++++++++++++++++- gateway/src/api_service/obj_service.rs | 5 +- gateway/src/https.rs | 12 ++--- gateway/src/model/mod.rs | 2 +- gateway/src/model/object_detail.rs | 3 +- git/src/hash.rs | 12 +++-- git/src/internal/diff/mod.rs | 3 +- git/src/internal/mod.rs | 20 +++---- git/src/internal/object/blob.rs | 2 +- git/src/internal/object/commit.rs | 17 +++--- git/src/internal/object/meta.rs | 16 +++--- git/src/internal/object/signature.rs | 24 ++++----- git/src/internal/object/tag.rs | 18 ++++--- git/src/internal/object/tree.rs | 38 +++++++------ git/src/internal/pack/cache.rs | 14 ++--- git/src/internal/pack/counter.rs | 2 +- git/src/internal/pack/cqueue.rs | 3 +- git/src/internal/pack/decode.rs | 3 +- git/src/internal/pack/encode.rs | 30 +++++------ git/src/internal/pack/iterator.rs | 14 ++--- git/src/internal/pack/mod.rs | 7 +-- git/src/internal/pack/preload.rs | 41 +++++++------- git/src/lfs/http.rs | 10 ++-- git/src/protocol/http.rs | 5 +- git/src/protocol/mod.rs | 15 +++--- git/src/protocol/pack.rs | 22 ++++---- git/src/protocol/ssh.rs | 19 ++++--- git/src/structure/conversion.rs | 20 +++---- git/src/structure/nodes.rs | 24 +++++---- kvcache/src/connector/fake/mod.rs | 8 +-- kvcache/src/connector/redis/mod.rs | 11 ++-- storage/entity/src/node.rs | 2 +- storage/entity/src/prelude.rs | 20 +++---- .../src/driver/file_storage/local_storage.rs | 14 ++--- 38 files changed, 349 insertions(+), 262 deletions(-) diff --git a/craft/src/vault/command.rs b/craft/src/vault/command.rs index 11db340d..d5c4ddbb 100644 --- a/craft/src/vault/command.rs +++ b/craft/src/vault/command.rs @@ -6,7 +6,7 @@ use clap::{arg, Args, Subcommand}; -use super::{ +use crate::vault::{ crypt::{decrypt_blob, encrypt_blob, generate_key_full}, init_rv_core, pgp_key::{delete_key, list_keys}, @@ -42,6 +42,13 @@ enum VaultMode { }, } +/// Handles different modes for interacting with the Rusty Vault. +/// +/// It initializes the Rusty Vault Core and performs operations based on the specified mode. +/// +/// # Arguments +/// +/// * `args` - A VaultArgs enum representing different modes of operation. pub fn handle(args: VaultArgs) { let (core, token) = init_rv_core(); // Match the mode with different functions @@ -79,7 +86,7 @@ pub fn handle(args: VaultArgs) { #[cfg(test)] mod tests { - use std::sync::{RwLock, Arc}; + use std::sync::{Arc, RwLock}; use rusty_vault::core::Core; @@ -91,14 +98,14 @@ mod tests { // Define a test function for generate-key-full mode // #[test] - fn test_generate_key_full(core: Arc>, token : &str) { + fn test_generate_key_full(core: Arc>, token: &str) { // generate a full key let _ = generate_key_full("Craft ", "secret/craft", core, token); } // Define a test function for encrypt mode // #[test] - fn test_encrypt(core: Arc>, token : &str) { + fn test_encrypt(core: Arc>, token: &str) { // generate key to crypt let _ = generate_key_full("User2 ", "secret/sci", core, token).unwrap(); // Create and run a new process to execute the encrypt_blob function @@ -131,7 +138,7 @@ mod tests { // Define a test function for decrypt mode // #[test] - fn test_decrypt(core: Arc>, token : &str) { + fn test_decrypt(core: Arc>, token: &str) { // Generate a key pair for testing let _ = generate_key_full( "User3 ", @@ -205,7 +212,7 @@ mod tests { // Define a test function for list-keys mode // #[test] - fn test_list_keys(core: Arc>, token : &str) { + fn test_list_keys(core: Arc>, token: &str) { let actual = list_keys("secret/", core, token).unwrap(); assert!(!actual.is_empty()); // Check if the output contains the expected key information @@ -213,8 +220,13 @@ mod tests { // Define a test function for delete-key mode // #[test] - fn test_delete_key(core: Arc>, token : &str) { - let _ = generate_key_full("Delete ", "secret/delete", core.clone(), token); + fn test_delete_key(core: Arc>, token: &str) { + let _ = generate_key_full( + "Delete ", + "secret/delete", + core.clone(), + token, + ); let _ = delete_key("secret/delete", core.clone(), token); } diff --git a/craft/src/vault/crypt.rs b/craft/src/vault/crypt.rs index d15ae955..5b009ad0 100644 --- a/craft/src/vault/crypt.rs +++ b/craft/src/vault/crypt.rs @@ -3,15 +3,16 @@ use std::{ sync::{Arc, RwLock}, }; -use anyhow::Ok; -use pgp_key::{decrypt_message, encrypt_message, generate_key_pair}; +use pgp_key::{ + decrypt_message, delete_key, encrypt_message, generate_key_pair, list_keys, KeyPair, +}; use rusty_vault::{ core::Core, logical::{Operation, Request}, }; use serde_json::json; -use crate::vault::pgp_key::{self, delete_key, list_keys, KeyPair}; +use crate::vault::pgp_key; // the trait and impl for KeyPair is a preparation for crate Tongsuo. // a trait for key @@ -58,7 +59,6 @@ impl Key for KeyPair { } } - // Generate full key with pubkey, seckey, primary id. // Arguments: primary_id, as &str, it should be written as "User "; key_name, git-craft will keep ur key file as key_namepub.asc pub fn generate_key_full( diff --git a/craft/src/vault/mod.rs b/craft/src/vault/mod.rs index 898bffaf..40111a43 100644 --- a/craft/src/vault/mod.rs +++ b/craft/src/vault/mod.rs @@ -1,7 +1,3 @@ -pub mod command; -pub mod crypt; -pub mod pgp_key; - use std::{ fs, path::Path, @@ -15,8 +11,22 @@ use rusty_vault::{ storage::{barrier_aes_gcm, physical}, }; +pub mod command; +pub mod crypt; +pub mod pgp_key; + pub const WORK_DIR_PATH_DEFAULT: &str = "/tmp/.mega/rusty_vault"; +/// Initializes the Rusty Vault Core. +/// +/// This function prepares the necessary configuration and initializes the Rusty Vault Core +/// based on the provided configuration or creates a default configuration if none exists. +/// It sets up storage, backend, and initializes encryption barriers required by the core. +/// If already initialized, it retrieves the root token and secret shares; otherwise, it initializes +/// the core, generates secret shares, and saves them securely. +/// +/// # Returns +/// Returns a tuple containing an Arc of the RwLock guarding the initialized Core and the root token. pub fn init_rv_core() -> (Arc>, String) { let path = Path::new(WORK_DIR_PATH_DEFAULT); let config_path = path.join("config.hcl"); @@ -100,4 +110,3 @@ pub fn init_rv_core() -> (Arc>, String) { } (Arc::clone(&c), token) } - diff --git a/craft/src/vault/pgp_key.rs b/craft/src/vault/pgp_key.rs index 7885e271..5097707c 100644 --- a/craft/src/vault/pgp_key.rs +++ b/craft/src/vault/pgp_key.rs @@ -1,11 +1,17 @@ -use anyhow::{Context, Result}; +//! +//! +//! +//! +//! +use std::{ + io::Cursor, + sync::{Arc, RwLock}, +}; +use anyhow::{Context, Result}; use pgp::{ - composed, - composed::signed_key::*, - crypto::{self, sym::SymmetricKeyAlgorithm}, - types::SecretKeyTrait, - Deserializable, Message, + crypto::sym::SymmetricKeyAlgorithm, types::SecretKeyTrait, Deserializable, KeyType, Message, + SecretKeyParamsBuilder, SignedPublicKey, SignedSecretKey, }; use rand::prelude::*; use rusty_vault::{ @@ -13,10 +19,6 @@ use rusty_vault::{ logical::{Operation, Request}, }; use smallvec::*; -use std::{ - io::Cursor, - sync::{Arc, RwLock}, -}; pub struct KeyPair { pub secret_key: pgp::SignedSecretKey, @@ -28,13 +30,13 @@ pub struct KeyPair { // Return: KeyPair, it has a signed secret key and a signed public key pub fn generate_key_pair(primary_user_id: &str) -> Result { // Set key_params with primary user id, Rsa with 2048 bites, symmetric algorithms key prefer to use is AES with 256 bit - let mut key_params = composed::key::SecretKeyParamsBuilder::default(); + let mut key_params = SecretKeyParamsBuilder::default(); key_params - .key_type(composed::KeyType::Rsa(2048)) + .key_type(KeyType::Rsa(2048)) .can_create_certificates(false) .can_sign(true) .primary_user_id(primary_user_id.into()) - .preferred_symmetric_algorithms(smallvec![crypto::sym::SymmetricKeyAlgorithm::AES256]); + .preferred_symmetric_algorithms(smallvec![SymmetricKeyAlgorithm::AES256]); // build a new SecretKeyParams let secret_key_params = key_params @@ -73,15 +75,11 @@ pub fn generate_key_pair(primary_user_id: &str) -> Result Result { let (pubkey, _) = SignedPublicKey::from_string(pubkey_str)?; // Requires a file name as the first arg, in this case I pass "none", as it's not used - let msg = composed::message::Message::new_literal("none", msg); + let msg = Message::new_literal("none", msg); // Encrypt let mut rng = StdRng::from_entropy(); - let new_msg = msg.encrypt_to_keys( - &mut rng, - crypto::sym::SymmetricKeyAlgorithm::AES128, - &[&pubkey], - )?; + let new_msg = msg.encrypt_to_keys(&mut rng, SymmetricKeyAlgorithm::AES128, &[&pubkey])?; Ok(new_msg.to_armored_string(None)?) } @@ -90,8 +88,8 @@ pub fn encrypt(msg: &str, pubkey_str: &str) -> Result { pub fn decrypt(armored: &str, seckey: &SignedSecretKey) -> Result { // Get encrypted contents let buf = Cursor::new(armored); - let (msg, _) = composed::message::Message::from_armor_single(buf) - .context("Failed to convert &str to armored message")?; + let (msg, _) = + Message::from_armor_single(buf).context("Failed to convert &str to armored message")?; // Set a decryptor let (decryptor, _) = msg .decrypt(|| String::from(""), &[seckey]) @@ -110,7 +108,6 @@ pub fn decrypt(armored: &str, seckey: &SignedSecretKey) -> Result Result { let (pubkey, _) = SignedPublicKey::from_string(pubkey)?; // Requires a file name as the first arg, in this case I pass "none", as it's not used typically, it's just meta data @@ -124,7 +121,6 @@ pub fn encrypt_message(msg: &str, pubkey: &str) -> Result { // Convert data from OpenPGP Message to String // Arguments: msg, OpenPGP Message; pk, a signed public key // Return: string -#[allow(unused)] pub fn generate_armored_string(msg: Message, pk: SignedPublicKey) -> Result { let mut rng = StdRng::from_entropy(); // encrypt the message @@ -135,7 +131,6 @@ pub fn generate_armored_string(msg: Message, pk: SignedPublicKey) -> Result Result { let (seckey, _) = SignedSecretKey::from_string(seckey)?; // get encrypted message @@ -158,9 +153,8 @@ pub fn decrypt_message(armored: &str, seckey: &str) -> Result>, token: &str) -> Result> { let core = core.read().unwrap(); let mut req = Request::new(key_path); diff --git a/docs/development.md b/docs/development.md index f2583000..06f159c7 100644 --- a/docs/development.md +++ b/docs/development.md @@ -2,4 +2,56 @@ ## Architect -![Mega Architect](images/mega-architect.png) \ No newline at end of file +![Mega Architect](images/mega-architect.png) + + +## Rust Dependency Import Order Guide + +This guide outlines the recommended order for importing dependencies in Rust projects. + +#### 1. Rust Standard Library + +Import dependencies from the Rust standard library. + +#### 2. Third-Party Crates +Import dependencies from third-party crates. + +#### 3. Other Modules in Workspace +Import dependencies from other modules within the project workspace. + +#### 4. Within Modules +Import functions and structs from within modules. + + +Example: +```rust + +// 1. Rust Standard Library +use std::collections::HashMap; +use std::path::PathBuf; +use std::str::FromStr; +use std::sync::{Arc, Mutex}; + +// 2. Third-Party Crates +use async_trait::async_trait; +use bytes::{BufMut, Bytes, BytesMut}; +use russh::server::{self, Auth, Msg, Session}; +use russh::{Channel, ChannelId}; +use russh_keys::key; +use tokio::io::{AsyncReadExt, BufReader}; + +// 3. Other Modules in Workspace +use storage::driver::database::storage::ObjectStorage; + +// 4. Other Files in the Same Module +use crate::protocol::pack::{self}; +use crate::protocol::ServiceType; +use crate::protocol::{PackProtocol, Protocol}; +``` + + +### Additional Notes: +- Always group imports with an empty line between different sections for better readability. +- Alphabetize imports within each section to maintain consistency. +- Avoid using extern crate syntax for Rust 2018 edition and later; prefer using use with crates. +- Do not use `super::` and `self::` in imports. It can lead to ambiguity and hinder code readability. Instead, use crate to reference the current crate's modules. diff --git a/gateway/src/api_service/obj_service.rs b/gateway/src/api_service/obj_service.rs index 27a78d49..f120b364 100644 --- a/gateway/src/api_service/obj_service.rs +++ b/gateway/src/api_service/obj_service.rs @@ -1,16 +1,15 @@ use std::collections::HashMap; - use std::sync::Arc; use axum::body::Full; use axum::response::{IntoResponse, Json}; use axum::{http::StatusCode, response::Response}; +use hyper::body::Bytes; -use storage::driver::database::storage::ObjectStorage; use git::internal::object::commit::Commit; use git::internal::object::tree::Tree; use git::internal::object::ObjectT; -use hyper::body::Bytes; +use storage::driver::database::storage::ObjectStorage; use crate::model::object_detail::{BlobObjects, Directories, Item}; use crate::model::query::DirectoryQuery; diff --git a/gateway/src/https.rs b/gateway/src/https.rs index 5dcbe9f2..6a0f6195 100644 --- a/gateway/src/https.rs +++ b/gateway/src/https.rs @@ -3,33 +3,29 @@ //! //! //! - use std::collections::HashMap; use std::ops::Deref; use std::path::PathBuf; use std::str::FromStr; use std::{net::SocketAddr, sync::Arc}; +use anyhow::Result; use axum::extract::{Query, State}; use axum::response::Response; use axum::routing::get; use axum::{Router, Server}; - -use tower::ServiceBuilder; -use tower_http::cors::{Any, CorsLayer}; - -use anyhow::Result; use clap::Args; use hyper::{Body, Request, StatusCode, Uri}; use regex::Regex; use serde::Deserialize; +use tower::ServiceBuilder; +use tower_http::cors::{Any, CorsLayer}; use common::enums::DataSource; use git::lfs::lfs_structs::LockListQuery; use git::lfs::{self, LfsConfig}; use git::protocol::{http, ServiceType}; use git::protocol::{PackProtocol, Protocol}; - use storage::driver::database; use storage::driver::database::storage::ObjectStorage; @@ -266,7 +262,7 @@ mod api_routers { }, }; - use super::AppState; + use crate::AppState; pub fn routers(state: AppState) -> Router { Router::new() diff --git a/gateway/src/model/mod.rs b/gateway/src/model/mod.rs index 92994093..c0c09559 100644 --- a/gateway/src/model/mod.rs +++ b/gateway/src/model/mod.rs @@ -1,2 +1,2 @@ pub mod object_detail; -pub mod query; \ No newline at end of file +pub mod query; diff --git a/gateway/src/model/object_detail.rs b/gateway/src/model/object_detail.rs index fd29a940..e64aae9d 100644 --- a/gateway/src/model/object_detail.rs +++ b/gateway/src/model/object_detail.rs @@ -1,6 +1,7 @@ -use entity::{node, repo_directory}; use serde::{Deserialize, Serialize}; +use entity::{node, repo_directory}; + #[derive(Serialize, Deserialize)] pub struct Directories { pub items: Vec, diff --git a/git/src/hash.rs b/git/src/hash.rs index 533fb3ae..7cecfc36 100644 --- a/git/src/hash.rs +++ b/git/src/hash.rs @@ -93,6 +93,8 @@ impl Hash { #[cfg(test)] mod tests { + use crate::hash::Hash; + #[test] fn test_hash_new() { // [98, 108, 111, 98] = blob @@ -100,11 +102,11 @@ mod tests { // [49, 52] = 14 // [0] = \x00 // [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33, 10] = Hello, World! + LF - // let hash = super::Hash::new(&vec![ + // let hash = Hash::new(&vec![ // 98, 108, 111, 98, 32, 49, 52, 0, 72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, // 100, 33, 10, // ]); - let hash = super::Hash::new_from_bytes(&[ + let hash = Hash::new_from_bytes(&[ 0x8a, 0xb6, 0x86, 0xea, 0xfe, 0xb1, 0xf4, 0x47, 0x02, 0x73, 0x8c, 0x8b, 0x0f, 0x24, 0xf2, 0x56, 0x7c, 0x36, 0xda, 0x6d, ]); @@ -116,7 +118,7 @@ mod tests { #[test] fn test_hash_new_from_str() { - let hash = super::Hash::new_from_str("8ab686eafeb1f44702738c8b0f24f2567c36da6d"); + let hash = Hash::new_from_str("8ab686eafeb1f44702738c8b0f24f2567c36da6d"); assert_eq!( hash.to_plain_str(), "8ab686eafeb1f44702738c8b0f24f2567c36da6d" @@ -125,7 +127,7 @@ mod tests { #[test] fn test_hash_to_data() { - let hash = super::Hash::new_from_str("8ab686eafeb1f44702738c8b0f24f2567c36da6d"); + let hash = Hash::new_from_str("8ab686eafeb1f44702738c8b0f24f2567c36da6d"); assert_eq!( hash.to_data(), vec![ @@ -137,7 +139,7 @@ mod tests { #[test] fn test_hash_from_bytes() { - let hash = super::Hash::new_from_bytes(&[ + let hash = Hash::new_from_bytes(&[ 0x8a, 0xb6, 0x86, 0xea, 0xfe, 0xb1, 0xf4, 0x47, 0x02, 0x73, 0x8c, 0x8b, 0x0f, 0x24, 0xf2, 0x56, 0x7c, 0x36, 0xda, 0x6d, ]); diff --git a/git/src/internal/diff/mod.rs b/git/src/internal/diff/mod.rs index 644d99bd..b9f1d862 100644 --- a/git/src/internal/diff/mod.rs +++ b/git/src/internal/diff/mod.rs @@ -244,8 +244,7 @@ mod tests{ use crate::internal::object::meta::Meta; use crate::internal::pack::delta::{undelta}; - - use super::DeltaDiff; + use crate::DeltaDiff; #[test] fn test_delta_fn(){ diff --git a/git/src/internal/mod.rs b/git/src/internal/mod.rs index 4fec8d39..cd775216 100644 --- a/git/src/internal/mod.rs +++ b/git/src/internal/mod.rs @@ -3,13 +3,13 @@ //! //! //! -pub mod object; -pub mod pack; -pub mod zlib; use std::fmt::Display; use crate::errors::GitError; +pub mod object; +pub mod pack; +pub mod zlib; /// In Git, each object type is assigned a unique integer value, which is used to identify the /// type of the object in Git repositories. /// @@ -152,22 +152,24 @@ impl ObjectType { #[cfg(test)] mod tests { + use crate::internal::ObjectType; + #[test] fn test_object_type_to_data() { - let blob = super::ObjectType::Blob; + let blob = ObjectType::Blob; let blob_bytes = blob.to_data().unwrap(); assert_eq!(blob_bytes, vec![0x62, 0x6c, 0x6f, 0x62]); } #[test] fn test_object_type_from_string() { - let tree = super::ObjectType::from_string("tree").unwrap(); - assert_eq!(tree, super::ObjectType::Tree); + let tree = ObjectType::from_string("tree").unwrap(); + assert_eq!(tree, ObjectType::Tree); } #[test] fn test_object_type_type2number() { - let commit = super::ObjectType::Commit; + let commit = ObjectType::Commit; let commit_number = commit.type2number(); assert_eq!(commit_number, 1); } @@ -175,7 +177,7 @@ mod tests { #[test] fn test_object_type_number2type() { let tag_number = 4; - let tag = super::ObjectType::number2type(tag_number).unwrap(); - assert_eq!(tag, super::ObjectType::Tag); + let tag = ObjectType::number2type(tag_number).unwrap(); + assert_eq!(tag, ObjectType::Tag); } } diff --git a/git/src/internal/object/blob.rs b/git/src/internal/object/blob.rs index 0a39dc95..c947ebce 100644 --- a/git/src/internal/object/blob.rs +++ b/git/src/internal/object/blob.rs @@ -31,10 +31,10 @@ use std::fmt::Display; use std::str; -use super::ObjectT; use crate::errors::GitError; use crate::hash::Hash; use crate::internal::object::tree::{TreeItem, TreeItemMode}; +use crate::internal::object::ObjectT; use crate::internal::ObjectType; /// **The Blob Object** diff --git a/git/src/internal/object/commit.rs b/git/src/internal/object/commit.rs index c037b2e2..64a7f5c1 100644 --- a/git/src/internal/object/commit.rs +++ b/git/src/internal/object/commit.rs @@ -17,16 +17,15 @@ use std::fmt::Display; use bstr::ByteSlice; + use entity::commit; use crate::errors::GitError; use crate::hash::Hash; -use crate::internal::ObjectType; - +use crate::internal::object::meta::Meta; use crate::internal::object::signature::Signature; - -use super::meta::Meta; -use super::ObjectT; +use crate::internal::object::ObjectT; +use crate::internal::ObjectType; /// The `Commit` struct is used to represent a commit object. /// @@ -72,7 +71,11 @@ impl From for Commit { Commit { id: Hash::new_from_str(&value.git_id), tree_id: Hash::new_from_str(&value.tree), - parent_tree_ids: value.pid.into_iter().map(|id| Hash::new_from_str(&id)).collect(), + parent_tree_ids: value + .pid + .into_iter() + .map(|id| Hash::new_from_str(&id)) + .collect(), author: Signature::new_from_data(value.author.unwrap().into()).unwrap(), committer: Signature::new_from_data(value.committer.unwrap().into()).unwrap(), message: value.content.unwrap(), @@ -276,7 +279,7 @@ mod tests { // fn test_to_file() { // let source = PathBuf::from(env::current_dir().unwrap().parent().unwrap()); // let mut source_file = source.clone(); - // let commit = super::Commit::new_from_file(source_file.to_str().unwrap()).unwrap(); + // let commit = Commit::new_from_file(source_file.to_str().unwrap()).unwrap(); // let mut dest_file = source.clone(); // dest_file.push("tests/objects/c5/170dd0aae2dc2a9142add9bb24597d326714d7"); diff --git a/git/src/internal/object/meta.rs b/git/src/internal/object/meta.rs index daf18f7a..cef82653 100644 --- a/git/src/internal/object/meta.rs +++ b/git/src/internal/object/meta.rs @@ -201,14 +201,16 @@ impl Meta { #[cfg(test)] mod tests { - use crate::internal::ObjectType; use std::env; use std::fs::remove_file; use std::path::PathBuf; + use crate::internal::object::meta::Meta; + use crate::internal::ObjectType; + #[test] fn test_meta_default() { - let meta = super::Meta::default(ObjectType::Blob); + let meta = Meta::default(ObjectType::Blob); assert_eq!(meta.object_type, ObjectType::Blob); assert_eq!(meta.size, 0); @@ -216,7 +218,7 @@ mod tests { #[test] fn test_new_from_data_with_object_type() { - let meta = super::Meta::new_from_data_with_object_type( + let meta = Meta::new_from_data_with_object_type( ObjectType::Blob, "Hello, World!".as_bytes().to_vec(), ); @@ -231,7 +233,7 @@ mod tests { #[test] fn test_to_folder_name() { - let meta = super::Meta::new_from_data_with_object_type( + let meta = Meta::new_from_data_with_object_type( ObjectType::Blob, "Hello, World!".as_bytes().to_vec(), ); @@ -241,7 +243,7 @@ mod tests { #[test] fn test_to_file_name() { - let meta = super::Meta::new_from_data_with_object_type( + let meta = Meta::new_from_data_with_object_type( ObjectType::Blob, "Hello, World!".as_bytes().to_vec(), ); @@ -271,7 +273,7 @@ mod tests { let mut source = PathBuf::from(env::current_dir().unwrap().parent().unwrap()); source.push("tests/data/objects/8a/b686eafeb1f44702738c8b0f24f2567c36da6d"); - let meta = super::Meta::new_from_file(source.as_path().to_str().unwrap()).unwrap(); + let meta = Meta::new_from_file(source.as_path().to_str().unwrap()).unwrap(); assert_eq!(meta.object_type, ObjectType::Blob); assert_eq!(meta.size, 14); @@ -287,7 +289,7 @@ mod tests { let mut source_file = source; source_file.push("tests/data/objects/8a/b686eafeb1f44702738c8b0f24f2567c36da6d"); - let m = super::Meta::new_from_file(source_file.as_path().to_str().unwrap()).unwrap(); + let m = Meta::new_from_file(source_file.as_path().to_str().unwrap()).unwrap(); let mut dest = PathBuf::from(env::current_dir().unwrap().parent().unwrap()); dest.push("tests/objects"); diff --git a/git/src/internal/object/signature.rs b/git/src/internal/object/signature.rs index f0abcc83..6dcbc816 100644 --- a/git/src/internal/object/signature.rs +++ b/git/src/internal/object/signature.rs @@ -182,43 +182,43 @@ impl Signature { mod tests { use std::str::FromStr; - use crate::internal::object::signature::Signature; + use crate::internal::object::signature::{Signature, SignatureType}; #[test] fn test_signature_type_from_str() { assert_eq!( - super::SignatureType::from_str("author").unwrap(), - super::SignatureType::Author + SignatureType::from_str("author").unwrap(), + SignatureType::Author ); assert_eq!( - super::SignatureType::from_str("committer").unwrap(), - super::SignatureType::Committer + SignatureType::from_str("committer").unwrap(), + SignatureType::Committer ); } #[test] fn test_signature_type_from_data() { assert_eq!( - super::SignatureType::from_data("author".to_string().into_bytes()).unwrap(), - super::SignatureType::Author + SignatureType::from_data("author".to_string().into_bytes()).unwrap(), + SignatureType::Author ); assert_eq!( - super::SignatureType::from_data("committer".to_string().into_bytes()).unwrap(), - super::SignatureType::Committer + SignatureType::from_data("committer".to_string().into_bytes()).unwrap(), + SignatureType::Committer ); } #[test] fn test_signature_type_to_bytes() { assert_eq!( - super::SignatureType::Author.to_bytes(), + SignatureType::Author.to_bytes(), "author".to_string().into_bytes() ); assert_eq!( - super::SignatureType::Committer.to_bytes(), + SignatureType::Committer.to_bytes(), "committer".to_string().into_bytes() ); } @@ -232,7 +232,7 @@ mod tests { ) .unwrap(); - assert_eq!(sign.signature_type, super::SignatureType::Author); + assert_eq!(sign.signature_type, SignatureType::Author); assert_eq!(sign.name, "Quanyi Ma"); assert_eq!(sign.email, "eli@patch.sh"); assert_eq!(sign.timestamp, 1678101573); diff --git a/git/src/internal/object/tag.rs b/git/src/internal/object/tag.rs index 714f08e7..a8de8b97 100644 --- a/git/src/internal/object/tag.rs +++ b/git/src/internal/object/tag.rs @@ -36,19 +36,19 @@ //! //! So, we can use the `git cat-file -p ` command to get the tag object, and the command not //! for the lightweight tag. +use std::fmt::Display; use bstr::ByteSlice; + use entity::objects; -use std::fmt::Display; use crate::errors::GitError; use crate::hash::Hash; use crate::internal::object::meta::Meta; use crate::internal::object::signature::Signature; +use crate::internal::object::ObjectT; use crate::internal::ObjectType; -use super::ObjectT; - /// The tag object is used to Annotated tag #[allow(unused)] #[derive(Clone)] @@ -181,10 +181,12 @@ impl ObjectT for Tag { let tagger = Signature::new_from_data(tagger_data).unwrap(); data = data[data.find_byte(0x0a).unwrap() + 1..].to_vec(); - let message = unsafe { data[data.find_byte(0x0a).unwrap()..] - .to_vec() - .to_str_unchecked() - .to_string() }; + let message = unsafe { + data[data.find_byte(0x0a).unwrap()..] + .to_vec() + .to_str_unchecked() + .to_string() + }; Tag { id: Hash([0u8; 20]), @@ -232,7 +234,7 @@ mod tests { let source = PathBuf::from(env::current_dir().unwrap().parent().unwrap()); let mut source_file = source; source_file.push("tests/data/objects/85/4aac1e94777f3ffc8722b69f087d1244587ab7"); - let _tag = super::Tag::new_from_file(source_file.to_str().unwrap()).unwrap(); + let _tag = Tag::new_from_file(source_file.to_str().unwrap()).unwrap(); let source = PathBuf::from(env::current_dir().unwrap().parent().unwrap()); let mut dest_file = source; diff --git a/git/src/internal/object/tree.rs b/git/src/internal/object/tree.rs index 590dcc08..a8f75c5e 100644 --- a/git/src/internal/object/tree.rs +++ b/git/src/internal/object/tree.rs @@ -14,18 +14,18 @@ //! have been added, modified, or deleted between two points in time. This allows Git to perform //! operations like merging and rebasing more quickly and accurately. //! +use std::fmt::Display; + use bstr::ByteSlice; use colored::Colorize; + use entity::objects; -use std::fmt::Display; use crate::errors::GitError; use crate::hash::Hash; - +use crate::internal::object::ObjectT; use crate::internal::ObjectType; -use super::ObjectT; - /// In Git, the mode field in a tree object's entry specifies the type of the object represented by /// that entry. The mode is a three-digit octal number that encodes both the permissions and the /// type of the object. The first digit specifies the object type, and the remaining two digits @@ -335,19 +335,23 @@ impl ObjectT for Tree { #[cfg(test)] mod tests { - use crate::internal::object::{meta::Meta, tree::Tree, ObjectT}; + use crate::internal::object::{ + meta::Meta, + tree::{Tree, TreeItem, TreeItemMode}, + ObjectT, + }; #[test] fn test_tree_item_new() { use crate::hash::Hash; - let tree_item = super::TreeItem::new( - super::TreeItemMode::Blob, + let tree_item = TreeItem::new( + TreeItemMode::Blob, Hash::new_from_str("8ab686eafeb1f44702738c8b0f24f2567c36da6d"), "hello-world".to_string(), ); - assert_eq!(tree_item.mode, super::TreeItemMode::Blob); + assert_eq!(tree_item.mode, TreeItemMode::Blob); assert_eq!( tree_item.id.to_plain_str(), "8ab686eafeb1f44702738c8b0f24f2567c36da6d" @@ -358,8 +362,8 @@ mod tests { fn test_tree_item_to_bytes() { use crate::hash::Hash; - let tree_item = super::TreeItem::new( - super::TreeItemMode::Blob, + let tree_item = TreeItem::new( + TreeItemMode::Blob, Hash::new_from_str("8ab686eafeb1f44702738c8b0f24f2567c36da6d"), "hello-world".to_string(), ); @@ -379,16 +383,16 @@ mod tests { fn test_tree_item_from_bytes() { use crate::hash::Hash; - let item = super::TreeItem::new( - super::TreeItemMode::Blob, + let item = TreeItem::new( + TreeItemMode::Blob, Hash::new_from_str("8ab686eafeb1f44702738c8b0f24f2567c36da6d"), "hello-world".to_string(), ); let bytes = item.to_data(); - let tree_item = super::TreeItem::new_from_bytes(bytes.as_slice()).unwrap(); + let tree_item = TreeItem::new_from_bytes(bytes.as_slice()).unwrap(); - assert_eq!(tree_item.mode, super::TreeItemMode::Blob); + assert_eq!(tree_item.mode, TreeItemMode::Blob); assert_eq!(tree_item.id.to_plain_str(), item.id.to_plain_str()); } @@ -403,7 +407,7 @@ mod tests { let tree = Tree::from_meta(m.clone()); println!("{}", tree); assert_eq!(tree.tree_items.len(), 1); - assert_eq!(tree.tree_items[0].mode, super::TreeItemMode::Blob); + assert_eq!(tree.tree_items[0].mode, TreeItemMode::Blob); assert_eq!( tree.tree_items[0].id.to_plain_str(), "8ab686eafeb1f44702738c8b0f24f2567c36da6d" @@ -428,7 +432,7 @@ mod tests { let tree = Tree::from_meta(m.clone()); for item in tree.tree_items.iter() { - if item.mode == super::TreeItemMode::Blob { + if item.mode == TreeItemMode::Blob { assert_eq!( item.id.to_plain_str(), "8ab686eafeb1f44702738c8b0f24f2567c36da6d" @@ -436,7 +440,7 @@ mod tests { assert_eq!(item.name, "hello-world"); } - if item.mode == super::TreeItemMode::Tree { + if item.mode == TreeItemMode::Tree { assert_eq!( item.id.to_plain_str(), "c44c09a88097e5fb0c833d4178b2df78055ad2e9" diff --git a/git/src/internal/pack/cache.rs b/git/src/internal/pack/cache.rs index ace8fb04..a9153abd 100644 --- a/git/src/internal/pack/cache.rs +++ b/git/src/internal/pack/cache.rs @@ -1,7 +1,8 @@ -use crate::hash::Hash; -use lru::LruCache; use std::{collections::HashMap, num::NonZeroUsize, cell::RefCell}; +use lru::LruCache; + +use crate::hash::Hash; #[derive(Hash, Clone, PartialEq, Eq)] struct OffHash { o: usize, @@ -109,11 +110,12 @@ where pub mod kvstore{ use std::cell::RefCell; use std::collections::HashMap; - use crate::internal::pack::Hash; - //use kvcache::connector::fake::FakeKVstore; + use kvcache::connector::redis::RedisClient; use kvcache::KVCache; - use super:: _Cache; + + use crate::internal::pack::Hash; + use crate::internal::pack::_Cache; pub struct ObjectCache { ioffset: RefCell>, @@ -165,7 +167,7 @@ mod test { use serde_json::to_vec; - use super::{ObjectCache, _Cache}; + use crate::internal::pack::{ObjectCache, _Cache}; use crate::{hash::Hash, internal::object::blob}; #[test] //TODO: to test fn test_cache() { diff --git a/git/src/internal/pack/counter.rs b/git/src/internal/pack/counter.rs index 5702207a..6c3908e4 100644 --- a/git/src/internal/pack/counter.rs +++ b/git/src/internal/pack/counter.rs @@ -79,7 +79,7 @@ impl Display for DecodeCounter{ #[cfg(test)] mod tests { - use super::GitTypeCounter; + use crate::internal::pack::counter::GitTypeCounter; #[test] fn test_git_type_counter(){ let mut counter = GitTypeCounter::default(); diff --git a/git/src/internal/pack/cqueue.rs b/git/src/internal/pack/cqueue.rs index 52083bf0..f8614bd6 100644 --- a/git/src/internal/pack/cqueue.rs +++ b/git/src/internal/pack/cqueue.rs @@ -80,7 +80,8 @@ impl CircularQueue { #[cfg(test)] mod tests { - use super::*; + use crate::internal::pack::cqueue::CircularQueue; + #[test] fn test_en_queue() { let mut circular_queue = CircularQueue::new(100); diff --git a/git/src/internal/pack/decode.rs b/git/src/internal/pack/decode.rs index 4c8a33db..77fa4e43 100644 --- a/git/src/internal/pack/decode.rs +++ b/git/src/internal/pack/decode.rs @@ -5,8 +5,8 @@ use sha1::digest::core_api::CoreWrapper; use sha1::Digest; use sha1::Sha1; -use super::{iterator::EntriesIter, Pack}; use crate::hash::Hash; +use crate::internal::pack::{iterator::EntriesIter, Pack}; use crate::{errors::GitError, utils}; #[allow(unused)] enum DecodeMod { @@ -171,5 +171,4 @@ mod test { assert_eq!(p.version, 2); assert_eq!(p.number_of_objects, p.number_of_objects()); } - } diff --git a/git/src/internal/pack/encode.rs b/git/src/internal/pack/encode.rs index f3243b48..90194aa0 100644 --- a/git/src/internal/pack/encode.rs +++ b/git/src/internal/pack/encode.rs @@ -1,13 +1,15 @@ -use entity::objects; -use sha1::{Digest, Sha1}; -use std::io::{Cursor, Write,Error}; +use std::io::{Cursor, Error, Write}; use std::sync::Arc; +use sha1::{Digest, Sha1}; + +use delta; +use entity::objects; + use crate::internal::object::ObjectT; +use crate::internal::pack::header::EntryHeader; use crate::internal::zlib::stream::deflate::Write as Writer; -use super::header::EntryHeader; -use delta; const SLID_WINDWOS: usize = 20; #[allow(unused)] @@ -186,20 +188,16 @@ fn u32_vec(value: u32) -> Vec { #[cfg(test)] mod tests { - - use tokio_test::block_on; - - use crate::{ - hash::Hash, - internal::{ - object::{blob::Blob, ObjectT}, - pack::Pack, - }, - }; use std::io::Cursor; use std::sync::Arc; - use super::{pack_encode, Encoder}; + use tokio_test::block_on; + + use crate::hash::Hash; + use crate::internal::object::blob::Blob; + use crate::internal::object::ObjectT; + use crate::internal::pack::encode::{pack_encode, Encoder}; + use crate::internal::pack::Pack; #[test] fn test_a_simple_encode() { diff --git a/git/src/internal/pack/iterator.rs b/git/src/internal/pack/iterator.rs index cd04eed3..1885ef5b 100644 --- a/git/src/internal/pack/iterator.rs +++ b/git/src/internal/pack/iterator.rs @@ -1,19 +1,21 @@ +use std::sync::Arc; + use storage::driver::database::storage::ObjectStorage; +use crate::internal::pack::cache::ObjectCache; +use crate::internal::pack::cache::_Cache; +use crate::internal::pack::delta::DeltaReader; use crate::{ internal::{ - object::{blob::Blob, commit::Commit, from_model, tag::Tag, tree::Tree, GitObjects}, - pack::delta::DeltaReader, + object::{ + blob::Blob, commit::Commit, from_model, tag::Tag, tree::Tree, GitObjects, ObjectT, + }, zlib::stream::inflate::ReadBoxed, GitError, ObjectType, }, utils, }; -use crate::internal::object::ObjectT; -use std::sync::Arc; - -use super::cache::{ObjectCache, _Cache}; type IteratorResult = Result, GitError>; type GitIteratorResult = Result; diff --git a/git/src/internal/pack/mod.rs b/git/src/internal/pack/mod.rs index aeb60fb0..49b04676 100644 --- a/git/src/internal/pack/mod.rs +++ b/git/src/internal/pack/mod.rs @@ -3,14 +3,15 @@ //! //! //! +use std::{path::PathBuf, sync::Arc}; + use self::{ cache::{ObjectCache, _Cache}, header::EntryHeader, }; -use super::object::ObjectT; use crate::hash::Hash; -use std::{path::PathBuf, sync::Arc}; +use crate::internal::object::ObjectT; mod cache; mod counter; @@ -125,9 +126,9 @@ pub mod git_object_size { #[cfg(test)] mod tests { - use super::*; use std::io::Cursor; + use crate::internal::pack::git_object_size::{decode, encode}; #[test] fn test_decode() { let data = [0x82, 0x01]; diff --git a/git/src/internal/pack/preload.rs b/git/src/internal/pack/preload.rs index b8487038..458a41c5 100644 --- a/git/src/internal/pack/preload.rs +++ b/git/src/internal/pack/preload.rs @@ -1,4 +1,24 @@ -use super::{counter::GitTypeCounter, EntryHeader, Pack}; +use std::{ + collections::HashMap, + io::{Cursor, Read}, + sync::{Arc, Mutex}, + time::Instant, +}; + +use num_cpus; +use rand::Rng; +use redis::{ErrorKind, FromRedisValue, RedisError, ToRedisArgs}; +use sea_orm::Set; +use serde::{Deserialize, Serialize}; +use sha1::{Digest, Sha1}; +use tokio::sync::{RwLock, RwLockReadGuard}; + +use delta; +use entity::{mr, objects}; +use storage::{driver::database::storage::ObjectStorage, utils::id_generator::generate_id}; + +use crate::internal::pack::cache::{kvstore::ObjectCache as kvObjectCache, ObjectCache, _Cache}; +use crate::internal::pack::{counter::GitTypeCounter, EntryHeader, Pack}; use crate::{ errors::GitError, internal::{ @@ -7,24 +27,7 @@ use crate::{ }, utils, }; -use super::cache::{ObjectCache,kvstore::ObjectCache as kvObjectCache, _Cache}; -use rand::Rng; -use serde::{Deserialize, Serialize}; -use storage::{driver::database::storage::ObjectStorage, utils::id_generator::generate_id}; -use entity::{objects, mr}; -use num_cpus; -use redis::{ToRedisArgs, FromRedisValue, RedisError, ErrorKind}; -use sea_orm::Set; -use sha1::{Digest, Sha1}; -use std::{ - collections::HashMap, - io::{Cursor, Read}, - sync::{Arc, Mutex}, - time::Instant -}; -use tokio::sync::{RwLock, RwLockReadGuard}; -use delta; /// /// One Pre loading Git object in memory @@ -261,7 +264,7 @@ pub async fn decode_load(p: PackPreload, storage: Arc) -> Res Ok(mr_id) } -use super::counter::CounterType::*; +use crate::internal::pack::counter::CounterType::*; /// Asynchronous function to produce Git objects. /// /// The `produce_object` function asynchronously generates Git objects based on the provided parameters. diff --git a/git/src/lfs/http.rs b/git/src/lfs/http.rs index 0b4dcfa6..54ae820d 100644 --- a/git/src/lfs/http.rs +++ b/git/src/lfs/http.rs @@ -10,12 +10,13 @@ use axum::body::Body; use axum::http::{Response, StatusCode}; use bytes::BytesMut; use chrono::{prelude::*, Duration}; -use common::errors::GitLFSError; -use entity::{locks, meta}; use futures::StreamExt; use hyper::Request; use rand::prelude::*; use sea_orm::{ActiveModelTrait, EntityTrait, Set}; + +use common::errors::GitLFSError; +use entity::{locks, meta}; use storage::driver::database::storage::ObjectStorage; use storage::driver::file_storage::local_storage::MetaObject; @@ -23,9 +24,8 @@ use crate::lfs::lfs_structs::{ BatchResponse, BatchVars, LockList, LockRequest, LockResponse, ObjectError, UnlockRequest, UnlockResponse, VerifiableLockList, VerifiableLockRequest, }; - -use super::lfs_structs::{Link, Lock, LockListQuery, Representation, RequestVars}; -use super::LfsConfig; +use crate::lfs::lfs_structs::{Link, Lock, LockListQuery, Representation, RequestVars}; +use crate::lfs::LfsConfig; pub async fn lfs_retrieve_lock( config: &LfsConfig, diff --git a/git/src/protocol/http.rs b/git/src/protocol/http.rs index 70a121ed..9382dd93 100644 --- a/git/src/protocol/http.rs +++ b/git/src/protocol/http.rs @@ -9,16 +9,13 @@ use anyhow::Result; use axum::body::Body; use axum::http::response::Builder; use axum::http::{Response, StatusCode}; - use bytes::{BufMut, Bytes, BytesMut}; - use futures::StreamExt; use hyper::body::Sender; use hyper::Request; - use tokio::io::{AsyncReadExt, BufReader}; -use super::{pack, PackProtocol}; +use crate::protocol::{pack, PackProtocol}; /// # Build Response headers for Smart Server. /// Clients MUST NOT reuse or revalidate a cached response. diff --git a/git/src/protocol/mod.rs b/git/src/protocol/mod.rs index 465432b2..7e093847 100644 --- a/git/src/protocol/mod.rs +++ b/git/src/protocol/mod.rs @@ -3,24 +3,23 @@ //! //! //! -pub mod http; -pub mod pack; -pub mod ssh; - use std::{ path::{Path, PathBuf}, str::FromStr, sync::Arc, }; -use storage::driver::{database::mysql_storage::MysqlStorage, database::storage::ObjectStorage}; - -use crate::protocol::pack::SP; +use sea_orm::{ActiveValue::NotSet, Set}; use common::{errors::MegaError, utils::ZERO_ID}; use entity::{mr_info, refs}; -use sea_orm::{ActiveValue::NotSet, Set}; +use storage::driver::{database::mysql_storage::MysqlStorage, database::storage::ObjectStorage}; + +use crate::protocol::pack::SP; +pub mod http; +pub mod pack; +pub mod ssh; #[derive(Clone)] pub struct PackProtocol { pub protocol: Protocol, diff --git a/git/src/protocol/pack.rs b/git/src/protocol/pack.rs index f96a6c89..5b193fdb 100644 --- a/git/src/protocol/pack.rs +++ b/git/src/protocol/pack.rs @@ -3,6 +3,17 @@ //! //! +use std::io::Write; +use std::{collections::HashSet, io::Cursor, sync::Arc}; + +use anyhow::Result; +use bytes::{Buf, BufMut, Bytes, BytesMut}; + +use storage::driver::database::storage::ObjectStorage; + +use crate::protocol::{ + new_mr_info, Capability, PackProtocol, Protocol, RefCommand, ServiceType, SideBind, +}; use crate::protocol::{RefsType, ZERO_ID}; use crate::structure::conversion; use crate::{ @@ -12,13 +23,6 @@ use crate::{ preload::{decode_load, PackPreload}, }, }; -use anyhow::Result; -use bytes::{Buf, BufMut, Bytes, BytesMut}; -use storage::driver::database::storage::ObjectStorage; -use std::io::Write; -use std::{collections::HashSet, io::Cursor, sync::Arc}; - -use super::{new_mr_info, Capability, PackProtocol, Protocol, RefCommand, ServiceType, SideBind}; const LF: char = '\n'; @@ -321,7 +325,6 @@ pub async fn unpack( let mut output = std::fs::File::create(path).unwrap(); output.write_all(pack_file).unwrap(); } - let curosr_pack = Cursor::new(pack_file); let reader = HashCounter::new(curosr_pack, count_hash); @@ -401,10 +404,9 @@ pub fn read_pkt_line(bytes: &mut Bytes) -> (usize, Bytes) { pub mod test { use bytes::{Bytes, BytesMut}; + use crate::protocol::pack::{add_pkt_line_string, read_pkt_line, read_until_white_space}; use crate::protocol::{Capability, CommandType, PackProtocol, RefCommand, RefsType}; - use super::{add_pkt_line_string, read_pkt_line, read_until_white_space}; - #[test] pub fn test_read_pkt_line() { let mut bytes = Bytes::from_static(b"001e# service=git-upload-pack\n"); diff --git a/git/src/protocol/ssh.rs b/git/src/protocol/ssh.rs index ef79cc4d..dd55de41 100644 --- a/git/src/protocol/ssh.rs +++ b/git/src/protocol/ssh.rs @@ -4,24 +4,23 @@ //! //! +use std::collections::HashMap; +use std::path::PathBuf; +use std::str::FromStr; +use std::sync::{Arc, Mutex}; + use async_trait::async_trait; use bytes::{BufMut, Bytes, BytesMut}; use russh::server::{self, Auth, Msg, Session}; use russh::{Channel, ChannelId}; - -use storage::driver::database::storage::ObjectStorage; use russh_keys::key; -use std::collections::HashMap; - -use std::path::PathBuf; -use std::str::FromStr; -use std::sync::{Arc, Mutex}; use tokio::io::{AsyncReadExt, BufReader}; -use crate::protocol::ServiceType; +use storage::driver::database::storage::ObjectStorage; -use super::pack::{self}; -use super::{PackProtocol, Protocol}; +use crate::protocol::pack::{self}; +use crate::protocol::ServiceType; +use crate::protocol::{PackProtocol, Protocol}; type ClientMap = HashMap<(usize, ChannelId), Channel>; diff --git a/git/src/structure/conversion.rs b/git/src/structure/conversion.rs index f27ef970..14e00068 100644 --- a/git/src/structure/conversion.rs +++ b/git/src/structure/conversion.rs @@ -2,7 +2,16 @@ use std::collections::HashMap; use std::path::{Component, Components, Path, PathBuf}; use std::{collections::HashSet, sync::Arc}; -use super::nodes::NodeBuilder; +use anyhow::Result; +use async_recursion::async_recursion; +use itertools::Itertools; +use sea_orm::ActiveValue::NotSet; +use sea_orm::{DbErr, Set, TransactionTrait}; + +use common::utils::ZERO_ID; +use entity::{objects, refs, repo_directory}; +use storage::driver::database::storage::ObjectStorage; + use crate::errors::GitError; use crate::hash::Hash; use crate::internal::object::blob::Blob; @@ -12,14 +21,7 @@ use crate::internal::object::tree::Tree; use crate::internal::object::ObjectT; use crate::internal::pack::encode::pack_encode; use crate::protocol::PackProtocol; -use anyhow::Result; -use async_recursion::async_recursion; -use common::utils::ZERO_ID; -use entity::{objects, refs, repo_directory}; -use itertools::Itertools; -use sea_orm::ActiveValue::NotSet; -use sea_orm::{DbErr, Set, TransactionTrait}; -use storage::driver::database::storage::ObjectStorage; +use crate::structure::nodes::NodeBuilder; impl PackProtocol { /// Asynchronously retrieves the full pack data for the specified repository path. diff --git a/git/src/structure/nodes.rs b/git/src/structure/nodes.rs index 3ef195c4..4b6e7176 100644 --- a/git/src/structure/nodes.rs +++ b/git/src/structure/nodes.rs @@ -7,14 +7,16 @@ use std::{ use anyhow::Ok; use async_recursion::async_recursion; +use sea_orm::{ActiveValue::NotSet, DatabaseTransaction, Set}; + use common::errors::MegaError; +use entity::{commit, node}; use storage::{ driver::database::storage::ObjectStorage, utils::id_generator::{self, generate_id}, }; -use entity::{commit, node}; -use sea_orm::{ActiveValue::NotSet, Set, DatabaseTransaction}; +use crate::structure::GitNodeObject; use crate::{ hash::Hash, internal::object::{ @@ -25,8 +27,6 @@ use crate::{ }, }; -use super::GitNodeObject; - pub struct NodeBuilder { // pub repo_root: Box, pub storage: Arc, @@ -315,7 +315,9 @@ impl NodeBuilder { .get_obj_data_by_id(&root_tree_id.to_plain_str()) .await .unwrap() - .unwrap_or_else(|| panic!("can't get obj data {} from db", root_tree_id.to_plain_str())); + .unwrap_or_else(|| { + panic!("can't get obj data {} from db", root_tree_id.to_plain_str()) + }); let mut obj = Tree::new_from_data(model.data.clone()); let hash = Hash::new_from_str(&model.git_id); obj.set_hash(hash); @@ -411,7 +413,11 @@ impl NodeBuilder { self.storage.save_commits(txn, save_models).await } - pub async fn save_nodes(&self, txn: Option<&DatabaseTransaction>, nodes: Vec) -> Result { + pub async fn save_nodes( + &self, + txn: Option<&DatabaseTransaction>, + nodes: Vec, + ) -> Result { self.storage.save_nodes(txn, nodes).await } } @@ -463,15 +469,11 @@ pub fn print_node(node: &dyn Node, depth: u32) { #[cfg(test)] mod test { - // use crate::mega::driver::{ - // structure::nodes::{Node, TreeNode}, - // utils::id_generator, - // }; use std::path::PathBuf; use storage::utils::id_generator; - use super::{FileNode, Node, TreeNode}; + use crate::structure::{FileNode, Node, TreeNode}; #[test] pub fn main() { diff --git a/kvcache/src/connector/fake/mod.rs b/kvcache/src/connector/fake/mod.rs index f6027f7a..18c27b9c 100644 --- a/kvcache/src/connector/fake/mod.rs +++ b/kvcache/src/connector/fake/mod.rs @@ -1,7 +1,9 @@ -use super::Connector; -use anyhow::Result; use std::{cell::RefCell, collections::HashMap, hash::Hash}; +use anyhow::Result; + +use crate::connector::Connector; + pub struct FakeKVstore { table: RefCell>, } @@ -35,7 +37,7 @@ where #[cfg(test)] mod tests { - use super::FakeKVstore; + use crate::connector::fake::FakeKVstore; use crate::KVCache; #[test] diff --git a/kvcache/src/connector/redis/mod.rs b/kvcache/src/connector/redis/mod.rs index fef71c12..2ef94646 100644 --- a/kvcache/src/connector/redis/mod.rs +++ b/kvcache/src/connector/redis/mod.rs @@ -1,11 +1,10 @@ -use crate::utils; -use super::Connector; -use anyhow::Result; -use redis::{ - Connection, ConnectionInfo, FromRedisValue,ToRedisArgs, IntoConnectionInfo, -}; use std::{cell::RefCell, marker::PhantomData}; +use anyhow::Result; +use redis::{Connection, ConnectionInfo, FromRedisValue, IntoConnectionInfo, ToRedisArgs}; + +use crate::connector::Connector; +use crate::utils; pub struct RedisClient { conn: RefCell, k: PhantomData, diff --git a/storage/entity/src/node.rs b/storage/entity/src/node.rs index df73ac9d..2066b9cc 100644 --- a/storage/entity/src/node.rs +++ b/storage/entity/src/node.rs @@ -67,7 +67,7 @@ impl Ord for Model { #[cfg(test)] mod tests{ - use super::Model; + use crate::node::Model; #[test] fn test_nodes_sort(){ // 示例使用 diff --git a/storage/entity/src/prelude.rs b/storage/entity/src/prelude.rs index 78f595be..e41b56ab 100644 --- a/storage/entity/src/prelude.rs +++ b/storage/entity/src/prelude.rs @@ -1,12 +1,12 @@ //! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3 -pub use super::commit::Entity as Commit; -pub use super::objects::Entity as GitObj; -pub use super::locks::Entity as Locks; -pub use super::meta::Entity as Meta; -pub use super::mr::Entity as Mr; -pub use super::mr_info::Entity as MrInfo; -pub use super::node::Entity as Node; -pub use super::refs::Entity as Refs; -pub use super::repo_directory::Entity as RepoDirectory; -pub use super::pull_request::Entity as PullRequest; +pub use crate::commit::Entity as Commit; +pub use crate::objects::Entity as GitObj; +pub use crate::locks::Entity as Locks; +pub use crate::meta::Entity as Meta; +pub use crate::mr::Entity as Mr; +pub use crate::mr_info::Entity as MrInfo; +pub use crate::node::Entity as Node; +pub use crate::refs::Entity as Refs; +pub use crate::repo_directory::Entity as RepoDirectory; +pub use crate::pull_request::Entity as PullRequest; diff --git a/storage/src/driver/file_storage/local_storage.rs b/storage/src/driver/file_storage/local_storage.rs index c263ae0d..ed59d747 100644 --- a/storage/src/driver/file_storage/local_storage.rs +++ b/storage/src/driver/file_storage/local_storage.rs @@ -1,12 +1,14 @@ -use async_trait::async_trait; -use bytes::Bytes; -use common::errors::MegaError; use std::fs; use std::io::prelude::*; use std::path; use std::path::PathBuf; -use super::FileStorage; +use async_trait::async_trait; +use bytes::Bytes; + +use common::errors::MegaError; + +use crate::driver::file_storage::FileStorage; #[derive(Default)] pub struct LocalStorage { @@ -64,9 +66,9 @@ impl FileStorage for LocalStorage { #[cfg(test)] mod tests { - use std::env; + use std::{env, path::PathBuf}; - use super::*; + use crate::driver::file_storage::{local_storage::{MetaObject, LocalStorage}, FileStorage}; // #[test] #[tokio::test]