workspace: add a root subcommand to print the workspace root path

This commit is contained in:
Samuel Tardieu 2023-01-24 12:46:59 +01:00
parent 9dd5fe108a
commit e4f6dadfad
4 changed files with 65 additions and 1 deletions

View file

@ -81,6 +81,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
by a "␛" character. That prevents them from interfering with the ANSI escapes
jj itself writes.
* `jj workspace root` prints the root path of the current workspace.
### Fixed bugs
* When sharing the working copy with a Git repo, we used to forget to export

View file

@ -70,7 +70,8 @@ directory together is called a "workspace". Each workspace can have a different
commit checked out.
Having multiple workspaces can be useful for running long-running tests in a one
while you continue developing in another, for example.
while you continue developing in another, for example. If needed,
`jj workspace root` prints the root path of the current workspace.
When you're done using a workspace, use `jj workspace forget` to make the repo
forget about it. The files can be deleted from disk separately (either before or

View file

@ -743,6 +743,7 @@ enum WorkspaceCommands {
Add(WorkspaceAddArgs),
Forget(WorkspaceForgetArgs),
List(WorkspaceListArgs),
Root(WorkspaceRootArgs),
UpdateStale(WorkspaceUpdateStaleArgs),
}
@ -773,6 +774,10 @@ struct WorkspaceForgetArgs {
#[derive(clap::Args, Clone, Debug)]
struct WorkspaceListArgs {}
/// Show the current workspace root directory
#[derive(clap::Args, Clone, Debug)]
struct WorkspaceRootArgs {}
/// Update a workspace that has become stale
///
/// For information about stale working copies, see
@ -3013,6 +3018,9 @@ fn cmd_workspace(
WorkspaceCommands::List(command_matches) => {
cmd_workspace_list(ui, command, command_matches)
}
WorkspaceCommands::Root(command_matches) => {
cmd_workspace_root(ui, command, command_matches)
}
WorkspaceCommands::UpdateStale(command_matches) => {
cmd_workspace_update_stale(ui, command, command_matches)
}
@ -3142,6 +3150,24 @@ fn cmd_workspace_list(
Ok(())
}
fn cmd_workspace_root(
ui: &mut Ui,
command: &CommandHelper,
_args: &WorkspaceRootArgs,
) -> Result<(), CommandError> {
let workspace_command = command.workspace_helper(ui)?;
let root =
workspace_command
.workspace_root()
.to_str()
.ok_or_else(|| CommandError::UserError {
message: String::from("The workspace root is not valid UTF-8"),
hint: None,
})?;
writeln!(ui, "{root}")?;
Ok(())
}
fn cmd_workspace_update_stale(
ui: &mut Ui,
command: &CommandHelper,

View file

@ -244,6 +244,41 @@ fn test_list_workspaces_template() {
"###);
}
/// Test getting the workspace root from primary and secondary workspaces
#[test]
fn test_workspaces_root() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "--git", "main"]);
let main_path = test_env.env_root().join("main");
let secondary_path = test_env.env_root().join("secondary");
let stdout = test_env.jj_cmd_success(&main_path, &["workspace", "root"]);
insta::assert_snapshot!(stdout, @r###"
$TEST_ENV/main
"###);
let main_subdir_path = main_path.join("subdir");
std::fs::create_dir(&main_subdir_path).unwrap();
let stdout = test_env.jj_cmd_success(&main_subdir_path, &["workspace", "root"]);
insta::assert_snapshot!(stdout, @r###"
$TEST_ENV/main
"###);
test_env.jj_cmd_success(
&main_path,
&["workspace", "add", "--name", "secondary", "../secondary"],
);
let stdout = test_env.jj_cmd_success(&secondary_path, &["workspace", "root"]);
insta::assert_snapshot!(stdout, @r###"
$TEST_ENV/secondary
"###);
let secondary_subdir_path = secondary_path.join("subdir");
std::fs::create_dir(&secondary_subdir_path).unwrap();
let stdout = test_env.jj_cmd_success(&secondary_subdir_path, &["workspace", "root"]);
insta::assert_snapshot!(stdout, @r###"
$TEST_ENV/secondary
"###);
}
fn get_log_output(test_env: &TestEnvironment, cwd: &Path) -> String {
test_env.jj_cmd_success(
cwd,