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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,5 @@ git2 = "0.19.0"
tempfile = "3.13.0"
home = "0.5.9"
ring = "0.17.8"
secp256k1 = "0.30.0"
cedar-policy = "4.2.1"
secp256k1 = "0.30.0"
21 changes: 14 additions & 7 deletions ceres/src/api_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,20 @@ pub trait ApiHandler: Send + Sync {
target: &TreeItem,
) -> Commit;

async fn get_blob_as_string(&self, file_path: PathBuf) -> Result<String, GitError> {
async fn get_blob_as_string(&self, file_path: PathBuf) -> Result<Option<String>, GitError> {
let filename = file_path.file_name().unwrap().to_str().unwrap();
let parent = file_path.parent().unwrap();
let mut plain_text = String::new();
if let Some(tree) = self.search_tree_by_path(parent).await? {
if let Some(item) = tree.tree_items.into_iter().find(|x| x.name == filename) {
plain_text = match self.get_raw_blob_by_hash(&item.id.to_plain_str()).await {
Ok(Some(model)) => String::from_utf8(model.data.unwrap()).unwrap(),
_ => String::new(),
match self.get_raw_blob_by_hash(&item.id.to_plain_str()).await {
Ok(Some(model)) => {
return Ok(Some(String::from_utf8(model.data.unwrap()).unwrap()))
}
_ => return Ok(None),
};
}
}
return Ok(plain_text);
return Ok(None);
}

async fn get_latest_commit(&self, path: PathBuf) -> Result<LatestCommitInfo, GitError> {
Expand Down Expand Up @@ -160,15 +161,21 @@ pub trait ApiHandler: Send + Sync {
.map(|x| (x.id.to_plain_str(), x))
.collect();

let root_commit: Option<Commit> = None;
for item in tree.tree_items {
let mut info: TreeCommitItem = item.clone().into();
if let Some(commit_id) = item_to_commit.get(&item.id.to_plain_str()) {
let commit = if let Some(commit) = commit_map.get(commit_id) {
commit
} else {
tracing::warn!("failed fecth commit: {}", commit_id);
let root_commit = if let Some(ref root_commit) = root_commit {
root_commit.clone()
} else {
self.get_root_commit().await
};
&self
.traverse_commit_history(&path, self.get_root_commit().await, &item)
.traverse_commit_history(&path, root_commit, &item)
.await
};
info.oid = commit.id.to_plain_str();
Expand Down
68 changes: 29 additions & 39 deletions ceres/src/api_service/mono_api_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,6 @@ impl ApiHandler for MonoApiService {
}

impl MonoApiService {
pub async fn init_monorepo(&self) {
self.context.services.mono_storage.init_monorepo().await
}

pub async fn mr_list(&self, status: &str) -> Result<Vec<MrInfoItem>, MegaError> {
let status = if status == "open" {
vec![MergeStatus::Open]
Expand Down Expand Up @@ -279,48 +275,42 @@ impl MonoApiService {
Err(MegaError::with_message("Can not find related MR by id"))
}

pub async fn merge_mr(&self, mr_link: &str) -> Result<(), MegaError> {
pub async fn merge_mr(&self, mr: &mut MergeRequest) -> Result<(), MegaError> {
let storage = self.context.services.mono_storage.clone();
if let Some(model) = storage.get_open_mr_by_link(mr_link).await.unwrap() {
let mut mr: MergeRequest = model.into();
let refs = storage.get_ref(&mr.path).await.unwrap().unwrap();

if mr.from_hash == refs.ref_commit_hash {
// update mr
mr.merge();
storage.update_mr(mr.clone().into()).await.unwrap();
let refs = storage.get_ref(&mr.path).await.unwrap().unwrap();

let commit: Commit = storage
.get_commit_by_hash(&mr.to_hash)
if mr.from_hash == refs.ref_commit_hash {
// update mr
mr.merge();
let commit: Commit = storage
.get_commit_by_hash(&mr.to_hash)
.await
.unwrap()
.unwrap()
.into();
// add conversation
storage
.add_mr_conversation(&mr.mr_link, 0, ConvType::Merged, None)
.await
.unwrap();
if mr.path != "/" {
let path = PathBuf::from(mr.path.clone());
// beacuse only parent tree is needed so we skip current directory
let (tree_vec, _) = self
.search_tree_for_update(path.parent().unwrap())
.await
.unwrap()
.unwrap()
.into();

// add conversation
storage
.add_mr_conversation(&mr.mr_link, 0, ConvType::Merged, None)
.unwrap();
self.update_parent_tree(path, tree_vec, commit)
.await
.unwrap();
if mr.path != "/" {
let path = PathBuf::from(mr.path.clone());
// beacuse only parent tree is needed so we skip current directory
let (tree_vec, _) = self
.search_tree_for_update(path.parent().unwrap())
.await
.unwrap();
self.update_parent_tree(path, tree_vec, commit)
.await
.unwrap();
// remove refs start with path
storage.remove_refs(&mr.path).await.unwrap();
// TODO: self.clean_dangling_commits().await;
}
} else {
return Err(MegaError::with_message("ref hash conflict"));
// remove refs start with path
storage.remove_refs(&mr.path).await.unwrap();
// TODO: self.clean_dangling_commits().await;
}
// update mr status last
storage.update_mr(mr.clone().into()).await.unwrap();
} else {
return Err(MegaError::with_message("Invalid mr id"));
return Err(MegaError::with_message("ref hash conflict"));
}
Ok(())
}
Expand Down
11 changes: 10 additions & 1 deletion common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,22 @@ impl Default for StorageConfig {
pub struct MonoConfig {
pub import_dir: PathBuf,
pub disable_http_push: bool,
pub admin: String,
pub root_dirs: Vec<String>,
}

impl Default for MonoConfig {
fn default() -> Self {
Self {
import_dir: PathBuf::from("/third-part"),
disable_http_push: false,
admin: String::from("admin"),
root_dirs: vec![
"third-part".to_string(),
"project".to_string(),
"doc".to_string(),
"release".to_string(),
],
}
}
}
Expand All @@ -237,7 +246,7 @@ impl Default for PackConfig {
pack_decode_cache_path: PathBuf::from("/tmp/.mega/cache"),
clean_cache_after_decode: true,
channel_message_size: 1_000_000,
maximum_pack_size: 1,
maximum_pack_size: 4,
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions docker/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,15 @@ obs_endpoint = "https://obs.cn-east-3.myhuaweicloud.com"
## Mega treats files under this directory as import repo and other directories as monorepo
import_dir = "/third-part"

# The current mono Git HTTP server does not support authentication, so we provided a disabled operations here
disable_http_push = false

# Set System Admin in directory init, replace the admin's github username here
admin = "admin"

# Set serveral root dirs in directory init
root_dirs = ["third-part", "project", "doc", "release"]

[pack]
# The maximum memory used by decode, Unit is GB
pack_decode_mem_size = 4
Expand All @@ -68,8 +75,7 @@ clean_cache_after_decode = true
channel_message_size = 1_000_000

# Maximum pack size, unit GB, enforces to use LFS off the limit
maximum_pack_size = 1

maximum_pack_size = 4

[lfs]
# LFS Server url
Expand Down
3 changes: 1 addition & 2 deletions gateway/src/https_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ pub async fn app(
ztm: ZtmOptions,
) -> Router {
let context = Context::new(config.clone()).await;
context.services.mono_storage.init_monorepo().await;
context.services.mono_storage.init_monorepo(&config.monorepo).await;
let state = AppState {
host,
port,
Expand Down Expand Up @@ -215,7 +215,6 @@ pub fn check_run_with_ztm(config: Config, ztm: ZtmOptions, http_port: u16) {
thread::sleep(time::Duration::from_secs(3));
tokio::spawn(async move {
let context = Context::new(config.clone()).await;
context.services.mono_storage.init_monorepo().await;
cache_public_repository(bootstrap_node, context, ztm_agent).await
});
}
Expand Down
1 change: 1 addition & 0 deletions jupiter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ path = "src/lib.rs"
callisto = { workspace = true }
common = { workspace = true }
mercury = { workspace = true }
saturn = { workspace = true }

sea-orm = { workspace = true, features = [
"sqlx-postgres",
Expand Down
5 changes: 3 additions & 2 deletions jupiter/src/storage/mono_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use callisto::db_enums::{ConvType, MergeStatus};
use callisto::{
mega_blob, mega_commit, mega_mr, mega_mr_conv, mega_refs, mega_tag, mega_tree, raw_blob,
};
use common::config::MonoConfig;
use common::errors::MegaError;
use common::utils::generate_id;
use mercury::internal::object::MegaObjectModel;
Expand Down Expand Up @@ -267,12 +268,12 @@ impl MonoStorage {
Ok(())
}

pub async fn init_monorepo(&self) {
pub async fn init_monorepo(&self, mono_config: &MonoConfig) {
if self.get_ref("/").await.unwrap().is_some() {
tracing::info!("Monorepo Directory Already Inited, skip init process!");
return;
}
let converter = MegaModelConverter::init();
let converter = MegaModelConverter::init(mono_config);
let commit: mega_commit::Model = converter.commit.into();
mega_commit::Entity::insert(commit.into_active_model())
.exec(self.get_connection())
Expand Down
75 changes: 36 additions & 39 deletions jupiter/src/utils/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::cell::RefCell;
use std::collections::HashMap;

use callisto::{mega_blob, mega_refs, mega_tree, raw_blob};
use common::config::MonoConfig;
use common::utils::generate_id;
use mercury::hash::SHA1;
use mercury::internal::object::blob::Blob;
Expand All @@ -21,40 +22,36 @@ pub fn generate_git_keep_with_timestamp() -> Blob {
Blob::from_content(&git_keep_content)
}

pub fn init_trees(git_keep: &Blob) -> (HashMap<SHA1, Tree>, Tree) {
let tree_item = TreeItem {
mode: TreeItemMode::Blob,
id: git_keep.id,
name: String::from(".gitkeep"),
};
let tree = Tree::from_tree_items(vec![tree_item.clone()]).unwrap();

let root_items = vec![
TreeItem {
mode: TreeItemMode::Tree,
id: tree.id,
name: String::from("third-part"),
},
TreeItem {
mode: TreeItemMode::Tree,
id: tree.id,
name: String::from("project"),
},
TreeItem {
mode: TreeItemMode::Tree,
id: tree.id,
name: String::from("doc"),
},
TreeItem {
pub fn init_trees(mono_config: &MonoConfig) -> (HashMap<SHA1, Tree>, HashMap<SHA1, Blob>, Tree) {
let mut root_items = Vec::new();
let mut trees = Vec::new();
let mut blobs = Vec::new();
for dir in mono_config.root_dirs.clone() {
let entity_str =
saturn::entitystore::generate_entity(&mono_config.admin, &format!("/{}", dir)).unwrap();
let blob = Blob::from_content(&entity_str);

let tree_item = TreeItem {
mode: TreeItemMode::Blob,
id: blob.id,
name: String::from(".mega_cedar.json"),
};
let tree = Tree::from_tree_items(vec![tree_item.clone()]).unwrap();
root_items.push(TreeItem {
mode: TreeItemMode::Tree,
id: tree.id,
name: String::from("release"),
},
];
name: dir,
});
trees.push(tree);
blobs.push(blob);
}

let root = Tree::from_tree_items(root_items).unwrap();
let trees = vec![tree];
(trees.into_iter().map(|x| (x.id, x)).collect(), root)
(
trees.into_iter().map(|x| (x.id, x)).collect(),
blobs.into_iter().map(|x| (x.id, x)).collect(),
root,
)
}

pub struct MegaModelConverter {
Expand Down Expand Up @@ -102,12 +99,9 @@ impl MegaModelConverter {
}
}

pub fn init() -> Self {
let git_keep = generate_git_keep();
let (tree_maps, root_tree) = init_trees(&git_keep);
pub fn init(mono_config: &MonoConfig) -> Self {
let (tree_maps, blob_maps, root_tree) = init_trees(mono_config);
let commit = Commit::from_tree_id(root_tree.id, vec![], "Init Mega Directory");
let mut blob_maps = HashMap::new();
blob_maps.insert(git_keep.id, git_keep);

let mega_ref = mega_refs::Model {
id: generate_id(),
Expand Down Expand Up @@ -138,19 +132,22 @@ mod test {

use std::str::FromStr;

use common::config::MonoConfig;
use mercury::{hash::SHA1, internal::object::commit::Commit};

use crate::utils::converter::MegaModelConverter;

#[test]
pub fn test_init_mega_dir() {
let converter = MegaModelConverter::init();
let mono_config = MonoConfig::default();
let converter = MegaModelConverter::init(&mono_config);
let mega_trees = converter.mega_trees.borrow().clone();
let mega_blobs = converter.mega_blobs.borrow().clone();
let raw_blob = converter.raw_blobs.borrow().clone();
assert_eq!(mega_trees.len(), 2);
assert_eq!(mega_blobs.len(), 1);
assert_eq!(raw_blob.len(), 1);
let dir_nums = mono_config.root_dirs.len();
assert_eq!(mega_trees.len(), dir_nums + 1);
assert_eq!(mega_blobs.len(), dir_nums);
assert_eq!(raw_blob.len(), dir_nums);
}

#[test]
Expand Down
9 changes: 8 additions & 1 deletion mega/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,15 @@ obs_endpoint = "https://obs.cn-east-3.myhuaweicloud.com"
## Mega treats files under this directory as import repo and other directories as monorepo
import_dir = "/third-part"

# The current mono Git HTTP server does not support authentication, so we provided a disabled operations here
disable_http_push = false

# Set System Admin in directory init, replace the admin's github username here
admin = "admin"

# Set serveral root dirs in directory init
root_dirs = ["third-part", "project", "doc", "release"]

[pack]
# The maximum memory used by decode, Unit is GB
pack_decode_mem_size = 4
Expand All @@ -68,7 +75,7 @@ clean_cache_after_decode = true
channel_message_size = 1_000_000

# Maximum pack size, unit GB, enforces to use LFS off the limit
maximum_pack_size = 1
maximum_pack_size = 4

[lfs]
# LFS Server url
Expand Down
Loading