From 14d3bb85bc4d41681106e15fd778650695898a9f Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Wed, 17 Jul 2024 18:29:50 -0500 Subject: [PATCH] workspace: use cwd for printing relative path The user probably would expect the path to be relative to their current directory rather than the workspace root. For instance, if the user is in a child directory and runs `jj workspace add ../../name`, then they might be surprised if we printed "../name" instead of "../../name". --- cli/src/commands/workspace.rs | 3 +-- cli/tests/test_workspaces.rs | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/cli/src/commands/workspace.rs b/cli/src/commands/workspace.rs index 7add4413bc..a89c0bd870 100644 --- a/cli/src/commands/workspace.rs +++ b/cli/src/commands/workspace.rs @@ -169,8 +169,7 @@ fn cmd_workspace_add( writeln!( ui.status(), "Created workspace in \"{}\"", - file_util::relative_path(old_workspace_command.workspace_root(), &destination_path) - .display() + file_util::relative_path(command.cwd(), &destination_path).display() )?; // Copy sparse patterns from workspace where the command was run diff --git a/cli/tests/test_workspaces.rs b/cli/tests/test_workspaces.rs index ef1847fb7a..80516979e4 100644 --- a/cli/tests/test_workspaces.rs +++ b/cli/tests/test_workspaces.rs @@ -252,6 +252,42 @@ fn test_workspaces_add_workspace_multiple_revisions() { "###); } +#[test] +fn test_workspaces_add_workspace_from_subdir() { + let test_env = TestEnvironment::default(); + test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "main"]); + let main_path = test_env.env_root().join("main"); + let subdir_path = main_path.join("subdir"); + let secondary_path = test_env.env_root().join("secondary"); + + std::fs::create_dir(&subdir_path).unwrap(); + std::fs::write(subdir_path.join("file"), "contents").unwrap(); + test_env.jj_cmd_ok(&main_path, &["commit", "-m", "initial"]); + + let stdout = test_env.jj_cmd_success(&main_path, &["workspace", "list"]); + insta::assert_snapshot!(stdout, @r###" + default: rlvkpnrz e1038e77 (empty) (no description set) + "###); + + // Create workspace while in sub-directory of current workspace + let (stdout, stderr) = + test_env.jj_cmd_ok(&subdir_path, &["workspace", "add", "../../secondary"]); + insta::assert_snapshot!(stdout.replace('\\', "/"), @""); + insta::assert_snapshot!(stderr.replace('\\', "/"), @r###" + Created workspace in "../../secondary" + Working copy now at: rzvqmyuk 7ad84461 (empty) (no description set) + Parent commit : qpvuntsm a3a43d9e initial + Added 1 files, modified 0 files, removed 0 files + "###); + + // Both workspaces show up when we list them + let stdout = test_env.jj_cmd_success(&secondary_path, &["workspace", "list"]); + insta::assert_snapshot!(stdout, @r###" + default: rlvkpnrz e1038e77 (empty) (no description set) + secondary: rzvqmyuk 7ad84461 (empty) (no description set) + "###); +} + /// Test making changes to the working copy in a workspace as it gets rewritten /// from another workspace #[test]