index : gitrs
A lightweight web interface for browsing Git repositories, implemented in Rust
about summary refs log tree commit diff
gitrs / src / main.rs
-rw-r--r-- blob 775f8cc679c43ad25c52539f02bb52fda0babe7f 5412 bytes (plain)
1
use crate::prelude::*;
2
use actix_web::{App as ActixApp, HttpServer, web};
3
use clap::Parser;
4
use std::io::Error;
5
6
use crate::cli::Args;
7
8
pub mod app;
9
pub mod cli;
10
pub mod error;
11
pub mod git;
12
pub mod markdown;
13
pub mod prelude;
14
pub mod routes;
15
pub mod syntax_highlight;
16
pub mod time;
17
18
#[actix_web::main]
19
async fn main() -> Result<(), Error> {
20
    let args = Args::parse();
21
22
    if args.install_service {
23
        return install_systemd_service(&args);
24
    }
25
26
    let app = web::Data::new(App::new(args.repos_dir.to_string()).await);
27
28
    HttpServer::new(move || {
29
        ActixApp::new()
30
            .app_data(app.clone())
31
            .service(routes::r#static::files)
32
            .service(routes::git::git_info_refs)
33
            .service(routes::git::git_upload_pack)
34
            .service(routes::repository::diff::patch)
35
            .service(routes::repository::snapshot::download)
36
            .service(routes::page)
37
            .service(routes::about::page)
38
            .service(routes::repository::about::page)
39
            .service(routes::repository::summary::page)
40
            .service(routes::repository::refs::page)
41
            .service(routes::repository::tag::page)
42
            .service(routes::repository::log::page)
43
            .service(routes::repository::tree::page)
44
            .service(routes::repository::tree::raw)
45
            .service(routes::repository::commit::page)
46
            .service(routes::repository::diff::page)
47
    })
48
    .bind((args.url, args.port))?
49
    .run()
50
    .await
51
}
52
53
fn install_systemd_service(args: &Args) -> Result<(), Error> {
54
    let exe = std::env::current_exe()?;
55
    let service = format!(
56
        "[Unit]\n\
57
         Description=gitrs — Lightweight Git web frontend\n\
58
         After=network-online.target\n\
59
         Wants=network-online.target\n\
60
         \n\
61
         [Service]\n\
62
         Type=simple\n\
63
         User=root\n\
64
         WorkingDirectory={repos_dir}\n\
65
         ExecStart={exe} --url {url} --port {port} --repos-dir {repos_dir}\n\
66
         Restart=on-failure\n\
67
         RestartSec=5\n\
68
         AmbientCapabilities=CAP_NET_BIND_SERVICE\n\
69
         \n\
70
         [Install]\n\
71
         WantedBy=multi-user.target\n",
72
        exe = exe.display(),
73
        url = args.url,
74
        port = args.port,
75
        repos_dir = args.repos_dir,
76
    );
77
78
    std::fs::write("/etc/systemd/system/gitrs.service", service)?;
79
    println!("Installed /etc/systemd/system/gitrs.service");
80
    Ok(())
81
}
82
83
#[cfg(test)]
84
mod tests {
85
    use super::*;
86
    use std::path::Path;
87
    use std::time::Instant;
88
89
    #[test]
90
    fn commit_list_populates_head_and_branch_tips_without_n_plus_one() {
91
        let repo = Repository::open(Path::new(env!("CARGO_MANIFEST_DIR"))).unwrap();
92
        let current_ref = Ref {
93
            name: repo.head_branch_name().unwrap(),
94
            is_head: true,
95
        };
96
        let gix_repo = repo.gix();
97
98
        let start = Instant::now();
99
        let (commits, _) = repo.commits(&gix_repo, &current_ref, 0, 50, None).unwrap();
100
        assert!(!commits.is_empty());
101
        assert!(commits[0].is_head(&repo).unwrap());
102
        assert!(!commits[0].branch_tips(&repo).unwrap().is_empty());
103
        assert!(start.elapsed().as_millis() < 100);
104
    }
105
106
    #[test]
107
    fn resolve_path_supports_explicit_commit_id() {
108
        let repo = Repository::open(Path::new(env!("CARGO_MANIFEST_DIR"))).unwrap();
109
        let current_ref = Ref {
110
            name: repo.head_branch_name().unwrap(),
111
            is_head: true,
112
        };
113
        let gix_repo = repo.gix();
114
        let (commits, _) = repo.commits(&gix_repo, &current_ref, 0, 2, None).unwrap();
115
        let oldest = commits.last().unwrap();
116
117
        let view = repo
118
            .resolve_path(&current_ref, Some(&oldest.id), "Cargo.toml")
119
            .unwrap();
120
        assert!(matches!(view, crate::git::tree::PathView::Blob(_)));
121
    }
122
123
    #[test]
124
    fn markdown_escapes_attribute_injections_in_media() {
125
        let md = "![demo & test](https://github.com/user-attachments/assets/1234?a=1&b=2)";
126
        let html = markdown::render(md, "gitrs", "main");
127
        assert!(html.contains(r#"alt="demo &amp; test""#));
128
        assert!(
129
            html.contains(r#"src="https://github.com/user-attachments/assets/1234?a=1&amp;b=2""#)
130
        );
131
    }
132
133
    #[test]
134
    fn shell_highlight_preserves_urls_and_highlights_commands_and_flags() {
135
        let block = syntax_highlight::highlight_block(
136
            "git clone https://github.com/y2w8/gitrs.git\nsudo $(which gitrs) --port 80\nNMRS_LOG=trace nmrs-tui\nexport PATH=\"$HOME/.cargo/bin:$PATH\"\n",
137
            "sh",
138
        );
139
        assert!(block.contains(r#"<span class="hl-function">git</span>"#));
140
        assert!(!block.contains("hl-comment"));
141
        assert!(
142
            block.contains(&v_htmlescape::escape("https://github.com/y2w8/gitrs.git").to_string())
143
        );
144
        assert!(block.contains(r#"<span class="hl-function-builtin">sudo</span>"#));
145
        assert!(block.contains(r#"<span class="hl-function-builtin">which</span>"#));
146
        assert!(block.contains(r#"<span class="hl-property">--port</span>"#));
147
        assert!(block.contains(r#"<span class="hl-number">80</span>"#));
148
        assert!(block.contains(r#"<span class="hl-constant">NMRS_LOG</span>"#));
149
        assert!(block.contains(r#"<span class="hl-function">nmrs-tui</span>"#));
150
        assert!(block.contains(r#"<span class="hl-variable">$HOME</span>"#));
151
    }
152
}