index : gitrs
A lightweight web interface for browsing Git repositories, implemented in Rust
about summary refs log tree commit diff
gitrs / src / routes / repository / tree.rs
-rw-r--r-- blob de9992fa558414544c30b3a87be5fdfbdc6c5953 1858 bytes (plain)
1
use crate::{
2
    git::tree::{self, Breadcrumb, PathView},
3
    prelude::*,
4
    routes::PageQuery,
5
};
6
use gix::objs::tree::EntryKind;
7
8
#[derive(Template)]
9
#[template(path = "repository/tree.html")]
10
struct TreePage {
11
    repo: Repository,
12
    branch_names: Vec<String>,
13
    current_ref: Ref,
14
    filepath: String,
15
    breadcrumbs: Vec<Breadcrumb>,
16
    parent_path: Option<String>,
17
    kind: PathView,
18
    query: web::Query<PageQuery>,
19
}
20
21
#[get("/{repo}/tree/{filepath:.*}")]
22
pub async fn page(
23
    path: web::Path<(String, String)>,
24
    query: web::Query<PageQuery>,
25
    data: web::Data<App>,
26
) -> Result<HttpResponse, Error> {
27
    let (name, filepath) = path.into_inner();
28
    let filepath = filepath.trim_matches('/').to_string();
29
30
    let repo = data.get_repo(&name)?;
31
    let current_ref = Ref::resolve(&repo, &query)?;
32
    let branch_names = repo.branch_names()?;
33
    let kind = repo.resolve_path(&current_ref, query.id.as_deref(), &filepath)?;
34
35
    render_html(&TreePage {
36
        repo,
37
        branch_names,
38
        current_ref,
39
        breadcrumbs: tree::build_breadcrumbs(&filepath),
40
        parent_path: tree::parent_path(&filepath),
41
        filepath,
42
        kind,
43
        query,
44
    })
45
}
46
47
#[get("/{repo}/raw/{path:.*}")]
48
pub async fn raw(
49
    info: web::Path<(String, String)>,
50
    query: web::Query<PageQuery>,
51
    data: web::Data<App>,
52
) -> Result<HttpResponse, Error> {
53
    let (name, path) = info.into_inner();
54
    let repo = data.get_repo(&name)?;
55
    let current_ref = Ref::resolve(&repo, &query)?;
56
57
    match repo.read_raw_blob(&current_ref, query.id.as_deref(), &path)? {
58
        Some(bytes) => {
59
            let mime = mime_guess::from_path(&path).first_or_text_plain();
60
            Ok(HttpResponse::Ok().content_type(mime.as_ref()).body(bytes))
61
        }
62
        None => Ok(HttpResponse::BadRequest().body("path is a directory")),
63
    }
64
}