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 all commits
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
2 changes: 1 addition & 1 deletion gix-fs/src/symlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn create(original: &Path, link: &Path) -> io::Result<()> {
pub fn is_collision_error(err: &std::io::Error) -> bool {
// TODO: use ::IsDirectory as well when stabilized instead of raw_os_error(), and ::FileSystemLoop respectively
err.kind() == AlreadyExists
|| err.raw_os_error() == Some(21)
|| err.raw_os_error() == Some(if cfg!(windows) { 5 } else { 21 })
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function here appears to be under #[cfg(not(windows))] making cfg!(windows) always false.
The #[cfg(windows)] below should probably be changed instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a great catch, thank you!

I pushed the fix into this standing PR.

|| err.raw_os_error() == Some(62) // no-follow on symlnk on mac-os
|| err.raw_os_error() == Some(40) // no-follow on symlnk on ubuntu
}
Expand Down
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(())
}
}
3 changes: 1 addition & 2 deletions gix-worktree/src/stack/delegate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::stack::mode_is_dir;
use crate::{stack::State, PathIdMapping};

/// Various aggregate numbers related to the stack delegate itself.
Expand Down Expand Up @@ -167,7 +166,7 @@ fn create_leading_directory(
mkdir_calls: &mut usize,
unlink_on_collision: bool,
) -> std::io::Result<()> {
if is_last_component && !mode_is_dir(mode).unwrap_or(false) {
if is_last_component && !crate::stack::mode_is_dir(mode).unwrap_or(false) {
return Ok(());
}
*mkdir_calls += 1;
Expand Down
Loading