mygit

[UNMAINTAINED] A cgit/webgit alternative, written in Rust
Log | Files | Refs | README | LICENSE

commit fe09c2bde2463c9550ea9dce93f8ed3fe575bfda
parent a4259e3b27bfb32a6e8adcfeaa5686bc9e5f1680
Author: Johann150 <johann@qwertqwefsday.eu>
Date:   Mon, 15 Mar 2021 18:57:41 +0100

handle empty repo correctly

Diffstat:
Msrc/main.rs | 31+++++++++++++++++++++++++++++++
Atemplates/repo-empty.html | 6++++++
2 files changed, 37 insertions(+), 0 deletions(-)

diff --git a/src/main.rs b/src/main.rs @@ -83,6 +83,12 @@ struct RepoHomeTemplate { readme_text: String, } +#[derive(Template)] +#[template(path = "repo-empty.html")] +struct RepoEmptyTemplate { + repo: Repository, +} + fn repo_from_request(repo_name: &str) -> Result<Repository> { let repo_name = percent_encoding::percent_decode_str(repo_name) .decode_utf8_lossy() @@ -103,6 +109,10 @@ async fn repo_home(req: Request<()>) -> tide::Result { } let repo = repo_from_request(&req.param("repo_name")?)?; + if repo.is_empty().unwrap() { + // we would not be able to find HEAD + return Ok(RepoEmptyTemplate { repo }.into()) + } let readme_text = { let mut format = ReadmeFormat::Plaintext; @@ -155,6 +165,13 @@ struct RepoLogTemplate<'a> { async fn repo_log(req: Request<()>) -> tide::Result { let repo = repo_from_request(&req.param("repo_name")?)?; + if repo.is_empty().unwrap() { + // redirect to start page of repo + let mut url = req.url().clone(); + url.path_segments_mut().unwrap().pop(); + return Ok(tide::Redirect::temporary(url.to_string()).into()); + } + let mut revwalk = repo.revwalk()?; match req.param("ref") { Ok(r) => revwalk.push_ref(&format!("refs/heads/{}", r))?, @@ -180,6 +197,13 @@ struct RepoRefTemplate<'a> { } async fn repo_refs(req: Request<()>) -> tide::Result { let repo = repo_from_request(&req.param("repo_name")?)?; + if repo.is_empty().unwrap() { + // redirect to start page of repo + let mut url = req.url().clone(); + url.path_segments_mut().unwrap().pop(); + return Ok(tide::Redirect::temporary(url.to_string()).into()); + } + let branches = repo .references()? .filter_map(|x| x.ok()) @@ -207,6 +231,13 @@ struct RepoTreeTemplate<'a> { async fn repo_tree(req: Request<()>) -> tide::Result { // TODO handle subtrees let repo = repo_from_request(&req.param("repo_name")?)?; + if repo.is_empty().unwrap() { + // redirect to start page of repo + let mut url = req.url().clone(); + url.path_segments_mut().unwrap().pop(); + return Ok(tide::Redirect::temporary(url.to_string()).into()); + } + // TODO accept reference or commit id let spec = req.param("ref").unwrap_or("HEAD"); let commit = repo.revparse_single(spec)?.peel_to_commit()?; diff --git a/templates/repo-empty.html b/templates/repo-empty.html @@ -0,0 +1,6 @@ +{% extends "base.html" %} + +{% block content %} + {% include "repo-navbar.html" %} + <em>(This repository is empty.)</em> +{% endblock %}