diff --git a/src/git.rs b/src/git.rs
@@ -14,7 +14,8 @@ pub type RepositoryMetadataList = BTreeMap<Option<String>, Vec<RepositoryMetadat
#[derive(Clone)]
pub struct Git {
- commits: moka::future::Cache<String, Arc<Commit>>,
+ commits: moka::future::Cache<Oid, Arc<Commit>>,
+ readme_cache: moka::future::Cache<PathBuf, Arc<str>>,
repository_metadata: Arc<ArcSwapOption<RepositoryMetadataList>>,
}
@@ -22,6 +23,7 @@ impl Default for Git {
fn default() -> Self {
Self {
commits: moka::future::Cache::new(100),
+ readme_cache: moka::future::Cache::new(100),
repository_metadata: Arc::new(ArcSwapOption::default()),
}
}
@@ -32,7 +34,7 @@ impl Git {
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();
@@ -45,6 +47,26 @@ impl Git {
.await
}
+ pub async fn get_readme(&self, repo: PathBuf) -> Arc<str> {
+ 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()
+ })
+ .await
+ }
+
pub async fn get_latest_commit<'a>(&'a self, repo: PathBuf) -> Commit {
tokio::task::spawn_blocking(move || {
let repo = Repository::open_bare(repo).unwrap();
diff --git a/src/methods/repo.rs b/src/methods/repo.rs
@@ -111,14 +111,21 @@ pub async fn handle_refs(Extension(repo): Extension<Repository>) -> Html<String>
}
#[allow(clippy::unused_async)]
-pub async fn handle_about(Extension(repo): Extension<Repository>) -> Html<String> {
+pub async fn handle_about(
+ Extension(repo): Extension<Repository>,
+ Extension(RepositoryPath(repository_path)): Extension<RepositoryPath>,
+ Extension(git): Extension<Git>
+) -> Html<String> {
#[derive(Template)]
#[template(path = "repo/about.html")]
pub struct View {
repo: Repository,
+ readme: Arc<str>,
}
- 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
@@ -3,4 +3,5 @@
{% block about_nav_class %}active{% endblock %}
{% block content %}
+<pre>{{ readme }}</pre>
{% endblock %}
\ No newline at end of file