Skip to content

Commit de0b55b

Browse files
authored
Merge pull request #9235 from gitbutlerapp/kv-branch-29
Implement the squash case of the but rub command
2 parents c537146 + 6a0b6aa commit de0b55b

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

crates/but/src/rub/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use gitbutler_command_context::CommandContext;
77
use gitbutler_project::Project;
88
mod amend;
99
mod assign;
10+
mod squash;
1011
mod undo;
1112

1213
use crate::id::CliId;
@@ -46,8 +47,8 @@ pub(crate) fn handle(
4647
bail!(makes_no_sense_error(&source, &target))
4748
}
4849
(CliId::Commit { oid }, CliId::Unassigned) => undo::commit(ctx, oid),
49-
(CliId::Commit { .. }, CliId::Commit { .. }) => {
50-
bail!(not_implemented("Squash commits", &source, &target))
50+
(CliId::Commit { oid: source }, CliId::Commit { oid: destination }) => {
51+
squash::commits(ctx, source, destination)
5152
}
5253
(CliId::Commit { .. }, CliId::Branch { .. }) => {
5354
bail!(not_implemented("Move commit to branch", &source, &target))

crates/but/src/rub/squash.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use gitbutler_command_context::CommandContext;
2+
use gitbutler_oxidize::ObjectIdExt;
3+
use gix::ObjectId;
4+
5+
use super::undo::stack_id_by_commit_id;
6+
7+
pub(crate) fn commits(
8+
ctx: &mut CommandContext,
9+
source: &ObjectId,
10+
destination: &ObjectId,
11+
) -> anyhow::Result<()> {
12+
let source_stack = stack_id_by_commit_id(ctx, source)?;
13+
let destination_stack = stack_id_by_commit_id(ctx, destination)?;
14+
if source_stack != destination_stack {
15+
anyhow::bail!("Cannot squash commits from different stacks");
16+
}
17+
18+
gitbutler_branch_actions::squash_commits(
19+
ctx,
20+
source_stack,
21+
vec![source.to_git2()],
22+
destination.to_git2(),
23+
)?;
24+
Ok(())
25+
}

crates/but/src/rub/undo.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ pub(crate) fn commit(ctx: &mut CommandContext, oid: &ObjectId) -> anyhow::Result
99
Ok(())
1010
}
1111

12-
fn stack_id_by_commit_id(ctx: &CommandContext, oid: &ObjectId) -> anyhow::Result<StackId> {
12+
pub(crate) fn stack_id_by_commit_id(
13+
ctx: &CommandContext,
14+
oid: &ObjectId,
15+
) -> anyhow::Result<StackId> {
1316
let stacks = crate::log::stacks(ctx)?
1417
.iter()
1518
.map(|s| crate::log::stack_details(ctx, s.id).map(|d| (s.id, d)))

0 commit comments

Comments
 (0)