mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-28 23:32:41 +00:00
cli: make rebase
and abandon
commands rebase descendants using new helper
This makes `jj rebase` and `jj abandon` rebase descendants using `MutableRepo::create_descendant_rebaser()`, except that `jj rebase -r` needs to be special-case since it doesn't rebase descendants onto the rewritten commit.
This commit is contained in:
parent
4883261c2a
commit
bbe20e0ea6
1 changed files with 21 additions and 27 deletions
|
@ -2164,36 +2164,32 @@ fn cmd_abandon(
|
||||||
sub_matches: &ArgMatches,
|
sub_matches: &ArgMatches,
|
||||||
) -> Result<(), CommandError> {
|
) -> Result<(), CommandError> {
|
||||||
let mut repo_command = command.repo_helper(ui)?;
|
let mut repo_command = command.repo_helper(ui)?;
|
||||||
let predecessors =
|
let to_abandon = repo_command.resolve_revset(ui, sub_matches.value_of("revision").unwrap())?;
|
||||||
repo_command.resolve_revset(ui, sub_matches.value_of("revision").unwrap())?;
|
repo_command.check_non_empty(&to_abandon)?;
|
||||||
repo_command.check_non_empty(&predecessors)?;
|
for commit in &to_abandon {
|
||||||
for predecessor in &predecessors {
|
repo_command.check_rewriteable(commit)?;
|
||||||
repo_command.check_rewriteable(predecessor)?;
|
|
||||||
}
|
}
|
||||||
let repo = repo_command.repo();
|
let repo = repo_command.repo();
|
||||||
let transaction_description = if predecessors.len() == 1 {
|
let transaction_description = if to_abandon.len() == 1 {
|
||||||
format!("abandon commit {}", predecessors[0].id().hex())
|
format!("abandon commit {}", to_abandon[0].id().hex())
|
||||||
} else {
|
} else {
|
||||||
format!(
|
format!(
|
||||||
"abandon commit {} and {} more",
|
"abandon commit {} and {} more",
|
||||||
predecessors[0].id().hex(),
|
to_abandon[0].id().hex(),
|
||||||
predecessors.len() - 1
|
to_abandon.len() - 1
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
let mut tx = repo_command.start_transaction(&transaction_description);
|
let mut tx = repo_command.start_transaction(&transaction_description);
|
||||||
let abandoned = predecessors
|
for commit in to_abandon {
|
||||||
.iter()
|
tx.mut_repo().record_abandoned_commit(commit.id().clone());
|
||||||
.map(|commit| commit.id().clone())
|
CommitBuilder::for_rewrite_from(ui.settings(), repo.store(), &commit)
|
||||||
.collect();
|
|
||||||
for predecessor in predecessors {
|
|
||||||
CommitBuilder::for_rewrite_from(ui.settings(), repo.store(), &predecessor)
|
|
||||||
.set_pruned(true)
|
.set_pruned(true)
|
||||||
.write_to_repo(tx.mut_repo());
|
.write_to_repo(tx.mut_repo());
|
||||||
}
|
}
|
||||||
let mut rebaser = DescendantRebaser::new(ui.settings(), tx.mut_repo(), hashmap! {}, abandoned);
|
let mut rebaser = tx.mut_repo().create_descendant_rebaser(ui.settings());
|
||||||
rebaser.rebase_all();
|
rebaser.rebase_all();
|
||||||
let num_rebased = rebaser.rebased().len();
|
let num_rebased = rebaser.rebased().len();
|
||||||
if num_rebased > 0 && num_rebased != 0 {
|
if num_rebased > 0 {
|
||||||
writeln!(
|
writeln!(
|
||||||
ui,
|
ui,
|
||||||
"Rebased {} descendant commits onto parents of abandoned commits",
|
"Rebased {} descendant commits onto parents of abandoned commits",
|
||||||
|
@ -2628,15 +2624,8 @@ fn cmd_rebase(
|
||||||
old_commit.id().hex()
|
old_commit.id().hex()
|
||||||
));
|
));
|
||||||
repo_command.check_rewriteable(&old_commit)?;
|
repo_command.check_rewriteable(&old_commit)?;
|
||||||
let new_commit = rebase_commit(ui.settings(), tx.mut_repo(), &old_commit, &parents);
|
rebase_commit(ui.settings(), tx.mut_repo(), &old_commit, &parents);
|
||||||
let mut rebaser = DescendantRebaser::new(
|
let mut rebaser = tx.mut_repo().create_descendant_rebaser(ui.settings());
|
||||||
ui.settings(),
|
|
||||||
tx.mut_repo(),
|
|
||||||
hashmap! {
|
|
||||||
old_commit.id().clone() => new_commit.id().clone()
|
|
||||||
},
|
|
||||||
hashset! {},
|
|
||||||
);
|
|
||||||
rebaser.rebase_all();
|
rebaser.rebase_all();
|
||||||
let num_rebased = rebaser.rebased().len() + 1;
|
let num_rebased = rebaser.rebased().len() + 1;
|
||||||
writeln!(ui, "Rebased {} commits", num_rebased)?;
|
writeln!(ui, "Rebased {} commits", num_rebased)?;
|
||||||
|
@ -2648,6 +2637,11 @@ fn cmd_rebase(
|
||||||
repo_command.start_transaction(&format!("rebase commit {}", old_commit.id().hex()));
|
repo_command.start_transaction(&format!("rebase commit {}", old_commit.id().hex()));
|
||||||
repo_command.check_rewriteable(&old_commit)?;
|
repo_command.check_rewriteable(&old_commit)?;
|
||||||
rebase_commit(ui.settings(), tx.mut_repo(), &old_commit, &parents);
|
rebase_commit(ui.settings(), tx.mut_repo(), &old_commit, &parents);
|
||||||
|
// Manually use DescendantRebaser instead of
|
||||||
|
// mut_repo.create_descendant_rebaser() because we don't want to rebase children
|
||||||
|
// onto the rewritten commit. (But we still want to record the commit as
|
||||||
|
// rewritten so branches and the working copy get updated to the
|
||||||
|
// rewritten commit.)
|
||||||
let mut rebaser = DescendantRebaser::new(
|
let mut rebaser = DescendantRebaser::new(
|
||||||
ui.settings(),
|
ui.settings(),
|
||||||
tx.mut_repo(),
|
tx.mut_repo(),
|
||||||
|
@ -2656,7 +2650,7 @@ fn cmd_rebase(
|
||||||
);
|
);
|
||||||
rebaser.rebase_all();
|
rebaser.rebase_all();
|
||||||
let num_rebased = rebaser.rebased().len();
|
let num_rebased = rebaser.rebased().len();
|
||||||
if num_rebased != 0 {
|
if num_rebased > 0 {
|
||||||
writeln!(
|
writeln!(
|
||||||
ui,
|
ui,
|
||||||
"Also rebased {} descendant commits onto parent of rebased commit",
|
"Also rebased {} descendant commits onto parent of rebased commit",
|
||||||
|
|
Loading…
Reference in a new issue