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".
This commit is contained in:
Scott Taylor 2024-07-17 18:29:50 -05:00 committed by Scott Taylor
parent 8df7857706
commit 14d3bb85bc
2 changed files with 37 additions and 2 deletions

View file

@ -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

View file

@ -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]