diff --git a/crates/oxide/src/scanner/allowed_paths.rs b/crates/oxide/src/scanner/allowed_paths.rs index dd8ef074ad7b..b906335f8979 100644 --- a/crates/oxide/src/scanner/allowed_paths.rs +++ b/crates/oxide/src/scanner/allowed_paths.rs @@ -33,9 +33,7 @@ pub fn resolve_allowed_paths(root: &Path) -> impl Iterator { #[tracing::instrument(skip_all)] pub fn resolve_paths(root: &Path) -> impl Iterator { - create_walk_builder(root) - .build() - .filter_map(Result::ok) + create_walk_builder(root).build().filter_map(Result::ok) } pub fn read_dir(root: &Path, depth: Option) -> impl Iterator { @@ -56,58 +54,58 @@ pub fn read_dir(root: &Path, depth: Option) -> impl Iterator WalkBuilder { - let mut builder = WalkBuilder::new(root); + let mut builder = WalkBuilder::new(root); - // Scan hidden files / directories - builder.hidden(false); + // Scan hidden files / directories + builder.hidden(false); - // By default, allow .gitignore files to be used regardless of whether or not - // a .git directory is present. This is an optimization for when projects - // are first created and may not be in a git repo yet. - builder.require_git(false); + // By default, allow .gitignore files to be used regardless of whether or not + // a .git directory is present. This is an optimization for when projects + // are first created and may not be in a git repo yet. + builder.require_git(false); - // Don't descend into .git directories inside the root folder - // This is necessary when `root` contains the `.git` dir. - builder.filter_entry(|entry| entry.file_name() != ".git"); + // Don't descend into .git directories inside the root folder + // This is necessary when `root` contains the `.git` dir. + builder.filter_entry(|entry| entry.file_name() != ".git"); - // If we are in a git repo then require it to ensure that only rules within - // the repo are used. For example, we don't want to consider a .gitignore file - // in the user's home folder if we're in a git repo. - // - // The alternative is using a call like `.parents(false)` but that will - // prevent looking at parent directories for .gitignore files from within - // the repo and that's not what we want. - // - // For example, in a project with this structure: - // - // home - // .gitignore - // my-project - // .gitignore - // apps - // .gitignore - // web - // {root} - // - // We do want to consider all .gitignore files listed: - // - home/.gitignore - // - my-project/.gitignore - // - my-project/apps/.gitignore - // - // However, if a repo is initialized inside my-project then only the following - // make sense for consideration: - // - my-project/.gitignore - // - my-project/apps/.gitignore - // - // Setting the require_git(true) flag conditionally allows us to do this. - for parent in root.ancestors() { - if parent.join(".git").exists() { - builder.require_git(true); - break; + // If we are in a git repo then require it to ensure that only rules within + // the repo are used. For example, we don't want to consider a .gitignore file + // in the user's home folder if we're in a git repo. + // + // The alternative is using a call like `.parents(false)` but that will + // prevent looking at parent directories for .gitignore files from within + // the repo and that's not what we want. + // + // For example, in a project with this structure: + // + // home + // .gitignore + // my-project + // .gitignore + // apps + // .gitignore + // web + // {root} + // + // We do want to consider all .gitignore files listed: + // - home/.gitignore + // - my-project/.gitignore + // - my-project/apps/.gitignore + // + // However, if a repo is initialized inside my-project then only the following + // make sense for consideration: + // - my-project/.gitignore + // - my-project/apps/.gitignore + // + // Setting the require_git(true) flag conditionally allows us to do this. + for parent in root.ancestors() { + if parent.join(".git").exists() { + builder.require_git(true); + break; + } } - } - builder + builder } pub fn is_allowed_content_path(path: &Path) -> bool { diff --git a/crates/oxide/tests/scanner.rs b/crates/oxide/tests/scanner.rs index 878050e0ad93..e2bfdf37e558 100644 --- a/crates/oxide/tests/scanner.rs +++ b/crates/oxide/tests/scanner.rs @@ -598,56 +598,62 @@ mod scanner { &[ // This file should always be picked up ("home/project/apps/web/index.html", "content-['index.html']"), - // Set up various ignore rules ("home/.gitignore", "ignore-home.html"), ("home/project/.gitignore", "ignore-project.html"), ("home/project/apps/.gitignore", "ignore-apps.html"), ("home/project/apps/web/.gitignore", "ignore-web.html"), - // Some of these should be ignored depending on which dir is the repo root - ("home/project/apps/web/ignore-home.html", "content-['ignore-home.html']"), - ("home/project/apps/web/ignore-project.html", "content-['ignore-project.html']"), - ("home/project/apps/web/ignore-apps.html", "content-['ignore-apps.html']"), - ("home/project/apps/web/ignore-web.html", "content-['ignore-web.html']"), + ( + "home/project/apps/web/ignore-home.html", + "content-['ignore-home.html']", + ), + ( + "home/project/apps/web/ignore-project.html", + "content-['ignore-project.html']", + ), + ( + "home/project/apps/web/ignore-apps.html", + "content-['ignore-apps.html']", + ), + ( + "home/project/apps/web/ignore-web.html", + "content-['ignore-web.html']", + ), ], ); - - let sources = vec![ - GlobEntry { - base: dir.join("home/project/apps/web").to_string_lossy().to_string(), - pattern: "**/*".to_owned(), - }, - ]; + let sources = vec![GlobEntry { + base: dir + .join("home/project/apps/web") + .to_string_lossy() + .to_string(), + pattern: "**/*".to_owned(), + }]; let candidates = Scanner::new(Some(sources.clone())).scan(); // All ignore files are applied because there's no git repo - assert_eq!( - candidates, - vec![ - "content-['index.html']".to_owned(), - ] - ); + assert_eq!(candidates, vec!["content-['index.html']".to_owned(),]); // Initialize `home` as a git repository and scan again // The results should be the same as before - _ = Command::new("git").arg("init").current_dir(dir.join("home")).output(); + _ = Command::new("git") + .arg("init") + .current_dir(dir.join("home")) + .output(); let candidates = Scanner::new(Some(sources.clone())).scan(); - assert_eq!( - candidates, - vec![ - "content-['index.html']".to_owned(), - ] - ); + assert_eq!(candidates, vec!["content-['index.html']".to_owned(),]); // Drop the .git folder fs::remove_dir_all(dir.join("home/.git")).unwrap(); // Initialize `home/project` as a git repository and scan again - _ = Command::new("git").arg("init").current_dir(dir.join("home/project")).output(); + _ = Command::new("git") + .arg("init") + .current_dir(dir.join("home/project")) + .output(); let candidates = Scanner::new(Some(sources.clone())).scan(); assert_eq!( @@ -662,7 +668,10 @@ mod scanner { fs::remove_dir_all(dir.join("home/project/.git")).unwrap(); // Initialize `home/project/apps` as a git repository and scan again - _ = Command::new("git").arg("init").current_dir(dir.join("home/project/apps")).output(); + _ = Command::new("git") + .arg("init") + .current_dir(dir.join("home/project/apps")) + .output(); let candidates = Scanner::new(Some(sources.clone())).scan(); assert_eq!( @@ -678,7 +687,10 @@ mod scanner { fs::remove_dir_all(dir.join("home/project/apps/.git")).unwrap(); // Initialize `home/project/apps` as a git repository and scan again - _ = Command::new("git").arg("init").current_dir(dir.join("home/project/apps/web")).output(); + _ = Command::new("git") + .arg("init") + .current_dir(dir.join("home/project/apps/web")) + .output(); let candidates = Scanner::new(Some(sources.clone())).scan(); assert_eq!(