Skip to content

Commit

Permalink
feat(move): Add --fixup to squash moved commits into the destination
Browse files Browse the repository at this point in the history
  • Loading branch information
claytonrcarter committed Oct 11, 2022
1 parent 2abfef4 commit e466d38
Show file tree
Hide file tree
Showing 5 changed files with 939 additions and 3 deletions.
4 changes: 4 additions & 0 deletions git-branchless-lib/src/core/rewrite/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,10 @@ mod in_memory {
"Applying patch for commit: {}",
fixup_commit_description
));
// TODO it's possible that we could skip much of this
// loop if, instead of creating a commit and then
// immediately (repeatedly) amending and re-creating it,
// we just amended the tree and then commited it once
let commit_tree = repo.amend_fast(
&rebased_commit,
&AmendFastOptions::FromCommit {
Expand Down
2 changes: 2 additions & 0 deletions git-branchless/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ fn do_main_and_drop_locals() -> eyre::Result<i32> {
dest,
base,
exact,
fixup,
insert,
move_options,
} => r#move::r#move(
Expand All @@ -230,6 +231,7 @@ fn do_main_and_drop_locals() -> eyre::Result<i32> {
dest,
base,
exact,
fixup,
insert,
&move_options,
)?,
Expand Down
25 changes: 22 additions & 3 deletions git-branchless/src/commands/move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub fn r#move(
dest: Option<Revset>,
bases: Vec<Revset>,
exacts: Vec<Revset>,
fixup: bool,
insert: bool,
move_options: &MoveOptions,
) -> eyre::Result<ExitCode> {
Expand Down Expand Up @@ -255,7 +256,7 @@ pub fn r#move(
let commits_to_move = commits_to_move.union(&union_all(
&exact_components.values().cloned().collect::<Vec<_>>(),
));
let commits_to_move = if insert {
let commits_to_move = if insert || fixup {
commits_to_move.union(&dag.query().children(CommitSet::from(dest_oid))?)
} else {
commits_to_move
Expand All @@ -274,7 +275,18 @@ pub fn r#move(

let source_roots = dag.query().roots(source_oids.clone())?;
for source_root in commit_set_to_vec(&source_roots)? {
builder.move_subtree(source_root, vec![dest_oid])?;
if fixup {
let commits = dag
.query()
.descendants(CommitSet::from(source_root))?
.difference(&dag.obsolete_commits);
let commits = commit_set_to_vec(&commits)?;
for commit in commits.iter() {
builder.fixup_commit(*commit, dest_oid)?;
}
} else {
builder.move_subtree(source_root, vec![dest_oid])?;
}
}

let component_roots: CommitSet = exact_components.keys().cloned().collect();
Expand Down Expand Up @@ -383,7 +395,14 @@ pub fn r#move(
}
}

builder.move_subtree(component_root, vec![component_dest_oid])?;
if fixup {
let commits = commit_set_to_vec(component)?;
for commit in commits.iter() {
builder.fixup_commit(*commit, dest_oid)?;
}
} else {
builder.move_subtree(component_root, vec![component_dest_oid])?;
}
}

if insert {
Expand Down
4 changes: 4 additions & 0 deletions git-branchless/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ pub enum Command {
#[clap(value_parser, short = 'd', long = "dest")]
dest: Option<Revset>,

/// Combine the moved commits and squash into the destination commit.
#[clap(action, short = 'F', long = "fixup", conflicts_with = "insert")]
fixup: bool,

/// Insert the subtree between the destination and it's children, if any.
/// Only supported if the moved subtree has a single head.
#[clap(action, short = 'I', long = "insert")]
Expand Down
Loading

0 comments on commit e466d38

Please sign in to comment.