Skip to content

Commit

Permalink
Auto merge of #5733 - withoutboats:cargo-new-respects-gitignore, r=al…
Browse files Browse the repository at this point in the history
…excrichton

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.
  • Loading branch information
bors committed Jul 26, 2018
2 parents 41df9a0 + 064a146 commit b5a1dbd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
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

0 comments on commit b5a1dbd

Please sign in to comment.