From 1405fe168a84ed2d1b8758bdabea8a69c53a4c48 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Wed, 06 Jul 2022 12:09:47 +0100 Subject: [PATCH] Allow READMEs with names other than README.md --- src/git.rs | 29 ++++++++++++++++++++++------- src/methods/repo.rs | 2 +- templates/repo/about.html | 6 +++++- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/git.rs b/src/git.rs index 8c38314..b20f84d 100644 --- a/src/git.rs +++ a/src/git.rs @@ -16,7 +16,7 @@ #[derive(Clone)] pub struct Git { commits: Cache>, - readme_cache: Cache>, + readme_cache: Cache>>, refs: Cache>, repository_metadata: Arc>, } @@ -96,7 +96,9 @@ .await } - pub async fn get_readme(&self, repo: PathBuf) -> Arc { + pub async fn get_readme(&self, repo: PathBuf) -> Option> { + const README_FILES: &[&str] = &["README.md", "README", "README.txt"]; + self.readme_cache .get_with(repo.clone(), async { tokio::task::spawn_blocking(move || { @@ -104,15 +106,22 @@ let head = repo.head().unwrap(); let commit = head.peel_to_commit().unwrap(); let tree = commit.tree().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())); + } - 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()) + None }) .await .unwrap() diff --git a/src/methods/repo.rs b/src/methods/repo.rs index cd5b8fc..f75710c 100644 --- a/src/methods/repo.rs +++ a/src/methods/repo.rs @@ -161,7 +161,7 @@ #[template(path = "repo/about.html")] pub struct View { repo: Repository, - readme: Arc, + readme: Option>, } 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 +++ a/templates/repo/about.html @@ -1,7 +1,11 @@ {% extends "repo/base.html" %} {% block about_nav_class %}active{% endblock %} {% block content %} -
{{ readme }}
+{% if let Some(readme) = readme %} +
{{ readme }}
+{% else %} + No README in repository HEAD. +{% endif %} {% endblock %}-- rgit 0.1.3