~aw/patches

mygit: simplify code v1 APPLIED

Johann Galle
Johann Galle: 2
 simplify code
 handle repos without readme

 3 files changed, 47 insertions(+), 40 deletions(-)
Applied, thank you! We're making some good progress -- I might be able
to dedicate a full day this weekend to getting an Alpha working

On Wed Mar 17, 2021 at 2:44 PM PDT, Johann Galle wrote:
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~aw/patches/patches/21261/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH mygit 1/2] simplify code Export this patch

Johann Galle
From: Johann150 <johann@qwertqwefsday.eu>

unnecessary to specify the name twice if it is identical to the field name
---
 src/main.rs | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 5f2c741..754a0c6 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -225,7 +225,7 @@ async fn repo_log(req: Request<()>) -> tide::Result {
    let tmpl = RepoLogTemplate {
        repo: &repo,
        commits,
        branch: branch,
        branch,
    };
    Ok(tmpl.into())
}
@@ -289,7 +289,7 @@ async fn repo_tree(req: Request<()>) -> tide::Result {
    let tmpl = RepoTreeTemplate {
        repo: &repo,
        tree,
        spec: spec,
        spec,
    };
    Ok(tmpl.into())
}
-- 
2.20.1

[PATCH mygit 2/2] handle repos without readme Export this patch

Johann Galle
From: Johann150 <johann@qwertqwefsday.eu>

---
 src/main.rs         | 77 +++++++++++++++++++++++----------------------
 templates/repo.html |  6 +++-
 2 files changed, 45 insertions(+), 38 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 754a0c6..f11d684 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -143,44 +143,47 @@ async fn repo_home(req: Request<()>) -> tide::Result {
        return Ok(RepoEmptyTemplate { repo }.into());
    }

    let readme_text = {
        let mut format = ReadmeFormat::Plaintext;
        let readme = repo
            .revparse_single("HEAD:README")
            .or_else(|_| repo.revparse_single("HEAD:README.txt"))
            .or_else(|_| {
                format = ReadmeFormat::Markdown;
                repo.revparse_single("HEAD:README.md")
            })
            .or_else(|_| repo.revparse_single("HEAD:README.mdown"))
            .or_else(|_| repo.revparse_single("HEAD:README.markdown"))
            .or_else(|_| {
                format = ReadmeFormat::Html;
                repo.revparse_single("HEAD:README.html")
            })
            .or_else(|_| repo.revparse_single("HEAD:README.htm"))?;
        let readme_text = str::from_utf8(readme.as_blob().unwrap().content())?;

        // render the file contents to HTML
        match format {
            // render plaintext as preformatted text
            ReadmeFormat::Plaintext => {
                let mut output = "<pre>".to_string();
                escape_html(&mut output, readme_text)?;
                output.push_str("</pre>");
                output
            }
            // already is HTML
            ReadmeFormat::Html => readme_text.to_string(),
            // render Markdown to HTML
            ReadmeFormat::Markdown => {
                let mut output = String::new();
                let parser = Parser::new_ext(readme_text, Options::empty());
                push_html(&mut output, parser);
                output
    let mut format = ReadmeFormat::Plaintext;
    let readme_text = repo
        .revparse_single("HEAD:README")
        .or_else(|_| repo.revparse_single("HEAD:README.txt"))
        .or_else(|_| {
            format = ReadmeFormat::Markdown;
            repo.revparse_single("HEAD:README.md")
        })
        .or_else(|_| repo.revparse_single("HEAD:README.mdown"))
        .or_else(|_| repo.revparse_single("HEAD:README.markdown"))
        .or_else(|_| {
            format = ReadmeFormat::Html;
            repo.revparse_single("HEAD:README.html")
        })
        .or_else(|_| repo.revparse_single("HEAD:README.htm"))
        .ok()
        .and_then(|readme| readme.into_blob().ok())
        .map(|blob| {
            let text = str::from_utf8(blob.content()).unwrap_or_default();

            // render the file contents to HTML
            match format {
                // render plaintext as preformatted text
                ReadmeFormat::Plaintext => {
                    let mut output = "<pre>".to_string();
                    escape_html(&mut output, text).unwrap();
                    output.push_str("</pre>");
                    output
                }
                // already is HTML
                ReadmeFormat::Html => text.to_string(),
                // render Markdown to HTML
                ReadmeFormat::Markdown => {
                    let mut output = String::new();
                    let parser = Parser::new_ext(text, Options::empty());
                    push_html(&mut output, parser);
                    output
                }
            }
        }
    };
        })
        .unwrap_or_default();

    Ok(RepoHomeTemplate { repo, readme_text }.into())
}
diff --git a/templates/repo.html b/templates/repo.html
index e2fcd0e..5598325 100644
--- a/templates/repo.html
+++ b/templates/repo.html
@@ -2,5 +2,9 @@

{% block content %}
  {% include "repo-navbar.html" %}
  {{ readme_text|safe }}
  {% if !readme_text.is_empty() -%}
    {{ readme_text|safe }}
  {% else -%}
    <em>(no README)</em>
  {%- endif %}
{% endblock %}
-- 
2.20.1
Applied, thank you! We're making some good progress -- I might be able
to dedicate a full day this weekend to getting an Alpha working

On Wed Mar 17, 2021 at 2:44 PM PDT, Johann Galle wrote: