From 5d85efd595e13863a443fb53a8b2954e5c6c77cc Mon Sep 17 00:00:00 2001 From: David Sherret Date: Fri, 8 Mar 2024 14:25:22 -0500 Subject: fix(publish): ability to un-exclude when .gitignore ignores everything (#22805) This is an unrealistic scenario, but it's still a good thing to fix and have a test for because it probably fixes some other underlying issues with how the gitignore was being resolved for the root directory. From https://github.com/denoland/deno/pull/22720#issuecomment-1986134425 --- cli/util/fs.rs | 7 +++++-- cli/util/gitignore.rs | 19 +++++++++++++++---- 2 files changed, 20 insertions(+), 6 deletions(-) (limited to 'cli') diff --git a/cli/util/fs.rs b/cli/util/fs.rs index 352e09999..e84f05d1e 100644 --- a/cli/util/fs.rs +++ b/cli/util/fs.rs @@ -373,8 +373,11 @@ impl bool> FileCollector { let path = e.path().to_path_buf(); let maybe_gitignore = maybe_git_ignores.as_mut().and_then(|git_ignores| { - let dir_path = if is_dir { &path } else { path.parent()? }; - git_ignores.get_resolved_git_ignore(dir_path) + if is_dir { + git_ignores.get_resolved_git_ignore_for_dir(&path) + } else { + git_ignores.get_resolved_git_ignore_for_file(&path) + } }); if !is_pattern_matched( maybe_gitignore.as_deref(), diff --git a/cli/util/gitignore.rs b/cli/util/gitignore.rs index f4185aa0d..5601b5db9 100644 --- a/cli/util/gitignore.rs +++ b/cli/util/gitignore.rs @@ -54,10 +54,22 @@ impl GitIgnoreTree { } } - pub fn get_resolved_git_ignore( + pub fn get_resolved_git_ignore_for_dir( &mut self, dir_path: &Path, ) -> Option> { + // for directories, provide itself in order to tell + // if it should stop searching for gitignores because + // maybe this dir_path is a .git directory + let parent = dir_path.parent()?; + self.get_resolved_git_ignore_inner(parent, Some(dir_path)) + } + + pub fn get_resolved_git_ignore_for_file( + &mut self, + file_path: &Path, + ) -> Option> { + let dir_path = file_path.parent()?; self.get_resolved_git_ignore_inner(dir_path, None) } @@ -141,9 +153,8 @@ mod test { let mut ignore_tree = GitIgnoreTree::new(Arc::new(fs), Vec::new()); let mut run_test = |path: &str, expected: bool| { let path = PathBuf::from(path); - let gitignore = ignore_tree - .get_resolved_git_ignore(path.parent().unwrap()) - .unwrap(); + let gitignore = + ignore_tree.get_resolved_git_ignore_for_file(&path).unwrap(); assert_eq!( gitignore.is_ignored(&path, /* is_dir */ false), expected, -- cgit v1.2.3