Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore .gitignore files that are directories #1386

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: ignore .gitignore files that are directories
This happens in real-life, and Git itself doesn't budge.
  • Loading branch information
Byron committed May 28, 2024
commit 31ade4a66141a4ce465ea7edfb9ec56636e77da4
23 changes: 18 additions & 5 deletions gix-glob/src/search/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,29 @@ fn read_in_full_ignore_missing(path: &Path, follow_symlinks: bool, buf: &mut Vec
};
Ok(match file {
Ok(mut file) => {
file.read_to_end(buf)?;
true
if let Err(err) = file.read_to_end(buf) {
if io_err_is_dir(&err) {
false
} else {
return Err(err);
}
} else {
true
}
}
Err(err) if err.kind() == std::io::ErrorKind::NotFound ||
// TODO: use the enum variant NotADirectory for this once stabilized
err.raw_os_error() == Some(20) /* Not a directory */ => false,
Err(err) if err.kind() == std::io::ErrorKind::NotFound || io_err_is_dir(&err) => false,
Err(err) => return Err(err),
})
}

fn io_err_is_dir(err: &std::io::Error) -> bool {
// TODO: use the enum variant NotADirectory for this once stabilized
let raw = err.raw_os_error();
raw == Some(if cfg!(windows) { 5 } else { 21 }) /* Not a directory */
/* Also that, but under different circumstances */
|| raw == Some(20)
}

/// Instantiation
impl<T> List<T>
where
Expand Down
14 changes: 13 additions & 1 deletion gix-glob/tests/search/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ mod list {
}

#[test]
fn from_file() {
fn from_file_that_does_not_exist() {
let mut buf = Vec::new();
for path in [
Path::new(".").join("non-existing-dir").join("pattern-file"),
Expand All @@ -95,4 +95,16 @@ mod list {
assert!(list.is_none(), "the file does not exist");
}
}

#[test]
fn from_file_that_is_a_directory() -> gix_testtools::Result<()> {
let tmp = gix_testtools::tempfile::TempDir::new()?;
let dir_path = tmp.path().join(".gitignore");
std::fs::create_dir(&dir_path)?;
let mut buf = Vec::new();
let list = List::<Dummy>::from_file(dir_path, None, false, &mut buf).expect("no io error");
assert!(list.is_none(), "directories are ignored just like Git does it");

Ok(())
}
}