index : gitrs
A lightweight web interface for browsing Git repositories, implemented in Rust
about summary refs log tree commit diff

Diffstat (limited to 'src/routes/repository/refs.rs')

-rw-r--r-- src/routes/repository/refs.rs 159
 1 file changed, 27 insertions(+), 132 deletions(-)

diff --git a/src/routes/repository/refs.rs b/src/routes/repository/refs.rs
index 4d34eb1..403181e 100644
--- a/src/routes/repository/refs.rs
+++ b/src/routes/repository/refs.rs
@@ -25,147 +25,42 @@
    path: web::Path<(String, String)>,
    query: web::Query<PageQuery>,
    data: web::Data<App>,
) -> ActixResult<HttpResponse> {
) -> Result<HttpResponse, Error> {
    let (name, mode) = path.into_inner();
    let repo = match data.find_repo(name) {
        Some(r) => r,
        None => return Ok(HttpResponse::NotFound().body("Repo not found")),
    };
    let repo = data.get_repo(&name)?;
    let gix_repo = repo.gix();

    let current_ref = match Ref::resolve(&repo, &query) {
        Ok(r) => r,
        Err(e) => return Ok(HttpResponse::InternalServerError().body(e.to_string())),
    };
    let branch_names = match repo.branch_names() {
        Ok(b) => b,
        Err(e) => return Ok(HttpResponse::InternalServerError().body(e.to_string())),
    };

    let current_ref = Ref::resolve(&repo, &query)?;
    let branch_names = repo.branch_names()?;
    let offset = query.offset.unwrap_or(0);
    let has_prev = offset >= 1;

    let rendered = match mode.as_ref() {
    let (branches, branches_has_more, tags, tags_has_more, has_prev) = match mode.as_str() {
        "heads" => {
            let (branches, branches_has_more) = match repo.branches(&gix_repo, offset, PAGE_SIZE) {
                Ok(b) => b,
                Err(e) => return Ok(HttpResponse::InternalServerError().body(e.to_string())),
            };

            RefsPage {
                repo: &repo,
                current_ref,
                offset,
                mode,
                branch_names,
                branches,
                tags: Vec::new(),
                has_prev,
                branches_has_more,
                tags_has_more: false,
                query,
            }
            .render()
            let (branches, has_more) = repo.branches(&gix_repo, offset, PAGE_SIZE)?;
            (branches, has_more, Vec::new(), false, offset >= 1)
        }

        "tags" => {
            let (tags, tags_has_more) = match repo.tags(offset, PAGE_SIZE) {
                Ok(t) => t,
                Err(e) => return Ok(HttpResponse::InternalServerError().body(e.to_string())),
            };

            RefsPage {
                repo: &repo,
                current_ref,
                offset,
                mode,
                branch_names,
                branches: Vec::new(),
                tags,
                has_prev,
                branches_has_more: false,
                tags_has_more,
                query,
            }
            .render()
            let (tags, has_more) = repo.tags(offset, PAGE_SIZE)?;
            (Vec::new(), false, tags, has_more, offset >= 1)
        }
        &_ => {
            let (branches, branches_has_more) = match repo.branches(&gix_repo, 0, PAGE_SIZE / 2) {
                Ok(b) => b,
                Err(e) => return Ok(HttpResponse::InternalServerError().body(e.to_string())),
            };

            let (tags, tags_has_more) = match repo.tags(0, PAGE_SIZE / 2) {
                Ok(t) => t,
                Err(e) => return Ok(HttpResponse::InternalServerError().body(e.to_string())),
            };

            RefsPage {
                repo: &repo,
                current_ref,
                offset,
                mode,
                branch_names,
                branches,
                tags,
                has_prev: false,
                branches_has_more,
                tags_has_more,
                query,
            }
            .render()
        _ => {
            let (branches, branches_has_more) = repo.branches(&gix_repo, 0, PAGE_SIZE / 2)?;
            let (tags, tags_has_more) = repo.tags(0, PAGE_SIZE / 2)?;
            (branches, branches_has_more, tags, tags_has_more, false)
        }
    };
    match rendered {
        Ok(html) => Ok(HttpResponse::Ok().content_type("text/html").body(html)),
        Err(e) => Ok(HttpResponse::InternalServerError().body(e.to_string())),
    }
}

// #[get("/{repo}/archive/{tag}.tar.gz")]
// pub async fn archive_tar_gz(
//     path: web::Path<(String, String)>,
//     data: web::Data<App>,
// ) -> ActixResult<HttpResponse> {
//     let (name, tag) = path.into_inner();
//     let repo = match data.find_repo(name) {
//         Some(r) => r,
//         None => return Ok(HttpResponse::NotFound().body("Repo not found")),
//     };
//
//     match repo.archive_tar_gz(&tag) {
//         Ok(bytes) => Ok(HttpResponse::Ok()
//             .content_type("application/gzip")
//             .append_header((
//                 "Content-Disposition",
//                 format!("attachment; filename=\"{}-{}.tar.gz\"", repo.name, tag),
//             ))
//             .body(bytes)),
//         // Most failures here are "no such tag" rather than a real server
//         // fault — 404 reads truer than 500 for a download endpoint.
//         Err(e) => Ok(HttpResponse::NotFound().body(e.to_string())),
//     }
// }
//
// #[get("/{repo}/archive/{tag}.zip")]
// pub async fn archive_zip(
//     path: web::Path<(String, String)>,
//     data: web::Data<App>,
// ) -> ActixResult<HttpResponse> {
//     let (name, tag) = path.into_inner();
//     let repo = match data.find_repo(&name) {
//         Some(r) => r,
//         None => return Ok(HttpResponse::NotFound().body("Repo not found")),
//     };
//
//     match repo.archive_zip(&tag) {
//         Ok(bytes) => Ok(HttpResponse::Ok()
//             .content_type("application/zip")
//             .append_header((
//                 "Content-Disposition",
//                 format!("attachment; filename=\"{}-{}.zip\"", repo.name, tag),
//             ))
//             .body(bytes)),
//         Err(e) => Ok(HttpResponse::NotFound().body(e.to_string())),
//     }
// }
    render_html(&RefsPage {
        repo: &repo,
        current_ref,
        offset,
        mode,
        branch_names,
        branches,
        tags,
        has_prev,
        branches_has_more,
        tags_has_more,
        query,
    })
}