Skip to content

Commit

Permalink
cli: move optimization for no-op rebases into MutableRepo (#111)
Browse files Browse the repository at this point in the history
Certain commands should never rewrite commits, or they take care of
rebasing descendants themselves. We have an optimization in
`commands.rs` for those commands, so they skip the usual automatic
rebasing before committing the transaction. That's risky to have to
remember and `MutableRepo` already knows if any commits have been
rewritten (that wasn't the case before, in the Evolution-based
code). So let's just have `MutableRepo` do the check instead.
  • Loading branch information
martinvonz committed Mar 24, 2022
1 parent d7b60c9 commit b0b92cb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
4 changes: 4 additions & 0 deletions lib/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,10 @@ impl MutableRepo {
}

pub fn rebase_descendants(&mut self, settings: &UserSettings) -> usize {
if self.rewritten_commits.is_empty() && self.abandoned_commits.is_empty() {
// Optimization
return 0;
}
let mut rebaser = self.create_descendant_rebaser(settings);
rebaser.rebase_all();
rebaser.rebased().len()
Expand Down
23 changes: 6 additions & 17 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,6 @@ struct WorkspaceCommandHelper {
may_update_working_copy: bool,
working_copy_shared_with_git: bool,
working_copy_committed: bool,
// Whether to rebase descendants when the transaction finishes. This should generally be true
// for commands that rewrite commits.
rebase_descendants: bool,
}

impl WorkspaceCommandHelper {
Expand All @@ -278,7 +275,6 @@ impl WorkspaceCommandHelper {
may_update_working_copy,
working_copy_shared_with_git,
working_copy_committed: false,
rebase_descendants: true,
};
if working_copy_shared_with_git && may_update_working_copy {
helper.import_git_refs_and_head(maybe_git_repo.as_ref().unwrap())?;
Expand Down Expand Up @@ -348,11 +344,6 @@ impl WorkspaceCommandHelper {
Ok(())
}

fn rebase_descendants(mut self, value: bool) -> Self {
self.rebase_descendants = value;
self
}

fn repo(&self) -> &Arc<ReadonlyRepo> {
&self.repo
}
Expand Down Expand Up @@ -657,11 +648,9 @@ impl WorkspaceCommandHelper {
writeln!(ui, "Nothing changed.")?;
return Ok(());
}
if self.rebase_descendants {
let num_rebased = mut_repo.rebase_descendants(ui.settings());
if num_rebased > 0 {
writeln!(ui, "Rebased {} descendant commits", num_rebased)?;
}
let num_rebased = mut_repo.rebase_descendants(ui.settings());
if num_rebased > 0 {
writeln!(ui, "Rebased {} descendant commits", num_rebased)?;
}
if self.working_copy_shared_with_git {
self.export_head_to_git(mut_repo)?;
Expand Down Expand Up @@ -3272,7 +3261,7 @@ don't make any changes, then the operation will be aborted.",
}

fn cmd_split(ui: &mut Ui, command: &CommandHelper, args: &SplitArgs) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?.rebase_descendants(false);
let mut workspace_command = command.workspace_helper(ui)?;
let commit = workspace_command.resolve_single_rev(ui, &args.revision)?;
workspace_command.check_rewriteable(&commit)?;
let repo = workspace_command.repo();
Expand Down Expand Up @@ -3385,7 +3374,7 @@ fn cmd_merge(ui: &mut Ui, command: &CommandHelper, args: &MergeArgs) -> Result<(
}

fn cmd_rebase(ui: &mut Ui, command: &CommandHelper, args: &RebaseArgs) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?.rebase_descendants(false);
let mut workspace_command = command.workspace_helper(ui)?;
let mut new_parents = vec![];
for revision_str in &args.destination {
let destination = workspace_command.resolve_single_rev(ui, revision_str)?;
Expand Down Expand Up @@ -3502,7 +3491,7 @@ fn is_fast_forward(repo: RepoRef, branch_name: &str, new_target_id: &CommitId) -
}

fn cmd_branch(ui: &mut Ui, command: &CommandHelper, args: &BranchArgs) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?.rebase_descendants(false);
let mut workspace_command = command.workspace_helper(ui)?;
let branch_name = &args.name;
if args.delete {
if workspace_command
Expand Down

0 comments on commit b0b92cb

Please sign in to comment.