diff --git a/giterated-daemon/src/backend/git.rs b/giterated-daemon/src/backend/git.rs index 54de66f..9620cf6 100644 --- a/giterated-daemon/src/backend/git.rs +++ b/giterated-daemon/src/backend/git.rs @@ -6,9 +6,10 @@ use giterated_models::instance::{Instance, RepositoryCreateRequest}; use giterated_models::repository::{ Commit, DefaultBranch, Description, IssueLabel, LatestCommit, Repository, - RepositoryFileInspectRequest, RepositoryIssue, RepositoryIssueLabelsRequest, - RepositoryIssuesCountRequest, RepositoryIssuesRequest, RepositoryObjectType, - RepositoryTreeEntry, RepositoryVisibility, Visibility, RepositoryFile, RepositoryFileFromIdRequest, RepositoryDiffRequest, RepositoryDiff, RepositoryCommitBeforeRequest, + RepositoryCommitBeforeRequest, RepositoryDiff, RepositoryDiffRequest, RepositoryFile, + RepositoryFileFromIdRequest, RepositoryFileInspectRequest, RepositoryIssue, + RepositoryIssueLabelsRequest, RepositoryIssuesCountRequest, RepositoryIssuesRequest, + RepositoryObjectType, RepositoryTreeEntry, RepositoryVisibility, Visibility, }; use giterated_models::settings::{AnySetting, Setting}; use giterated_models::user::{User, UserParseError}; @@ -178,12 +179,16 @@ impl GitBackend { } } - pub async fn open_repository_and_check_permissions(&self, owner: &User, name: &str, requester: Option<&User>) -> Result { + pub async fn open_repository_and_check_permissions( + &self, + owner: &User, + name: &str, + requester: Option<&User>, + ) -> Result { let repository = match self .find_by_owner_user_name( // &request.owner.instance.url, - owner, - name, + owner, name, ) .await { @@ -546,7 +551,9 @@ impl RepositoryBackend for GitBackend { repository: &Repository, request: &RepositoryFileInspectRequest, ) -> Result, Error> { - let git = self.open_repository_and_check_permissions(&repository.owner, &repository.name, requester).await?; + let git = self + .open_repository_and_check_permissions(&repository.owner, &repository.name, requester) + .await?; // Try and parse the input as a reference and get the object ID let mut tree_id = match &request.rev { @@ -569,7 +576,12 @@ impl RepositoryBackend for GitBackend { if tree_id.is_none() { match git2::Oid::from_str(request.rev.as_ref().unwrap()) { Ok(oid) => tree_id = Some(oid), - Err(_) => return Err(Box::new(GitBackendError::RefNotFound(request.rev.clone().unwrap())).into()), + Err(_) => { + return Err(Box::new(GitBackendError::RefNotFound( + request.rev.clone().unwrap(), + )) + .into()) + } } } @@ -578,7 +590,7 @@ impl RepositoryBackend for GitBackend { let commit = git.find_commit(tree_id.unwrap()).unwrap(); // this is stupid - let mut current_path = request.rev.clone().unwrap(); + let mut current_path = request.rev.clone().unwrap_or_else(|| "master".to_string()); // Get the commit tree let git_tree = if let Some(path) = &request.path { @@ -609,8 +621,12 @@ impl RepositoryBackend for GitBackend { git2::ObjectType::Blob => RepositoryObjectType::Blob, _ => unreachable!(), }; - let mut tree_entry = - RepositoryTreeEntry::new(entry.id().to_string().as_str(), entry.name().unwrap(), object_type, entry.filemode()); + let mut tree_entry = RepositoryTreeEntry::new( + entry.id().to_string().as_str(), + entry.name().unwrap(), + object_type, + entry.filemode(), + ); if request.extra_metadata { // Get the file size if It's a blob @@ -653,17 +669,25 @@ impl RepositoryBackend for GitBackend { repository: &Repository, request: &RepositoryFileFromIdRequest, ) -> Result { - let git = self.open_repository_and_check_permissions(&repository.owner, &repository.name, requester).await?; + let git = self + .open_repository_and_check_permissions(&repository.owner, &repository.name, requester) + .await?; // Parse the passed object id let oid = match git2::Oid::from_str(request.0.as_str()) { Ok(oid) => oid, - Err(_) => return Err(Box::new(GitBackendError::InvalidObjectId(request.0.clone())).into()), + Err(_) => { + return Err(Box::new(GitBackendError::InvalidObjectId(request.0.clone())).into()) + } }; // Find the file and turn it into our own struct let file = match git.find_blob(oid) { - Ok(blob) => RepositoryFile { content: blob.content().to_vec(), binary: blob.is_binary(), size: blob.size() }, + Ok(blob) => RepositoryFile { + content: blob.content().to_vec(), + binary: blob.is_binary(), + size: blob.size(), + }, Err(_) => return Err(Box::new(GitBackendError::BlobNotFound(oid.to_string())).into()), }; @@ -676,32 +700,52 @@ impl RepositoryBackend for GitBackend { repository: &Repository, request: &RepositoryDiffRequest, ) -> Result { - let git = self.open_repository_and_check_permissions(&repository.owner, &repository.name, requester).await?; + let git = self + .open_repository_and_check_permissions(&repository.owner, &repository.name, requester) + .await?; // Parse the passed object ids let oid_old = match git2::Oid::from_str(request.old_id.as_str()) { Ok(oid) => oid, - Err(_) => return Err(Box::new(GitBackendError::InvalidObjectId(request.old_id.clone())).into()), + Err(_) => { + return Err( + Box::new(GitBackendError::InvalidObjectId(request.old_id.clone())).into(), + ) + } }; let oid_new = match git2::Oid::from_str(request.new_id.as_str()) { Ok(oid) => oid, - Err(_) => return Err(Box::new(GitBackendError::InvalidObjectId(request.new_id.clone())).into()), + Err(_) => { + return Err( + Box::new(GitBackendError::InvalidObjectId(request.new_id.clone())).into(), + ) + } }; // Get the trees of those object ids let tree_old = match git.find_tree(oid_old) { Ok(tree) => tree, - Err(_) => return Err(Box::new(GitBackendError::TreeNotFound(oid_old.to_string())).into()), + Err(_) => { + return Err(Box::new(GitBackendError::TreeNotFound(oid_old.to_string())).into()) + } }; let tree_new = match git.find_tree(oid_new) { Ok(tree) => tree, - Err(_) => return Err(Box::new(GitBackendError::TreeNotFound(oid_new.to_string())).into()), + Err(_) => { + return Err(Box::new(GitBackendError::TreeNotFound(oid_new.to_string())).into()) + } }; // Diff the two trees against each other let diff = match git.diff_tree_to_tree(Some(&tree_old), Some(&tree_new), None) { Ok(diff) => diff, - Err(_) => return Err(Box::new(GitBackendError::FailedDiffing(oid_old.to_string(), oid_new.to_string())).into()), + Err(_) => { + return Err(Box::new(GitBackendError::FailedDiffing( + oid_old.to_string(), + oid_new.to_string(), + )) + .into()) + } }; // Should be safe to unwrap? @@ -709,13 +753,14 @@ impl RepositoryBackend for GitBackend { // Honestly not quite sure what is going on here, could not find documentation. // Print the entire patch - let mut patch = String::new(); + let mut patch = String::new(); diff.print(git2::DiffFormat::Patch, |_, _, line| { patch.push_str(std::str::from_utf8(line.content()).unwrap()); true - }).unwrap(); - + }) + .unwrap(); + Ok(RepositoryDiff { files_changed: stats.files_changed(), insertions: stats.insertions(), @@ -730,12 +775,16 @@ impl RepositoryBackend for GitBackend { repository: &Repository, request: &RepositoryCommitBeforeRequest, ) -> Result { - let git = self.open_repository_and_check_permissions(&repository.owner, &repository.name, requester).await?; + let git = self + .open_repository_and_check_permissions(&repository.owner, &repository.name, requester) + .await?; // Parse the passed object id let oid = match git2::Oid::from_str(request.0.as_str()) { Ok(oid) => oid, - Err(_) => return Err(Box::new(GitBackendError::InvalidObjectId(request.0.clone())).into()), + Err(_) => { + return Err(Box::new(GitBackendError::InvalidObjectId(request.0.clone())).into()) + } }; // Find the commit using the parsed oid @@ -747,7 +796,9 @@ impl RepositoryBackend for GitBackend { // Get the first parent it has let parent = match commit.parent(0) { Ok(parent) => Commit::from(parent), - Err(_) => return Err(Box::new(GitBackendError::CommitParentNotFound(oid.to_string())).into()), + Err(_) => { + return Err(Box::new(GitBackendError::CommitParentNotFound(oid.to_string())).into()) + } }; Ok(parent) diff --git a/giterated-daemon/src/backend/mod.rs b/giterated-daemon/src/backend/mod.rs index 4a98352..aeb956b 100644 --- a/giterated-daemon/src/backend/mod.rs +++ b/giterated-daemon/src/backend/mod.rs @@ -16,9 +16,10 @@ use giterated_models::instance::{ }; use giterated_models::repository::{ - IssueLabel, Repository, RepositoryFileInspectRequest, RepositoryIssue, - RepositoryIssueLabelsRequest, RepositoryIssuesCountRequest, RepositoryIssuesRequest, - RepositorySummary, RepositoryTreeEntry, RepositoryFileFromIdRequest, RepositoryFile, RepositoryDiffRequest, RepositoryDiff, Commit, RepositoryCommitBeforeRequest, + Commit, IssueLabel, Repository, RepositoryCommitBeforeRequest, RepositoryDiff, + RepositoryDiffRequest, RepositoryFile, RepositoryFileFromIdRequest, + RepositoryFileInspectRequest, RepositoryIssue, RepositoryIssueLabelsRequest, + RepositoryIssuesCountRequest, RepositoryIssuesRequest, RepositorySummary, RepositoryTreeEntry, }; use giterated_models::settings::AnySetting; use giterated_models::user::User; diff --git a/giterated-daemon/src/database_backend/handler.rs b/giterated-daemon/src/database_backend/handler.rs index e56e6df..ab9b6ca 100644 --- a/giterated-daemon/src/database_backend/handler.rs +++ b/giterated-daemon/src/database_backend/handler.rs @@ -7,8 +7,10 @@ use giterated_models::{ object_backend::ObjectBackend, operation::{AnyOperation, GiteratedOperation}, repository::{ - DefaultBranch, Description, LatestCommit, Repository, RepositoryFileInspectRequest, - RepositoryInfoRequest, RepositorySummary, RepositoryView, Visibility, RepositoryFile, RepositoryFileFromIdRequest, RepositoryDiff, RepositoryDiffRequest, RepositoryCommitBeforeRequest, Commit, + Commit, DefaultBranch, Description, LatestCommit, Repository, + RepositoryCommitBeforeRequest, RepositoryDiff, RepositoryDiffRequest, RepositoryFile, + RepositoryFileFromIdRequest, RepositoryFileInspectRequest, RepositoryInfoRequest, + RepositorySummary, RepositoryView, Visibility, }, settings::{AnySetting, GetSetting, GetSettingError, SetSetting, SetSettingError}, user::{User, UserRepositoriesRequest}, @@ -276,7 +278,8 @@ pub fn repository_file_from_id( drop(repository_backend); Ok(file) - }.boxed() + } + .boxed() } pub fn repository_diff( @@ -294,17 +297,14 @@ pub fn repository_diff( let mut repository_backend = state.repository_backend.lock().await; let file = repository_backend - .repository_diff( - None, - object.object(), - &operation, - ) + .repository_diff(None, object.object(), &operation) .await .map_err(|err| OperationError::Internal(format!("{:?}", err)))?; drop(repository_backend); Ok(file) - }.boxed() + } + .boxed() } pub fn repository_commit_before( @@ -322,17 +322,14 @@ pub fn repository_commit_before( let mut repository_backend = state.repository_backend.lock().await; let file = repository_backend - .repository_commit_before( - None, - object.object(), - &operation, - ) + .repository_commit_before(None, object.object(), &operation) .await .map_err(|err| OperationError::Internal(format!("{:?}", err)))?; drop(repository_backend); Ok(file) - }.boxed() + } + .boxed() } pub fn repository_get_value( diff --git a/giterated-daemon/src/database_backend/mod.rs b/giterated-daemon/src/database_backend/mod.rs index 1c2d29c..c6c52df 100644 --- a/giterated-daemon/src/database_backend/mod.rs +++ b/giterated-daemon/src/database_backend/mod.rs @@ -17,8 +17,9 @@ use tokio::sync::Mutex; use crate::backend::{RepositoryBackend, UserBackend}; use self::handler::{ - repository_get_setting, repository_get_value, repository_info, repository_set_setting, - user_get_repositories, user_get_setting, user_get_value, user_set_setting, OperationHandlers, repository_file_from_id, repository_diff, repository_commit_before, + repository_commit_before, repository_diff, repository_file_from_id, repository_get_setting, + repository_get_value, repository_info, repository_set_setting, user_get_repositories, + user_get_setting, user_get_value, user_set_setting, OperationHandlers, }; #[derive(Clone, Debug)] @@ -257,8 +258,9 @@ mod test { use giterated_models::object_backend::ObjectBackend; use giterated_models::repository::{ - Description, Repository, RepositoryFileInspectRequest, RepositorySummary, - RepositoryTreeEntry, RepositoryFileFromIdRequest, RepositoryFile, RepositoryDiff, RepositoryDiffRequest, RepositoryCommitBeforeRequest, Commit + Commit, Description, Repository, RepositoryCommitBeforeRequest, RepositoryDiff, + RepositoryDiffRequest, RepositoryFile, RepositoryFileFromIdRequest, + RepositoryFileInspectRequest, RepositorySummary, RepositoryTreeEntry, }; use giterated_models::settings::AnySetting; use giterated_models::user::{DisplayName, User};