Skip to content

Commit 98c77d0

Browse files
committed
feat(repo): Add warehouse file viewing function and optimize file list
1 parent fa79d6a commit 98c77d0

File tree

11 files changed

+696
-6
lines changed

11 files changed

+696
-6
lines changed

api/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rsession::SessionBuilder;
1010
use std::net::SocketAddr;
1111
use tracing::{error, info};
1212
use crate::repo::branch::repo_branch;
13+
use crate::repo::cat_file::repo_cat_file;
1314
use crate::repo::dash::repo_dash;
1415
use crate::repo::tree::repo_tree;
1516
use crate::repo::commits::repo_commits;
@@ -63,6 +64,7 @@ impl ApiService {
6364
scope("/{owner}/{repo}")
6465
.route("",get().to(repo_dash))
6566
.route("/tree/{path:.*}",get().to(repo_tree))
67+
.route("/cat_file/{path:.*}",get().to(repo_cat_file))
6668
.route("/commits",get().to(repo_commits))
6769
.route("/branches", get().to(repo_branch))
6870
)

api/src/repo/cat_file.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use std::collections::HashMap;
2+
use actix_web::{HttpResponse, Responder};
3+
use actix_web::web::{Data, Path, Query};
4+
use serde_json::json;
5+
use infra::App;
6+
7+
pub async fn repo_cat_file(
8+
path: Path<(String,String,String)>,
9+
app: Data<App>,
10+
query: Query<HashMap<String,String>>
11+
) -> impl Responder {
12+
let (owner,repo,path) = path.into_inner();
13+
let repo = match app.repository_dash(repo,owner).await {
14+
Ok(x) => x.repo,
15+
Err(e) => return HttpResponse::Ok()
16+
.json(json!({ "code": 500, "message": e.to_string()})),
17+
};
18+
let branch = query.get("branch").map(|x|x.clone());
19+
let commit = query.get("commit").map(|x|x.clone());
20+
match app.cat_file(repo,path.as_ref(),branch,commit).await {
21+
Ok(x) => HttpResponse::Ok()
22+
.body(x),
23+
Err(e) => HttpResponse::Ok()
24+
.json(json!({ "code": 500, "message": e.to_string()})),
25+
}
26+
}

api/src/repo/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ pub mod list;
33
pub mod dash;
44
pub mod tree;
55
pub mod commits;
6-
pub mod branch;
6+
pub mod branch;
7+
pub mod cat_file;

git/src/cat_file.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use crate::AppGit;
2+
use std::path::Path;
3+
4+
impl AppGit {
5+
pub fn cat_file(&self, branch: Option<String>, commit: Option<String>, path: &str) -> anyhow::Result<Vec<u8>> {
6+
let repo = self.git()?;
7+
let refs = match branch {
8+
Some(branch) => repo
9+
.find_branch(&branch, git2::BranchType::Local)?
10+
.into_reference(),
11+
None => repo.head()?,
12+
};
13+
let commit = match commit {
14+
Some(oid) => repo.find_commit(git2::Oid::from_str(&oid)?)?,
15+
None => repo.find_commit(
16+
refs.target()
17+
.ok_or(anyhow::anyhow!("Failed to get target from reference"))?,
18+
)?,
19+
};
20+
let tree = commit.tree()?;
21+
let tree = tree.get_path(Path::new(path))?;
22+
let object = tree.to_object(&repo)?;
23+
let blob = object.as_blob()
24+
.ok_or(anyhow::anyhow!("Failed to get blob from object"))?;
25+
return Ok(blob.content().to_vec());
26+
}
27+
}

git/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@ pub mod commit;
4747
pub mod remote;
4848
pub mod tag;
4949
pub mod tree;
50+
pub mod cat_file;

infra/src/service/cat_file.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use git::AppGit;
2+
use crate::App;
3+
use crate::entities::repository::RepositoryModel;
4+
5+
impl App {
6+
pub async fn cat_file(&self, repo: RepositoryModel, path: &str, branch: Option<String>, commit: Option<String>) -> anyhow::Result<Vec<u8>> {
7+
let git = AppGit::new(repo.to_path());
8+
return git.cat_file(branch, commit, path);
9+
}
10+
}

infra/src/service/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod auth;
22
pub mod repository;
33
pub mod sync_hook;
4-
pub mod branch;
4+
pub mod branch;
5+
pub mod cat_file;

views/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,15 @@
3737
"react-icons": "^5.5.0",
3838
"react-markdown": "^10.1.0",
3939
"react-router-dom": "^7.7.1",
40+
"react-syntax-highlighter": "^15.6.1",
41+
"rehype-katex": "^7.0.1",
4042
"rehype-raw": "^7.0.0",
4143
"remark-breaks": "^4.0.0",
44+
"remark-emoji": "^5.0.1",
45+
"remark-gemoji": "^8.0.0",
4246
"remark-gfm": "^4.0.1",
47+
"remark-math": "^6.0.0",
48+
"remark-slug": "^7.0.1",
4349
"zustand": "^5.0.3"
4450
},
4551
"devDependencies": {
@@ -50,6 +56,7 @@
5056
"@types/node": "^20",
5157
"@types/react": "^19.1.8",
5258
"@types/react-dom": "^19.1.6",
59+
"@types/react-syntax-highlighter": "^15.5.13",
5360
"@vitejs/plugin-react-swc": "^3.10.2",
5461
"eslint": "^9.30.1",
5562
"eslint-config-next": "15.2.0",

0 commit comments

Comments
 (0)