Skip to content

Commit b3ea95f

Browse files
committed
squash: fix -A -B behavior
and add the corresponding test. The children of the new commit were not properly rebased on their parents. fix: #7636
1 parent 39c73e4 commit b3ea95f

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
3030
* `jj metaedit --author-timestamp` twice with the same value no longer
3131
edits the change twice in some cases.
3232

33+
* `jj squash`: fixed improper revision rebase when both `--insert-after` and
34+
`--insert-before` were used.
35+
3336
## [0.34.0] - 2025-10-01
3437

3538
### Release highlights

cli/src/commands/squash.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
use std::collections::HashMap;
16+
use std::iter::once;
1617

1718
use clap_complete::ArgValueCandidates;
1819
use clap_complete::ArgValueCompleter;
@@ -273,11 +274,21 @@ pub(crate) fn cmd_squash(
273274
.write()?;
274275
let mut rewritten = HashMap::new();
275276
tx.repo_mut()
276-
.transform_descendants(child_ids, async |mut rewriter| {
277+
.transform_descendants(child_ids.clone(), async |mut rewriter| {
277278
let old_commit_id = rewriter.old_commit().id().clone();
278279
for parent_id in &parent_ids {
279280
rewriter.replace_parent(parent_id, [commit.id()]);
280281
}
282+
let new_parents = rewriter.new_parents();
283+
if child_ids.contains(&old_commit_id) && !new_parents.contains(commit.id()) {
284+
rewriter.set_new_parents(
285+
new_parents
286+
.iter()
287+
.cloned()
288+
.chain(once(commit.id().clone()))
289+
.collect(),
290+
);
291+
}
281292
let new_commit = rewriter.rebase().await?.write()?;
282293
rewritten.insert(old_commit_id, new_commit);
283294
num_rebased += 1;

cli/tests/test_squash_command.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,6 +2211,41 @@ fn test_squash_to_new_commit() {
22112211
-- operation a8bb9104802c new empty commit
22122212
[EOF]
22132213
");
2214+
2215+
// --before and --after together
2216+
work_dir.run_jj(["op", "restore", &setup_opid]).success();
2217+
let output = work_dir.run_jj([
2218+
"squash",
2219+
"-m",
2220+
"file 3&4",
2221+
"-f",
2222+
"kkmpptxzrspx::",
2223+
"--insert-after",
2224+
"root()",
2225+
"--insert-before",
2226+
"rlvkpnrzqnoo",
2227+
]);
2228+
insta::assert_snapshot!(output, @r"
2229+
------- stderr -------
2230+
Created new commit pyoswmwk d5aa6638 file 3&4
2231+
Rebased 1 descendant commits
2232+
Working copy (@) now at: yqnpwwmq 68513612 (empty) (no description set)
2233+
Parent commit (@-) : rlvkpnrz ed79225c file2
2234+
[EOF]
2235+
");
2236+
2237+
insta::assert_snapshot!(get_log_with_summary(&work_dir), @r"
2238+
@ yqnpwwmqtwyk
2239+
○ rlvkpnrzqnoo file2
2240+
├─╮ A file2
2241+
│ ○ pyoswmwkkqyt file 3&4
2242+
│ │ A file3
2243+
│ │ A file4
2244+
○ │ qpvuntsmwlqt file1
2245+
├─╯ A file1
2246+
◆ zzzzzzzzzzzz
2247+
[EOF]
2248+
");
22142249
}
22152250

22162251
#[must_use]

0 commit comments

Comments
 (0)