Skip to content
Merged
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
13 changes: 12 additions & 1 deletion src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,19 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
Ok(())
}

// Check if we are in an existing repo. We define that to be true if either:
//
// 1. We are in a git repo and the path to the new project 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 {
GitRepo::discover(path, cwd).is_ok() || HgRepo::discover(path, cwd).is_ok()
fn in_git_repo(path: &Path, cwd: &Path) -> bool {
if let Ok(repo) = GitRepo::discover(path, cwd) {
repo.is_path_ignored(path).map(|ignored| !ignored).unwrap_or(true)
} else { false }
}

in_git_repo(path, cwd) || HgRepo::discover(path, cwd).is_ok()
}

fn mk(config: &Config, opts: &MkOptions) -> CargoResult<()> {
Expand Down
4 changes: 2 additions & 2 deletions tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ fn does_not_warn_about_clean_working_directory() {
fn does_not_warn_about_dirty_ignored_files() {
let p = project()
.file("src/lib.rs", "pub fn foo() {}")
.file(".gitignore", "foo\n")
.file(".gitignore", "bar\n")
.build();

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

assert_that(
p.cargo("fix"),
Expand Down
30 changes: 30 additions & 0 deletions tests/testsuite/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,36 @@ fn subpackage_no_git() {
);
}

#[test]
fn subpackage_git_with_gitignore() {
assert_that(
cargo_process("new").arg("foo").env("USER", "foo"),
execs().with_status(0),
);

let gitignore = paths::root().join("foo").join(".gitignore");
fs::write(gitignore, b"components").unwrap();

let subpackage = paths::root().join("foo").join("components");
fs::create_dir(&subpackage).unwrap();
assert_that(
cargo_process("new")
.arg("foo/components/subcomponent")
.env("USER", "foo"),
execs().with_status(0),
);

assert_that(
&paths::root().join("foo").join("components").join("subcomponent").join(".git"),
existing_dir(),
);
assert_that(
&paths::root().join("foo").join("components").join("subcomponent").join(".gitignore"),
existing_file(),
);

}

#[test]
fn subpackage_git_with_vcs_arg() {
assert_that(
Expand Down