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(-)
@@ -5,10 +5,11 @@ use serde::Deserialize;
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 @@ pub async fn handle(
) -> 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();
@@ -58,3 +58,29 @@ pub async fn handle(
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![])
}
@@ -41,6 +41,8 @@ use crate::{
layers::UnwrapInfallible,
};
pub const DEFAULT_BRANCHES: [&str; 2] = ["refs/heads/master", "refs/heads/main"];
#[allow(clippy::trait_duplication_in_bounds)] pub async fn service<ReqBody>(mut request: Request<ReqBody>) -> Response
@@ -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 @@ pub async fn handle(
.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 @@ pub async fn handle(
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))
@@ -6,10 +6,11 @@ use axum::{response::Response, Extension};
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 @@ pub async fn handle(
) -> 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();
@@ -53,3 +53,19 @@ pub async fn handle(
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![])
}