Skip to content

Commit

Permalink
Run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
thecrypticace authored and RobinMalfait committed Jan 28, 2025
1 parent 51dd2ef commit b7d5e4c
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 78 deletions.
96 changes: 47 additions & 49 deletions crates/oxide/src/scanner/allowed_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ pub fn resolve_allowed_paths(root: &Path) -> impl Iterator<Item = DirEntry> {

#[tracing::instrument(skip_all)]
pub fn resolve_paths(root: &Path) -> impl Iterator<Item = DirEntry> {
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<usize>) -> impl Iterator<Item = DirEntry> {
Expand All @@ -56,58 +54,58 @@ pub fn read_dir(root: &Path, depth: Option<usize>) -> impl Iterator<Item = DirEn
}

fn create_walk_builder(root: &Path) -> 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 {
Expand Down
70 changes: 41 additions & 29 deletions crates/oxide/tests/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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!(
Expand All @@ -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!(
Expand Down

0 comments on commit b7d5e4c

Please sign in to comment.