Skip to content

Commit

Permalink
workspace: use cwd for printing relative path
Browse files Browse the repository at this point in the history
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".
  • Loading branch information
scott2000 committed Jul 21, 2024
1 parent 8df7857 commit 14d3bb8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
3 changes: 1 addition & 2 deletions cli/src/commands/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions cli/tests/test_workspaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 14d3bb8

Please sign in to comment.