From fb33221b29832fa9e2d57c94cbc4f63a92c22dbd Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Wed, 06 Jul 2022 02:09:28 +0100 Subject: [PATCH] Implement repository about page --- src/git.rs | 26 +++++++++++++++++++++++++- src/methods/repo.rs | 11 +++++++++++ templates/repo/about.html | 1 + 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/git.rs b/src/git.rs index cfb66f8..943206f 100644 --- a/src/git.rs +++ a/src/git.rs @@ -14,7 +14,8 @@ #[derive(Clone)] pub struct Git { - commits: moka::future::Cache>, + commits: moka::future::Cache>, + readme_cache: moka::future::Cache>, repository_metadata: Arc>, } @@ -22,6 +23,7 @@ fn default() -> Self { Self { commits: moka::future::Cache::new(100), + readme_cache: moka::future::Cache::new(100), repository_metadata: Arc::new(ArcSwapOption::default()), } } @@ -32,12 +34,32 @@ let commit = Oid::from_str(commit).unwrap(); self.commits - .get_with(commit.to_string(), async { + .get_with(commit, async { tokio::task::spawn_blocking(move || { let repo = Repository::open_bare(repo).unwrap(); let commit = repo.find_commit(commit).unwrap(); Arc::new(Commit::from(commit)) + }) + .await + .unwrap() + }) + .await + } + + pub async fn get_readme(&self, repo: PathBuf) -> Arc { + self.readme_cache + .get_with(repo.clone(), async { + tokio::task::spawn_blocking(move || { + let repo = Repository::open_bare(repo).unwrap(); + let head = repo.head().unwrap(); + let commit = head.peel_to_commit().unwrap(); + let tree = commit.tree().unwrap(); + + let object = tree.get_name("README.md").unwrap().to_object(&repo).unwrap(); + let blob = object.into_blob().unwrap(); + + Arc::from(String::from_utf8(blob.content().to_vec()).unwrap()) }) .await .unwrap() diff --git a/src/methods/repo.rs b/src/methods/repo.rs index 915ea2f..3926eff 100644 --- a/src/methods/repo.rs +++ a/src/methods/repo.rs @@ -111,14 +111,21 @@ } #[allow(clippy::unused_async)] -pub async fn handle_about(Extension(repo): Extension) -> Html { +pub async fn handle_about( + Extension(repo): Extension, + Extension(RepositoryPath(repository_path)): Extension, + Extension(git): Extension +) -> Html { #[derive(Template)] #[template(path = "repo/about.html")] pub struct View { repo: Repository, + readme: Arc, } - Html(View { repo }.render().unwrap()) + let readme = git.get_readme(repository_path).await; + + Html(View { repo, readme }.render().unwrap()) } #[derive(Deserialize)] diff --git a/templates/repo/about.html b/templates/repo/about.html index f4d3acc..692ea07 100644 --- a/templates/repo/about.html +++ a/templates/repo/about.html @@ -1,6 +1,7 @@ {% extends "repo/base.html" %} {% block about_nav_class %}active{% endblock %} {% block content %} +
{{ readme }}
{% endblock %}-- rgit 0.1.3