🏡 index : ~doyle/rgit.git

author Jordan Doyle <jordan@doyle.la> 2022-07-09 2:08:19.0 +01:00:00
committer Jordan Doyle <jordan@doyle.la> 2022-07-09 2:08:19.0 +01:00:00
commit
2cbafe165bc89b914b80bea1dd81fbb2ac49e290 [patch]
tree
8de0f6ee9698a8eee4b0a6ae331ce4dd32d0d6ee
parent
cfc3ca8ac96d7d9b982c54432f7cb2da70463f89
download
2cbafe165bc89b914b80bea1dd81fbb2ac49e290.tar.gz

Allow looking up a tree by branch



Diff

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