index : gitrs
A lightweight web interface for browsing Git repositories, implemented in Rust
about summary refs log tree commit diff
gitrs / src / routes / repository / refs.rs
-rw-r--r-- blob 403181ef3045b1e21dae5374635c4ae5f4439bd6 1843 bytes (plain)
1
use crate::{
2
    git::tag::Tag,
3
    prelude::*,
4
    routes::{PAGE_SIZE, PageQuery},
5
};
6
7
#[derive(Template)]
8
#[template(path = "repository/refs.html")]
9
struct RefsPage<'repo> {
10
    repo: &'repo Repository,
11
    current_ref: Ref,
12
    offset: usize,
13
    mode: String,
14
    branch_names: Vec<String>,
15
    branches: Vec<Branch<'repo>>,
16
    tags: Vec<Tag>,
17
    has_prev: bool,
18
    branches_has_more: bool,
19
    tags_has_more: bool,
20
    query: web::Query<PageQuery>,
21
}
22
23
#[get("/{repo}/refs/{mode:.*}")]
24
pub async fn page(
25
    path: web::Path<(String, String)>,
26
    query: web::Query<PageQuery>,
27
    data: web::Data<App>,
28
) -> Result<HttpResponse, Error> {
29
    let (name, mode) = path.into_inner();
30
    let repo = data.get_repo(&name)?;
31
    let gix_repo = repo.gix();
32
33
    let current_ref = Ref::resolve(&repo, &query)?;
34
    let branch_names = repo.branch_names()?;
35
    let offset = query.offset.unwrap_or(0);
36
37
    let (branches, branches_has_more, tags, tags_has_more, has_prev) = match mode.as_str() {
38
        "heads" => {
39
            let (branches, has_more) = repo.branches(&gix_repo, offset, PAGE_SIZE)?;
40
            (branches, has_more, Vec::new(), false, offset >= 1)
41
        }
42
        "tags" => {
43
            let (tags, has_more) = repo.tags(offset, PAGE_SIZE)?;
44
            (Vec::new(), false, tags, has_more, offset >= 1)
45
        }
46
        _ => {
47
            let (branches, branches_has_more) = repo.branches(&gix_repo, 0, PAGE_SIZE / 2)?;
48
            let (tags, tags_has_more) = repo.tags(0, PAGE_SIZE / 2)?;
49
            (branches, branches_has_more, tags, tags_has_more, false)
50
        }
51
    };
52
53
    render_html(&RefsPage {
54
        repo: &repo,
55
        current_ref,
56
        offset,
57
        mode,
58
        branch_names,
59
        branches,
60
        tags,
61
        has_prev,
62
        branches_has_more,
63
        tags_has_more,
64
        query,
65
    })
66
}