index : sparkle-git.git

ascending towards madness

author Jordan Doyle <jordan@doyle.la> 2022-07-06 1:09:28.0 +00:00:00
committer Jordan Doyle <jordan@doyle.la> 2022-07-06 1:09:28.0 +00:00:00
commit
fb33221b29832fa9e2d57c94cbc4f63a92c22dbd [patch]
tree
bb3ea8bc4ed294f51b0da0e4dccea5c578e285ee
parent
c856af78b169e9609eba4475d043a025cd59efd2
download
fb33221b29832fa9e2d57c94cbc4f63a92c22dbd.tar.gz

Implement repository about page



Diff

 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
+++ 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
index 915ea2f..3926eff 100644
--- 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
index f4d3acc..692ea07 100644
--- 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