diff --git a/packages/tauri/src/virtual_branches/controller.rs b/packages/tauri/src/virtual_branches/controller.rs index 7bfe34bfc..239adc191 100644 --- a/packages/tauri/src/virtual_branches/controller.rs +++ b/packages/tauri/src/virtual_branches/controller.rs @@ -526,14 +526,9 @@ impl Controller { 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) + super::flush_applied_vbranches(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(()) }) }) diff --git a/packages/tauri/src/virtual_branches/virtual.rs b/packages/tauri/src/virtual_branches/virtual.rs index ff0c25fd7..d0aeeb211 100644 --- a/packages/tauri/src/virtual_branches/virtual.rs +++ b/packages/tauri/src/virtual_branches/virtual.rs @@ -417,9 +417,28 @@ pub fn unapply_branch( bail!("cannot unapply, project is in a conflicted state"); } - let (default_target, applied_statuses) = if let Some(result) = - flush_vbranch_as_tree(gb_repository, project_repository, branch_id, false)? - { + let (default_target, applied_statuses) = if let Some(result) = { + let session = &gb_repository + .get_or_create_current_session() + .context("failed to get or create currnt session")?; + + let current_session_reader = sessions::Reader::open(gb_repository, session) + .context("failed to open current session")?; + + let branch_reader = branch::Reader::new(¤t_session_reader); + + let mut target_branch = branch_reader + .read(branch_id) + .context("failed to read branch")?; + + if !target_branch.applied { + bail!("branch is not applied"); + } + + target_branch.applied = false; + + flush_vbranch_as_tree(gb_repository, project_repository, session, target_branch)? + } { result } else { return Ok(()); @@ -463,26 +482,17 @@ pub fn unapply_branch( Ok(()) } -pub fn flush_vbranch_as_tree( +fn flush_vbranch_as_tree( gb_repository: &gb_repository::Repository, project_repository: &project_repository::Repository, - branch_id: &BranchId, - applied: bool, + session: &sessions::Session, + branch: Branch, ) -> Result> { - let session = &gb_repository - .get_or_create_current_session() - .context("failed to get or create currnt session")?; + let branch_writer = branch::Writer::new(gb_repository); let current_session_reader = sessions::Reader::open(gb_repository, session).context("failed to open current session")?; - let branch_reader = branch::Reader::new(¤t_session_reader); - let branch_writer = branch::Writer::new(gb_repository); - - let mut target_branch = branch_reader - .read(branch_id) - .context("failed to read branch")?; - let default_target = match get_default_target(¤t_session_reader) .context("failed to get default target")? { @@ -490,10 +500,6 @@ pub fn flush_vbranch_as_tree( None => return Ok(None), }; - if !target_branch.applied { - bail!("branch is not applied"); - } - let applied_branches = Iterator::new(¤t_session_reader) .context("failed to create branch iterator")? .collect::, reader::Error>>() @@ -512,15 +518,15 @@ pub fn flush_vbranch_as_tree( let status = applied_statuses .iter() - .find(|(s, _)| s.id == *branch_id) + .find(|(s, _)| s.id == branch.id) .context("failed to find status for branch"); if let Ok((_, files)) = status { let tree = write_tree(project_repository, &default_target, files)?; - target_branch.tree = tree; - target_branch.applied = applied; - branch_writer.write(&target_branch)?; + let mut branch = branch; + branch.tree = tree; + branch_writer.write(&branch)?; } Ok(Some((default_target, applied_statuses))) @@ -530,19 +536,7 @@ fn unapply_all_branches( gb_repository: &gb_repository::Repository, project_repository: &project_repository::Repository, ) -> Result<()> { - let current_session = gb_repository - .get_or_create_current_session() - .context("failed to get or create currnt session")?; - let current_session_reader = sessions::Reader::open(gb_repository, ¤t_session) - .context("failed to open current session")?; - - let applied_virtual_branches = Iterator::new(¤t_session_reader) - .context("failed to create branch iterator")? - .collect::, reader::Error>>() - .context("failed to read virtual branches")? - .into_iter() - .filter(|branch| branch.applied) - .collect::>(); + let applied_virtual_branches = list_applied_vbranches(gb_repository)?; for branch in applied_virtual_branches { let branch_id = branch.id; @@ -553,6 +547,66 @@ fn unapply_all_branches( Ok(()) } +pub fn flush_applied_vbranches( + gb_repository: &gb_repository::Repository, + project_repository: &project_repository::Repository, +) -> Result<()> { + let applied_branches = list_applied_vbranches(gb_repository)?; + + let session = &gb_repository + .get_or_create_current_session() + .context("failed to get or create currnt session")?; + + let current_session_reader = + sessions::Reader::open(gb_repository, session).context("failed to open current session")?; + + let default_target = match get_default_target(¤t_session_reader) + .context("failed to get default target")? + { + Some(target) => target, + None => return Ok(()), + }; + + let applied_statuses = get_applied_status( + gb_repository, + project_repository, + &default_target, + applied_branches, + ) + .context("failed to get status by branch")?; + + let branch_writer = branch::Writer::new(gb_repository); + + for (b, files) in applied_statuses { + if b.applied { + let tree = write_tree(project_repository, &default_target, &files)?; + let mut branch = b; + branch.tree = tree; + branch_writer.write(&branch)?; + } + } + + Ok(()) +} + +fn list_applied_vbranches( + gb_repository: &gb_repository::Repository, +) -> Result, anyhow::Error> { + let current_session = gb_repository + .get_or_create_current_session() + .context("failed to get or create currnt session")?; + let current_session_reader = sessions::Reader::open(gb_repository, ¤t_session) + .context("failed to open current session")?; + let applied_virtual_branches = Iterator::new(¤t_session_reader) + .context("failed to create branch iterator")? + .collect::, reader::Error>>() + .context("failed to read virtual branches")? + .into_iter() + .filter(|branch| branch.applied) + .collect::>(); + Ok(applied_virtual_branches) +} + fn find_base_tree<'a>( repo: &'a git::Repository, branch_commit: &'a git::Commit<'a>,