diff --git a/giterated-daemon/src/backend/git.rs b/giterated-daemon/src/backend/git.rs index 75ad90d..20d24bd 100644 --- a/giterated-daemon/src/backend/git.rs +++ b/giterated-daemon/src/backend/git.rs @@ -6,14 +6,14 @@ use giterated_models::instance::{Instance, RepositoryCreateRequest}; use giterated_models::repository::{ AccessList, Commit, DefaultBranch, Description, IssueLabel, Repository, RepositoryBranch, - RepositoryBranchesRequest, RepositoryChunkLine, RepositoryCommitBeforeRequest, - RepositoryCommitFromIdRequest, RepositoryDiff, RepositoryDiffFile, RepositoryDiffFileChunk, - RepositoryDiffFileInfo, RepositoryDiffFileStatus, RepositoryDiffPatchRequest, - RepositoryDiffRequest, RepositoryFile, RepositoryFileFromIdRequest, + RepositoryBranchFilter, RepositoryBranchesRequest, RepositoryChunkLine, + RepositoryCommitBeforeRequest, RepositoryCommitFromIdRequest, RepositoryDiff, + RepositoryDiffFile, RepositoryDiffFileChunk, RepositoryDiffFileInfo, RepositoryDiffFileStatus, + RepositoryDiffPatchRequest, RepositoryDiffRequest, RepositoryFile, RepositoryFileFromIdRequest, RepositoryFileFromPathRequest, RepositoryFileInspectRequest, RepositoryIssue, RepositoryIssueLabelsRequest, RepositoryIssuesCountRequest, RepositoryIssuesRequest, RepositoryLastCommitOfFileRequest, RepositoryObjectType, RepositoryStatistics, - RepositoryStatisticsRequest, RepositoryTreeEntry, RepositoryVisibility, Visibility, RepositoryBranchFilter, + RepositoryStatisticsRequest, RepositoryTreeEntry, RepositoryVisibility, Visibility, }; use giterated_models::user::User; @@ -150,10 +150,10 @@ pub enum GitBackendError { } pub struct GitBackend { - pub pg_pool: PgPool, - pub repository_folder: String, - pub instance: Instance, - pub stack: Arc>, + pg_pool: PgPool, + repository_folder: String, + instance: Instance, + stack: Arc>, } impl GitBackend { @@ -167,7 +167,8 @@ impl GitBackend { Self { pg_pool: pg_pool.clone(), - repository_folder: repository_folder.to_string(), + // We make sure there's no end slash + repository_folder: repository_folder.trim_end_matches(&['/', '\\']).to_string(), instance, stack, } @@ -369,9 +370,7 @@ impl GitBackend { } else if let Ok(object) = git.revparse_single(rev) { Ok(object.id()) } else { - Err( - Box::new(GitBackendError::RefNotFound(rev.to_string())).into(), - ) + Err(Box::new(GitBackendError::RefNotFound(rev.to_string())).into()) } } @@ -494,7 +493,8 @@ impl RepositoryBackend for GitBackend { error!("Failed creating repository on disk {:?}", err); // Delete repository from database - self.delete_from_database(&request.owner, request.name.as_str()).await?; + self.delete_from_database(&request.owner, request.name.as_str()) + .await?; // ??? Err(err) @@ -781,33 +781,38 @@ impl RepositoryBackend for GitBackend { .await?; // Could be done better with the RepositoryBranchFilter::None check done beforehand. - let mut filtered_branches = git.branches(None)?.filter_map(|branch| { - let branch = branch.ok()?.0; + let mut filtered_branches = git + .branches(None)? + .filter_map(|branch| { + let branch = branch.ok()?.0; - let Some(name) = branch.name().ok().flatten() else { - return None; - }; + let Some(name) = branch.name().ok().flatten() else { + return None; + }; - // TODO: Non UTF-8? - let Some(commit) = GitBackend::get_last_commit_in_rev(&git, branch.get().name().unwrap()).ok() else { - return None; - }; + // TODO: Non UTF-8? + let Some(commit) = + GitBackend::get_last_commit_in_rev(&git, branch.get().name().unwrap()).ok() + else { + return None; + }; - // TODO: Implement stale with configurable age - let stale = false; + // TODO: Implement stale with configurable age + let stale = false; - // Filter based on if the branch is stale or not - if request.filter != RepositoryBranchFilter::None { - #[allow(clippy::if_same_then_else)] - if stale && request.filter == RepositoryBranchFilter::Active { - return None; - } else if !stale && request.filter == RepositoryBranchFilter::Stale { - return None; + // Filter based on if the branch is stale or not + if request.filter != RepositoryBranchFilter::None { + #[allow(clippy::if_same_then_else)] + if stale && request.filter == RepositoryBranchFilter::Active { + return None; + } else if !stale && request.filter == RepositoryBranchFilter::Stale { + return None; + } } - } - Some((name.to_string(), branch, stale, commit)) - }).collect::>(); + Some((name.to_string(), branch, stale, commit)) + }) + .collect::>(); // Sort the branches by commit date filtered_branches.sort_by(|(_, _, _, c1), (_, _, _, c2)| c2.time.cmp(&c1.time)); @@ -825,12 +830,18 @@ impl RepositoryBackend for GitBackend { // Get how many commits are ahead of and behind of the head let ahead_behind_head = if head.target().is_some() && branch.get().target().is_some() { - git.graph_ahead_behind(branch.get().target().unwrap(), head.target().unwrap()).ok() + git.graph_ahead_behind(branch.get().target().unwrap(), head.target().unwrap()) + .ok() } else { None }; - branches.push(RepositoryBranch { name: name.to_string(), stale: *stale, last_commit: Some(commit.clone()), ahead_behind_head }) + branches.push(RepositoryBranch { + name: name.to_string(), + stale: *stale, + last_commit: Some(commit.clone()), + ahead_behind_head, + }) } Ok(branches) diff --git a/giterated-daemon/src/database_backend/mod.rs b/giterated-daemon/src/database_backend/mod.rs index 7c55f7a..26e2ee5 100644 --- a/giterated-daemon/src/database_backend/mod.rs +++ b/giterated-daemon/src/database_backend/mod.rs @@ -7,7 +7,9 @@ use std::sync::Arc; use anyhow::Context; use giterated_models::instance::Instance; -use giterated_models::repository::{DefaultBranch, Description, Repository, Visibility, CommitBodyType}; +use giterated_models::repository::{ + CommitBodyType, DefaultBranch, Description, Repository, Visibility, +}; use giterated_models::user::{Bio, DisplayName, User}; use giterated_stack::provider::MetadataProvider; use giterated_stack::{AnyObject, AnySetting, GiteratedStack, ObjectMeta, SubstackBuilder}; diff --git a/giterated-daemon/src/main.rs b/giterated-daemon/src/main.rs index 602261c..e20c9c8 100644 --- a/giterated-daemon/src/main.rs +++ b/giterated-daemon/src/main.rs @@ -59,17 +59,16 @@ async fn main() -> Result<(), Error> { })); let repository_backend: Arc> = - Arc::new(Mutex::new(GitBackend { - pg_pool: db_pool.clone(), - repository_folder: String::from( + Arc::new(Mutex::new(GitBackend::new( + &db_pool, + &String::from( config["giterated"]["backend"]["git"]["root"] .as_str() .unwrap(), ), - instance: Instance::from_str(config["giterated"]["instance"].as_str().unwrap()) - .unwrap(), - stack: stack_cell.clone(), - })); + Instance::from_str(config["giterated"]["instance"].as_str().unwrap()).unwrap(), + stack_cell.clone(), + ))); let token_granter = Arc::new(Mutex::new(AuthenticationTokenGranter { config: config.clone(), diff --git a/giterated-models/src/repository/operations.rs b/giterated-models/src/repository/operations.rs index 15c4631..cea036d 100644 --- a/giterated-models/src/repository/operations.rs +++ b/giterated-models/src/repository/operations.rs @@ -8,8 +8,8 @@ use crate::{ }; use super::{ - Commit, IssueLabel, Repository, RepositoryBranch, RepositoryDiff, RepositoryFile, - RepositoryIssue, RepositoryStatistics, RepositoryTreeEntry, RepositoryView, RepositoryBranchFilter, + Commit, IssueLabel, Repository, RepositoryBranch, RepositoryBranchFilter, RepositoryDiff, + RepositoryFile, RepositoryIssue, RepositoryStatistics, RepositoryTreeEntry, RepositoryView, }; /// A request to get a repository's information. @@ -400,11 +400,14 @@ impl + std::fmt::Debug> Object<'_, S range_end: usize, operation_state: &S, ) -> Result, OperationError> { - self.request::(RepositoryBranchesRequest { - filter, - range: (range_start, range_end), - }, operation_state) - .await + self.request::( + RepositoryBranchesRequest { + filter, + range: (range_start, range_end), + }, + operation_state, + ) + .await } // pub async fn issues_count(&mut self) -> Result> { diff --git a/giterated-models/src/repository/settings.rs b/giterated-models/src/repository/settings.rs index 04a97ae..b827579 100644 --- a/giterated-models/src/repository/settings.rs +++ b/giterated-models/src/repository/settings.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; use crate::{settings::Setting, user::User}; -use super::{DefaultBranch, CommitBodyType}; +use super::{CommitBodyType, DefaultBranch}; impl Setting for DefaultBranch { fn name() -> &'static str { diff --git a/giterated-models/src/repository/values.rs b/giterated-models/src/repository/values.rs index a8d73e3..9b10fd0 100644 --- a/giterated-models/src/repository/values.rs +++ b/giterated-models/src/repository/values.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use crate::{settings::Setting, value::GiteratedObjectValue}; -use super::{Commit, Repository, RepositoryVisibility, CommitBodyType}; +use super::{Commit, CommitBodyType, Repository, RepositoryVisibility}; // pub struct RepositorySetting(pub V);