From 2cbafe165bc89b914b80bea1dd81fbb2ac49e290 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sat, 09 Jul 2022 02:08:19 +0100 Subject: [PATCH] Allow looking up a tree by branch --- src/git.rs | 9 +++++++-- src/methods/repo.rs | 13 ++++++++++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/git.rs b/src/git.rs index 318cfbd..65d5fc2 100644 --- a/src/git.rs +++ a/src/git.rs @@ -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, path: Option, tree_id: Option<&str>) -> PathDestination { + pub async fn path(self: Arc, path: Option, tree_id: Option<&str>, branch: Option) -> 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() diff --git a/src/methods/repo.rs b/src/methods/repo.rs index 9f2e05a..afe96cb 100644 --- a/src/methods/repo.rs +++ a/src/methods/repo.rs @@ -249,12 +249,21 @@ #[derive(Deserialize)] pub struct TreeQuery { id: Option, + #[serde(rename = "h")] + branch: Option, } 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 }), } -- rgit 0.1.3