🏡 index : ~doyle/rgit.git

author Jordan Doyle <jordan@doyle.la> 2023-09-01 2:50:53.0 +01:00:00
committer Jordan Doyle <jordan@doyle.la> 2023-09-01 2:50:53.0 +01:00:00
commit
de44b40fa4fa7b3408f93c8e564ee36da8da158f [patch]
tree
7fdd39e300cb0079bc1ede3d5cea1b6b9ca2f7a3
parent
89ff4697c9d00d88cf16199959694e3591da4820
download
de44b40fa4fa7b3408f93c8e564ee36da8da158f.tar.gz

Allow ?raw=true to be passed to blob view

Fixes #25

Diff

 src/git.rs               |  7 +++++++
 src/methods/repo/tree.rs | 19 ++++++++++++++++++-
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/src/git.rs b/src/git.rs
index 97fe616..e9699d9 100644
--- a/src/git.rs
+++ a/src/git.rs
@@ -80,6 +80,7 @@
        path: Option<PathBuf>,
        tree_id: Option<&str>,
        branch: Option<String>,
        formatted: bool,
    ) -> Result<PathDestination> {
        let tree_id = tree_id
            .map(Oid::from_str)
@@ -119,7 +120,11 @@
                        .extension()
                        .or_else(|| path.file_name())
                        .map_or_else(|| Cow::Borrowed(""), OsStr::to_string_lossy);
                    let content = format_file(blob.content(), &extension, &self.git.syntax_set)?;
                    let content = if formatted {
                        format_file(blob.content(), &extension, &self.git.syntax_set)?
                    } else {
                        String::from_utf8_lossy(blob.content()).to_string()
                    };

                    return Ok(PathDestination::File(FileWithContent {
                        metadata: File {
diff --git a/src/methods/repo/tree.rs b/src/methods/repo/tree.rs
index 0101353..06d2e3c 100644
--- a/src/methods/repo/tree.rs
+++ a/src/methods/repo/tree.rs
@@ -1,10 +1,10 @@
use std::{
    fmt::{Display, Formatter},
    sync::Arc,
};

use askama::Template;
use axum::{extract::Query, response::Response, Extension};
use axum::{extract::Query, http, response::IntoResponse, response::Response, Extension};
use serde::Deserialize;

use crate::{
@@ -22,6 +22,8 @@
    id: Option<String>,
    #[serde(rename = "h")]
    branch: Option<String>,
    #[serde(default)]
    raw: bool,
}

impl Display for UriQuery {
@@ -68,10 +70,23 @@

    Ok(
        match open_repo
            .path(child_path, query.id.as_deref(), query.branch.clone())
            .path(
                child_path,
                query.id.as_deref(),
                query.branch.clone(),
                !query.raw,
            )
            .await?
        {
            PathDestination::Tree(items) => into_response(&TreeView { repo, items, query }),
            PathDestination::File(file) if query.raw => {
                let headers = [(
                    http::header::CONTENT_TYPE,
                    http::HeaderValue::from_static("text/plain"),
                )];

                (headers, file.content).into_response()
            }
            PathDestination::File(file) => into_response(&FileView { repo, file }),
        },
    )