Skip to content

Commit 256a8dd

Browse files
committed
Propagate existing VCS to subprojects
With this post, when creating a new project within an existing project, any existing VCS is reused by creating a matching ignore file in the new subfolder. (see issue rust-lang#6357)
1 parent abba15f commit 256a8dd

File tree

4 files changed

+46
-20
lines changed

4 files changed

+46
-20
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -538,30 +538,44 @@ fn write_ignore_file(
538538
}
539539

540540
/// Initializes the correct VCS system based on the provided config.
541-
fn init_vcs(path: &Path, vcs: VersionControl, config: &Config) -> CargoResult<()> {
541+
fn init_vcs(
542+
path: &Path,
543+
vcs: VersionControl,
544+
config: &Config,
545+
in_existing_vcs: bool,
546+
explicit: bool,
547+
) -> CargoResult<()> {
542548
match vcs {
543549
VersionControl::Git => {
544550
if !path.join(".git").exists() {
545551
// Temporary fix to work around bug in libgit2 when creating a
546552
// directory in the root of a posix filesystem.
547553
// See: https://github.com/libgit2/libgit2/issues/5130
548554
paths::create_dir_all(path)?;
549-
GitRepo::init(path, config.cwd())?;
555+
if explicit || !in_existing_vcs {
556+
GitRepo::init(path, config.cwd())?;
557+
}
550558
}
551559
}
552560
VersionControl::Hg => {
553561
if !path.join(".hg").exists() {
554-
HgRepo::init(path, config.cwd())?;
562+
if explicit || !in_existing_vcs {
563+
HgRepo::init(path, config.cwd())?;
564+
}
555565
}
556566
}
557567
VersionControl::Pijul => {
558568
if !path.join(".pijul").exists() {
559-
PijulRepo::init(path, config.cwd())?;
569+
if explicit || !in_existing_vcs {
570+
PijulRepo::init(path, config.cwd())?;
571+
}
560572
}
561573
}
562574
VersionControl::Fossil => {
563575
if !path.join(".fossil").exists() {
564-
FossilRepo::init(path, config.cwd())?;
576+
if explicit || !in_existing_vcs {
577+
FossilRepo::init(path, config.cwd())?;
578+
}
565579
}
566580
}
567581
VersionControl::NoVcs => {
@@ -585,16 +599,25 @@ fn mk(config: &Config, opts: &MkOptions<'_>) -> CargoResult<()> {
585599
ignore.push("Cargo.lock", "glob:Cargo.lock");
586600
}
587601

588-
let vcs = opts.version_control.unwrap_or_else(|| {
589-
let in_existing_vcs = existing_vcs_repo(path.parent().unwrap_or(path), config.cwd());
590-
match (cfg.version_control, in_existing_vcs) {
591-
(None, false) => VersionControl::Git,
592-
(Some(opt), false) => opt,
593-
(_, true) => VersionControl::NoVcs,
602+
let existing_vcs = existing_vcs_repo(path.parent().unwrap_or(path), config.cwd());
603+
let in_existing_vcs = existing_vcs != VersionControl::NoVcs;
604+
let mut explicit = false;
605+
let vcs = match opts.version_control {
606+
Some(opt) => {
607+
explicit = true;
608+
opt
594609
}
595-
});
610+
None => match (cfg.version_control, in_existing_vcs) {
611+
(None, false) => VersionControl::Git,
612+
(Some(opt), _) => {
613+
explicit = true;
614+
opt
615+
}
616+
(None, true) => existing_vcs,
617+
},
618+
};
596619

597-
init_vcs(path, vcs, config)?;
620+
init_vcs(path, vcs, config, in_existing_vcs, explicit)?;
598621
write_ignore_file(path, &ignore, vcs)?;
599622

600623
let (author_name, email) = discover_author()?;

src/cargo/ops/fix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use rustfix::diagnostics::Diagnostic;
5252
use rustfix::{self, CodeFix};
5353

5454
use crate::core::Workspace;
55-
use crate::ops::{self, CompileOptions};
55+
use crate::ops::{self, CompileOptions, VersionControl};
5656
use crate::util::diagnostic_server::{Message, RustfixDiagnosticServer};
5757
use crate::util::errors::CargoResult;
5858
use crate::util::{self, paths};
@@ -146,7 +146,7 @@ fn check_version_control(opts: &FixOptions<'_>) -> CargoResult<()> {
146146
return Ok(());
147147
}
148148
let config = opts.compile_opts.config;
149-
if !existing_vcs_repo(config.cwd(), config.cwd()) {
149+
if VersionControl::NoVcs == existing_vcs_repo(config.cwd(), config.cwd()) {
150150
failure::bail!(
151151
"no VCS found for this package and `cargo fix` can potentially \
152152
perform destructive changes; if you'd like to suppress this \

src/cargo/util/vcs.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::ops::VersionControl;
12
use crate::util::paths;
23
use crate::util::{process, CargoResult};
34
use git2;
@@ -8,7 +9,7 @@ use std::path::Path;
89
// 1. We are in a git repo and the path to the new package is not an ignored
910
// path in that repo.
1011
// 2. We are in an HG repo.
11-
pub fn existing_vcs_repo(path: &Path, cwd: &Path) -> bool {
12+
pub fn existing_vcs_repo(path: &Path, cwd: &Path) -> VersionControl {
1213
fn in_git_repo(path: &Path, cwd: &Path) -> bool {
1314
if let Ok(repo) = GitRepo::discover(path, cwd) {
1415
// Don't check if the working directory itself is ignored.
@@ -22,7 +23,12 @@ pub fn existing_vcs_repo(path: &Path, cwd: &Path) -> bool {
2223
}
2324
}
2425

25-
in_git_repo(path, cwd) || HgRepo::discover(path, cwd).is_ok()
26+
if in_git_repo(path, cwd) {
27+
return VersionControl::Git;
28+
} else if HgRepo::discover(path, cwd).is_ok() {
29+
return VersionControl::Hg;
30+
}
31+
VersionControl::NoVcs
2632
}
2733

2834
pub struct HgRepo;

tests/testsuite/new.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,6 @@ fn subpackage_no_git() {
422422
assert!(!paths::root()
423423
.join("foo/components/subcomponent/.git")
424424
.is_file());
425-
assert!(!paths::root()
426-
.join("foo/components/subcomponent/.gitignore")
427-
.is_file());
428425
}
429426

430427
#[cargo_test]

0 commit comments

Comments
 (0)