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

Propagate existing VCS to subprojects #7673

Closed
wants to merge 1 commit 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
49 changes: 36 additions & 13 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,30 +538,44 @@ 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() {
// Temporary fix to work around bug in libgit2 when creating a
// 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 => {
Expand All @@ -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()?;
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 \
Expand Down
10 changes: 8 additions & 2 deletions src/cargo/util/vcs.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::ops::VersionControl;
use crate::util::paths;
use crate::util::{process, CargoResult};
use git2;
Expand All @@ -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.
Expand All @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions tests/testsuite/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down