JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Utilize for GitBackend and trim end slashes in folder

Emilia - ⁨1⁩ year ago

parent: tbd commit: ⁨4b440ef

Showing ⁨⁨6⁩ changed files⁩ with ⁨⁨69⁩ insertions⁩ and ⁨⁨54⁩ deletions⁩

giterated-daemon/src/backend/git.rs

View file
@@ -6,14 +6,14 @@ use giterated_models::instance::{Instance, RepositoryCreateRequest};
6 6
7 7 use giterated_models::repository::{
8 8 AccessList, Commit, DefaultBranch, Description, IssueLabel, Repository, RepositoryBranch,
9 RepositoryBranchesRequest, RepositoryChunkLine, RepositoryCommitBeforeRequest,
10 RepositoryCommitFromIdRequest, RepositoryDiff, RepositoryDiffFile, RepositoryDiffFileChunk,
11 RepositoryDiffFileInfo, RepositoryDiffFileStatus, RepositoryDiffPatchRequest,
12 RepositoryDiffRequest, RepositoryFile, RepositoryFileFromIdRequest,
9 RepositoryBranchFilter, RepositoryBranchesRequest, RepositoryChunkLine,
10 RepositoryCommitBeforeRequest, RepositoryCommitFromIdRequest, RepositoryDiff,
11 RepositoryDiffFile, RepositoryDiffFileChunk, RepositoryDiffFileInfo, RepositoryDiffFileStatus,
12 RepositoryDiffPatchRequest, RepositoryDiffRequest, RepositoryFile, RepositoryFileFromIdRequest,
13 13 RepositoryFileFromPathRequest, RepositoryFileInspectRequest, RepositoryIssue,
14 14 RepositoryIssueLabelsRequest, RepositoryIssuesCountRequest, RepositoryIssuesRequest,
15 15 RepositoryLastCommitOfFileRequest, RepositoryObjectType, RepositoryStatistics,
16 RepositoryStatisticsRequest, RepositoryTreeEntry, RepositoryVisibility, Visibility, RepositoryBranchFilter,
16 RepositoryStatisticsRequest, RepositoryTreeEntry, RepositoryVisibility, Visibility,
17 17 };
18 18
19 19 use giterated_models::user::User;
@@ -150,10 +150,10 @@ pub enum GitBackendError {
150 150 }
151 151
152 152 pub struct GitBackend {
153 pub pg_pool: PgPool,
154 pub repository_folder: String,
155 pub instance: Instance,
156 pub stack: Arc<OnceCell<GiteratedStack>>,
153 pg_pool: PgPool,
154 repository_folder: String,
155 instance: Instance,
156 stack: Arc<OnceCell<GiteratedStack>>,
157 157 }
158 158
159 159 impl GitBackend {
@@ -167,7 +167,8 @@ impl GitBackend {
167 167
168 168 Self {
169 169 pg_pool: pg_pool.clone(),
170 repository_folder: repository_folder.to_string(),
170 // We make sure there's no end slash
171 repository_folder: repository_folder.trim_end_matches(&['/', '\\']).to_string(),
171 172 instance,
172 173 stack,
173 174 }
@@ -369,9 +370,7 @@ impl GitBackend {
369 370 } else if let Ok(object) = git.revparse_single(rev) {
370 371 Ok(object.id())
371 372 } else {
372 Err(
373 Box::new(GitBackendError::RefNotFound(rev.to_string())).into(),
374 )
373 Err(Box::new(GitBackendError::RefNotFound(rev.to_string())).into())
375 374 }
376 375 }
377 376
@@ -494,7 +493,8 @@ impl RepositoryBackend for GitBackend {
494 493 error!("Failed creating repository on disk {:?}", err);
495 494
496 495 // Delete repository from database
497 self.delete_from_database(&request.owner, request.name.as_str()).await?;
496 self.delete_from_database(&request.owner, request.name.as_str())
497 .await?;
498 498
499 499 // ???
500 500 Err(err)
@@ -781,33 +781,38 @@ impl RepositoryBackend for GitBackend {
781 781 .await?;
782 782
783 783 // Could be done better with the RepositoryBranchFilter::None check done beforehand.
784 let mut filtered_branches = git.branches(None)?.filter_map(|branch| {
785 let branch = branch.ok()?.0;
784 let mut filtered_branches = git
785 .branches(None)?
786 .filter_map(|branch| {
787 let branch = branch.ok()?.0;
786 788
787 let Some(name) = branch.name().ok().flatten() else {
788 return None;
789 };
789 let Some(name) = branch.name().ok().flatten() else {
790 return None;
791 };
790 792
791 // TODO: Non UTF-8?
792 let Some(commit) = GitBackend::get_last_commit_in_rev(&git, branch.get().name().unwrap()).ok() else {
793 return None;
794 };
793 // TODO: Non UTF-8?
794 let Some(commit) =
795 GitBackend::get_last_commit_in_rev(&git, branch.get().name().unwrap()).ok()
796 else {
797 return None;
798 };
795 799
796 // TODO: Implement stale with configurable age
797 let stale = false;
800 // TODO: Implement stale with configurable age
801 let stale = false;
798 802
799 // Filter based on if the branch is stale or not
800 if request.filter != RepositoryBranchFilter::None {
801 #[allow(clippy::if_same_then_else)]
802 if stale && request.filter == RepositoryBranchFilter::Active {
803 return None;
804 } else if !stale && request.filter == RepositoryBranchFilter::Stale {
805 return None;
803 // Filter based on if the branch is stale or not
804 if request.filter != RepositoryBranchFilter::None {
805 #[allow(clippy::if_same_then_else)]
806 if stale && request.filter == RepositoryBranchFilter::Active {
807 return None;
808 } else if !stale && request.filter == RepositoryBranchFilter::Stale {
809 return None;
810 }
806 811 }
807 }
808 812
809 Some((name.to_string(), branch, stale, commit))
810 }).collect::<Vec<_>>();
813 Some((name.to_string(), branch, stale, commit))
814 })
815 .collect::<Vec<_>>();
811 816
812 817 // Sort the branches by commit date
813 818 filtered_branches.sort_by(|(_, _, _, c1), (_, _, _, c2)| c2.time.cmp(&c1.time));
@@ -825,12 +830,18 @@ impl RepositoryBackend for GitBackend {
825 830
826 831 // Get how many commits are ahead of and behind of the head
827 832 let ahead_behind_head = if head.target().is_some() && branch.get().target().is_some() {
828 git.graph_ahead_behind(branch.get().target().unwrap(), head.target().unwrap()).ok()
833 git.graph_ahead_behind(branch.get().target().unwrap(), head.target().unwrap())
834 .ok()
829 835 } else {
830 836 None
831 837 };
832 838
833 branches.push(RepositoryBranch { name: name.to_string(), stale: *stale, last_commit: Some(commit.clone()), ahead_behind_head })
839 branches.push(RepositoryBranch {
840 name: name.to_string(),
841 stale: *stale,
842 last_commit: Some(commit.clone()),
843 ahead_behind_head,
844 })
834 845 }
835 846
836 847 Ok(branches)

giterated-daemon/src/database_backend/mod.rs

View file
@@ -7,7 +7,9 @@ use std::sync::Arc;
7 7 use anyhow::Context;
8 8
9 9 use giterated_models::instance::Instance;
10 use giterated_models::repository::{DefaultBranch, Description, Repository, Visibility, CommitBodyType};
10 use giterated_models::repository::{
11 CommitBodyType, DefaultBranch, Description, Repository, Visibility,
12 };
11 13 use giterated_models::user::{Bio, DisplayName, User};
12 14 use giterated_stack::provider::MetadataProvider;
13 15 use giterated_stack::{AnyObject, AnySetting, GiteratedStack, ObjectMeta, SubstackBuilder};

giterated-daemon/src/main.rs

View file
@@ -59,17 +59,16 @@ async fn main() -> Result<(), Error> {
59 59 }));
60 60
61 61 let repository_backend: Arc<Mutex<dyn RepositoryBackend + Send>> =
62 Arc::new(Mutex::new(GitBackend {
63 pg_pool: db_pool.clone(),
64 repository_folder: String::from(
62 Arc::new(Mutex::new(GitBackend::new(
63 &db_pool,
64 &String::from(
65 65 config["giterated"]["backend"]["git"]["root"]
66 66 .as_str()
67 67 .unwrap(),
68 68 ),
69 instance: Instance::from_str(config["giterated"]["instance"].as_str().unwrap())
70 .unwrap(),
71 stack: stack_cell.clone(),
72 }));
69 Instance::from_str(config["giterated"]["instance"].as_str().unwrap()).unwrap(),
70 stack_cell.clone(),
71 )));
73 72
74 73 let token_granter = Arc::new(Mutex::new(AuthenticationTokenGranter {
75 74 config: config.clone(),

giterated-models/src/repository/operations.rs

View file
@@ -8,8 +8,8 @@ use crate::{
8 8 };
9 9
10 10 use super::{
11 Commit, IssueLabel, Repository, RepositoryBranch, RepositoryDiff, RepositoryFile,
12 RepositoryIssue, RepositoryStatistics, RepositoryTreeEntry, RepositoryView, RepositoryBranchFilter,
11 Commit, IssueLabel, Repository, RepositoryBranch, RepositoryBranchFilter, RepositoryDiff,
12 RepositoryFile, RepositoryIssue, RepositoryStatistics, RepositoryTreeEntry, RepositoryView,
13 13 };
14 14
15 15 /// A request to get a repository's information.
@@ -400,11 +400,14 @@ impl<S: Clone + Send + Sync, B: ObjectBackend<S> + std::fmt::Debug> Object<'_, S
400 400 range_end: usize,
401 401 operation_state: &S,
402 402 ) -> Result<Vec<RepositoryBranch>, OperationError<RepositoryError>> {
403 self.request::<RepositoryBranchesRequest>(RepositoryBranchesRequest {
404 filter,
405 range: (range_start, range_end),
406 }, operation_state)
407 .await
403 self.request::<RepositoryBranchesRequest>(
404 RepositoryBranchesRequest {
405 filter,
406 range: (range_start, range_end),
407 },
408 operation_state,
409 )
410 .await
408 411 }
409 412
410 413 // pub async fn issues_count(&mut self) -> Result<u64, OperationError<RepositoryError>> {

giterated-models/src/repository/settings.rs

View file
@@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
2 2
3 3 use crate::{settings::Setting, user::User};
4 4
5 use super::{DefaultBranch, CommitBodyType};
5 use super::{CommitBodyType, DefaultBranch};
6 6
7 7 impl Setting for DefaultBranch {
8 8 fn name() -> &'static str {

giterated-models/src/repository/values.rs

View file
@@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
4 4
5 5 use crate::{settings::Setting, value::GiteratedObjectValue};
6 6
7 use super::{Commit, Repository, RepositoryVisibility, CommitBodyType};
7 use super::{Commit, CommitBodyType, Repository, RepositoryVisibility};
8 8
9 9 // pub struct RepositorySetting<V: GiteratedObjectValue>(pub V);
10 10