~aw/patches

mygit: resolve some warnings v1 APPLIED

Johann Galle
Johann Galle: 4
 resolve some warnings
 improve command line arguments
 improve file output on page
 make homepage link absolute

 5 files changed, 42 insertions(+), 24 deletions(-)
Thank you!
On Thu Mar 18, 2021 at 3:13 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/21277/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH mygit 1/4] resolve some warnings Export this patch

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

also using cargo clippy
---
 src/main.rs | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 9c9e203..fa72790 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -5,13 +5,12 @@ use git2::{
};
use once_cell::sync::Lazy;
use pico_args;
use serde::{Deserialize, Serialize};
use serde::Deserialize;
use std::fs;
use std::path::Path;
use std::str;
use syntect::highlighting::{Color, ThemeSet};
use syntect::parsing::{SyntaxReference, SyntaxSet};
use tide::prelude::*;
use tide::Request;

#[derive(Deserialize, Debug)]
@@ -88,7 +87,7 @@ struct IndexTemplate {
    repos: Vec<Repository>,
}

async fn index(req: Request<()>) -> tide::Result {
async fn index(_req: Request<()>) -> tide::Result {
    let repos = fs::read_dir(&CONFIG.projectroot)
        .map(|entries| {
            entries
@@ -223,12 +222,16 @@ async fn repo_log(req: Request<()>) -> tide::Result {
        };
        revwalk.set_sorting(git2::Sort::TIME).unwrap();
        revwalk
            .filter_map(|oid| repo.find_commit(oid.unwrap()).ok().clone()) // TODO error handling
            .filter_map(|oid| repo.find_commit(oid.unwrap()).ok()) // TODO error handling
            .take(100)
            .collect()
    };
    let head_branch = repo.head()?;
    let branch = req.param("ref").unwrap_or(head_branch.shorthand().unwrap());
    let branch = req
        .param("ref")
        .ok()
        .or_else(|| head_branch.shorthand())
        .unwrap();
    let tmpl = RepoLogTemplate {
        repo: &repo,
        commits,
@@ -290,7 +293,7 @@ async fn repo_tree(req: Request<()>) -> tide::Result {

    // TODO accept reference or commit id
    let head = repo.head()?;
    let spec = req.param("ref").unwrap_or(head.shorthand().unwrap());
    let spec = req.param("ref").ok().or_else(|| head.shorthand()).unwrap();
    let commit = repo.revparse_single(spec)?.peel_to_commit()?;
    let tree = commit.tree()?;
    let tmpl = RepoTreeTemplate {
@@ -349,7 +352,7 @@ async fn repo_file(req: Request<()>) -> tide::Result {
    let repo = repo_from_request(req.param("repo_name")?)?;
    // If directory -- show tree TODO
    let head = repo.head()?;
    let spec = req.param("ref").unwrap_or(head.shorthand().unwrap());
    let spec = req.param("ref").ok().or_else(|| head.shorthand()).unwrap();
    let commit = repo.revparse_single(spec)?.peel_to_commit()?;
    let tree = commit.tree()?;
    let tree_entry = tree.get_name(req.param("object_name")?).unwrap();
@@ -363,7 +366,7 @@ async fn repo_file(req: Request<()>) -> tide::Result {
        .unwrap_or("");
    let syntax_reference = syntax_set
        .find_syntax_by_extension(extension)
        .unwrap_or(syntax_set.find_syntax_plain_text());
        .unwrap_or_else(|| syntax_set.find_syntax_plain_text());
    let ts = ThemeSet::load_defaults();
    let theme = &ts.themes["InspiredGitHub"]; // TODO make customizable
    let tree_obj = tree_entry.to_object(&repo)?;
-- 
2.20.1

[PATCH mygit 2/4] improve command line arguments Export this patch

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

---
 src/main.rs | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index fa72790..7d51a11 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -48,12 +48,18 @@ mod defaults {
}

const HELP: &str = "\
mygit
Usage: mygit

FLAGS:
  -h, --help            Prints help information
  -h, --help            Prints this help information and exits.
OPTIONS:
  -c                    Path to config file
  -c, --config <FILE>   Use a specific configuration file.
                        default is ./mygit.toml

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

Report bugs at https://todo.sr.ht/~aw/mygit
";

static CONFIG: Lazy<Config> = Lazy::new(args);
@@ -68,14 +74,22 @@ fn args() -> Config {
        std::process::exit(0);
    }

    let toml_text = fs::read_to_string("mygit.toml").unwrap_or_else(|_| {
        tide::log::warn!("mygit.toml configuration file not found, using defaults");
    let config_filename = pargs
        .opt_value_from_str(["-c", "--config"])
        .unwrap()
        .unwrap_or("mygit.toml".to_string());

    let toml_text = fs::read_to_string(&config_filename).unwrap_or_else(|_| {
        tide::log::warn!(
            "configuration file {:?} not found, using defaults",
            config_filename
        );
        String::new()
    });
    match toml::from_str(&toml_text) {
        Ok(config) => config,
        Err(e) => {
            eprintln!("could not read configuration file: {}", e);
            eprintln!("could not parse configuration file: {}", e);
            std::process::exit(1);
        }
    }
@@ -363,7 +377,7 @@ async fn repo_file(req: Request<()>) -> tide::Result {
    let extension = std::path::Path::new(tree_entry.name().unwrap())
        .extension()
        .and_then(std::ffi::OsStr::to_str)
        .unwrap_or("");
        .unwrap_or_default();
    let syntax_reference = syntax_set
        .find_syntax_by_extension(extension)
        .unwrap_or_else(|| syntax_set.find_syntax_plain_text());
-- 
2.20.1

[PATCH mygit 3/4] improve file output on page Export this patch

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

Do not render a space as that would be included when copying.
Use a CSS margin instead. Also replace the spaces in the line number by
styling the line number with CSS.
---
 src/main.rs                |  4 ++--
 templates/static/style.css | 13 +++++++------
 2 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 7d51a11..26ba66a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -393,8 +393,8 @@ async fn repo_file(req: Request<()>) -> tide::Result {
    for (n, line) in syntect::util::LinesWithEndings::from(file_string).enumerate() {
        let regions = highlighter.highlight(line, &syntax_set);
        output.push_str(&format!(
            "<a href='#L{0}' id='L{0}' class='line'>{0:>6}</a>&nbsp;",
            n
            "<a href='#L{0}' id='L{0}' class='line'>{0}</a>",
            n + 1
        ));
        syntect::html::append_highlighted_html_for_styled_line(
            &regions[..],
diff --git a/templates/static/style.css b/templates/static/style.css
index d860821..8e9f1be 100644
--- a/templates/static/style.css
+++ b/templates/static/style.css
@@ -27,13 +27,17 @@ body {
}

.line {
 display: inline-block;
 width: 6em;
 margin-right: .5em;
 text-align: right;
 text-decoration: none;
 -webkit-touch-callout: none; /* iOS Safari */
 -webkit-user-select: none; /* Safari */
 -khtml-user-select: none; /* Konqueror HTML */
 -moz-user-select: none; /* Old versions of Firefox */
 -ms-user-select: none; /* Internet Explorer/Edge */
 user-select: none; /* Non-prefixed version, currently
 user-select: none; /* Non-prefixed version, currently */
}

.clone-url {
@@ -49,10 +53,7 @@ hr.thin {
  border-bottom: 1px solid rgba(255, 255, 255, 0.3);
}

h1 {
   margin: 0px;
}
h2 {
h1, h2 {
   margin: 0px;
}

@@ -76,7 +77,7 @@ td {

table td {
    padding: 0 0.1em;
  }
}

table.core {
  width: 100%;
-- 
2.20.1

[PATCH mygit 4/4] make homepage link absolute Export this patch

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

---
 templates/base.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/templates/base.html b/templates/base.html
index 1141225..151515e 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -15,7 +15,7 @@
    </div>
  <hr class="thin">
  <div class="footer">
  Server running <a href="git.alexwennerberg.com/mygit">mygit</a>
  Server running <a href="https://git.alexwennerberg.com/mygit">mygit</a>
  </div>
  </body>
</html>
-- 
2.20.1
Thank you!
On Thu Mar 18, 2021 at 3:13 PM PDT, Johann Galle wrote: