cli: canonicalize cwd earlier and rely on that

This commit is contained in:
Yuya Nishihara 2024-03-01 18:31:42 +09:00
parent ee4a06d847
commit 42f9ced423
3 changed files with 18 additions and 19 deletions

View file

@ -611,6 +611,10 @@ impl CommandHelper {
&self.app
}
/// Canonical form of the current working directory path.
///
/// A loaded `Workspace::workspace_root()` also returns a canonical path, so
/// relative paths can be easily computed from these paths.
pub fn cwd(&self) -> &Path {
&self.cwd
}
@ -2954,7 +2958,11 @@ impl CliRunner {
ui: &mut Ui,
mut layered_configs: LayeredConfigs,
) -> Result<(), CommandError> {
let cwd = env::current_dir().map_err(|_| {
// `cwd` is canonicalized for consistency with `Workspace::workspace_root()` and
// to easily compute relative paths between them.
let cwd = env::current_dir()
.and_then(|cwd| cwd.canonicalize())
.map_err(|_| {
user_error_with_hint(
"Could not determine current directory",
"Did you check-out a commit where the directory doesn't exist?",
@ -3010,9 +3018,6 @@ impl CliRunner {
}
}
// `cwd` is canonicalized for consistency with `Workspace::workspace_root()` and
// to easily compute relative paths between them.
let cwd = cwd.canonicalize().unwrap_or(cwd);
let settings = UserSettings::from_config(config);
let working_copy_factories = self
.working_copy_factories

View file

@ -492,10 +492,7 @@ fn cmd_git_init(
command: &CommandHelper,
args: &GitInitArgs,
) -> Result<(), CommandError> {
let cwd = command
.cwd()
.canonicalize()
.map_err(|e| user_error_with_message("Could not determine current directory", e))?;
let cwd = command.cwd();
let wc_path = cwd.join(&args.destination);
let wc_path = file_util::create_or_reuse_dir(&wc_path)
.and_then(|_| wc_path.canonicalize())
@ -509,7 +506,7 @@ fn cmd_git_init(
args.git_repo.as_deref(),
)?;
let relative_wc_path = file_util::relative_path(&cwd, &wc_path);
let relative_wc_path = file_util::relative_path(cwd, &wc_path);
writeln!(
ui.stderr(),
r#"Initialized repo in "{}""#,

View file

@ -49,10 +49,7 @@ pub(crate) fn cmd_init(
command: &CommandHelper,
args: &InitArgs,
) -> Result<(), CommandError> {
let cwd = command
.cwd()
.canonicalize()
.map_err(|e| user_error_with_message("Could not determine current directory", e))?;
let cwd = command.cwd();
let wc_path = cwd.join(&args.destination);
let wc_path = file_util::create_or_reuse_dir(&wc_path)
.and_then(|_| wc_path.canonicalize())
@ -79,7 +76,7 @@ Set `ui.allow-init-native` to allow initializing a repo with the native backend.
Workspace::init_local(command.settings(), &wc_path)?;
}
let relative_wc_path = file_util::relative_path(&cwd, &wc_path);
let relative_wc_path = file_util::relative_path(cwd, &wc_path);
writeln!(
ui.stderr(),
"Initialized repo in \"{}\"",