index : gitrs
A lightweight web interface for browsing Git repositories, implemented in Rust
about summary refs log tree commit diff
gitrs / src / routes / git.rs
-rw-r--r-- blob d94b7e41433d3d62aab881a5fb2b063e6f7a1fe2 3106 bytes (plain)
1
use std::{
2
    collections::HashMap,
3
    io::Write,
4
    process::{Command, Stdio},
5
};
6
7
use actix_web::web::Bytes;
8
9
use crate::prelude::*;
10
11
#[get("/{repo}/info/refs")]
12
pub async fn git_info_refs(
13
    path: web::Path<String>,
14
    req_query: web::Query<HashMap<String, String>>,
15
    data: web::Data<App>,
16
) -> ActixResult<HttpResponse> {
17
    let service = match req_query.get("service") {
18
        Some(s) => s,
19
        None => return Ok(HttpResponse::BadRequest().body("Missing service parameter")),
20
    };
21
22
    if service != "git-upload-pack" && service != "git-receive-pack" {
23
        return Ok(HttpResponse::BadRequest().body("Unsupported service"));
24
    }
25
26
    let name = path.into_inner();
27
    let repo = match data.find_repo(&name) {
28
        Some(r) => r,
29
        None => return Ok(HttpResponse::NotFound().body("Repo not found")),
30
    };
31
32
    let first_line = format!("# service={}\n", service);
33
    let pkt_line = format!("{:04x}{}", first_line.len() + 4, first_line);
34
    let zero_line = "0000";
35
36
    let output = Command::new("git")
37
        .arg(service.strip_prefix("git-").unwrap())
38
        .arg("--stateless-rpc")
39
        .arg("--advertise-refs")
40
        .arg(&repo.path)
41
        .output();
42
43
    match output {
44
        Ok(out) => {
45
            let mut body = Vec::new();
46
            body.extend_from_slice(pkt_line.as_bytes());
47
            body.extend_from_slice(zero_line.as_bytes());
48
            body.extend_from_slice(&out.stdout);
49
50
            Ok(HttpResponse::Ok()
51
                .content_type(format!("application/x-{}-advertisement", service))
52
                .body(body))
53
        }
54
        Err(_) => Ok(HttpResponse::InternalServerError().body("Git command failed")),
55
    }
56
}
57
58
#[post("/{repo}/git-upload-pack")]
59
pub async fn git_upload_pack(
60
    path: web::Path<String>,
61
    data: web::Data<App>,
62
    body: Bytes,
63
) -> ActixResult<HttpResponse> {
64
    let name = path.into_inner();
65
    let repo = match data.find_repo(&name) {
66
        Some(r) => r,
67
        None => return Ok(HttpResponse::NotFound().body("Repo not found")),
68
    };
69
70
    Ok(handle_git_rpc("upload-pack", repo, body).await)
71
}
72
73
#[post("/{repo}/git-receive-pack")]
74
pub async fn git_receive_pack(
75
    path: web::Path<String>,
76
    data: web::Data<App>,
77
    body: Bytes,
78
) -> ActixResult<HttpResponse> {
79
    let name = path.into_inner();
80
    let repo = match data.find_repo(&name) {
81
        Some(r) => r,
82
        None => return Ok(HttpResponse::NotFound().body("Repo not found")),
83
    };
84
85
    Ok(handle_git_rpc("receive-pack", repo, body).await)
86
}
87
88
async fn handle_git_rpc(service: &str, repo: Repository, body: Bytes) -> HttpResponse {
89
    let mut child = Command::new("git")
90
        .arg(service)
91
        .arg("--stateless-rpc")
92
        .arg(&repo.path)
93
        .stdin(Stdio::piped())
94
        .stdout(Stdio::piped())
95
        .spawn()
96
        .unwrap();
97
98
    if let Some(mut stdin) = child.stdin.take() {
99
        stdin.write_all(&body).unwrap();
100
    }
101
102
    let output = child.wait_with_output().unwrap();
103
104
    HttpResponse::Ok()
105
        .content_type(format!("application/x-{}-result", service))
106
        .body(output.stdout)
107
}