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
This commit is contained in:
David Sherret 2024-03-08 14:25:22 -05:00 committed by GitHub
parent fee4943f76
commit 5d85efd595
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 48 additions and 6 deletions

View file

@ -373,8 +373,11 @@ impl<TFilter: Fn(WalkEntry) -> bool> FileCollector<TFilter> {
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(),

View file

@ -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<Rc<DirGitIgnores>> {
// 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<Rc<DirGitIgnores>> {
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,

View file

@ -458,6 +458,34 @@ fn not_include_gitignored_file_unless_exact_match_in_include() {
assert_not_contains!(output, "sub_ignored.ts");
}
#[test]
fn gitignore_everything_exlcuded_override() {
let context = publish_context_builder().build();
let temp_dir = context.temp_dir().path();
temp_dir.join(".gitignore").write("*\n");
temp_dir.join("deno.json").write_json(&json!({
"name": "@foo/bar",
"version": "1.0.0",
"exports": "./root_main.ts",
"publish": {
// should opt out of .gitignore even though everything
// is .gitignored
"exclude": ["!**"]
}
}));
temp_dir.join("root_main.ts").write("");
let sub_dir = temp_dir.join("sub");
sub_dir.create_dir_all();
sub_dir.join("sub_main.ts").write("");
let output = context.new_command().arg("publish").arg("--dry-run").run();
output.assert_exit_code(0);
let output = output.combined_output();
assert_contains!(output, "root_main.ts");
assert_contains!(output, "sub_main.ts");
}
#[test]
fn includes_directories_with_gitignore_when_unexcluded() {
let context = publish_context_builder().build();