Skip to content

Commit

Permalink
Add test for stack pruning
Browse files Browse the repository at this point in the history
  • Loading branch information
krlvi committed Oct 28, 2024
1 parent dc07cd1 commit 7c61a9a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 9 deletions.
10 changes: 1 addition & 9 deletions crates/gitbutler-stack/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,16 +443,8 @@ impl Stack {
return Err(anyhow!("Stack has not been initialized"));
}
self.updated_timestamp_ms = gitbutler_time::time::now_ms();
let repo = ctx.repository();
let state = branch_state(ctx);
let default_target = state.get_default_target()?;
let merge_base = repo.find_commit(repo.merge_base(self.head(), default_target.sha)?)?;
let mut commit_ids: Vec<CommitOrChangeId> = repo
.log(self.head(), LogUntil::Commit(merge_base.id()), false)?
.into_iter()
.map(|c| c.into())
.collect_vec();
commit_ids.insert(0, merge_base.into());
let commit_ids = stack_patches(ctx, &state, self.head(), true)?;
let new_heads = self
.heads
.iter()
Expand Down
77 changes: 77 additions & 0 deletions crates/gitbutler-stack/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ fn add_multiple_series() -> Result<()> {
head_names(&test_ctx),
vec!["head_1", "head_2", "a-branch-2", "head_4"]
);

// prune is noop
let before_prune = test_ctx.branch.heads.clone();
test_ctx.branch.prune_integrated_heads(&ctx)?;
assert_eq!(before_prune, test_ctx.branch.heads);
Ok(())
}

Expand Down Expand Up @@ -1086,6 +1091,78 @@ fn set_legacy_refname_pushed() -> Result<()> {
Ok(())
}

#[test]
fn prune_heads_noop() -> Result<()> {
let (ctx, _temp_dir) = command_ctx("multiple-commits")?;
let mut test_ctx = test_ctx(&ctx)?;
test_ctx.branch.initialize(&ctx)?;
let initial_state = test_ctx.branch.heads.clone();
test_ctx.branch.prune_integrated_heads(&ctx)?;
assert_eq!(initial_state, test_ctx.branch.heads);
// Assert persisted
assert_eq!(
test_ctx.branch,
test_ctx.handle.get_branch(test_ctx.branch.id)?
);
Ok(())
}

#[test]
fn prune_heads_success() -> Result<()> {
let (ctx, _temp_dir) = command_ctx("multiple-commits")?;
let mut test_ctx = test_ctx(&ctx)?;
test_ctx.branch.initialize(&ctx)?;
let initial_state = test_ctx.branch.heads.clone();
// adding a commit that is not in the stack
test_ctx.branch.heads.insert(
0,
PatchReference {
target: test_ctx.other_commits.first().cloned().unwrap().into(),
name: "foo".to_string(),
description: None,
},
);
assert_eq!(test_ctx.branch.heads.len(), 2);
test_ctx.branch.prune_integrated_heads(&ctx)?;
assert_eq!(test_ctx.branch.heads.len(), 1);
assert_eq!(initial_state, test_ctx.branch.heads);
// Assert persisted
assert_eq!(
test_ctx.branch,
test_ctx.handle.get_branch(test_ctx.branch.id)?
);
Ok(())
}

#[test]
fn does_not_prune_head_on_merge_base() -> Result<()> {
let (ctx, _temp_dir) = command_ctx("multiple-commits")?;
let mut test_ctx = test_ctx(&ctx)?;
test_ctx.branch.initialize(&ctx)?;
let merge_base = ctx.repository().find_commit(
ctx.repository()
.merge_base(test_ctx.branch.head(), test_ctx.default_target.sha)?,
)?;
test_ctx.branch.add_series(
&ctx,
PatchReference {
target: merge_base.into(),
name: "bottom".to_string(),
description: None,
},
None,
)?;
let initial_state = test_ctx.branch.heads.clone();
test_ctx.branch.prune_integrated_heads(&ctx)?;
assert_eq!(initial_state, test_ctx.branch.heads);
// Assert persisted
assert_eq!(
test_ctx.branch,
test_ctx.handle.get_branch(test_ctx.branch.id)?
);
Ok(())
}

fn command_ctx(name: &str) -> Result<(CommandContext, TempDir)> {
gitbutler_testsupport::writable::fixture("stacking.sh", name)
}
Expand Down

0 comments on commit 7c61a9a

Please sign in to comment.