Skip to content

Commit

Permalink
cli: canonicalize cwd earlier and rely on that
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed Mar 2, 2024
1 parent 4717090 commit 8b25e96
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
23 changes: 14 additions & 9 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -2954,12 +2958,16 @@ impl CliRunner {
ui: &mut Ui,
mut layered_configs: LayeredConfigs,
) -> Result<(), CommandError> {
let cwd = env::current_dir().map_err(|_| {
user_error_with_hint(
"Could not determine current directory",
"Did you check-out a commit where the directory doesn't exist?",
)
})?;
// `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?",
)
})?;
// Use cwd-relative workspace configs to resolve default command and
// aliases. WorkspaceLoader::init() won't do any heavy lifting other
// than the path resolution.
Expand Down Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions cli/src/commands/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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 "{}""#,
Expand Down
7 changes: 2 additions & 5 deletions cli/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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 \"{}\"",
Expand Down

0 comments on commit 8b25e96

Please sign in to comment.