verify order of all branches every time

This commit is contained in:
Nikita Galaiko 2023-07-31 12:36:33 +02:00
parent 2db0f65b21
commit 94387b41d8

View File

@ -1144,40 +1144,32 @@ pub fn create_virtual_branch(
.context("failed to find commit")?; .context("failed to find commit")?;
let tree = commit.tree().context("failed to find tree")?; let tree = commit.tree().context("failed to find tree")?;
let all_virtual_branches = Iterator::new(&current_session_reader) let mut all_virtual_branches = Iterator::new(&current_session_reader)
.context("failed to create branch iterator")? .context("failed to create branch iterator")?
.collect::<Result<Vec<branch::Branch>, reader::Error>>() .collect::<Result<Vec<branch::Branch>, reader::Error>>()
.context("failed to read virtual branches")? .context("failed to read virtual branches")?
.into_iter() .into_iter()
.collect::<Vec<branch::Branch>>(); .collect::<Vec<branch::Branch>>();
all_virtual_branches.sort_by_key(|branch| branch.order);
let mut applied_virtual_branches = all_virtual_branches let order = create
.iter() .order
.filter(|branch| branch.applied) .unwrap_or(all_virtual_branches.len())
.cloned() .clamp(0, all_virtual_branches.len());
.collect::<Vec<branch::Branch>>();
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); let branch_writer = branch::Writer::new(gb_repository);
// make space for the new branch // 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(); let mut branch = branch.clone();
branch.order += 1; let new_order = if i < order { i } else { i + 1 };
if branch.order != new_order {
branch.order = new_order;
branch_writer branch_writer
.write(&branch) .write(&branch)
.context("failed to write branch")?; .context("failed to write branch")?;
} }
}
let now = time::UNIX_EPOCH let now = time::UNIX_EPOCH
.elapsed() .elapsed()