Skip to content

Commit

Permalink
Auto merge of #6767 - ehuss:fix-git-ignore-of-root, r=Eh2406
Browse files Browse the repository at this point in the history
Allow `cargo fix` if gitignore matches root working dir.

If `.gitignore` had a rule that matched the root working directory, it would believe that the whole thing is ignored. This is not usually how git works (AFAIK), so don't check gitignore against the root.

Closes #6766
  • Loading branch information
bors committed Mar 20, 2019
2 parents ff1c9de + 00c562d commit 5c54fcb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/cargo/util/vcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
23 changes: 23 additions & 0 deletions tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 5c54fcb

Please sign in to comment.