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

Cargo new in empty dir #2008

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 4 additions & 0 deletions src/bin/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ fn execute(flags: Flags, config: &Config) -> CliResult<Option<()>> {
}

fn find_closest(cmd: &str) -> Option<String> {
if cmd == "init" {
return Some("new .".to_string())
}

let cmds = list_commands();
// Only consider candidates with a lev_distance of 3 or less so we don't
// suggest out-of-the-blue options.
Expand Down
38 changes: 36 additions & 2 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,41 @@ struct CargoNewConfig {
version_control: Option<VersionControl>,
}

fn can_we_use_this_path_for_a_new_project (path: &Path) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love this fn name

if let Some(path_metadata) = fs::metadata(&path).ok() {
if path_metadata.is_dir() {
if let Some(already_existing_files) = fs::read_dir(&path).ok() {
for entry in already_existing_files {
if let Some(entry2) = entry.ok() {
if let Some(preexisting_file) = entry2.file_name().to_str() {
if preexisting_file == ".git" { continue }
if preexisting_file == ".hg" { continue }
if preexisting_file == ".gitignore" { continue }
if preexisting_file == ".hgignore" { continue }
if preexisting_file == ".svn" { continue }
return false // directory with unknown file
} else {
return false // directory with a non-UTF8 filename
}
} else {
return false // directory where some file can't be enumerated
}
}
true // directory with only VCS-related files
} else {
false // can't enumerate files
}
} else {
false // not a directory
}
} else {
true // does not exist
}
}

pub fn new(opts: NewOptions, config: &Config) -> CargoResult<()> {
let path = config.cwd().join(opts.path);
if fs::metadata(&path).is_ok() {
if ! can_we_use_this_path_for_a_new_project(&path) {
return Err(human(format!("Destination `{}` already exists",
path.display())))
}
Expand Down Expand Up @@ -132,7 +164,9 @@ fn mk(config: &Config, path: &Path, name: &str,
try!(file(&path.join(".hgignore"), ignore.as_bytes()));
},
VersionControl::NoVcs => {
try!(fs::create_dir(path));
if ! fs::metadata(path).is_ok() {
try!(fs::create_dir(path));
}
},
};

Expand Down
1 change: 1 addition & 0 deletions tests/test_cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Usage:
test!(existing {
let dst = paths::root().join("foo");
fs::create_dir(&dst).unwrap();
File::create(&dst.join("Cargo.toml")).unwrap().write_all(b"qqq").unwrap();
assert_that(cargo_process("new").arg("foo"),
execs().with_status(101)
.with_stderr(format!("Destination `{}` already exists\n",
Expand Down