Skip to content

Commit 5257014

Browse files
committed
fix: ignore .gitignore files that are directories
This happens in real-life, and Git itself doesn't budge.
1 parent b3ead8a commit 5257014

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

gix-glob/src/search/pattern.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,26 @@ fn read_in_full_ignore_missing(path: &Path, follow_symlinks: bool, buf: &mut Vec
4848
};
4949
Ok(match file {
5050
Ok(mut file) => {
51-
file.read_to_end(buf)?;
52-
true
51+
if let Err(err) = file.read_to_end(buf) {
52+
if io_err_is_dir(&err) {
53+
false
54+
} else {
55+
return Err(err);
56+
}
57+
} else {
58+
true
59+
}
5360
}
54-
Err(err) if err.kind() == std::io::ErrorKind::NotFound ||
55-
// TODO: use the enum variant NotADirectory for this once stabilized
56-
err.raw_os_error() == Some(20) /* Not a directory */ => false,
61+
Err(err) if err.kind() == std::io::ErrorKind::NotFound || io_err_is_dir(&err) => false,
5762
Err(err) => return Err(err),
5863
})
5964
}
6065

66+
fn io_err_is_dir(err: &std::io::Error) -> bool {
67+
// TODO: use the enum variant NotADirectory for this once stabilized
68+
err.raw_os_error() == Some(21) /* Not a directory */
69+
}
70+
6171
/// Instantiation
6272
impl<T> List<T>
6373
where

gix-glob/tests/search/pattern.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ mod list {
8585
}
8686

8787
#[test]
88-
fn from_file() {
88+
fn from_file_that_does_not_exist() {
8989
let mut buf = Vec::new();
9090
for path in [
9191
Path::new(".").join("non-existing-dir").join("pattern-file"),
@@ -95,4 +95,16 @@ mod list {
9595
assert!(list.is_none(), "the file does not exist");
9696
}
9797
}
98+
99+
#[test]
100+
fn from_file_that_is_a_directory() -> gix_testtools::Result<()> {
101+
let tmp = gix_testtools::tempfile::TempDir::new()?;
102+
let dir_path = tmp.path().join(".gitignore");
103+
std::fs::create_dir(&dir_path)?;
104+
let mut buf = Vec::new();
105+
let list = List::<Dummy>::from_file(dir_path, None, false, &mut buf).expect("no io error");
106+
assert!(list.is_none(), "directories are ignored just like Git does it");
107+
108+
Ok(())
109+
}
98110
}

0 commit comments

Comments
 (0)