Skip to content

Commit

Permalink
Merge pull request #438 from benjamin-747/main
Browse files Browse the repository at this point in the history
add module css file, add mr-file api
  • Loading branch information
genedna authored Jul 5, 2024
2 parents d988859 + 0957fab commit 34e1065
Show file tree
Hide file tree
Showing 17 changed files with 369 additions and 161 deletions.
200 changes: 192 additions & 8 deletions ceres/src/api_service/mono_api_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use venus::monorepo::converter;
use crate::api_service::ApiHandler;
use crate::model::create_file::CreateFileInfo;
use crate::model::mr::{MRDetail, MrInfoItem};
use crate::model::tree::MRFileTree;

#[derive(Clone)]
pub struct MonoApiService {
Expand Down Expand Up @@ -217,16 +216,57 @@ impl MonoApiService {
Ok(None)
}

pub async fn mr_tree_files(&self, mr_id: i64) -> Result<MRFileTree, MegaError> {
pub async fn mr_tree_files(&self, mr_id: i64) -> Result<Vec<PathBuf>, MegaError> {
let storage = self.context.services.mega_storage.clone();
let model = storage.get_mr(mr_id).await.unwrap();
if let Some(model) = model {
let start_tree = storage.get_commit_by_hash(&model.to_hash).await.unwrap().unwrap().tree;
let mut stack = VecDeque::new();
stack.push_back(start_tree);
// while let Some(_) = stack.pop_front() {
let to_tree_id = storage
.get_commit_by_hash(&model.to_hash)
.await
.unwrap()
.unwrap()
.tree;

let from_tree_id = storage
.get_commit_by_hash(&model.from_hash)
.await
.unwrap()
.unwrap()
.tree;

let mut tree_comparation = TreeComparation {
left_tree: VecDeque::new(),
result_file: vec![],
};
tree_comparation.left_tree.push_back((
SHA1::from_str(&to_tree_id).unwrap(),
Some(SHA1::from_str(&from_tree_id).unwrap()),
PathBuf::from(model.path),
));
while let Some((new_tree, base_tree, mut current_path)) =
tree_comparation.left_tree.pop_front()
{
let new_tree: Tree = storage
.get_tree_by_hash(&new_tree.to_plain_str())
.await
.unwrap()
.unwrap()
.into();

// }
let base_tree = if let Some(base_tree) = base_tree {
let base_tree: Tree = storage
.get_tree_by_hash(&base_tree.to_plain_str())
.await
.unwrap()
.unwrap()
.into();
Some(base_tree)
} else {
None
};
tree_comparation.compare(new_tree, base_tree, &mut current_path);
}
return Ok(tree_comparation.result_file);
}
Err(MegaError::with_message("Can not find related MR by id"))
}
Expand Down Expand Up @@ -337,9 +377,91 @@ impl MonoApiService {
}
}

pub struct TreeComparation {
pub left_tree: VecDeque<(SHA1, Option<SHA1>, PathBuf)>,
pub result_file: Vec<PathBuf>,
}

impl TreeComparation {
// fn compare(&mut self, new: Tree, base: Option<Tree>) {
// let new_set: HashSet<_> = new.tree_items.into_iter().collect();
// let base_set = if let Some(tree) = base {
// tree.tree_items.into_iter().collect()
// } else {
// HashSet::new()
// };
// let diff: Vec<_> = new_set.symmetric_difference(&base_set).cloned().collect();
// for item in diff {
// if item.mode == TreeItemMode::Tree {
// let t_id = item.id.to_plain_str();
// if !self.left_tree.contains(&t_id) {
// self.left_tree.push_back(t_id);
// }
// } else {
// self.result_file.push(item.name)
// }
// }
// }

pub fn compare(&mut self, new: Tree, base: Option<Tree>, current_path: &mut PathBuf) {
let mut map_name_to_item = HashMap::new();
let mut map_id_to_item = HashMap::new();

if let Some(tree) = base {
for item in tree.tree_items {
map_name_to_item.insert(item.name.clone(), item.clone());
map_id_to_item.insert(item.id, item.clone());
}
}

for item in new.tree_items {
match (
map_name_to_item.get(&item.name),
map_id_to_item.get(&item.id),
) {
(Some(base_item), _) if base_item.id != item.id => {
self.process_diff(item, Some(base_item.id), current_path);
}
(_, Some(base_item)) if base_item.name != item.name => {
self.process_diff(item, None, current_path);
}
(None, None) => {
self.process_diff(item, None, current_path);
}
_ => {}
}
}
}

fn process_diff(&mut self, item: TreeItem, base_id: Option<SHA1>, current_path: &mut PathBuf) {
if item.mode == TreeItemMode::Tree {
if !self
.left_tree
.contains(&(item.id, base_id, current_path.to_path_buf()))
{
current_path.push(item.name);
self.left_tree
.push_back((item.id, base_id, current_path.to_path_buf()));
current_path.pop();
}
} else {
current_path.push(item.name);
self.result_file.push(current_path.to_path_buf());
current_path.pop();
}
}
}

#[cfg(test)]
mod test {
use std::path::PathBuf;
use std::{collections::VecDeque, path::PathBuf};

use mercury::{
hash::SHA1,
internal::object::tree::{Tree, TreeItem, TreeItemMode},
};

use crate::api_service::mono_api_service::TreeComparation;

#[test]
pub fn test() {
Expand All @@ -351,4 +473,66 @@ mod test {
println!("name: {}, path: {:?}", name, full_path);
}
}

#[test]
fn test_compare_tree() {
let base = Tree::from_tree_items(vec![
TreeItem {
mode: TreeItemMode::Tree,
id: SHA1::new(&"src".as_bytes().to_vec()),
name: "src".to_string(),
},
TreeItem {
mode: TreeItemMode::Tree,
id: SHA1::new(&"mega".as_bytes().to_vec()),
name: "mega".to_string(),
},
TreeItem {
mode: TreeItemMode::Blob,
id: SHA1::new(&"delete.txt".as_bytes().to_vec()),
name: "delete.txt".to_string(),
},
TreeItem {
mode: TreeItemMode::Blob,
id: SHA1::new(&"README.md".as_bytes().to_vec()),
name: "README.md".to_string(),
},
])
.unwrap();

let new = Tree::from_tree_items(vec![
TreeItem {
mode: TreeItemMode::Tree,
id: SHA1::new(&"src".as_bytes().to_vec()),
name: "src_new".to_string(),
},
TreeItem {
mode: TreeItemMode::Tree,
id: SHA1::new(&"mega222".as_bytes().to_vec()),
name: "mega".to_string(),
},
TreeItem {
mode: TreeItemMode::Blob,
id: SHA1::new(&"Cargo.toml".as_bytes().to_vec()),
name: "Cargo.toml".to_string(),
},
TreeItem {
mode: TreeItemMode::Blob,
id: SHA1::new(&"README.md".as_bytes().to_vec()),
name: "README.md".to_string(),
},
])
.unwrap();

let mut tree_comparation = TreeComparation {
left_tree: VecDeque::new(),
result_file: vec![],
};
// let mut current_path = Path::new("/");
tree_comparation.compare(new, Some(base), &mut PathBuf::from("/"));
println!(
"files: {:?} left Tree: {:?}",
tree_comparation.result_file, tree_comparation.left_tree
);
}
}
5 changes: 2 additions & 3 deletions gateway/src/api/mr_router.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{collections::HashMap, path::PathBuf};

use axum::{
extract::{Path, Query, State},
Expand All @@ -10,7 +10,6 @@ use axum::{
use crate::api::ApiServiceState;
use ceres::model::{
mr::{MRDetail, MrInfoItem},
tree::MRFileTree,
CommonResult,
};

Expand Down Expand Up @@ -62,7 +61,7 @@ async fn mr_detail(
async fn get_mr_files(
Path(mr_id): Path<i64>,
state: State<ApiServiceState>,
) -> Result<Json<CommonResult<MRFileTree>>, (StatusCode, String)> {
) -> Result<Json<CommonResult<Vec<PathBuf>>>, (StatusCode, String)> {
let res = state.monorepo().mr_tree_files(mr_id).await;
let res = match res {
Ok(data) => CommonResult::success(Some(data)),
Expand Down
2 changes: 1 addition & 1 deletion moon/src/components/Bottombar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const navigation = [
},
]

export default function Bottombar() {
export default function BottomBar() {
const currentYear = new Date().getFullYear();
return (
<footer className="bg-white">
Expand Down
5 changes: 5 additions & 0 deletions moon/src/components/BreadCrumb.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.breadCrumb {
width: 80%;
margin-left: 18%;
margin-top: 20px;
}
9 changes: 5 additions & 4 deletions moon/src/components/BreadCrumb.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

import 'github-markdown-css/github-markdown-light.css';
import { useRouter } from 'next/router';
import { Breadcrumb } from 'antd/lib';
import 'github-markdown-css/github-markdown-light.css'
import { useRouter } from 'next/router'
import { Breadcrumb } from 'antd/lib'
import styles from './BreadCrumb.module.css'

const Bread = () => {
const router = useRouter();
Expand All @@ -18,7 +19,7 @@ const Bread = () => {
}));

return (
<Breadcrumb className='breadCrumb'
<Breadcrumb className={styles.breadCrumb}
items={breadCrumbItems}
/>
);
Expand Down
41 changes: 41 additions & 0 deletions moon/src/components/CodeContent.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
.codeShowContainer {
border-radius: 1rem;
padding-top: 70px;
}

.codeLineNumber {
margin-left: 15px;
margin-right: 25px;
}

.viewChangeTab {
background-color: rgba(53, 53, 53, 0.103);
display: flex;
position: absolute;
width: 80%;
height: 50px;
border-top-left-radius: 1rem;
border-top-right-radius: 1rem;
}

.viewChangeTabButton {
padding-left: 20px;
padding-right: 20px;
height: 100%;
border-radius: 1rem;
border: none;
background-color: transparent;
}

.viewChangeTabButton:hover,
.viewChangeTabButton:checked {
background-color: rgba(0, 0, 0, 0.121);
}

.fileCodeContainer {
width: 80%;
margin-left: 18%;
border-radius: 0.5rem;
margin-top: 10px;

}
25 changes: 13 additions & 12 deletions moon/src/components/CodeContent.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Editor from './editor/Editor';
import 'github-markdown-css/github-markdown-light.css';
import { Highlight, themes } from "prism-react-renderer";
import { useState } from 'react';
import { createRoot } from 'react-dom/client';
import Editor from './editor/Editor'
import 'github-markdown-css/github-markdown-light.css'
import { Highlight, themes } from "prism-react-renderer"
import { useState } from 'react'
import { createRoot } from 'react-dom/client'
import styles from './CodeContent.module.css'

const CodeContent = ({ fileContent }) => {

Expand Down Expand Up @@ -34,12 +35,12 @@ const CodeContent = ({ fileContent }) => {


return (
<div className="fileCodeContainer">
<div className="viewChangeTab">
<button className='viewChangeTabButton'>
<div className={styles.fileCodeContainer} >
<div className={styles.viewChangeTab}>
<button className={styles.viewChangeTabButton}>
Code
</button>
<button className='viewChangeTabButton'>
<button className={styles.viewChangeTabButton}>
Blame
</button>
</div>
Expand All @@ -50,11 +51,11 @@ const CodeContent = ({ fileContent }) => {
language="rust"
>
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<pre style={style} className="codeShowContainer">
<pre style={style} className={styles.codeShowContainer}>
{tokens.map((line, i) => (
<div key={i} {...getLineProps({ line })}>
<button onClick={(event) => handleLineNumberClick(i)} className="lineNumberButton" style={{ marginLeft: '8px', backgroundColor: 'rgb(247, 237, 224, 0.7)', width: '25px', height: '17px', lineHeight: '17px', borderRadius: '3px', marginTop: '5px', border: 'none' }}>+</button>
<span className="codeLineNumber">{i + 1}</span>
<button onClick={(event) => handleLineNumberClick(i)} className={styles.lineNumberButton} style={{ marginLeft: '8px', backgroundColor: 'rgb(247, 237, 224, 0.7)', width: '25px', height: '17px', lineHeight: '17px', borderRadius: '3px', marginTop: '5px', border: 'none' }}>+</button>
<span className={styles.codeLineNumber}>{i + 1}</span>
{line.map((token, key) => (
<span key={key} {...getTokenProps({ token })} />
))}
Expand Down
Loading

1 comment on commit 34e1065

@vercel
Copy link

@vercel vercel bot commented on 34e1065 Jul 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

mega – ./

mega-git-main-gitmono.vercel.app
mega-gitmono.vercel.app
gitmega.dev
www.gitmega.dev

Please sign in to comment.