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