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/tree.rs')

-rw-r--r-- src/routes/repository/tree.rs 90
 1 file changed, 17 insertions(+), 73 deletions(-)

diff --git a/src/routes/repository/tree.rs b/src/routes/repository/tree.rs
index 4677cdd..7949868 100644
--- a/src/routes/repository/tree.rs
+++ b/src/routes/repository/tree.rs
@@ -23,32 +23,16 @@
    path: web::Path<(String, String)>,
    query: web::Query<PageQuery>,
    data: web::Data<App>,
) -> ActixResult<HttpResponse> {
) -> Result<HttpResponse, Error> {
    let (name, filepath) = path.into_inner();
    let filepath = filepath.trim_matches('/').to_string(); // fixes the double-slash bug from before
    let filepath = filepath.trim_matches('/').to_string();

    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 current_ref = Ref::resolve(&repo, &query)?;
    let branch_names = repo.branch_names()?;
    let kind = repo.resolve_path(&current_ref, &filepath)?;

    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())),
    };

    // was `&ref_name` (undefined) — should reference the resolved ref
    let kind = match repo.resolve_path(&current_ref, &filepath) {
        Ok(k) => k,
        Err(e) => return Ok(HttpResponse::InternalServerError().body(e.to_string())),
    };

    let rendered = TreePage {
    render_html(&TreePage {
        repo,
        branch_names,
        current_ref,
@@ -57,13 +41,7 @@
        filepath,
        kind,
        query,
    }
    .render();

    match rendered {
        Ok(html) => Ok(HttpResponse::Ok().content_type("text/html").body(html)),
        Err(e) => Ok(HttpResponse::InternalServerError().body(e.to_string())),
    }
    })
}

#[get("/{repo}/raw/{path:.*}")]
@@ -71,49 +49,15 @@
    info: web::Path<(String, String)>,
    query: web::Query<PageQuery>,
    data: web::Data<App>,
) -> ActixResult<HttpResponse> {
) -> Result<HttpResponse, Error> {
    let (name, path) = info.into_inner();
    let path = path.trim_matches('/');

    let repo = match data.find_repo(name) {
        Some(r) => r,
        None => return Ok(HttpResponse::NotFound().body("Repo not found")),
    };
    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())),
    };

    match repo.resolve_path(&current_ref, path) {
        Ok(PathView::Blob(blob)) => {
            // Re-read content rather than store it on Blob — keeps Blob
            // light for the tree-view case, which only needs metadata + lines.
            let object_id = match gix::ObjectId::from_hex(blob.id.as_bytes()) {
                Ok(id) => id,
                Err(e) => {
                    return Ok(
                        HttpResponse::InternalServerError().body(format!("invalid object id: {e}"))
                    );
                }
            };

            let object = match gix_repo.find_object(object_id) {
                Ok(o) => o,
                Err(e) => return Ok(HttpResponse::InternalServerError().body(e.to_string())),
            };

            let blob_obj = match object.try_into_blob() {
                Ok(b) => b,
                Err(e) => return Ok(HttpResponse::InternalServerError().body(e.to_string())),
            };

            Ok(HttpResponse::Ok()
                .content_type("text/plain; charset=utf-8")
                .body(blob_obj.data.clone()))
        }
        Ok(PathView::Tree(_)) => Ok(HttpResponse::BadRequest().body("path is a directory")),
        Err(e) => Ok(HttpResponse::InternalServerError().body(e.to_string())),
    let repo = data.get_repo(&name)?;
    let current_ref = Ref::resolve(&repo, &query)?;

    match repo.read_raw_blob(&current_ref, &path)? {
        Some(bytes) => Ok(HttpResponse::Ok()
            .content_type("text/plain; charset=utf-8")
            .body(bytes)),
        None => Ok(HttpResponse::BadRequest().body("path is a directory")),
    }
}