diff --git a/packages/butler/src/app.rs b/packages/butler/src/app.rs index 56183c289..28576f684 100644 --- a/packages/butler/src/app.rs +++ b/packages/butler/src/app.rs @@ -65,11 +65,11 @@ impl App { } pub fn project_repository(&self) -> project_repository::Repository { - project_repository::Repository::try_from(&self.project).unwrap() + project_repository::Repository::open(&self.project).unwrap() } pub fn gb_repository(&self) -> gb_repository::Repository { - let project_repository = project_repository::Repository::try_from(&self.project) + let project_repository = project_repository::Repository::open(&self.project) .expect("failed to open project repository"); let gb_repository = gb_repository::Repository::open( &self.local_data_dir, diff --git a/packages/tauri/src/app.rs b/packages/tauri/src/app.rs index 128a4acf1..1a9d2fdfc 100644 --- a/packages/tauri/src/app.rs +++ b/packages/tauri/src/app.rs @@ -76,46 +76,6 @@ impl App { Ok(()) } - pub fn list_sessions( - &self, - project_id: &ProjectId, - earliest_timestamp_ms: Option, - ) -> Result> { - let sessions = self - .sessions_database - .list_by_project_id(project_id, earliest_timestamp_ms)?; - - let project = self.projects.get(project_id)?; - let project_repository = project_repository::Repository::try_from(&project)?; - let user = self.users.get_user().context("failed to get user")?; - let gb_repository = gb_repository::Repository::open( - &self.local_data_dir, - &project_repository, - user.as_ref(), - )?; - - // this is a hack to account for a case when we have a session created, but fs was never - // touched, so the wathcer never picked up the session - let current_session = gb_repository.get_current_session()?; - let have_to_index = matches!( - (current_session.as_ref(), sessions.first()), - (Some(_), None) - ); - if !have_to_index { - return Ok(sessions); - } - - let sessions_iter = gb_repository.get_sessions_iterator()?; - let mut sessions = sessions_iter.collect::, _>>()?; - self.sessions_database - .insert(project_id, &sessions.iter().collect::>())?; - if let Some(session) = current_session { - self.sessions_database.insert(project_id, &[&session])?; - sessions.insert(0, session); - } - Ok(sessions) - } - pub fn list_session_files( &self, project_id: &ProjectId, @@ -129,7 +89,7 @@ impl App { .context("session not found")?; let user = self.users.get_user().context("failed to get user")?; let project = self.projects.get(project_id)?; - let project_repository = project_repository::Repository::try_from(&project)?; + let project_repository = project_repository::Repository::open(&project)?; let gb_repo = gb_repository::Repository::open( &self.local_data_dir, &project_repository, @@ -146,7 +106,7 @@ impl App { pub fn mark_resolved(&self, project_id: &ProjectId, path: &str) -> Result<(), Error> { let project = self.projects.get(project_id)?; - let project_repository = project_repository::Repository::try_from(&project)?; + let project_repository = project_repository::Repository::open(&project)?; // mark file as resolved conflicts::resolve(&project_repository, path)?; Ok(()) @@ -154,7 +114,7 @@ impl App { pub fn fetch_from_target(&self, project_id: &ProjectId) -> Result<(), Error> { let project = self.projects.get(project_id)?; - let project_repository = project_repository::Repository::try_from(&project)?; + let project_repository = project_repository::Repository::open(&project)?; let user = self.users.get_user().context("failed to get user")?; let gb_repo = gb_repository::Repository::open( &self.local_data_dir, @@ -190,7 +150,7 @@ impl App { context_lines: u32, ) -> Result, Error> { let project = self.projects.get(project_id)?; - let project_repository = project_repository::Repository::try_from(&project)?; + let project_repository = project_repository::Repository::open(&project)?; let diff = diff::workdir( &project_repository.git_repository, @@ -226,7 +186,7 @@ impl App { project_id: &ProjectId, ) -> Result, Error> { let project = self.projects.get(project_id)?; - let project_repository = project_repository::Repository::try_from(&project)?; + let project_repository = project_repository::Repository::open(&project)?; project_repository .git_remote_branches() .map_err(Error::Other) @@ -234,7 +194,7 @@ impl App { pub fn git_head(&self, project_id: &ProjectId) -> Result { let project = self.projects.get(project_id)?; - let project_repository = project_repository::Repository::try_from(&project)?; + let project_repository = project_repository::Repository::open(&project)?; let head = project_repository .get_head() .context("failed to get repository head")?; diff --git a/packages/tauri/src/bookmarks/commands.rs b/packages/tauri/src/bookmarks/commands.rs index 7c59bf8fe..095974d50 100644 --- a/packages/tauri/src/bookmarks/commands.rs +++ b/packages/tauri/src/bookmarks/commands.rs @@ -13,6 +13,7 @@ use super::{ impl From for Error { fn from(value: UpsertError) -> Self { match value { + UpsertError::OpenProjectRepository(error) => Error::from(error), UpsertError::GetUser(error) => Error::from(error), UpsertError::GetProject(error) => Error::from(error), UpsertError::Other(error) => { diff --git a/packages/tauri/src/bookmarks/controller.rs b/packages/tauri/src/bookmarks/controller.rs index 2b343f53a..243d0160e 100644 --- a/packages/tauri/src/bookmarks/controller.rs +++ b/packages/tauri/src/bookmarks/controller.rs @@ -42,6 +42,8 @@ pub enum UpsertError { #[error(transparent)] GetUser(#[from] users::GetError), #[error(transparent)] + OpenProjectRepository(#[from] project_repository::OpenError), + #[error(transparent)] Other(#[from] anyhow::Error), } @@ -54,8 +56,7 @@ pub enum ListError { impl Controller { pub fn upsert(&self, bookmark: &Bookmark) -> Result, UpsertError> { let project = self.projects.get(&bookmark.project_id)?; - let project_repository = project_repository::Repository::try_from(&project) - .context("failed to open project repository")?; + let project_repository = project_repository::Repository::open(&project)?; let user = self.users.get_user()?; let gb_repository = gb_repository::Repository::open( &self.local_data_dir, diff --git a/packages/tauri/src/commands.rs b/packages/tauri/src/commands.rs index 22dafca2c..13f477226 100644 --- a/packages/tauri/src/commands.rs +++ b/packages/tauri/src/commands.rs @@ -169,8 +169,7 @@ pub async fn project_flush_and_push(handle: tauri::AppHandle, id: &str) -> Resul let project = projects.get(&id).context("failed to get project")?; let user = users.get_user()?; - let project_repository = - project_repository::Repository::try_from(&project).context("failed to open repository")?; + let project_repository = project_repository::Repository::open(&project)?; let gb_repo = gb_repository::Repository::open(&local_data_dir, &project_repository, user.as_ref()) .context("failed to open repository")?; diff --git a/packages/tauri/src/gb_repository/repository.rs b/packages/tauri/src/gb_repository/repository.rs index f51db4426..0b6bac86a 100644 --- a/packages/tauri/src/gb_repository/repository.rs +++ b/packages/tauri/src/gb_repository/repository.rs @@ -369,7 +369,7 @@ impl Repository { match sessions::Session::try_from(reader) { Result::Ok(session) => Ok(session), Err(sessions::SessionError::NoSession) => { - let project_repository = project_repository::Repository::try_from(&self.project) + let project_repository = project_repository::Repository::open(&self.project) .context("failed to open project repository")?; let session = self .create_current_session(&project_repository) diff --git a/packages/tauri/src/project_repository/repository.rs b/packages/tauri/src/project_repository/repository.rs index ec8a5a0f4..79df2aea0 100644 --- a/packages/tauri/src/project_repository/repository.rs +++ b/packages/tauri/src/project_repository/repository.rs @@ -40,30 +40,19 @@ impl From for crate::error::Error { } } -impl TryFrom for Repository { - type Error = OpenError; - - fn try_from(project: projects::Project) -> Result { - let git_repository = git::Repository::open(&project.path).map_err(|error| match error { - git::Error::NotFound(_) => OpenError::NotFound(project.path.clone()), - other => OpenError::Other(other.into()), - })?; - Ok(Self { - git_repository, - project, - }) - } -} - -impl TryFrom<&projects::Project> for Repository { - type Error = OpenError; - - fn try_from(project: &projects::Project) -> Result { - Self::try_from(project.clone()) - } -} - impl Repository { + pub fn open(project: &projects::Project) -> Result { + git::Repository::open(&project.path) + .map_err(|error| match error { + git::Error::NotFound(_) => OpenError::NotFound(project.path.clone()), + other => OpenError::Other(other.into()), + }) + .map(|git_repository| Self { + git_repository, + project: project.clone(), + }) + } + pub fn path(&self) -> &path::Path { path::Path::new(&self.project.path) } diff --git a/packages/tauri/src/projects/commands.rs b/packages/tauri/src/projects/commands.rs index 22e8c4b9e..3a3419151 100644 --- a/packages/tauri/src/projects/commands.rs +++ b/packages/tauri/src/projects/commands.rs @@ -48,6 +48,7 @@ impl From for Error { code: Code::Projects, message: "Project already exists".to_string(), }, + controller::AddError::OpenProjectRepository(error) => error.into(), controller::AddError::NotADirectory => Error::UserError { code: Code::Projects, message: "Not a directory".to_string(), diff --git a/packages/tauri/src/projects/controller.rs b/packages/tauri/src/projects/controller.rs index 767dad2b0..d2555c196 100644 --- a/packages/tauri/src/projects/controller.rs +++ b/packages/tauri/src/projects/controller.rs @@ -77,8 +77,7 @@ impl Controller { // create all required directories to avoid racing later let user = self.users.get_user()?; - let project_repository = project_repository::Repository::try_from(&project) - .context("failed to open repository")?; + let project_repository = project_repository::Repository::open(&project)?; gb_repository::Repository::open(&self.local_data_dir, &project_repository, user.as_ref()) .context("failed to open repository")?; @@ -219,5 +218,7 @@ pub enum AddError { #[error(transparent)] User(#[from] users::GetError), #[error(transparent)] + OpenProjectRepository(#[from] project_repository::OpenError), + #[error(transparent)] Other(#[from] anyhow::Error), } diff --git a/packages/tauri/src/sessions/commands.rs b/packages/tauri/src/sessions/commands.rs index 620bc414c..57e3a6a95 100644 --- a/packages/tauri/src/sessions/commands.rs +++ b/packages/tauri/src/sessions/commands.rs @@ -13,6 +13,7 @@ impl From for Error { match value { ListError::UsersError(error) => Error::from(error), ListError::ProjectsError(error) => Error::from(error), + ListError::ProjectRepositoryError(error) => Error::from(error), ListError::Other(error) => { tracing::error!(?error); Error::Unknown diff --git a/packages/tauri/src/sessions/controller.rs b/packages/tauri/src/sessions/controller.rs index 57ddb0533..3f0d11a54 100644 --- a/packages/tauri/src/sessions/controller.rs +++ b/packages/tauri/src/sessions/controller.rs @@ -35,6 +35,8 @@ pub enum ListError { #[error(transparent)] ProjectsError(#[from] projects::GetError), #[error(transparent)] + ProjectRepositoryError(#[from] project_repository::OpenError), + #[error(transparent)] UsersError(#[from] users::GetError), #[error(transparent)] Other(#[from] anyhow::Error), @@ -51,8 +53,7 @@ impl Controller { .list_by_project_id(project_id, earliest_timestamp_ms)?; let project = self.projects.get(project_id)?; - let project_repository = project_repository::Repository::try_from(&project) - .context("failed to open project repository")?; + let project_repository = project_repository::Repository::open(&project)?; let user = self.users.get_user()?; let gb_repository = gb_repository::Repository::open( &self.local_data_dir, diff --git a/packages/tauri/src/test_utils.rs b/packages/tauri/src/test_utils.rs index f76329dae..cc5e04d7a 100644 --- a/packages/tauri/src/test_utils.rs +++ b/packages/tauri/src/test_utils.rs @@ -83,7 +83,7 @@ pub struct Case<'a> { impl<'a> Case<'a> { fn new(suite: &'a Suite, project: projects::Project) -> Case<'a> { - let project_repository = project_repository::Repository::try_from(&project) + let project_repository = project_repository::Repository::open(&project) .expect("failed to create project repository"); let gb_repository = gb_repository::Repository::open(&suite.local_app_data, &project_repository, None) @@ -108,7 +108,7 @@ impl<'a> Case<'a> { .projects .get(&self.project.id) .expect("failed to get project"); - let project_repository = project_repository::Repository::try_from(&project) + let project_repository = project_repository::Repository::open(&project) .expect("failed to create project repository"); let user = self.suite.users.get_user().expect("failed to get user"); let credentials = git::credentials::Factory::new( diff --git a/packages/tauri/src/virtual_branches/controller.rs b/packages/tauri/src/virtual_branches/controller.rs index bcabf1550..7dc1622ed 100644 --- a/packages/tauri/src/virtual_branches/controller.rs +++ b/packages/tauri/src/virtual_branches/controller.rs @@ -131,7 +131,7 @@ impl Controller { branch_name: &git::BranchName, ) -> Result { let project = self.projects.get(project_id)?; - let project_repository = project_repository::Repository::try_from(&project)?; + let project_repository = project_repository::Repository::open(&project)?; let user = self.users.get_user().context("failed to get user")?; let gb_repository = gb_repository::Repository::open( &self.local_data_dir, @@ -149,7 +149,7 @@ impl Controller { branch_id: &BranchId, ) -> Result { let project = self.projects.get(project_id)?; - let project_repository = project_repository::Repository::try_from(&project)?; + let project_repository = project_repository::Repository::open(&project)?; let user = self.users.get_user().context("failed to get user")?; let gb_repository = gb_repository::Repository::open( &self.local_data_dir, @@ -241,7 +241,7 @@ impl Controller { project_id: &ProjectId, ) -> Result, Error> { let project = self.projects.get(project_id)?; - let project_repository = project_repository::Repository::try_from(&project)?; + let project_repository = project_repository::Repository::open(&project)?; let user = self.users.get_user().context("failed to get user")?; let gb_repository = gb_repository::Repository::open( &self.local_data_dir, @@ -260,7 +260,7 @@ impl Controller { commit_oid: git::Oid, ) -> Result, Error> { let project = self.projects.get(project_id)?; - let project_repository = project_repository::Repository::try_from(&project)?; + let project_repository = project_repository::Repository::open(&project)?; let commit = project_repository .git_repository .find_commit(commit_oid) @@ -278,7 +278,7 @@ impl Controller { let user = self.users.get_user().context("failed to get user")?; - let project_repository = project_repository::Repository::try_from(&project)?; + let project_repository = project_repository::Repository::open(&project)?; let gb_repository = gb_repository::Repository::open( &self.local_data_dir, @@ -514,7 +514,7 @@ impl Controller { ) -> Result, ) -> Result { let project = self.projects.get(project_id)?; - let project_repository = project_repository::Repository::try_from(&project)?; + let project_repository = project_repository::Repository::open(&project)?; let user = self.users.get_user().context("failed to get user")?; let gb_repository = gb_repository::Repository::open( &self.local_data_dir, @@ -574,7 +574,7 @@ impl Controller { project_id: &ProjectId, ) -> Result, Error> { let project = self.projects.get(project_id)?; - let project_repository = project_repository::Repository::try_from(&project)?; + let project_repository = project_repository::Repository::open(&project)?; let user = self.users.get_user().context("failed to get user")?; let gb_repository = gb_repository::Repository::open( &self.local_data_dir, diff --git a/packages/tauri/src/watcher/handlers/fetch_gitbutler_data.rs b/packages/tauri/src/watcher/handlers/fetch_gitbutler_data.rs index a78364e48..3f3aea1e4 100644 --- a/packages/tauri/src/watcher/handlers/fetch_gitbutler_data.rs +++ b/packages/tauri/src/watcher/handlers/fetch_gitbutler_data.rs @@ -78,8 +78,8 @@ impl HandlerInner { anyhow::bail!("sync disabled"); } - let project_repository = project_repository::Repository::try_from(&project) - .context("failed to open repository")?; + let project_repository = + project_repository::Repository::open(&project).context("failed to open repository")?; let gb_repo = gb_repository::Repository::open( &self.local_data_dir, &project_repository, diff --git a/packages/tauri/src/watcher/handlers/fetch_project_data.rs b/packages/tauri/src/watcher/handlers/fetch_project_data.rs index 4702e9b08..358fec1ee 100644 --- a/packages/tauri/src/watcher/handlers/fetch_project_data.rs +++ b/packages/tauri/src/watcher/handlers/fetch_project_data.rs @@ -78,7 +78,7 @@ impl HandlerInner { .projects .get(project_id) .context("failed to get project")?; - let project_repository = project_repository::Repository::try_from(&project) + let project_repository = project_repository::Repository::open(&project) .context("failed to open repository. Make sure the project is configured correctly.")?; let gb_repo = gb_repository::Repository::open( &self.local_data_dir, diff --git a/packages/tauri/src/watcher/handlers/flush_session.rs b/packages/tauri/src/watcher/handlers/flush_session.rs index bdba94feb..1bef897f3 100644 --- a/packages/tauri/src/watcher/handlers/flush_session.rs +++ b/packages/tauri/src/watcher/handlers/flush_session.rs @@ -44,8 +44,8 @@ impl Handler { .context("failed to get project")?; let user = self.users.get_user()?; - let project_repository = project_repository::Repository::try_from(&project) - .context("failed to open repository")?; + let project_repository = + project_repository::Repository::open(&project).context("failed to open repository")?; let gb_repo = gb_repository::Repository::open( &self.local_data_dir, &project_repository, diff --git a/packages/tauri/src/watcher/handlers/git_file_change.rs b/packages/tauri/src/watcher/handlers/git_file_change.rs index 03b4c9304..63207ff72 100644 --- a/packages/tauri/src/watcher/handlers/git_file_change.rs +++ b/packages/tauri/src/watcher/handlers/git_file_change.rs @@ -41,7 +41,7 @@ impl Handler { .get(project_id) .context("failed to get project")?; - let project_repository = project_repository::Repository::try_from(&project) + let project_repository = project_repository::Repository::open(&project) .context("failed to open project repository for project")?; match path.as_ref().to_str().unwrap() { diff --git a/packages/tauri/src/watcher/handlers/index_handler.rs b/packages/tauri/src/watcher/handlers/index_handler.rs index dbbaa4ef9..28b64eccd 100644 --- a/packages/tauri/src/watcher/handlers/index_handler.rs +++ b/packages/tauri/src/watcher/handlers/index_handler.rs @@ -71,8 +71,8 @@ impl Handler { pub fn reindex(&self, project_id: &ProjectId) -> Result> { let user = self.users.get_user()?; let project = self.projects.get(project_id)?; - let project_repository = project_repository::Repository::try_from(&project) - .context("failed to open repository")?; + let project_repository = + project_repository::Repository::open(&project).context("failed to open repository")?; let gb_repository = gb_repository::Repository::open( &self.local_data_dir, &project_repository, @@ -95,8 +95,8 @@ impl Handler { ) -> Result> { let user = self.users.get_user()?; let project = self.projects.get(project_id)?; - let project_repository = project_repository::Repository::try_from(&project) - .context("failed to open repository")?; + let project_repository = + project_repository::Repository::open(&project).context("failed to open repository")?; let gb_repository = gb_repository::Repository::open( &self.local_data_dir, &project_repository, diff --git a/packages/tauri/src/watcher/handlers/project_file_change.rs b/packages/tauri/src/watcher/handlers/project_file_change.rs index 68e9f06b2..86e104674 100644 --- a/packages/tauri/src/watcher/handlers/project_file_change.rs +++ b/packages/tauri/src/watcher/handlers/project_file_change.rs @@ -86,7 +86,7 @@ impl Handler { .get(project_id) .context("failed to get project")?; - let project_repository = project_repository::Repository::try_from(&project) + let project_repository = project_repository::Repository::open(&project) .with_context(|| "failed to open project repository for project")?; let user = self.users.get_user().context("failed to get user")?; diff --git a/packages/tauri/src/watcher/handlers/push_gitbutler_data.rs b/packages/tauri/src/watcher/handlers/push_gitbutler_data.rs index 00754d7c4..6fb7c4d53 100644 --- a/packages/tauri/src/watcher/handlers/push_gitbutler_data.rs +++ b/packages/tauri/src/watcher/handlers/push_gitbutler_data.rs @@ -68,8 +68,8 @@ impl HandlerInner { pub fn handle(&self, project_id: &ProjectId) -> Result> { let user = self.users.get_user()?; let project = self.projects.get(project_id)?; - let project_repository = project_repository::Repository::try_from(&project) - .context("failed to open repository")?; + let project_repository = + project_repository::Repository::open(&project).context("failed to open repository")?; let gb_repo = gb_repository::Repository::open( &self.local_data_dir, &project_repository, diff --git a/packages/tauri/src/watcher/handlers/push_project_to_gitbutler.rs b/packages/tauri/src/watcher/handlers/push_project_to_gitbutler.rs index ddd3f2406..e7fb1836c 100644 --- a/packages/tauri/src/watcher/handlers/push_project_to_gitbutler.rs +++ b/packages/tauri/src/watcher/handlers/push_project_to_gitbutler.rs @@ -77,8 +77,8 @@ impl HandlerInner { .context("failed to get project")?; let user = self.users.get_user()?; - let project_repository = project_repository::Repository::try_from(&project) - .context("failed to open repository")?; + let project_repository = + project_repository::Repository::open(&project).context("failed to open repository")?; if project_repository.project().is_sync_enabled() && project_repository.project().has_code_url() diff --git a/packages/tauri/src/watcher/handlers/tick_handler.rs b/packages/tauri/src/watcher/handlers/tick_handler.rs index 57aee990c..151b63cd4 100644 --- a/packages/tauri/src/watcher/handlers/tick_handler.rs +++ b/packages/tauri/src/watcher/handlers/tick_handler.rs @@ -45,7 +45,7 @@ impl Handler { let user = self.users.get_user()?; let project = self.projects.get(project_id)?; - let project_repository = match project_repository::Repository::try_from(&project) { + let project_repository = match project_repository::Repository::open(&project) { Ok(project_repository) => Ok(project_repository), Err(project_repository::OpenError::NotFound(_)) => return Ok(vec![]), Err(error) => Err(error), diff --git a/packages/tauri/tests/gb_repository/mod.rs b/packages/tauri/tests/gb_repository/mod.rs index 3f19e12f7..3b9059f29 100644 --- a/packages/tauri/tests/gb_repository/mod.rs +++ b/packages/tauri/tests/gb_repository/mod.rs @@ -19,7 +19,7 @@ mod init { std::fs::write(project.path.join("file"), "content").unwrap(); std::fs::hard_link(project.path.join("file"), project.path.join("link")).unwrap(); - let project_repository = project_repository::Repository::try_from(project).unwrap(); + let project_repository = project_repository::Repository::open(&project).unwrap(); gb_repository::Repository::open(&data_dir, &project_repository, None).unwrap(); } @@ -40,7 +40,7 @@ mod init { std::os::unix::fs::symlink(project.path.join("dir"), project.path.join("dir_link")) .unwrap(); - let project_repository = project_repository::Repository::try_from(project).unwrap(); + let project_repository = project_repository::Repository::open(&project).unwrap(); gb_repository::Repository::open(&data_dir, &project_repository, None).unwrap(); } @@ -66,7 +66,7 @@ mod init { ) .unwrap(); - let project_repository = project_repository::Repository::try_from(project).unwrap(); + let project_repository = project_repository::Repository::open(&project).unwrap(); gb_repository::Repository::open(&data_dir, &project_repository, None).unwrap(); } @@ -86,7 +86,7 @@ mod flush { .add(test_project.path()) .expect("failed to add project"); - let project_repository = project_repository::Repository::try_from(&project).unwrap(); + let project_repository = project_repository::Repository::open(&project).unwrap(); let gb_repo = gb_repository::Repository::open(&data_dir, &project_repository, None).unwrap(); @@ -108,7 +108,7 @@ mod flush { .add(test_project.path()) .expect("failed to add project"); - let project_repository = project_repository::Repository::try_from(&project).unwrap(); + let project_repository = project_repository::Repository::open(&project).unwrap(); let gb_repo = gb_repository::Repository::open(&data_dir, &project_repository, None).unwrap();