From cfc3ca8ac96d7d9b982c54432f7cb2da70463f89 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sat, 09 Jul 2022 01:56:04 +0100 Subject: [PATCH] Retain tree id whilst traversing tree --- 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, path: Option, tree_id: Option) -> PathDestination { + pub async fn path(self: Arc, path: Option, 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, } +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, Extension(RepositoryPath(repository_path)): Extension, @@ -262,6 +273,7 @@ pub struct TreeView { pub repo: Repository, pub items: Vec, + 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) %}
{{ tree.mode|file_perms }}
-
{{ tree.name }}
+
{{ tree.name }}
{% when crate::git::TreeItem::File with (file) %}
{{ file.mode|file_perms }}
-
{{ file.name }}
+
{{ file.name }}
{{ file.size }}
{% endmatch %} -- rgit 0.1.3