Skip to content

Commit

Permalink
cli: error out early if -R path is invalid
Browse files Browse the repository at this point in the history
It should be better to handle invalid -R path globally. The error message is
a bit worse, but I think it's still okay.

This helps to load temporary config from the cwd-relative path. If the command
processing continued with an invalid -R path, the temporary config would have
to be explicitly discarded.
  • Loading branch information
yuja committed Dec 23, 2023
1 parent e092300 commit 47a21af
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 10 deletions.
6 changes: 4 additions & 2 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2975,8 +2975,10 @@ impl CliRunner {
}

let maybe_workspace_loader = if let Some(path) = &args.global_args.repository {
WorkspaceLoader::init(&cwd.join(path))
.map_err(|err| map_workspace_load_error(err, Some(path)))
// Invalid -R path is an error. No need to proceed.
let loader = WorkspaceLoader::init(&cwd.join(path))
.map_err(|err| map_workspace_load_error(err, Some(path)))?;
Ok(loader)
} else {
WorkspaceLoader::init(find_workspace_dir(&cwd))
.map_err(|err| map_workspace_load_error(err, None))
Expand Down
3 changes: 0 additions & 3 deletions cli/src/commands/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,6 @@ fn cmd_git_clone(
command: &CommandHelper,
args: &GitCloneArgs,
) -> Result<(), CommandError> {
if command.global_args().repository.is_some() {
return Err(user_error("'--repository' cannot be used with 'git clone'"));
}
let remote_name = "origin";
let source = absolute_git_source(command.cwd(), &args.source);
let wc_path_str = args
Expand Down
3 changes: 0 additions & 3 deletions cli/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ pub(crate) fn cmd_init(
command: &CommandHelper,
args: &InitArgs,
) -> Result<(), CommandError> {
if command.global_args().repository.is_some() {
return Err(user_error("'--repository' cannot be used with 'init'"));
}
let wc_path = command.cwd().join(&args.destination);
match fs::create_dir(&wc_path) {
Ok(()) => {}
Expand Down
4 changes: 2 additions & 2 deletions cli/tests/test_global_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ fn test_repo_arg_with_init() {
let test_env = TestEnvironment::default();
let stderr = test_env.jj_cmd_failure(test_env.env_root(), &["init", "-R=.", "repo"]);
insta::assert_snapshot!(stderr, @r###"
Error: '--repository' cannot be used with 'init'
Error: There is no jj repo in "."
"###);
}

Expand All @@ -142,7 +142,7 @@ fn test_repo_arg_with_git_clone() {
let test_env = TestEnvironment::default();
let stderr = test_env.jj_cmd_failure(test_env.env_root(), &["git", "clone", "-R=.", "remote"]);
insta::assert_snapshot!(stderr, @r###"
Error: '--repository' cannot be used with 'git clone'
Error: There is no jj repo in "."
"###);
}

Expand Down

0 comments on commit 47a21af

Please sign in to comment.