diff --git a/CHANGELOG.md b/CHANGELOG.md index 45693ab275..8cdc0116ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#2971](https://github.com/martinvonz/jj/issues/2971)). This may become the default depending on feedback. +* When creating a new workspace, the sparse patterns are now copied over from + the current workspace. + ### Fixed bugs * On Windows, symlinks in the repo are now materialized as regular files in the diff --git a/cli/src/commands/workspace.rs b/cli/src/commands/workspace.rs index 5158ab8241..0ea79dd77c 100644 --- a/cli/src/commands/workspace.rs +++ b/cli/src/commands/workspace.rs @@ -44,6 +44,8 @@ use crate::ui::Ui; /// Each workspace has its own working-copy commit. When you have more than one /// workspace attached to a repo, they are indicated by `@` in /// `jj log`. +/// +/// Each workspace also has own sparse patterns. #[derive(Subcommand, Clone, Debug)] pub(crate) enum WorkspaceCommand { Add(WorkspaceAddArgs), @@ -54,6 +56,8 @@ pub(crate) enum WorkspaceCommand { } /// Add a workspace +/// +/// Sparse patterns will be copied over from the current workspace. #[derive(clap::Args, Clone, Debug)] pub(crate) struct WorkspaceAddArgs { /// Where to create the new workspace @@ -167,7 +171,20 @@ fn cmd_workspace_add( .display() )?; + // Copy sparse patterns from workspace where the command was run let mut new_workspace_command = WorkspaceCommandHelper::new(ui, command, new_workspace, repo)?; + let (mut locked_ws, _wc_commit) = new_workspace_command.start_working_copy_mutation()?; + let sparse_patterns = old_workspace_command + .working_copy() + .sparse_patterns()? + .to_vec(); + locked_ws + .locked_wc() + .set_sparse_patterns(sparse_patterns) + .map_err(|err| internal_error_with_message("Failed to set sparse patterns", err))?; + let operation_id = locked_ws.locked_wc().old_operation_id().clone(); + locked_ws.finish(operation_id)?; + let mut tx = new_workspace_command.start_transaction(); // If no parent revisions are specified, create a working-copy commit based diff --git a/cli/tests/cli-reference@.md.snap b/cli/tests/cli-reference@.md.snap index 323117b30e..586b8c3db9 100644 --- a/cli/tests/cli-reference@.md.snap +++ b/cli/tests/cli-reference@.md.snap @@ -1888,6 +1888,8 @@ Workspaces let you add additional working copies attached to the same repo. A co Each workspace has its own working-copy commit. When you have more than one workspace attached to a repo, they are indicated by `@` in `jj log`. +Each workspace also has own sparse patterns. + **Usage:** `jj workspace ` ###### **Subcommands:** @@ -1904,6 +1906,8 @@ Each workspace has its own working-copy commit. When you have more than one work Add a workspace +Sparse patterns will be copied over from the current workspace. + **Usage:** `jj workspace add [OPTIONS] ` ###### **Arguments:** diff --git a/cli/tests/test_workspaces.rs b/cli/tests/test_workspaces.rs index 8e3047df9b..008eddbbad 100644 --- a/cli/tests/test_workspaces.rs +++ b/cli/tests/test_workspaces.rs @@ -69,6 +69,30 @@ fn test_workspaces_add_second_workspace() { "###); } +/// Test how sparse patterns are inherited +#[test] +fn test_workspaces_sparse_patterns() { + let test_env = TestEnvironment::default(); + test_env.jj_cmd_ok(test_env.env_root(), &["init", "--git", "ws1"]); + let ws1_path = test_env.env_root().join("ws1"); + let ws2_path = test_env.env_root().join("ws2"); + let ws3_path = test_env.env_root().join("ws3"); + + test_env.jj_cmd_ok(&ws1_path, &["sparse", "set", "--clear", "--add=foo"]); + test_env.jj_cmd_ok(&ws1_path, &["workspace", "add", "../ws2"]); + let stdout = test_env.jj_cmd_success(&ws2_path, &["sparse", "list"]); + insta::assert_snapshot!(stdout, @r###" + foo + "###); + test_env.jj_cmd_ok(&ws2_path, &["sparse", "set", "--add=bar"]); + test_env.jj_cmd_ok(&ws2_path, &["workspace", "add", "../ws3"]); + let stdout = test_env.jj_cmd_success(&ws3_path, &["sparse", "list"]); + insta::assert_snapshot!(stdout, @r###" + bar + foo + "###); +} + /// Test adding a second workspace while the current workspace is editing a /// merge #[test]