Skip to content

Commit 064a146

Browse files
author
Without Boats
committed
Respect .gitignore during cargo new
When running `cargo new`, we check to see if you are inside a git repository. If you are, we do not initialize a new git repo for your project unless you specifically asked for it using --vcs. (See #1210 for more background). This commit changes that behavior to *also* create a new repo if the project would be an ignored path in the parent repository. This way, if your home directory is a git repository, as long as you have ignored the directory you are creating a new project in, we will instantiate a git repository without you having to specifically request it.
1 parent cef7c56 commit 064a146

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

src/cargo/ops/cargo_new.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,8 +409,19 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
409409
Ok(())
410410
}
411411

412+
// Check if we are in an existing repo. We define that to be true if either:
413+
//
414+
// 1. We are in a git repo and the path to the new project is not an ignored
415+
// path in that repo.
416+
// 2. We are in an HG repo.
412417
pub fn existing_vcs_repo(path: &Path, cwd: &Path) -> bool {
413-
GitRepo::discover(path, cwd).is_ok() || HgRepo::discover(path, cwd).is_ok()
418+
fn in_git_repo(path: &Path, cwd: &Path) -> bool {
419+
if let Ok(repo) = GitRepo::discover(path, cwd) {
420+
repo.is_path_ignored(path).map(|ignored| !ignored).unwrap_or(true)
421+
} else { false }
422+
}
423+
424+
in_git_repo(path, cwd) || HgRepo::discover(path, cwd).is_ok()
414425
}
415426

416427
fn mk(config: &Config, opts: &MkOptions) -> CargoResult<()> {

tests/testsuite/fix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ fn does_not_warn_about_clean_working_directory() {
826826
fn does_not_warn_about_dirty_ignored_files() {
827827
let p = project()
828828
.file("src/lib.rs", "pub fn foo() {}")
829-
.file(".gitignore", "foo\n")
829+
.file(".gitignore", "bar\n")
830830
.build();
831831

832832
let repo = git2::Repository::init(&p.root()).unwrap();
@@ -836,7 +836,7 @@ fn does_not_warn_about_dirty_ignored_files() {
836836
drop(cfg);
837837
git::add(&repo);
838838
git::commit(&repo);
839-
File::create(p.root().join("foo")).unwrap();
839+
File::create(p.root().join("bar")).unwrap();
840840

841841
assert_that(
842842
p.cargo("fix"),

tests/testsuite/new.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,36 @@ fn subpackage_no_git() {
495495
);
496496
}
497497

498+
#[test]
499+
fn subpackage_git_with_gitignore() {
500+
assert_that(
501+
cargo_process("new").arg("foo").env("USER", "foo"),
502+
execs().with_status(0),
503+
);
504+
505+
let gitignore = paths::root().join("foo").join(".gitignore");
506+
fs::write(gitignore, b"components").unwrap();
507+
508+
let subpackage = paths::root().join("foo").join("components");
509+
fs::create_dir(&subpackage).unwrap();
510+
assert_that(
511+
cargo_process("new")
512+
.arg("foo/components/subcomponent")
513+
.env("USER", "foo"),
514+
execs().with_status(0),
515+
);
516+
517+
assert_that(
518+
&paths::root().join("foo").join("components").join("subcomponent").join(".git"),
519+
existing_dir(),
520+
);
521+
assert_that(
522+
&paths::root().join("foo").join("components").join("subcomponent").join(".gitignore"),
523+
existing_file(),
524+
);
525+
526+
}
527+
498528
#[test]
499529
fn subpackage_git_with_vcs_arg() {
500530
assert_that(

0 commit comments

Comments
 (0)