Skip to content

Commit 94ad1e3

Browse files
author
Gal Ben David
committed
replaced atomic bool with crossbeam atomic cell
1 parent 1ad89d1 commit 94ad1e3

File tree

2 files changed

+18
-26
lines changed

2 files changed

+18
-26
lines changed

src/git_repository_scanner.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::rules_manager;
22

33
use chrono::prelude::*;
4+
use crossbeam_utils::atomic::AtomicCell;
45
use crossbeam_utils::thread as crossbeam_thread;
56
use crossbeam::queue::SegQueue;
67
use git2::{Oid, Repository, Delta};
@@ -9,12 +10,11 @@ use pyo3::prelude::*;
910
use std::collections::HashMap;
1011
use std::path::Path;
1112
use std::sync::Arc;
12-
use std::sync::atomic::{AtomicBool, Ordering};
1313
use std::thread;
1414
use std::time;
1515

1616
fn scan_commit_oid(
17-
should_stop: &AtomicBool,
17+
should_stop: &AtomicCell<bool>,
1818
git_repo: &Repository,
1919
oid: &Oid,
2020
rules_manager: &rules_manager::RulesManager,
@@ -39,7 +39,7 @@ fn scan_commit_oid(
3939
};
4040

4141
for delta in commit_diff.deltas() {
42-
if should_stop.load(Ordering::Relaxed) {
42+
if should_stop.load() {
4343
break;
4444
}
4545

@@ -80,7 +80,6 @@ fn scan_commit_oid(
8080
if let Some(scan_matches) = scan_matches {
8181
for scan_match in scan_matches.iter() {
8282
let mut match_hashmap = HashMap::with_capacity(9);
83-
let commit_date = Utc.timestamp(commit.time().seconds(), 0);
8483
match_hashmap.insert(
8584
"commit_id",
8685
commit.id().to_string(),
@@ -91,7 +90,7 @@ fn scan_commit_oid(
9190
);
9291
match_hashmap.insert(
9392
"commit_time",
94-
commit_date.format("%Y-%m-%dT%H:%M:%S").to_string(),
93+
Utc.timestamp(commit.time().seconds(), 0).format("%Y-%m-%dT%H:%M:%S").to_string(),
9594
);
9695
match_hashmap.insert(
9796
"author_name",
@@ -185,22 +184,19 @@ pub fn scan_repository(
185184
return Err(pyo3::exceptions::PyRuntimeError::new_err(error.to_string()))
186185
},
187186
}
187+
188188
py.check_signals()?;
189189

190190
let mut py_signal_error: PyResult<()> = Ok(());
191191

192+
let should_stop = AtomicCell::new(false);
192193
crossbeam_thread::scope(
193194
|scope| {
194-
let should_stop = Arc::new(AtomicBool::new(false));
195-
196195
for _ in 0..num_cpus::get() {
197-
let output_matches = output_matches.clone();
198-
let oids_queue = oids_queue.clone();
199-
let should_stop = should_stop.clone();
200196
scope.spawn(
201-
move |_| {
197+
|_| {
202198
if let Ok(git_repo) = Repository::open(repository_path) {
203-
while !should_stop.load(Ordering::Relaxed) {
199+
while !should_stop.load() {
204200
if let Some(oid) = oids_queue.pop() {
205201
scan_commit_oid(
206202
&should_stop,
@@ -221,7 +217,7 @@ pub fn scan_repository(
221217
while !oids_queue.is_empty() {
222218
py_signal_error = py.check_signals();
223219
if py_signal_error.is_err() {
224-
should_stop.store(true, Ordering::Relaxed);
220+
should_stop.store(true);
225221

226222
break;
227223
}

src/rules_manager.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,25 +183,21 @@ impl RulesManager {
183183
&self,
184184
file_path: &str,
185185
) -> bool {
186-
let skip_file = self.file_extensions_to_skip.iter().any(
186+
if self.file_extensions_to_skip.iter().any(
187187
|file_extension_to_skip| {
188188
file_path.ends_with(file_extension_to_skip)
189189
}
190-
);
191-
if skip_file {
192-
return false;
193-
}
194-
195-
let skip_file = self.file_paths_to_skip.iter().any(
190+
) {
191+
false
192+
} else if self.file_paths_to_skip.iter().any(
196193
|file_path_to_skip| {
197194
file_path.contains(file_path_to_skip)
198195
}
199-
);
200-
if skip_file {
201-
return false;
196+
) {
197+
false
198+
} else {
199+
true
202200
}
203-
204-
true
205201
}
206202

207203
#[text_signature = "(file_path, content, /)"]
@@ -213,7 +209,7 @@ impl RulesManager {
213209
let mut scan_matches = Vec::new();
214210

215211
for file_path_rule in self.file_path_rules.iter() {
216-
for _match_text in file_path_rule.regex.find_iter(file_path) {
212+
if file_path_rule.regex.is_match(file_path) {
217213
let mut scan_match = HashMap::<&str, String>::new();
218214
scan_match.insert("rule_name", file_path_rule.name.clone());
219215
scan_match.insert("match_text", file_path.to_string());

0 commit comments

Comments
 (0)