Skip to content

Commit 5c54fcb

Browse files
committed
Auto merge of #6767 - ehuss:fix-git-ignore-of-root, r=Eh2406
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
2 parents ff1c9de + 00c562d commit 5c54fcb

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/cargo/util/vcs.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ use crate::util::{process, CargoResult};
1313
pub fn existing_vcs_repo(path: &Path, cwd: &Path) -> bool {
1414
fn in_git_repo(path: &Path, cwd: &Path) -> bool {
1515
if let Ok(repo) = GitRepo::discover(path, cwd) {
16-
repo.is_path_ignored(path)
17-
.map(|ignored| !ignored)
18-
.unwrap_or(true)
16+
// Don't check if the working directory itself is ignored.
17+
if repo.workdir().map_or(false, |workdir| workdir == path) {
18+
true
19+
} else {
20+
!repo.is_path_ignored(path).unwrap_or(false)
21+
}
1922
} else {
2023
false
2124
}

tests/testsuite/fix.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,3 +1289,26 @@ fn fix_with_common() {
12891289

12901290
assert_eq!(p.read_file("tests/common/mod.rs"), "pub fn r#try() {}");
12911291
}
1292+
1293+
#[test]
1294+
fn fix_in_existing_repo_weird_ignore() {
1295+
// Check that ignore doesn't ignore the repo itself.
1296+
let p = git::new("foo", |project| {
1297+
project
1298+
.file("src/lib.rs", "")
1299+
.file(".gitignore", "foo\ninner\n")
1300+
.file("inner/file", "")
1301+
})
1302+
.unwrap();
1303+
1304+
p.cargo("fix").run();
1305+
// This is questionable about whether it is the right behavior. It should
1306+
// probably be checking if any source file for the current project is
1307+
// ignored.
1308+
p.cargo("fix")
1309+
.cwd(p.root().join("inner"))
1310+
.with_stderr_contains("[ERROR] no VCS found[..]")
1311+
.with_status(101)
1312+
.run();
1313+
p.cargo("fix").cwd(p.root().join("src")).run();
1314+
}

0 commit comments

Comments
 (0)