Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions src/bin/git-stack/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,16 +461,30 @@ pub fn stack(args: &crate::args::Args) -> proc_exit::ExitResult {
fn plan_changes(state: &State, stack: &StackState) -> eyre::Result<git_stack::legacy::git::Script> {
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),
)?;
git_stack::legacy::graph::protect_branches(&mut graph, &state.repo, &state.protected_branches);
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),
)?;
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 {
git_stack::legacy::graph::protect_large_branches(&mut graph, protect_commit_count);
}
Expand Down Expand Up @@ -585,21 +599,31 @@ 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),
)?;
git_stack::legacy::graph::protect_branches(
&mut graph,
let onto_commit = state
.repo
.find_commit(stack.onto.id)
.expect("onto branch is valid");
graph.insert(
&state.repo,
&state.protected_branches,
);
git_stack::legacy::graph::Node::new(onto_commit),
)?;
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);
Expand Down
12 changes: 10 additions & 2 deletions src/legacy/graph/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<git2::Oid>,
) {
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)
Expand Down