Skip to content

Commit

Permalink
fix: Don't read .gitignore from above the git root
Browse files Browse the repository at this point in the history
Turns out `ignore` crate will do this unless `require_git` is set.

Fixes #450
  • Loading branch information
sourcefrog committed Nov 24, 2024
1 parent b87dab8 commit c95bc57
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/copy_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ pub fn copy_tree(
console.start_copy(dest);
for entry in WalkBuilder::new(from_path)
.standard_filters(gitignore)
.hidden(false)
.ignore(false)
.require_git(false)
.hidden(false) // copy hidden files
.ignore(false) // don't use .ignore
.require_git(true) // stop at git root; only read gitignore files inside git trees
.filter_entry(|entry| {
!SOURCE_EXCLUDE.contains(&entry.file_name().to_string_lossy().as_ref())
})
Expand Down Expand Up @@ -111,3 +111,39 @@ pub fn copy_tree(
debug!(?total_bytes, ?total_files, temp_dir = ?temp_dir.path(), "Copied source tree");
Ok(temp_dir)
}

#[cfg(test)]
mod test {
use std::fs::{create_dir, write};

use camino::Utf8PathBuf;
use tempfile::TempDir;

use crate::console::Console;
use crate::Result;

use super::copy_tree;

/// Test for regression of <https://github.com/sourcefrog/cargo-mutants/issues/450>
#[test]
fn copy_tree_with_parent_ignoring_star() -> Result<()> {
let tmp_dir = TempDir::new().unwrap();
let tmp = tmp_dir.path();
write(tmp.join(".gitignore"), "*\n")?;

let a = Utf8PathBuf::try_from(tmp.join("a")).unwrap();
create_dir(&a)?;
write(a.join("Cargo.toml"), "[package]\nname = a")?;
let src = a.join("src");
create_dir(&src)?;
write(src.join("main.rs"), "fn main() {}")?;

let dest_tmpdir = copy_tree(&a, "a", true, &Console::new())?;
let dest = dest_tmpdir.path();
assert!(dest.join("Cargo.toml").is_file());
assert!(dest.join("src").is_dir());
assert!(dest.join("src/main.rs").is_file());

Ok(())
}
}

0 comments on commit c95bc57

Please sign in to comment.