| 1 |
// Copyright 2020 The Jujutsu Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
use std::io::Write as _; |
| 16 |
|
| 17 |
use clap_complete::ArgValueCandidates; |
| 18 |
use jj_lib::file_util; |
| 19 |
use jj_lib::ref_name::WorkspaceNameBuf; |
| 20 |
use jj_lib::workspace_store::SimpleWorkspaceStore; |
| 21 |
use jj_lib::workspace_store::WorkspaceStore as _; |
| 22 |
use tracing::instrument; |
| 23 |
|
| 24 |
use crate::cli_util::CommandHelper; |
| 25 |
use crate::command_error::CommandError; |
| 26 |
use crate::command_error::user_error; |
| 27 |
use crate::command_error::user_error_with_message; |
| 28 |
use crate::complete; |
| 29 |
use crate::ui::Ui; |
| 30 |
|
| 31 |
/// Show the workspace root directory |
| 32 |
#[derive(clap::Args, Clone, Debug)] |
| 33 |
pub struct WorkspaceRootArgs { |
| 34 |
/// Name of the workspace (defaults to current) |
| 35 |
#[arg(long, value_name = "NAME", add = ArgValueCandidates::new(complete::workspaces))] |
| 36 |
name: Option<WorkspaceNameBuf>, |
| 37 |
} |
| 38 |
|
| 39 |
#[instrument(skip_all)] |
| 40 |
pub async fn cmd_workspace_root( |
| 41 |
ui: &mut Ui, |
| 42 |
command: &CommandHelper, |
| 43 |
args: &WorkspaceRootArgs, |
| 44 |
) -> Result<(), CommandError> { |
| 45 |
let path = if let Some(ws_name) = &args.name { |
| 46 |
let workspace_command = command.workspace_helper_no_snapshot(ui).await?; |
| 47 |
let workspace_store = SimpleWorkspaceStore::load(workspace_command.repo_path())?; |
| 48 |
|
| 49 |
if workspace_command |
| 50 |
.repo() |
| 51 |
.view() |
| 52 |
.wc_commit_ids() |
| 53 |
.contains_key(ws_name) |
| 54 |
{ |
| 55 |
let path = workspace_store |
| 56 |
.get_workspace_path(ws_name)? |
| 57 |
.ok_or_else(|| { |
| 58 |
user_error(format!( |
| 59 |
"Workspace has no recorded path: {}", |
| 60 |
ws_name.as_symbol() |
| 61 |
)) |
| 62 |
})?; |
| 63 |
let full_path = workspace_command.repo_path().join(path); |
| 64 |
dunce::canonicalize(&full_path).map_err(|err| { |
| 65 |
user_error_with_message( |
| 66 |
format!( |
| 67 |
"Cannot resolve absolute workspace path: {}", |
| 68 |
full_path.display() |
| 69 |
), |
| 70 |
err, |
| 71 |
) |
| 72 |
})? |
| 73 |
} else { |
| 74 |
return Err(user_error(format!( |
| 75 |
"No such workspace: {}", |
| 76 |
ws_name.as_symbol() |
| 77 |
))); |
| 78 |
} |
| 79 |
} else { |
| 80 |
command.workspace_loader()?.workspace_root().into() |
| 81 |
}; |
| 82 |
|
| 83 |
let path_bytes = file_util::path_to_bytes(&path).map_err(user_error)?; |
| 84 |
ui.stdout().write_all(path_bytes)?; |
| 85 |
writeln!(ui.stdout())?; |
| 86 |
Ok(()) |
| 87 |
} |