🏡 index : ~doyle/rgit.git

author Jordan Doyle <jordan@doyle.la> 2022-07-23 19:33:14.0 +01:00:00
committer Jordan Doyle <jordan@doyle.la> 2022-07-23 19:33:14.0 +01:00:00
commit
9a93c7442869f6813edfded6f66db72240ee3a0a [patch]
tree
3297d081f5641d4707cbd763b4c31e9c72ded3c8
parent
ddf51c31b66572aea9f06fc723e0437288f8139b
download
9a93c7442869f6813edfded6f66db72240ee3a0a.tar.gz

Allow 'main' to be used as a default branch



Diff

 src/methods/repo/log.rs       | 34 ++++++++++++++++++++++++++++++++--
 src/methods/repo/mod.rs       |  2 ++
 src/methods/repo/smart_git.rs |  8 ++++----
 src/methods/repo/summary.rs   | 22 +++++++++++++++++++++-
 4 files changed, 55 insertions(+), 11 deletions(-)

diff --git a/src/methods/repo/log.rs b/src/methods/repo/log.rs
index 096d6de..c0ed726 100644
--- a/src/methods/repo/log.rs
+++ a/src/methods/repo/log.rs
@@ -5,10 +5,11 @@
use yoke::Yoke;

use crate::{
    database::schema::{commit::YokedCommit, repository::YokedRepository},
    into_response,
    methods::{
        filters,
        repo::{Repository, Result},
        repo::{Repository, Result, DEFAULT_BRANCHES},
    },
};

@@ -36,11 +37,10 @@
) -> Result<Response> {
    let offset = query.offset.unwrap_or(0);

    let reference = format!("refs/heads/{}", query.branch.as_deref().unwrap_or("master"));
    let repository = crate::database::schema::repository::Repository::open(&db, &*repo)?
        .context("Repository does not exist")?;
    let commit_tree = repository.get().commit_tree(&db, &reference)?;
    let mut commits = commit_tree.fetch_latest(101, offset).await;
    let mut commits =
        get_branch_commits(&repository, &db, query.branch.as_deref(), 101, offset).await?;

    let next_offset = if commits.len() == 101 {
        commits.pop();
@@ -57,4 +57,30 @@
        next_offset,
        branch: query.branch,
    }))
}

pub async fn get_branch_commits(
    repository: &YokedRepository,
    database: &sled::Db,
    branch: Option<&str>,
    amount: usize,
    offset: usize,
) -> Result<Vec<YokedCommit>> {
    let reference = branch.map(|branch| format!("refs/heads/{branch}"));

    if let Some(reference) = reference {
        let commit_tree = repository.get().commit_tree(database, &reference)?;
        return Ok(commit_tree.fetch_latest(amount, offset).await);
    }

    for branch in DEFAULT_BRANCHES {
        let commit_tree = repository.get().commit_tree(database, branch)?;
        let commits = commit_tree.fetch_latest(amount, 0).await;

        if !commits.is_empty() {
            return Ok(commits);
        }
    }

    Ok(vec![])
}
diff --git a/src/methods/repo/mod.rs b/src/methods/repo/mod.rs
index e8ea4b3..c6bbfbe 100644
--- a/src/methods/repo/mod.rs
+++ a/src/methods/repo/mod.rs
@@ -41,6 +41,8 @@
    layers::UnwrapInfallible,
};

pub const DEFAULT_BRANCHES: [&str; 2] = ["refs/heads/master", "refs/heads/main"];

// this is some wicked, wicked abuse of axum right here...
#[allow(clippy::trait_duplication_in_bounds)] // clippy seems a bit.. lost
pub async fn service<ReqBody>(mut request: Request<ReqBody>) -> Response
diff --git a/src/methods/repo/smart_git.rs b/src/methods/repo/smart_git.rs
index 1e077dc..04adc0b 100644
--- a/src/methods/repo/smart_git.rs
+++ a/src/methods/repo/smart_git.rs
@@ -1,4 +1,4 @@
use std::{io::ErrorKind, path::PathBuf, process::Stdio, str::FromStr};
use std::{io::ErrorKind, path::Path, process::Stdio, str::FromStr};

use anyhow::{bail, Context};
use axum::{
@@ -62,7 +62,7 @@
        .context("Failed to read git http-backend response")?;
    let resp = cgi_to_response(&out.stdout)?;

    if out.stderr.len() > 0 {
    if !out.stderr.is_empty() {
        warn!(
            "Git returned an error: `{}`",
            String::from_utf8_lossy(&out.stderr)
@@ -72,9 +72,9 @@
    Ok(resp)
}

fn extract_path<'a>(uri: &'a Uri, repository: &PathBuf) -> Result<&'a str> {
fn extract_path<'a>(uri: &'a Uri, repository: &Path) -> Result<&'a str> {
    let path = uri.path();
    let path = path.strip_prefix("/").unwrap_or(path);
    let path = path.strip_prefix('/').unwrap_or(path);

    if let Some(prefix) = repository.as_os_str().to_str() {
        Ok(path.strip_prefix(prefix).unwrap_or(path))
diff --git a/src/methods/repo/summary.rs b/src/methods/repo/summary.rs
index 1fa9e9b..af88bce 100644
--- a/src/methods/repo/summary.rs
+++ a/src/methods/repo/summary.rs
@@ -6,10 +6,11 @@
use yoke::Yoke;

use crate::{
    database::schema::{commit::YokedCommit, repository::YokedRepository},
    into_response,
    methods::{
        filters,
        repo::{Refs, Repository, Result},
        repo::{Refs, Repository, Result, DEFAULT_BRANCHES},
    },
};

@@ -27,8 +28,7 @@
) -> Result<Response> {
    let repository = crate::database::schema::repository::Repository::open(&db, &*repo)?
        .context("Repository does not exist")?;
    let commit_tree = repository.get().commit_tree(&db, "refs/heads/master")?;
    let commits = commit_tree.fetch_latest(11, 0).await;
    let commits = get_default_branch_commits(&repository, &db).await?;
    let commit_list = commits.iter().map(Yoke::get).collect();

    let mut heads = BTreeMap::new();
@@ -52,4 +52,20 @@
        refs: Refs { heads, tags },
        commit_list,
    }))
}

pub async fn get_default_branch_commits(
    repository: &YokedRepository,
    database: &sled::Db,
) -> Result<Vec<YokedCommit>> {
    for branch in DEFAULT_BRANCHES {
        let commit_tree = repository.get().commit_tree(database, branch)?;
        let commits = commit_tree.fetch_latest(11, 0).await;

        if !commits.is_empty() {
            return Ok(commits);
        }
    }

    Ok(vec![])
}