diff --git a/src/cargo/ops/cargo_new.rs b/src/cargo/ops/cargo_new.rs index bd02a0ed4fa..6149fd552e6 100644 --- a/src/cargo/ops/cargo_new.rs +++ b/src/cargo/ops/cargo_new.rs @@ -538,7 +538,13 @@ fn write_ignore_file( } /// Initializes the correct VCS system based on the provided config. -fn init_vcs(path: &Path, vcs: VersionControl, config: &Config) -> CargoResult<()> { +fn init_vcs( + path: &Path, + vcs: VersionControl, + config: &Config, + in_existing_vcs: bool, + explicit: bool, +) -> CargoResult<()> { match vcs { VersionControl::Git => { if !path.join(".git").exists() { @@ -546,22 +552,30 @@ fn init_vcs(path: &Path, vcs: VersionControl, config: &Config) -> CargoResult<() // directory in the root of a posix filesystem. // See: https://github.com/libgit2/libgit2/issues/5130 paths::create_dir_all(path)?; - GitRepo::init(path, config.cwd())?; + if explicit || !in_existing_vcs { + GitRepo::init(path, config.cwd())?; + } } } VersionControl::Hg => { if !path.join(".hg").exists() { - HgRepo::init(path, config.cwd())?; + if explicit || !in_existing_vcs { + HgRepo::init(path, config.cwd())?; + } } } VersionControl::Pijul => { if !path.join(".pijul").exists() { - PijulRepo::init(path, config.cwd())?; + if explicit || !in_existing_vcs { + PijulRepo::init(path, config.cwd())?; + } } } VersionControl::Fossil => { if !path.join(".fossil").exists() { - FossilRepo::init(path, config.cwd())?; + if explicit || !in_existing_vcs { + FossilRepo::init(path, config.cwd())?; + } } } VersionControl::NoVcs => { @@ -585,16 +599,25 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> { ignore.push("Cargo.lock", "glob:Cargo.lock"); } - let vcs = opts.version_control.unwrap_or_else(|| { - let in_existing_vcs = existing_vcs_repo(path.parent().unwrap_or(path), config.cwd()); - match (cfg.version_control, in_existing_vcs) { - (None, false) => VersionControl::Git, - (Some(opt), false) => opt, - (_, true) => VersionControl::NoVcs, + let existing_vcs = existing_vcs_repo(path.parent().unwrap_or(path), config.cwd()); + let in_existing_vcs = existing_vcs != VersionControl::NoVcs; + let mut explicit = false; + let vcs = match opts.version_control { + Some(opt) => { + explicit = true; + opt } - }); + None => match (cfg.version_control, in_existing_vcs) { + (None, false) => VersionControl::Git, + (Some(opt), _) => { + explicit = true; + opt + } + (None, true) => existing_vcs, + }, + }; - init_vcs(path, vcs, config)?; + init_vcs(path, vcs, config, in_existing_vcs, explicit)?; write_ignore_file(path, &ignore, vcs)?; let (author_name, email) = discover_author()?; diff --git a/src/cargo/ops/fix.rs b/src/cargo/ops/fix.rs index dfad1e48123..c79e1505d11 100644 --- a/src/cargo/ops/fix.rs +++ b/src/cargo/ops/fix.rs @@ -52,7 +52,7 @@ use rustfix::diagnostics::Diagnostic; use rustfix::{self, CodeFix}; use crate::core::Workspace; -use crate::ops::{self, CompileOptions}; +use crate::ops::{self, CompileOptions, VersionControl}; use crate::util::diagnostic_server::{Message, RustfixDiagnosticServer}; use crate::util::errors::CargoResult; use crate::util::{self, paths}; @@ -146,7 +146,7 @@ fn check_version_control(opts: &FixOptions<'_>) -> CargoResult<()> { return Ok(()); } let config = opts.compile_opts.config; - if !existing_vcs_repo(config.cwd(), config.cwd()) { + if VersionControl::NoVcs == existing_vcs_repo(config.cwd(), config.cwd()) { failure::bail!( "no VCS found for this package and `cargo fix` can potentially \ perform destructive changes; if you'd like to suppress this \ diff --git a/src/cargo/util/vcs.rs b/src/cargo/util/vcs.rs index d716ba61403..2720ac5e54f 100644 --- a/src/cargo/util/vcs.rs +++ b/src/cargo/util/vcs.rs @@ -1,3 +1,4 @@ +use crate::ops::VersionControl; use crate::util::paths; use crate::util::{process, CargoResult}; use git2; @@ -8,7 +9,7 @@ use std::path::Path; // 1. We are in a git repo and the path to the new package is not an ignored // path in that repo. // 2. We are in an HG repo. -pub fn existing_vcs_repo(path: &Path, cwd: &Path) -> bool { +pub fn existing_vcs_repo(path: &Path, cwd: &Path) -> VersionControl { fn in_git_repo(path: &Path, cwd: &Path) -> bool { if let Ok(repo) = GitRepo::discover(path, cwd) { // Don't check if the working directory itself is ignored. @@ -22,7 +23,12 @@ pub fn existing_vcs_repo(path: &Path, cwd: &Path) -> bool { } } - in_git_repo(path, cwd) || HgRepo::discover(path, cwd).is_ok() + if in_git_repo(path, cwd) { + return VersionControl::Git; + } else if HgRepo::discover(path, cwd).is_ok() { + return VersionControl::Hg; + } + VersionControl::NoVcs } pub struct HgRepo; diff --git a/tests/testsuite/new.rs b/tests/testsuite/new.rs index 1a7f344619a..5cc6d887e14 100644 --- a/tests/testsuite/new.rs +++ b/tests/testsuite/new.rs @@ -422,9 +422,6 @@ fn subpackage_no_git() { assert!(!paths::root() .join("foo/components/subcomponent/.git") .is_file()); - assert!(!paths::root() - .join("foo/components/subcomponent/.gitignore") - .is_file()); } #[cargo_test]