From f9f8addb7276c2d7843b5f4ecf76cf9459aa2438 Mon Sep 17 00:00:00 2001 From: Kiril Videlov Date: Mon, 8 Jul 2024 10:47:37 +0200 Subject: [PATCH] move repo related commands out of the project controller --- .../gitbutler-core/src/projects/controller.rs | 31 +--------------- crates/gitbutler-repo/src/commands.rs | 36 +++++++++++++++++++ crates/gitbutler-repo/src/lib.rs | 3 ++ crates/gitbutler-tauri/src/repo.rs | 18 +++++----- 4 files changed, 48 insertions(+), 40 deletions(-) create mode 100644 crates/gitbutler-repo/src/commands.rs diff --git a/crates/gitbutler-core/src/projects/controller.rs b/crates/gitbutler-core/src/projects/controller.rs index e0f506382..022517cca 100644 --- a/crates/gitbutler-core/src/projects/controller.rs +++ b/crates/gitbutler-core/src/projects/controller.rs @@ -5,12 +5,10 @@ use std::{ use anyhow::{bail, Context, Result}; use async_trait::async_trait; -use bstr::BString; use super::{storage, storage::UpdateRequest, Project, ProjectId}; +use crate::error; use crate::projects::AuthKey; -use crate::{error, project_repository}; -use crate::{git::RepositoryExt, project_repository::Config}; #[async_trait] pub trait Watchers { @@ -213,31 +211,4 @@ impl Controller { Ok(()) } - - pub fn get_local_config(&self, id: ProjectId, key: &str) -> Result> { - let project = self.projects_storage.get(id)?; - - let project_repo = project_repository::ProjectRepo::open(&project)?; - let config: Config = project_repo.repo().into(); - config.get_local(key) - } - - pub fn set_local_config(&self, id: ProjectId, key: &str, value: &str) -> Result<()> { - let project = self.projects_storage.get(id)?; - - let project_repo = project_repository::ProjectRepo::open(&project)?; - let config: Config = project_repo.repo().into(); - config.set_local(key, value) - } - - pub fn check_signing_settings(&self, id: ProjectId) -> Result { - let project = self.projects_storage.get(id)?; - - let repo = project_repository::ProjectRepo::open(&project)?; - let signed = repo.repo().sign_buffer(&BString::new("test".into()).into()); - match signed { - Ok(_) => Ok(true), - Err(e) => Err(e), - } - } } diff --git a/crates/gitbutler-repo/src/commands.rs b/crates/gitbutler-repo/src/commands.rs new file mode 100644 index 000000000..267cb7792 --- /dev/null +++ b/crates/gitbutler-repo/src/commands.rs @@ -0,0 +1,36 @@ +use anyhow::Result; +use bstr::BString; +use gitbutler_core::{ + git::RepositoryExt, + project_repository::{self, Config}, + projects::Project, +}; + +pub trait RepoCommands { + fn get_local_config(&self, key: &str) -> Result>; + fn set_local_config(&self, key: &str, value: &str) -> Result<()>; + fn check_signing_settings(&self) -> Result; +} + +impl RepoCommands for Project { + fn get_local_config(&self, key: &str) -> Result> { + let project_repo = project_repository::ProjectRepo::open(self)?; + let config: Config = project_repo.repo().into(); + config.get_local(key) + } + + fn set_local_config(&self, key: &str, value: &str) -> Result<()> { + let project_repo = project_repository::ProjectRepo::open(self)?; + let config: Config = project_repo.repo().into(); + config.set_local(key, value) + } + + fn check_signing_settings(&self) -> Result { + let repo = project_repository::ProjectRepo::open(self)?; + let signed = repo.repo().sign_buffer(&BString::new("test".into()).into()); + match signed { + Ok(_) => Ok(true), + Err(e) => Err(e), + } + } +} diff --git a/crates/gitbutler-repo/src/lib.rs b/crates/gitbutler-repo/src/lib.rs index 484474187..a8126274a 100644 --- a/crates/gitbutler-repo/src/lib.rs +++ b/crates/gitbutler-repo/src/lib.rs @@ -2,3 +2,6 @@ pub mod rebase; mod repository; pub use repository::{LogUntil, RepoActions}; + +mod commands; +pub use commands::RepoCommands; diff --git a/crates/gitbutler-tauri/src/repo.rs b/crates/gitbutler-tauri/src/repo.rs index d704abd8b..ee68ea368 100644 --- a/crates/gitbutler-tauri/src/repo.rs +++ b/crates/gitbutler-tauri/src/repo.rs @@ -1,6 +1,7 @@ pub mod commands { use crate::error::Error; - use gitbutler_core::projects::{controller::Controller, ProjectId}; + use gitbutler_core::projects::{self, ProjectId}; + use gitbutler_repo::RepoCommands; use tauri::Manager; use tracing::instrument; @@ -11,7 +12,8 @@ pub mod commands { id: ProjectId, key: &str, ) -> Result, Error> { - Ok(handle.state::().get_local_config(id, key)?) + let project = handle.state::().get(id)?; + Ok(project.get_local_config(key)?) } #[tauri::command(async)] @@ -22,10 +24,8 @@ pub mod commands { key: &str, value: &str, ) -> Result<(), Error> { - handle - .state::() - .set_local_config(id, key, value) - .map_err(Into::into) + let project = handle.state::().get(id)?; + project.set_local_config(key, value).map_err(Into::into) } #[tauri::command(async)] @@ -34,9 +34,7 @@ pub mod commands { handle: tauri::AppHandle, id: ProjectId, ) -> Result { - handle - .state::() - .check_signing_settings(id) - .map_err(Into::into) + let project = handle.state::().get(id)?; + project.check_signing_settings().map_err(Into::into) } }