Skip to content

fix(graph): Gracefully handle squash-merges #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 8, 2021
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
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/git-fixture/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ edition = "2018"

[dependencies]
serde = { version = "1", features = ["derive"] }
humantime = "2"
humantime-serde = "1"
bstr = { version = "0.2", features = ["serde1"] }
derive_more = "0.99.0"
eyre = "0.6"
Expand Down
19 changes: 10 additions & 9 deletions crates/git-fixture/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,26 @@ impl Dag {
}

let mut marks: std::collections::HashMap<String, String> = Default::default();
Self::run_events(self.events, cwd, &self.import_root, &mut marks)?;
self.run_events(&self.events, cwd, &self.import_root, &mut marks)?;

Ok(())
}

// Note: shelling out to git to minimize programming bugs
fn run_events(
events: Vec<Event>,
&self,
events: &[Event],
cwd: &std::path::Path,
import_root: &std::path::Path,
marks: &mut std::collections::HashMap<String, String>,
) -> eyre::Result<()> {
for event in events.into_iter() {
for event in events.iter() {
match event {
Event::Import(path) => {
let path = import_root.join(path);
let mut child_dag = Dag::load(&path)?;
child_dag.init = false;
child_dag.sleep = child_dag.sleep.or(self.sleep);
child_dag.run(cwd).wrap_err_with(|| {
format!("Failed when running imported fixcture {}", path.display())
})?;
Expand Down Expand Up @@ -113,6 +115,9 @@ impl Dag {
p.arg("--author").arg(author);
}
p.ok()?;
if let Some(sleep) = self.sleep {
std::thread::sleep(sleep);
}

if let Some(branch) = tree.branch.as_ref() {
let _ = std::process::Command::new("git")
Expand All @@ -135,15 +140,11 @@ impl Dag {
}
}
}
Event::Children(mut events) => {
Event::Children(events) => {
let start_commit = current_oid(cwd)?;
let last_run = events.pop();
for run in events {
Self::run_events(run, cwd, import_root, marks)?;
checkout(cwd, &start_commit)?;
}
if let Some(last_run) = last_run {
Self::run_events(last_run, cwd, import_root, marks)?;
self.run_events(run, cwd, import_root, marks)?;
}
}
Event::Head(reference) => {
Expand Down
7 changes: 6 additions & 1 deletion crates/git-fixture/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ struct Args {
input: Option<std::path::PathBuf>,
#[structopt(short, long)]
output: Option<std::path::PathBuf>,
/// Sleep between commits
#[structopt(long, parse(try_from_str))]
sleep: Option<humantime::Duration>,

#[structopt(short, long, group = "mode")]
schema: Option<std::path::PathBuf>,
Expand All @@ -24,11 +27,13 @@ fn run() -> proc_exit::ExitResult {
let args = Args::from_args();
let output = args
.output
.clone()
.unwrap_or_else(|| std::env::current_dir().unwrap());

if let Some(input) = args.input.as_deref() {
std::fs::create_dir_all(&output)?;
let dag = git_fixture::Dag::load(input).with_code(proc_exit::Code::CONFIG_ERR)?;
let mut dag = git_fixture::Dag::load(input).with_code(proc_exit::Code::CONFIG_ERR)?;
dag.sleep = dag.sleep.or_else(|| args.sleep.map(|s| s.into()));
dag.run(&output).with_code(proc_exit::Code::FAILURE)?;
} else if let Some(schema_path) = args.schema.as_deref() {
let schema = schemars::schema_for!(git_fixture::Dag);
Expand Down
4 changes: 4 additions & 0 deletions crates/git-fixture/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ pub struct Dag {
#[serde(default = "init_default")]
pub init: bool,
#[serde(default)]
#[serde(serialize_with = "humantime_serde::serialize")]
#[serde(deserialize_with = "humantime_serde::deserialize")]
pub sleep: Option<std::time::Duration>,
#[serde(default)]
pub events: Vec<Event>,
#[serde(skip)]
pub import_root: std::path::PathBuf,
Expand Down
19 changes: 14 additions & 5 deletions src/bin/git-stack/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,16 @@ pub fn stack(args: &crate::args::Args, colored_stdout: bool) -> proc_exit::ExitR

fn plan_rebase(state: &State, stack: &StackState) -> eyre::Result<git_stack::git::Script> {
let mut graphed_branches = stack.graphed_branches();
let mut root = git_stack::graph::Node::new(state.head_commit.clone(), &mut graphed_branches);
let base_commit = state
.repo
.find_commit(stack.base.id)
.expect("base branch is valid");
let mut root = git_stack::graph::Node::new(base_commit, &mut graphed_branches);
root = root.extend_branches(&state.repo, graphed_branches)?;
git_stack::graph::protect_branches(&mut root, &state.repo, &state.protected_branches);

git_stack::graph::rebase_branches(&mut root, stack.onto.id);
git_stack::graph::drop_by_tree_id(&mut root);

let script = git_stack::graph::to_script(&root);

Expand Down Expand Up @@ -374,14 +379,18 @@ fn show(state: &State, colored_stdout: bool) -> eyre::Result<()> {
.iter()
.map(|stack| -> eyre::Result<git_stack::graph::Node> {
let mut graphed_branches = stack.graphed_branches();
let mut root =
git_stack::graph::Node::new(state.head_commit.clone(), &mut graphed_branches);
let base_commit = state
.repo
.find_commit(stack.base.id)
.expect("base branch is valid");
let mut root = git_stack::graph::Node::new(base_commit, &mut graphed_branches);
root = root.extend_branches(&state.repo, graphed_branches)?;
git_stack::graph::protect_branches(&mut root, &state.repo, &state.protected_branches);

if state.dry_run {
// Show as-if we performed all mutations
git_stack::graph::rebase_branches(&mut root, stack.onto.id);
git_stack::graph::drop_by_tree_id(&mut root);
}

eyre::Result::Ok(root)
Expand Down Expand Up @@ -680,7 +689,7 @@ fn drop_branches(
if branch.name == potential_head {
continue;
} else if head_branch_name == Some(branch.name.as_str()) {
// Dom't leave HEAD detached but instead switch to the branch we pulled
// Don't leave HEAD detached but instead switch to the branch we pulled
log::trace!("git switch {}", potential_head);
if !dry_run {
repo.switch(potential_head)?;
Expand Down Expand Up @@ -1187,7 +1196,7 @@ fn format_commit_status<'d>(
if node.action.is_protected() {
format!("")
} else if node.action.is_delete() {
format!("{} ", palette.warn.paint("(drop)"))
format!("{} ", palette.error.paint("(drop)"))
} else if 1 < repo
.raw()
.find_commit(node.local_commit.id)
Expand Down
Loading