From 53166e7a3ed41e5608adc35b8bc7cdd12eb8b67c Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Wed, 06 Jul 2022 12:04:55 +0100 Subject: [PATCH] Allow branch selection on log view --- src/git.rs | 28 +++++++++++++++++++++++----- src/methods/repo.rs | 10 ++++++++++ templates/repo/log.html | 6 +++++- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/src/git.rs b/src/git.rs index 0f5ef48..8c38314 100644 --- a/src/git.rs +++ a/src/git.rs @@ -7,7 +7,7 @@ }; use arc_swap::ArcSwapOption; -use git2::{ObjectType, Oid, Repository, Signature}; +use git2::{Oid, Repository, Signature}; use moka::future::Cache; use time::OffsetDateTime; @@ -59,16 +59,16 @@ .await } - pub async fn get_refs<'a>(&'a self, repo: PathBuf) -> Arc { + pub async fn get_refs(&self, repo: PathBuf) -> Arc { self.refs .get_with(repo.clone(), async { tokio::task::spawn_blocking(move || { let repo = git2::Repository::open_bare(repo).unwrap(); - let refs = repo.references().unwrap(); + let ref_iter = repo.references().unwrap(); let mut built_refs = Refs::default(); - for ref_ in refs { + for ref_ in ref_iter { let ref_ = ref_.unwrap(); if ref_.is_branch() { @@ -120,7 +120,7 @@ .await } - pub async fn get_latest_commit<'a>(&'a self, repo: PathBuf) -> Commit { + pub async fn get_latest_commit(&self, repo: PathBuf) -> Commit { tokio::task::spawn_blocking(move || { let repo = Repository::open_bare(repo).unwrap(); let head = repo.head().unwrap(); @@ -134,7 +134,7 @@ pub async fn fetch_repository_metadata(&self) -> Arc { if let Some(metadata) = self.repository_metadata.load().as_ref() { - return Arc::clone(&metadata); + return Arc::clone(metadata); } let start = Path::new("../test-git").canonicalize().unwrap(); @@ -153,13 +153,25 @@ repos } - pub async fn get_commits(&self, repo: PathBuf, offset: usize) -> (Vec, Option) { + pub async fn get_commits( + &self, + repo: PathBuf, + branch: Option<&str>, + offset: usize, + ) -> (Vec, Option) { const AMOUNT: usize = 200; + let ref_name = branch.map(|branch| format!("refs/heads/{}", branch)); + tokio::task::spawn_blocking(move || { let repo = Repository::open_bare(repo).unwrap(); let mut revs = repo.revwalk().unwrap(); - revs.push_head().unwrap(); + + if let Some(ref_name) = ref_name.as_deref() { + revs.push_ref(ref_name).unwrap(); + } else { + revs.push_head().unwrap(); + } let mut commits: Vec = revs .skip(offset) diff --git a/src/methods/repo.rs b/src/methods/repo.rs index 1de00d8..cd5b8fc 100644 --- a/src/methods/repo.rs +++ a/src/methods/repo.rs @@ -93,6 +93,8 @@ pub struct LogQuery { #[serde(rename = "ofs")] offset: Option, + #[serde(rename = "h")] + branch: Option, } #[allow(clippy::unused_async)] @@ -108,10 +110,15 @@ repo: Repository, commits: Vec, next_offset: Option, + branch: Option, } let (commits, next_offset) = git - .get_commits(repository_path, query.offset.unwrap_or(0)) + .get_commits( + repository_path, + query.branch.as_deref(), + query.offset.unwrap_or(0), + ) .await; Html( @@ -119,6 +126,7 @@ repo, commits, next_offset, + branch: query.branch, } .render() .unwrap(), diff --git a/templates/repo/log.html b/templates/repo/log.html index ce9dd7a..1f06e7d 100644 --- a/templates/repo/log.html +++ a/templates/repo/log.html @@ -28,7 +28,11 @@ {% if let Some(next_offset) = next_offset %}
- [next] + {% if let Some(branch) = branch %} + [next] + {% else %} + [next] + {% endif %}
{% endif %} {% endblock %}-- rgit 0.1.3