Skip to content
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
20 changes: 8 additions & 12 deletions src/directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,47 +75,43 @@ pub fn parse<R: BufRead>(
if let Ok(line) = line_result {
// Tags
for captures in tag_regex.captures_iter(&line) {
// If we got a match, then `captures.get(1)` is guaranteed to return a `Some`. Hence
// we are justified in unwrapping.
let (_, [label]) = captures.extract();
tags.push(Directive {
r#type: Type::Tag,
label: captures.get(1).unwrap().as_str().to_owned(),
label: label.to_owned(),
path: path.to_owned(),
line_number: line_number + 1,
});
}

// Refs
for captures in ref_regex.captures_iter(&line) {
// If we got a match, then `captures.get(1)` is guaranteed to return a `Some`. Hence
// we are justified in unwrapping.
let (_, [label]) = captures.extract();
refs.push(Directive {
r#type: Type::Ref,
label: captures.get(1).unwrap().as_str().to_owned(),
label: label.to_owned(),
path: path.to_owned(),
line_number: line_number + 1,
});
}

// Files
for captures in file_regex.captures_iter(&line) {
// If we got a match, then `captures.get(1)` is guaranteed to return a `Some`. Hence
// we are justified in unwrapping.
let (_, [label]) = captures.extract();
files.push(Directive {
r#type: Type::File,
label: captures.get(1).unwrap().as_str().to_owned(),
label: label.to_owned(),
path: path.to_owned(),
line_number: line_number + 1,
});
}

// Directories
for captures in dir_regex.captures_iter(&line) {
// If we got a match, then `captures.get(1)` is guaranteed to return a `Some`. Hence
// we are justified in unwrapping.
let (_, [label]) = captures.extract();
dirs.push(Directive {
r#type: Type::Dir,
label: captures.get(1).unwrap().as_str().to_owned(),
label: label.to_owned(),
path: path.to_owned(),
line_number: line_number + 1,
});
Expand Down
51 changes: 23 additions & 28 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,34 +114,29 @@ fn entry() -> Result<(), String> {
let project_root = config.project_root;
let ignore_rules = config.ignore_rules;
let project_root_clone = project_root.clone();
let files_scanned = walk::walk(
std::slice::from_ref(&project_root),
&project_root,
&ignore_rules,
move |file_path, file| {
// The `unwrap` is safe because the walker is rooted at `project_root_clone`.
let relative_file_path = file_path.strip_prefix(&project_root_clone).unwrap();
let directives = directive::parse(
&tag_regex_clone,
&ref_regex_clone,
&file_regex_clone,
&dir_regex_clone,
relative_file_path,
BufReader::new(file),
);
for tag in directives.tags {
tags_clone
.lock()
.unwrap() // Safe assuming no poisoning
.entry(tag.label.clone())
.or_insert_with(Vec::new)
.push(tag.clone());
}
refs_clone.lock().unwrap().extend(directives.refs); // Safe assuming no poisoning
files_clone.lock().unwrap().extend(directives.files); // Safe assuming no poisoning
dirs_clone.lock().unwrap().extend(directives.dirs); // Safe assuming no poisoning
},
)?;
let files_scanned = walk::walk(&project_root, &ignore_rules, move |file_path, file| {
// The `unwrap` is safe because the walker is rooted at `project_root_clone`.
let relative_file_path = file_path.strip_prefix(&project_root_clone).unwrap();
let directives = directive::parse(
&tag_regex_clone,
&ref_regex_clone,
&file_regex_clone,
&dir_regex_clone,
relative_file_path,
BufReader::new(file),
);
for tag in directives.tags {
tags_clone
.lock()
.unwrap() // Safe assuming no poisoning
.entry(tag.label.clone())
.or_insert_with(Vec::new)
.push(tag.clone());
}
refs_clone.lock().unwrap().extend(directives.refs); // Safe assuming no poisoning
files_clone.lock().unwrap().extend(directives.files); // Safe assuming no poisoning
dirs_clone.lock().unwrap().extend(directives.dirs); // Safe assuming no poisoning
})?;

// Decide what to do based on the subcommand.
match cli.command.unwrap_or(Subcommand::Check) {
Expand Down
72 changes: 34 additions & 38 deletions src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ignore::{
};
use std::{
fs::File,
path::{Path, PathBuf},
path::Path,
sync::{
Arc,
atomic::{AtomicUsize, Ordering},
Expand All @@ -15,53 +15,49 @@ use std::{
// and the file. It skips files which cannot be read (e.g., due to lack of permissions). It also
// skips over symlinks. The number of files traversed is returned.
pub fn walk<T: 'static + Clone + Send + FnMut(&Path, File)>(
paths: &[PathBuf],
current_dir: &Path,
path: &Path,
ignore_rules: &[String],
callback: T,
) -> Result<usize, String> {
// Keep track of the number of files traversed, and allow multiple threads to update it.
let files_scanned = Arc::new(AtomicUsize::new(0));
let overrides = build_overrides(current_dir, ignore_rules)?;
let overrides = build_overrides(path, ignore_rules)?;

// Scan each of the given paths.
for path in paths {
// Traverse the filesystem in parallel.
WalkBuilder::new(path)
.current_dir(current_dir)
.hidden(false)
.require_git(false)
.parents(false)
.overrides(overrides.clone())
.build_parallel()
.run(|| {
// These clones will be moved into the closure below, and that closure will be sent
// to a new thread.
let mut callback = callback.clone();
let files_scanned = files_scanned.clone();
// Traverse the filesystem in parallel.
WalkBuilder::new(path)
.current_dir(path)
.hidden(false)
.require_git(false)
.parents(false)
.overrides(overrides)
.build_parallel()
.run(|| {
// These clones will be moved into the closure below, and that closure will be sent
// to a new thread.
let mut callback = callback.clone();
let files_scanned = files_scanned.clone();

// This closure will be sent to a new thread.
Box::new(move |result| {
// Proceed if we have access to the path.
if let Ok(dir_entry) = result {
// Here, `file_type()` should always return a `Some`. It could only return
// `None` if the file represents STDIN, and that isn't the case here.
if dir_entry.file_type().unwrap().is_file() {
// Try to open the file.
let possible_file = File::open(dir_entry.path());
if let Ok(file) = possible_file {
// Process the file and increment the counter.
callback(dir_entry.path(), file);
files_scanned.fetch_add(1, Ordering::SeqCst);
}
// This closure will be sent to a new thread.
Box::new(move |result| {
// Proceed if we have access to the path.
if let Ok(dir_entry) = result {
// Here, `file_type()` should always return a `Some`. It could only return
// `None` if the file represents STDIN, and that isn't the case here.
if dir_entry.file_type().unwrap().is_file() {
// Try to open the file.
let possible_file = File::open(dir_entry.path());
if let Ok(file) = possible_file {
// Process the file and increment the counter.
callback(dir_entry.path(), file);
files_scanned.fetch_add(1, Ordering::SeqCst);
}
}
}

// Don't stop...believing!
WalkState::Continue
})
});
}
// Don't stop...believing!
WalkState::Continue
})
});

// Return the number of files traversed.
Ok(files_scanned.load(Ordering::SeqCst))
Expand Down