Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: inline CommandHelper::new() #3181

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 20 additions & 45 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,43 +607,14 @@ pub struct CommandHelper {
}

impl CommandHelper {
#[allow(clippy::too_many_arguments)]
pub fn new(
app: Command,
cwd: PathBuf,
string_args: Vec<String>,
matches: ArgMatches,
global_args: GlobalArgs,
settings: UserSettings,
layered_configs: LayeredConfigs,
commit_template_extension: Option<Arc<dyn CommitTemplateLanguageExtension>>,
maybe_workspace_loader: Result<WorkspaceLoader, CommandError>,
store_factories: StoreFactories,
working_copy_factories: HashMap<String, Box<dyn WorkingCopyFactory>>,
) -> Self {
// `cwd` is canonicalized for consistency with `Workspace::workspace_root()` and
// to easily compute relative paths between them.
let cwd = cwd.canonicalize().unwrap_or(cwd);

Self {
app,
cwd,
string_args,
matches,
global_args,
settings,
layered_configs,
commit_template_extension,
maybe_workspace_loader,
store_factories,
working_copy_factories,
}
}

pub fn app(&self) -> &Command {
&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 @@ -2987,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 @@ -3047,19 +3022,19 @@ impl CliRunner {
let working_copy_factories = self
.working_copy_factories
.unwrap_or_else(default_working_copy_factories);
let command_helper = CommandHelper::new(
self.app,
let command_helper = CommandHelper {
app: self.app,
cwd,
string_args,
matches,
args.global_args,
global_args: args.global_args,
settings,
layered_configs,
self.commit_template_extension,
commit_template_extension: self.commit_template_extension,
maybe_workspace_loader,
self.store_factories.unwrap_or_default(),
store_factories: self.store_factories.unwrap_or_default(),
working_copy_factories,
);
};
for start_hook_fn in self.start_hook_fns {
start_hook_fn(ui, &command_helper)?;
}
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