Skip to content

Commit

Permalink
cli: make jj workspace add preserve all parents of current workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
martinvonz committed Oct 30, 2023
1 parent 2d3fe7e commit 23a0bab
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* The new template keywords `local_branches`/`remote_branches` are added to show
only local/remote branches.

* `jj workspace add` now preserves all parents of the old working-copy commit
instead of just the first one.

### Fixed bugs

* Updating the working copy to a commit where a file that's currently ignored
Expand Down
19 changes: 13 additions & 6 deletions cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1698,23 +1698,30 @@ fn cmd_workspace_add(
&name
));

let new_wc_commit = if let Some(specific_rev) = &args.revision {
old_workspace_command.resolve_single_rev(specific_rev, ui)?
let parents = if let Some(specific_rev) = &args.revision {
vec![old_workspace_command.resolve_single_rev(specific_rev, ui)?]
} else {
// Check out a parent of the current workspace's working-copy commit, or the
// Check out parents of the current workspace's working-copy commit, or the
// root if there is no working-copy commit in the current workspace.
if let Some(old_wc_commit_id) = tx
.base_repo()
.view()
.get_wc_commit_id(old_workspace_command.workspace_id())
{
tx.repo().store().get_commit(old_wc_commit_id)?.parents()[0].clone()
tx.repo().store().get_commit(old_wc_commit_id)?.parents()
} else {
tx.repo().store().root_commit()
vec![tx.repo().store().root_commit()]
}
};

tx.check_out(&new_wc_commit)?;
let tree = merge_commit_trees(tx.repo(), &parents)?;
let parent_ids = parents.iter().map(|c| c.id().clone()).collect_vec();
let new_wc_commit = tx
.mut_repo()
.new_commit(command.settings(), parent_ids, tree.id())
.write()?;

tx.edit(&new_wc_commit)?;
tx.finish(ui)?;
Ok(())
}
Expand Down
35 changes: 35 additions & 0 deletions cli/tests/test_workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,41 @@ fn test_workspaces_add_second_workspace() {
"###);
}

/// Test adding a second workspace while the current workspace is editing a
/// merge
#[test]
fn test_workspaces_add_second_workspace_on_merge() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["init", "--git", "main"]);
let main_path = test_env.env_root().join("main");

test_env.jj_cmd_ok(&main_path, &["describe", "-m=left"]);
test_env.jj_cmd_ok(&main_path, &["new", "@-", "-m=right"]);
test_env.jj_cmd_ok(&main_path, &["new", "all:@-+", "-m=merge"]);

let stdout = test_env.jj_cmd_success(&main_path, &["workspace", "list"]);
insta::assert_snapshot!(stdout, @r###"
default: zsuskuln 21a0ea6d (empty) merge
"###);

test_env.jj_cmd_ok(
&main_path,
&["workspace", "add", "--name", "second", "../secondary"],
);

// The new workspace's working-copy commit shares all parents with the old one.
insta::assert_snapshot!(get_log_output(&test_env, &main_path), @r###"
◉ 6d4c2b8ab610148410b6a346d900cb7ad6298ede second@
├─╮
│ │ @ 21a0ea6d1c86c413cb30d7f7d216a74754b148c4 default@
╭─┬─╯
│ ◉ 09ba8d9dfa21b8572c2361e6947ca024d8f0a198
◉ │ 1694f2ddf8ecf9e55ca3cd9554bc0654186b07e0
├─╯
◉ 0000000000000000000000000000000000000000
"###);
}

/// Test adding a workspace, but at a specific revision using '-r'
#[test]
fn test_workspaces_add_workspace_at_revision() {
Expand Down

0 comments on commit 23a0bab

Please sign in to comment.