From ae3a6f3ebe5505666d1c3da878ec46319f1285b1 Mon Sep 17 00:00:00 2001 From: extrawurst <776816+extrawurst@users.noreply.github.com> Date: Mon, 16 Oct 2023 13:18:01 +0200 Subject: [PATCH] feat: add flush_vbranches method to Controller struct (#1383) * feat: add flush_vbranches method to Controller struct The `flush_vbranches` method has been added to the `Controller` struct. This method allows flushing virtual branches as a tree for a specific project. It iterates over the virtual branches of the project and calls the `flush_vbranch_as_tree` function to flush each virtual branch. This method is used in the `Handler` struct to flush virtual branches for a project before flushing the session. * cleanup error handling * make controller async for consistency and block on the caller site --- .../tauri/src/virtual_branches/controller.rs | 18 +++++++++++++++ .../src/watcher/handlers/flush_session.rs | 23 ++++++++++--------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/packages/tauri/src/virtual_branches/controller.rs b/packages/tauri/src/virtual_branches/controller.rs index f0217d02a..7bee5af6a 100644 --- a/packages/tauri/src/virtual_branches/controller.rs +++ b/packages/tauri/src/virtual_branches/controller.rs @@ -17,6 +17,7 @@ use super::{ RemoteBranchFile, }; +#[derive(Clone)] pub struct Controller { local_data_dir: DataDir, semaphores: Arc>>, @@ -530,4 +531,21 @@ impl Controller { let _permit = semaphore.acquire().await; action() } + + pub async fn flush_vbranches(&self, project_id: ProjectId) -> Result<(), Error> { + self.with_lock(&project_id, || { + self.with_verify_branch(&project_id, |gb_repository, project_repository, _| { + let vbranches = super::list_virtual_branches(gb_repository, project_repository) + .map_err(Error::Other)?; + + for b in &vbranches { + super::flush_vbranch_as_tree(gb_repository, project_repository, &b.id, true) + .map_err(Error::Other)?; + } + + Ok(()) + }) + }) + .await + } } diff --git a/packages/tauri/src/watcher/handlers/flush_session.rs b/packages/tauri/src/watcher/handlers/flush_session.rs index 79fef70b8..267774ffa 100644 --- a/packages/tauri/src/watcher/handlers/flush_session.rs +++ b/packages/tauri/src/watcher/handlers/flush_session.rs @@ -1,12 +1,12 @@ use anyhow::{Context, Result}; -use tauri::AppHandle; +use tauri::{AppHandle, Manager}; use crate::{ gb_repository, paths::DataDir, project_repository, projects::{self, ProjectId}, - sessions, users, + sessions, users, virtual_branches, }; use super::events; @@ -15,6 +15,7 @@ use super::events; pub struct Handler { local_data_dir: DataDir, project_store: projects::Controller, + vbrach_controller: virtual_branches::Controller, users: users::Controller, } @@ -25,6 +26,10 @@ impl TryFrom<&AppHandle> for Handler { Ok(Self { local_data_dir: DataDir::try_from(value)?, project_store: projects::Controller::try_from(value)?, + vbrach_controller: value + .state::() + .inner() + .clone(), users: users::Controller::from(value), }) } @@ -51,15 +56,11 @@ impl Handler { ) .context("failed to open repository")?; - // TODO: fixme - // if let Some(branch_id) = &session.meta.branch { - // virtual_branches::flush_vbranch_as_tree( - // &gb_repo, - // &project_repository, - // branch_id, - // true, - // )?; - // } + futures::executor::block_on(async { + self.vbrach_controller + .flush_vbranches(project_repository.project().id) + .await + })?; let session = gb_repo .flush_session(&project_repository, session, user.as_ref())