From 76f91f6c530f6f74600088baef2d2dd90476ecef Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 2 May 2023 11:53:32 -0500 Subject: [PATCH 1/2] fix(stack): Don't panic on non-branch onto's --- src/bin/git-stack/stack.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/bin/git-stack/stack.rs b/src/bin/git-stack/stack.rs index 7acb4bb6..85cbaec2 100644 --- a/src/bin/git-stack/stack.rs +++ b/src/bin/git-stack/stack.rs @@ -461,15 +461,23 @@ pub fn stack(args: &crate::args::Args) -> proc_exit::ExitResult { fn plan_changes(state: &State, stack: &StackState) -> eyre::Result { log::trace!("Planning stack changes with base={}", stack.base,); let graphed_branches = stack.branches.clone(); + let mut graph = git_stack::legacy::graph::Graph::from_branches(&state.repo, graphed_branches)?; let base_commit = state .repo .find_commit(stack.base.id) .expect("base branch is valid"); - let mut graph = git_stack::legacy::graph::Graph::from_branches(&state.repo, graphed_branches)?; graph.insert( &state.repo, git_stack::legacy::graph::Node::new(base_commit), )?; + let onto_commit = state + .repo + .find_commit(stack.onto.id) + .expect("onto branch is valid"); + graph.insert( + &state.repo, + git_stack::legacy::graph::Node::new(onto_commit), + )?; git_stack::legacy::graph::protect_branches(&mut graph, &state.repo, &state.protected_branches); if let Some(protect_commit_count) = state.protect_commit_count { git_stack::legacy::graph::protect_large_branches(&mut graph, protect_commit_count); @@ -585,16 +593,24 @@ fn show(state: &State) -> eyre::Result<()> { } log::trace!("Rendering stack base={}", stack.base,); + let mut graph = + git_stack::legacy::graph::Graph::from_branches(&state.repo, graphed_branches)?; let base_commit = state .repo .find_commit(stack.base.id) .expect("base branch is valid"); - let mut graph = - git_stack::legacy::graph::Graph::from_branches(&state.repo, graphed_branches)?; graph.insert( &state.repo, git_stack::legacy::graph::Node::new(base_commit), )?; + let onto_commit = state + .repo + .find_commit(stack.onto.id) + .expect("onto branch is valid"); + graph.insert( + &state.repo, + git_stack::legacy::graph::Node::new(onto_commit), + )?; git_stack::legacy::graph::protect_branches( &mut graph, &state.repo, From a28e2a2f6179432a00f908ecaad5a49fe3cad60d Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 2 May 2023 12:42:43 -0500 Subject: [PATCH 2/2] fix(stack): Protect onto --- src/bin/git-stack/stack.rs | 20 ++++++++++++++------ src/legacy/graph/ops.rs | 12 ++++++++++-- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/bin/git-stack/stack.rs b/src/bin/git-stack/stack.rs index 85cbaec2..892ef779 100644 --- a/src/bin/git-stack/stack.rs +++ b/src/bin/git-stack/stack.rs @@ -478,7 +478,13 @@ fn plan_changes(state: &State, stack: &StackState) -> eyre::Result = state + .protected_branches + .iter() + .flat_map(|(_, branches)| branches.iter().map(|b| b.id)) + .collect(); + protected_oids.insert(stack.onto.id); + git_stack::legacy::graph::protect_commits(&mut graph, &state.repo, protected_oids); if let Some(protect_commit_count) = state.protect_commit_count { git_stack::legacy::graph::protect_large_branches(&mut graph, protect_commit_count); } @@ -611,11 +617,13 @@ fn show(state: &State) -> eyre::Result<()> { &state.repo, git_stack::legacy::graph::Node::new(onto_commit), )?; - git_stack::legacy::graph::protect_branches( - &mut graph, - &state.repo, - &state.protected_branches, - ); + let mut protected_oids: std::collections::HashSet<_> = state + .protected_branches + .iter() + .flat_map(|(_, branches)| branches.iter().map(|b| b.id)) + .collect(); + protected_oids.insert(stack.onto.id); + git_stack::legacy::graph::protect_commits(&mut graph, &state.repo, protected_oids); if let Some(protect_commit_count) = state.protect_commit_count { let protected = git_stack::legacy::graph::protect_large_branches(&mut graph, protect_commit_count); diff --git a/src/legacy/graph/ops.rs b/src/legacy/graph/ops.rs index a8488b06..cbf5e89a 100644 --- a/src/legacy/graph/ops.rs +++ b/src/legacy/graph/ops.rs @@ -10,13 +10,21 @@ pub fn protect_branches( repo: &dyn crate::legacy::git::Repo, protected_branches: &crate::legacy::git::Branches, ) { - let root_id = graph.root_id(); - let protected_oids: HashSet<_> = protected_branches .iter() .flat_map(|(_, branches)| branches.iter().map(|b| b.id)) .collect(); + protect_commits(graph, repo, protected_oids); +} + +pub fn protect_commits( + graph: &mut Graph, + repo: &dyn crate::legacy::git::Repo, + protected_oids: HashSet, +) { + let root_id = graph.root_id(); + for protected_oid in protected_oids.into_iter().filter(|protected_oid| { repo.merge_base(root_id, *protected_oid) .map(|merge_base_oid| merge_base_oid == root_id)