From 900300cf5f608c9b9e74a9545d71c35f900272ef Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Wed, 9 Aug 2023 12:37:13 +0900 Subject: [PATCH] cli: do not panic if clone destination directory can't be created wc_path.canonicalize() can also fail, but that's probably because of racy clone and the directory creation would have effectively failed. --- cli/src/commands/git.rs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/cli/src/commands/git.rs b/cli/src/commands/git.rs index d0fa8f1e0..8d95b7a2f 100644 --- a/cli/src/commands/git.rs +++ b/cli/src/commands/git.rs @@ -402,22 +402,24 @@ fn cmd_git_clone( .or_else(|| clone_destination_for_source(&source)) .ok_or_else(|| user_error("No destination specified and wasn't able to guess it"))?; let wc_path = command.cwd().join(wc_path_str); - let wc_path_existed = wc_path.exists(); - if wc_path_existed { - if !is_empty_dir(&wc_path) { - return Err(user_error( - "Destination path exists and is not an empty directory", - )); - } - } else { - fs::create_dir(&wc_path).unwrap(); + let wc_path_existed = match fs::create_dir(&wc_path) { + Ok(()) => false, + Err(err) if err.kind() == io::ErrorKind::AlreadyExists => true, + Err(err) => return Err(user_error(format!("Failed to create {wc_path_str}: {err}"))), + }; + if wc_path_existed && !is_empty_dir(&wc_path) { + return Err(user_error( + "Destination path exists and is not an empty directory", + )); } - let canonical_wc_path: PathBuf = wc_path.canonicalize().unwrap(); + // Canonicalize because fs::remove_dir_all() doesn't seem to like e.g. + // `/some/path/.` + let canonical_wc_path: PathBuf = wc_path + .canonicalize() + .map_err(|err| user_error(format!("Failed to create {wc_path_str}: {err}")))?; let clone_result = do_git_clone(ui, command, args.colocate, &source, &canonical_wc_path); if clone_result.is_err() { - // Canonicalize because fs::remove_dir_all() doesn't seem to like e.g. - // `/some/path/.` let clean_up_dirs = || -> io::Result<()> { fs::remove_dir_all(canonical_wc_path.join(".jj"))?; if args.colocate {