cli rebase: do not allow -r --skip-empty to drop emptied descendants

This follows up on @matts1 's #2609.

We still allow the `-r` commit to become empty. I would be more comfortable if
there was a test for that, but I haven't done that (yet?) and it seems pretty
safe. If that's a problem, I'm happy to forbid `-r --skip-empty` entirely,
since it is far less useful than `-s --skip-empty` or `-b --skip-empty`.

I think it is undesired to abandon emptied descendants. As far as descendants
of `A` are concerned, `jj rebase -r A` should be equivalent to `jj abandon A`,
and `jj abandon` does not remove emptied commits. It also doesn't seem very
useful to do that, since I think descendant commits of an abandoned (or moved
with `-r`) commit only become empty in pathological cases.

Additionally, if we did want -r to empty descendants of `A`, we'd have to add
thorough tests and possibly improve the algorithm. I want to refactor `rebase
-r` and add features to it, and having to consider cases of commits becoming
abandoned makes everything harder.

For example, if we have

```
root -> A -> B -> C
```

and `jj rebase -r A -d C` empties commit `B` (or `C`), I do not know whether
the current algorithm will work correctly. It seems possible that it would, but
that depends on the fact that empty merge commits are not abandoned for
descendants. That seems dangerous to rely on without tests.

I hope (but can't promise) that in the near future, making DescendantRebaser
return more information  should help make it possible to create such
functionality in a more robust way. I am likely to attempt this as part of
implementing `-r --after`.
This commit is contained in:
Ilya Grigoriev 2023-11-25 21:43:25 -08:00
parent c78425cf65
commit 3967f637dc
2 changed files with 12 additions and 21 deletions

View file

@ -22,7 +22,7 @@ use jj_lib::backend::{CommitId, ObjectId};
use jj_lib::commit::Commit;
use jj_lib::repo::{ReadonlyRepo, Repo};
use jj_lib::revset::{RevsetExpression, RevsetIteratorExt};
use jj_lib::rewrite::{rebase_commit_with_options, EmptyBehaviour, RebaseOptions};
use jj_lib::rewrite::{rebase_commit, rebase_commit_with_options, EmptyBehaviour, RebaseOptions};
use jj_lib::settings::UserSettings;
use tracing::instrument;
@ -357,22 +357,15 @@ fn rebase_revision(
rebased_commit_ids.insert(
child_commit.id().clone(),
rebase_commit_with_options(
settings,
tx.mut_repo(),
child_commit,
&new_child_parents,
rebase_options,
)?
.id()
.clone(),
rebase_commit(settings, tx.mut_repo(), child_commit, &new_child_parents)?
.id()
.clone(),
);
}
// Now, rebase the descendants of the children.
rebased_commit_ids.extend(
tx.mut_repo()
.rebase_descendants_return_map(settings, rebase_options.clone())?,
);
// TODO(ilyagr): Consider making it possible for these descendants to become
// emptied, like --skip_empty. This would require writing careful tests.
rebased_commit_ids.extend(tx.mut_repo().rebase_descendants_return_map(settings)?);
let num_rebased_descendants = rebased_commit_ids.len();
// We now update `new_parents` to account for the rebase of all of
@ -405,11 +398,7 @@ fn rebase_revision(
&new_parents,
rebase_options,
)?;
debug_assert_eq!(
tx.mut_repo()
.rebase_descendants_with_options(settings, rebase_options.clone())?,
0
);
debug_assert_eq!(tx.mut_repo().rebase_descendants(settings)?, 0);
if num_rebased_descendants > 0 {
writeln!(

View file

@ -881,10 +881,12 @@ impl MutableRepo {
pub fn rebase_descendants_return_map(
&mut self,
settings: &UserSettings,
options: RebaseOptions,
) -> Result<HashMap<CommitId, CommitId>, TreeMergeError> {
Ok(self
.rebase_descendants_return_rebaser(settings, options)?
// We do not set RebaseOptions here, since this function does not currently return
// enough information to describe the results of a rebase if some commits got
// abandoned
.rebase_descendants_return_rebaser(settings, Default::default())?
.map_or(HashMap::new(), |rebaser| rebaser.rebased().clone()))
}