这是indexloc提供的服务,不要输入任何密码
Skip to content

feat: respect gitignore during turbo prune #9711

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

Merged
merged 9 commits into from
Jan 17, 2025
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/turborepo-fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ workspace = true

[dependencies]
fs-err = "2.9.0"
ignore = "0.4.22"
thiserror = "1.0.38"
turbopath = { workspace = true }
walkdir = "2.3.3"

[dev-dependencies]
tempfile = { workspace = true }
63 changes: 58 additions & 5 deletions crates/turborepo-fs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::{
};

use fs_err as fs;
use ignore::WalkBuilder;
use turbopath::{AbsoluteSystemPath, AnchoredSystemPathBuf};
use walkdir::WalkDir;

#[derive(Debug, thiserror::Error)]
pub enum Error {
Expand All @@ -16,7 +16,7 @@ pub enum Error {
#[error(transparent)]
Io(#[from] io::Error),
#[error("error walking directory during recursive copy: {0}")]
Walk(#[from] walkdir::Error),
Walk(#[from] ignore::Error),
}

pub fn recursive_copy(
Expand All @@ -28,8 +28,14 @@ pub fn recursive_copy(
let src_metadata = src.symlink_metadata()?;

if src_metadata.is_dir() {
let walker = WalkDir::new(src.as_path()).follow_links(false);
for entry in walker.into_iter() {
let walker = WalkBuilder::new(src.as_path())
.hidden(false)
.git_ignore(true)
.git_global(false)
.git_exclude(true)
.build();

for entry in walker {
match entry {
Err(e) => {
if e.io_error().is_some() {
Expand All @@ -43,7 +49,9 @@ pub fn recursive_copy(
Ok(entry) => {
let path = entry.path();
let path = AbsoluteSystemPath::from_std_path(path)?;
let file_type = entry.file_type();
let file_type = entry
.file_type()
.expect("all dir entries aside from stdin should have a file type");

// Note that we also don't currently copy broken symlinks
if file_type.is_symlink() && path.stat().is_err() {
Expand Down Expand Up @@ -310,6 +318,51 @@ mod tests {
Ok(())
}

#[test]
fn test_recursive_copy_gitignore() -> Result<(), Error> {
// Directory layout:
//
// <src>/
// .gitignore
// invisible.txt <- ignored
// dist/ <- ignored
// output.txt
// child/
// seen.txt
// .hidden
let (_src_tmp, src_dir) = tmp_dir()?;
// Need to create this for `.gitignore` to be respected
src_dir.join_component(".git").create_dir_all()?;
src_dir
.join_component(".gitignore")
.create_with_contents("invisible.txt\ndist/\n")?;
src_dir
.join_component("invisible.txt")
.create_with_contents("not here")?;
let output = src_dir.join_components(&["dist", "output.txt"]);
output.ensure_dir()?;
output.create_with_contents("hi!")?;

let child = src_dir.join_component("child");
let seen = child.join_component("seen.txt");
seen.ensure_dir()?;
seen.create_with_contents("here")?;
let hidden = child.join_component(".hidden");
hidden.create_with_contents("polo")?;

let (_dst_tmp, dst_dir) = tmp_dir()?;
recursive_copy(&src_dir, &dst_dir)?;

assert!(dst_dir.join_component(".gitignore").exists());
assert!(!dst_dir.join_component("invisible.txt").exists());
assert!(!dst_dir.join_component("dist").exists());
assert!(dst_dir.join_component("child").exists());
assert!(dst_dir.join_components(&["child", "seen.txt"]).exists());
assert!(dst_dir.join_components(&["child", ".hidden"]).exists());

Ok(())
}

fn assert_file_matches(a: impl AsRef<AbsoluteSystemPath>, b: impl AsRef<AbsoluteSystemPath>) {
let a = a.as_ref();
let b = b.as_ref();
Expand Down
Loading