From 94387b41d83f821c0bfd8a500e24e7da5cbf9ed3 Mon Sep 17 00:00:00 2001 From: Nikita Galaiko Date: Mon, 31 Jul 2023 12:36:33 +0200 Subject: [PATCH] verify order of all branches every time --- src-tauri/src/virtual_branches/mod.rs | 36 +++++++++++---------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src-tauri/src/virtual_branches/mod.rs b/src-tauri/src/virtual_branches/mod.rs index 1a4c61aa3..9f3d37130 100644 --- a/src-tauri/src/virtual_branches/mod.rs +++ b/src-tauri/src/virtual_branches/mod.rs @@ -1144,39 +1144,31 @@ pub fn create_virtual_branch( .context("failed to find commit")?; let tree = commit.tree().context("failed to find tree")?; - let all_virtual_branches = Iterator::new(¤t_session_reader) + let mut all_virtual_branches = Iterator::new(¤t_session_reader) .context("failed to create branch iterator")? .collect::, reader::Error>>() .context("failed to read virtual branches")? .into_iter() .collect::>(); + all_virtual_branches.sort_by_key(|branch| branch.order); - let mut applied_virtual_branches = all_virtual_branches - .iter() - .filter(|branch| branch.applied) - .cloned() - .collect::>(); + let order = create + .order + .unwrap_or(all_virtual_branches.len()) + .clamp(0, all_virtual_branches.len()); - applied_virtual_branches.sort_by_key(|branch| branch.order); - - let order = if let Some(order) = create.order { - if order > applied_virtual_branches.len() { - applied_virtual_branches.len() - } else { - order - } - } else { - applied_virtual_branches.len() - }; let branch_writer = branch::Writer::new(gb_repository); // make space for the new branch - for branch in applied_virtual_branches.iter().skip(order) { + for (i, branch) in all_virtual_branches.iter().enumerate() { let mut branch = branch.clone(); - branch.order += 1; - branch_writer - .write(&branch) - .context("failed to write branch")?; + let new_order = if i < order { i } else { i + 1 }; + if branch.order != new_order { + branch.order = new_order; + branch_writer + .write(&branch) + .context("failed to write branch")?; + } } let now = time::UNIX_EPOCH