-
Notifications
You must be signed in to change notification settings - Fork 38
feat: allow user to pass multiple files to fix and check commands
#119
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,112 +1,69 @@ | ||
| use std::{ | ||
| fs, | ||
| io::{self, Error, ErrorKind}, | ||
| io, | ||
| path::{Path, PathBuf}, | ||
| }; | ||
|
|
||
| use crate::dirs; | ||
| use ignore::{overrides::OverrideBuilder, WalkBuilder}; | ||
|
|
||
| use ignore::{ | ||
| Error as IgnoreError, Match, | ||
| gitignore::{Gitignore, GitignoreBuilder}, | ||
| }; | ||
| /// Walks through target paths and returns an iterator of .nix files, respecting gitignore rules. | ||
| /// | ||
| /// # Arguments | ||
| /// * `ignore_patterns` - Globs of file patterns to skip (e.g., "*.tmp", "build/*") | ||
| /// * `targets` - File or directory paths to walk through | ||
| /// * `unrestricted` - If true, don't respect .gitignore files; if false, respect them | ||
| pub fn walk_nix_files<P: AsRef<Path>>( | ||
| ignore_patterns: &[String], | ||
| targets: &[P], | ||
| unrestricted: bool, | ||
| ) -> Result<impl Iterator<Item = PathBuf>, io::Error> { | ||
| let mut targets_iter = targets.iter(); | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct Walker { | ||
| dirs: Vec<PathBuf>, | ||
| files: Vec<PathBuf>, | ||
| ignore: Gitignore, | ||
| } | ||
| let first_target = targets_iter | ||
| .next() | ||
| .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "No targets provided"))?; | ||
|
|
||
| impl Walker { | ||
| pub fn new<P: AsRef<Path>>(target: P, ignore: Gitignore) -> io::Result<Self> { | ||
| let target = target.as_ref().to_path_buf(); | ||
| if !target.exists() { | ||
| Err(Error::new( | ||
| ErrorKind::NotFound, | ||
| format!("file not found: {}", target.display()), | ||
| )) | ||
| } else if target.is_dir() { | ||
| Ok(Self { | ||
| dirs: vec![target], | ||
| files: vec![], | ||
| ignore, | ||
| }) | ||
| } else { | ||
| Ok(Self { | ||
| dirs: vec![], | ||
| files: vec![target], | ||
| ignore, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| let mut builder = WalkBuilder::new(first_target.as_ref()); | ||
|
|
||
| impl Iterator for Walker { | ||
| type Item = PathBuf; | ||
| fn next(&mut self) -> Option<Self::Item> { | ||
| self.files.pop().or_else(|| { | ||
| while let Some(dir) = self.dirs.pop() { | ||
| if dir.is_dir() | ||
| && let Match::None | Match::Whitelist(_) = self.ignore.matched(&dir, true) | ||
| { | ||
| let mut found = false; | ||
| for entry in fs::read_dir(&dir).ok()? { | ||
| let entry = entry.ok()?; | ||
| let path = entry.path(); | ||
| if path.is_dir() { | ||
| self.dirs.push(path); | ||
| } else if path.is_file() | ||
| && let Match::None | Match::Whitelist(_) = | ||
| self.ignore.matched(&path, false) | ||
| { | ||
| found = true; | ||
| self.files.push(path); | ||
| } | ||
| } | ||
| if found { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| self.files.pop() | ||
| }) | ||
| for target in targets_iter { | ||
| builder.add(target.as_ref()); | ||
| } | ||
| } | ||
|
|
||
| pub fn build_ignore_set<P: AsRef<Path>>( | ||
| ignore: &[String], | ||
| target: P, | ||
| unrestricted: bool, | ||
| ) -> Result<Gitignore, IgnoreError> { | ||
| let gitignore_path = target.as_ref().join(".gitignore"); | ||
| builder.git_ignore(!unrestricted); | ||
|
|
||
| // Looks like GitignoreBuilder::new does not source globs | ||
| // within gitignore_path by default, we have to enforce that | ||
| // using GitignoreBuilder::add. Probably a bug in the ignore | ||
| // crate? | ||
| let mut gitignore = GitignoreBuilder::new(&gitignore_path); | ||
| // Add files/directories to ignore set passed in --ignore | ||
| if !ignore_patterns.is_empty() { | ||
| let mut override_builder = OverrideBuilder::new(""); | ||
|
|
||
| // if we are to "restrict" aka "respect" .gitignore, then | ||
| // add globs from gitignore path as well | ||
| if !unrestricted { | ||
| gitignore.add(&gitignore_path); | ||
| for pattern in ignore_patterns { | ||
| // Note: The `!` prefix has inverted semantics in OverrideBuilder compared to gitignore. | ||
| // In OverrideBuilder: `!pattern` means "ignore files matching pattern" | ||
| // In gitignore: `!pattern` means "don't ignore files matching pattern" (whitelist) | ||
| // So we add `!` to make ignore_patterns actually ignore files. | ||
| override_builder | ||
| .add(&format!("!{pattern}")) | ||
| .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; | ||
| } | ||
|
|
||
| // ignore .git by default, nobody cares about .git, i'm sure | ||
| gitignore.add_line(None, ".git")?; | ||
| } | ||
| let overrides = override_builder | ||
| .build() | ||
| .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?; | ||
|
|
||
| for i in ignore { | ||
| gitignore.add_line(None, i.as_str())?; | ||
| builder.overrides(overrides); | ||
| } | ||
|
|
||
| gitignore.build() | ||
| } | ||
| let walker = builder.build(); | ||
|
|
||
| pub fn walk_nix_files<P: AsRef<Path>>( | ||
| ignore: Gitignore, | ||
| target: P, | ||
| ) -> Result<impl Iterator<Item = PathBuf>, io::Error> { | ||
| let walker = dirs::Walker::new(target, ignore)?; | ||
| Ok(walker.filter(|path: &PathBuf| matches!(path.extension(), Some(e) if e == "nix"))) | ||
| Ok(walker | ||
| .filter_map(|result| match result { | ||
| Ok(entry) => Some(entry), | ||
| Err(err) => { | ||
| eprintln!("Warning: Error reading directory entry: {err}"); | ||
| None | ||
| } | ||
| }) | ||
| .filter_map(|entry| { | ||
| let path = entry.path(); | ||
| (path.is_file() && matches!(path.extension(), Some(ext) if ext == "nix")) | ||
| .then(|| path.to_path_buf()) | ||
| })) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.