Skip to content

Commit

Permalink
abandon: add --restore-descendants flag
Browse files Browse the repository at this point in the history
  • Loading branch information
samueltardieu committed Sep 23, 2024
1 parent 3c852f5 commit 1343785
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
* `jj diffedit` now accepts a `--restore-descendants` flag. When used,
descendants of the edited commit will keep their original content.

* `jj abandon` now accepts a `--restore-descendants` flag. When used,
descendants of the abandoned commits will keep their original content.

### Fixed bugs

* Update working copy before reporting changes. This prevents errors during reporting
Expand Down
9 changes: 7 additions & 2 deletions cli/src/commands/abandon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub(crate) struct AbandonArgs {
/// Ignored (but lets you pass `-r` for consistency with other commands)
#[arg(short = 'r', hide = true, action = clap::ArgAction::Count)]
unused_revision: u8,
/// Do not modify the content of the children of the abandoned commits
#[arg(long)]
restore_descendants: bool,
}

#[instrument(skip_all)]
Expand All @@ -66,7 +69,8 @@ pub(crate) fn cmd_abandon(
for commit in &to_abandon {
tx.repo_mut().record_abandoned_commit(commit.id().clone());
}
let num_rebased = tx.repo_mut().rebase_descendants(command.settings())?;
let (num_rebased, extra_msg) =
tx.rebase_or_reparent(command.settings(), args.restore_descendants)?;

if let Some(mut formatter) = ui.status_formatter() {
if to_abandon.len() == 1 {
Expand All @@ -88,7 +92,8 @@ pub(crate) fn cmd_abandon(
if num_rebased > 0 {
writeln!(
formatter,
"Rebased {num_rebased} descendant commits onto parents of abandoned commits"
"Rebased {num_rebased} descendant commits onto parents of abandoned \
commits{extra_msg}",
)?;
}
}
Expand Down
1 change: 1 addition & 0 deletions cli/tests/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ If a working-copy commit gets abandoned, it will be given a new, empty commit. T
###### **Options:**
* `-s`, `--summary` — Do not print every abandoned commit on a separate line
* `--restore-descendants` — Do not modify the content of the children of the abandoned commits
Expand Down
36 changes: 36 additions & 0 deletions cli/tests/test_abandon_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,42 @@ fn test_double_abandon() {
"###);
}

#[test]
fn test_rebase_restore_descendants() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let repo_path = test_env.env_root().join("repo");

std::fs::write(repo_path.join("file"), "foo\n").unwrap();
test_env.jj_cmd_ok(&repo_path, &["new"]);
std::fs::write(repo_path.join("file"), "bar\n").unwrap();
test_env.jj_cmd_ok(&repo_path, &["new"]);
std::fs::write(repo_path.join("file"), "baz\n").unwrap();

// Remove the commit containing "bar"
let (stdout, stderr) = test_env.jj_cmd_ok(
&repo_path,
&["abandon", "-r", "@-", "--restore-descendants"],
);
insta::assert_snapshot!(stdout, @"");
insta::assert_snapshot!(stderr, @r#"
Abandoned commit rlvkpnrz 225adef1 (no description set)
Rebased 1 descendant commits onto parents of abandoned commits (while preserving their content)
Working copy now at: kkmpptxz a734deb0 (no description set)
Parent commit : qpvuntsm 485d52a9 (no description set)
"#);
let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "--git"]);
insta::assert_snapshot!(stdout, @r#"
diff --git a/file b/file
index 257cc5642c..76018072e0 100644
--- a/file
+++ b/file
@@ -1,1 +1,1 @@
-foo
+baz
"#);
}

fn get_log_output(test_env: &TestEnvironment, repo_path: &Path) -> String {
test_env.jj_cmd_success(
repo_path,
Expand Down

0 comments on commit 1343785

Please sign in to comment.