🏡 index : ~doyle/rgit.git

author Jordan Doyle <jordan@doyle.la> 2022-07-09 1:56:04.0 +01:00:00
committer Jordan Doyle <jordan@doyle.la> 2022-07-09 1:56:04.0 +01:00:00
commit
cfc3ca8ac96d7d9b982c54432f7cb2da70463f89 [patch]
tree
b26efb8430d0d7daf91c3d7f5637f264010f7dc6
parent
52b7379a20fb2ca60152046dbfe678adf03e8e81
download
cfc3ca8ac96d7d9b982c54432f7cb2da70463f89.tar.gz

Retain tree id whilst traversing tree



Diff

 src/git.rs               |  6 ++++--
 src/methods/repo.rs      | 16 +++++++++++++++-
 templates/repo/tree.html |  4 ++--
 3 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/src/git.rs b/src/git.rs
index 597174f..318cfbd 100644
--- a/src/git.rs
+++ a/src/git.rs
@@ -97,12 +97,14 @@
}

impl OpenRepository {
    pub async fn path(self: Arc<Self>, path: Option<PathBuf>, tree_id: Option<String>) -> PathDestination {
    pub async fn path(self: Arc<Self>, path: Option<PathBuf>, tree_id: Option<&str>) -> PathDestination {
        let tree_id = tree_id.map(Oid::from_str).transpose().unwrap();

        tokio::task::spawn_blocking(move || {
            let repo = self.repo.lock();

            let mut tree = if let Some(tree_id) = tree_id {
                repo.find_tree(Oid::from_str(&tree_id).unwrap()).unwrap()
                repo.find_tree(tree_id).unwrap()
            } else {
                let head = repo.head().unwrap();
                head.peel_to_tree().unwrap()
diff --git a/src/methods/repo.rs b/src/methods/repo.rs
index ef3bd08..9f2e05a 100644
--- a/src/methods/repo.rs
+++ a/src/methods/repo.rs
@@ -1,8 +1,9 @@
use std::{
    ops::Deref,
    path::{Path, PathBuf},
    sync::Arc,
};
use std::fmt::{Display, Formatter};

use askama::Template;
use axum::{
@@ -250,6 +251,16 @@
    id: Option<String>,
}

impl Display for TreeQuery {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if let Some(id) = self.id.as_deref() {
            write!(f, "?id={}", id)?;
        }

        Ok(())
    }
}

pub async fn handle_tree(
    Extension(repo): Extension<Repository>,
    Extension(RepositoryPath(repository_path)): Extension<RepositoryPath>,
@@ -262,6 +273,7 @@
    pub struct TreeView {
        pub repo: Repository,
        pub items: Vec<TreeItem>,
        pub query: TreeQuery,
    }

    #[derive(Template)]
@@ -273,8 +285,8 @@

    let open_repo = git.repo(repository_path).await;

    match open_repo.path(child_path, query.id).await {
        PathDestination::Tree(items) => into_response(&TreeView { repo, items }),
    match open_repo.path(child_path, query.id.as_deref()).await {
        PathDestination::Tree(items) => into_response(&TreeView { repo, items, query }),
        PathDestination::File(file) => into_response(&FileView { repo, file }),
    }
}
diff --git a/templates/repo/tree.html b/templates/repo/tree.html
index cb27281..3c8ff7a 100644
--- a/templates/repo/tree.html
+++ a/templates/repo/tree.html
@@ -18,12 +18,12 @@
        {% match item %}
            {% when crate::git::TreeItem::Tree with (tree) %}
                <td><pre>{{ tree.mode|file_perms }}</pre></td>
                <td><pre><a class="nested-tree" href="/{{ repo.display() }}/tree/{{ tree.path.display() }}">{{ tree.name }}</a></pre></td>
                <td><pre><a class="nested-tree" href="/{{ repo.display() }}/tree/{{ tree.path.display() }}{{ query }}">{{ tree.name }}</a></pre></td>
                <td></td>
                <td></td>
            {% when crate::git::TreeItem::File with (file) %}
                <td><pre>{{ file.mode|file_perms }}</pre></td>
                <td><pre><a href="/{{ repo.display() }}/tree/{{ file.path.display() }}">{{ file.name }}</a></pre></td>
                <td><pre><a href="/{{ repo.display() }}/tree/{{ file.path.display() }}{{ query }}">{{ file.name }}</a></pre></td>
                <td><pre>{{ file.size }}</pre></td>
                <td></td>
        {% endmatch %}