Allow cargo fix if gitignore matches root working dir.

This commit is contained in:
Eric Huss 2019-03-20 12:22:44 -07:00
parent ff1c9de432
commit 00c562df56
2 changed files with 29 additions and 3 deletions

View File

@ -13,9 +13,12 @@ use crate::util::{process, CargoResult};
pub fn existing_vcs_repo(path: &Path, cwd: &Path) -> bool {
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)
// Don't check if the working directory itself is ignored.
if repo.workdir().map_or(false, |workdir| workdir == path) {
true
} else {
!repo.is_path_ignored(path).unwrap_or(false)
}
} else {
false
}

View File

@ -1289,3 +1289,26 @@ fn fix_with_common() {
assert_eq!(p.read_file("tests/common/mod.rs"), "pub fn r#try() {}");
}
#[test]
fn fix_in_existing_repo_weird_ignore() {
// Check that ignore doesn't ignore the repo itself.
let p = git::new("foo", |project| {
project
.file("src/lib.rs", "")
.file(".gitignore", "foo\ninner\n")
.file("inner/file", "")
})
.unwrap();
p.cargo("fix").run();
// This is questionable about whether it is the right behavior. It should
// probably be checking if any source file for the current project is
// ignored.
p.cargo("fix")
.cwd(p.root().join("inner"))
.with_stderr_contains("[ERROR] no VCS found[..]")
.with_status(101)
.run();
p.cargo("fix").cwd(p.root().join("src")).run();
}