Allow looking up a tree by branch
Diff
src/git.rs | 9 +++++++--
src/methods/repo.rs | 13 ++++++++++++-
2 files changed, 16 insertions(+), 6 deletions(-)
@@ -7,9 +7,7 @@
};
use arc_swap::ArcSwapOption;
use git2::{
DiffFormat, DiffLineType, DiffOptions, DiffStatsFormat, ObjectType, Oid, Repository, Signature,
};
use git2::{BranchType, DiffFormat, DiffLineType, DiffOptions, DiffStatsFormat, ObjectType, Oid, Repository, Signature};
use moka::future::Cache;
use parking_lot::Mutex;
use syntect::html::{ClassStyle, ClassedHTMLGenerator};
@@ -97,7 +95,7 @@
}
impl OpenRepository {
pub async fn path(self: Arc<Self>, path: Option<PathBuf>, tree_id: Option<&str>) -> PathDestination {
pub async fn path(self: Arc<Self>, path: Option<PathBuf>, tree_id: Option<&str>, branch: Option<String>) -> PathDestination {
let tree_id = tree_id.map(Oid::from_str).transpose().unwrap();
tokio::task::spawn_blocking(move || {
@@ -105,6 +103,9 @@
let mut tree = if let Some(tree_id) = tree_id {
repo.find_tree(tree_id).unwrap()
} else if let Some(branch) = branch {
let branch = repo.find_branch(&branch, BranchType::Local).unwrap();
branch.get().peel_to_tree().unwrap()
} else {
let head = repo.head().unwrap();
head.peel_to_tree().unwrap()
@@ -249,12 +249,21 @@
#[derive(Deserialize)]
pub struct TreeQuery {
id: Option<String>,
#[serde(rename = "h")]
branch: Option<String>,
}
impl Display for TreeQuery {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let mut prefix = "?";
if let Some(id) = self.id.as_deref() {
write!(f, "?id={}", id)?;
write!(f, "{}id={}", prefix, id)?;
prefix = "&";
}
if let Some(branch) = self.branch.as_deref() {
write!(f, "{}h={}", prefix, branch)?;
}
Ok(())
@@ -285,7 +294,7 @@
let open_repo = git.repo(repository_path).await;
match open_repo.path(child_path, query.id.as_deref()).await {
match open_repo.path(child_path, query.id.as_deref(), query.branch.clone()).await {
PathDestination::Tree(items) => into_response(&TreeView { repo, items, query }),
PathDestination::File(file) => into_response(&FileView { repo, file }),
}