index : sparkle-git.git

ascending towards madness

author Jordan Doyle <jordan@doyle.la> 2022-07-06 11:09:47.0 +00:00:00
committer Jordan Doyle <jordan@doyle.la> 2022-07-06 11:09:47.0 +00:00:00
commit
1405fe168a84ed2d1b8758bdabea8a69c53a4c48 [patch]
tree
0f53080600de57f1decbbc751b4a9e3b8d57634a
parent
53166e7a3ed41e5608adc35b8bc7cdd12eb8b67c
download
1405fe168a84ed2d1b8758bdabea8a69c53a4c48.tar.gz

Allow READMEs with names other than README.md



Diff

 src/git.rs                | 27 ++++++++++++++++++---------
 src/methods/repo.rs       |  2 +-
 templates/repo/about.html |  6 +++++-
 3 files changed, 24 insertions(+), 11 deletions(-)

diff --git a/src/git.rs b/src/git.rs
index 8c38314..b20f84d 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -16,7 +16,7 @@ pub type RepositoryMetadataList = BTreeMap<Option<String>, Vec<RepositoryMetadat
#[derive(Clone)]
pub struct Git {
    commits: Cache<Oid, Arc<Commit>>,
    readme_cache: Cache<PathBuf, Arc<str>>,
    readme_cache: Cache<PathBuf, Option<Arc<str>>>,
    refs: Cache<PathBuf, Arc<Refs>>,
    repository_metadata: Arc<ArcSwapOption<RepositoryMetadataList>>,
}
@@ -96,7 +96,9 @@ impl Git {
            .await
    }

    pub async fn get_readme(&self, repo: PathBuf) -> Arc<str> {
    pub async fn get_readme(&self, repo: PathBuf) -> Option<Arc<str>> {
        const README_FILES: &[&str] = &["README.md", "README", "README.txt"];

        self.readme_cache
            .get_with(repo.clone(), async {
                tokio::task::spawn_blocking(move || {
@@ -105,14 +107,21 @@ impl Git {
                    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();
                    for file in README_FILES {
                        let object = if let Some(o) = tree.get_name(file) {
                            o
                        } else {
                            continue;
                        };

                        let object = object.to_object(&repo)
                            .unwrap();
                        let blob = object.into_blob().unwrap();

                        return Some(Arc::from(String::from_utf8(blob.content().to_vec()).unwrap()));
                    }

                    Arc::from(String::from_utf8(blob.content().to_vec()).unwrap())
                    None
                })
                .await
                .unwrap()
diff --git a/src/methods/repo.rs b/src/methods/repo.rs
index cd5b8fc..f75710c 100644
--- a/src/methods/repo.rs
+++ b/src/methods/repo.rs
@@ -161,7 +161,7 @@ pub async fn handle_about(
    #[template(path = "repo/about.html")]
    pub struct View {
        repo: Repository,
        readme: Arc<str>,
        readme: Option<Arc<str>>,
    }

    let readme = git.get_readme(repository_path).await;
diff --git a/templates/repo/about.html b/templates/repo/about.html
index 692ea07..3f899d0 100644
--- a/templates/repo/about.html
+++ b/templates/repo/about.html
@@ -3,5 +3,9 @@
{% block about_nav_class %}active{% endblock %}

{% block content %}
<pre>{{ readme }}</pre>
{% if let Some(readme) = readme %}
    <pre>{{ readme }}</pre>
{% else %}
    No README in repository HEAD.
{% endif %}
{% endblock %}
\ No newline at end of file