🏡 index : ~doyle/rgit.git

author Jordan Doyle <jordan@doyle.la> 2022-07-06 12:04:55.0 +01:00:00
committer Jordan Doyle <jordan@doyle.la> 2022-07-06 12:04:55.0 +01:00:00
commit
53166e7a3ed41e5608adc35b8bc7cdd12eb8b67c [patch]
tree
0ab5863f03a73be4c9d80c40cc7bb858e88896ca
parent
204eb55d23d186c138fb7cab18e6f6555352bda8
download
53166e7a3ed41e5608adc35b8bc7cdd12eb8b67c.tar.gz

Allow branch selection on log view



Diff

 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<Refs> {
    pub async fn get_refs(&self, repo: PathBuf) -> Arc<Refs> {
        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<RepositoryMetadataList> {
        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<Commit>, Option<usize>) {
    pub async fn get_commits(
        &self,
        repo: PathBuf,
        branch: Option<&str>,
        offset: usize,
    ) -> (Vec<Commit>, Option<usize>) {
        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<Commit> = 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<usize>,
    #[serde(rename = "h")]
    branch: Option<String>,
}

#[allow(clippy::unused_async)]
@@ -108,10 +110,15 @@
        repo: Repository,
        commits: Vec<Commit>,
        next_offset: Option<usize>,
        branch: Option<String>,
    }

    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 %}
<div class="mt-2 text-center">
    <a href="?ofs={{ next_offset }}">[next]</a>
    {% if let Some(branch) = branch %}
        <a href="?h={{ branch }}&ofs={{ next_offset }}">[next]</a>
    {% else %}
        <a href="?ofs={{ next_offset }}">[next]</a>
    {% endif %}
</div>
{% endif %}
{% endblock %}