@@ -3,9 +3,10 @@ use crate::rules_manager;
33use chrono:: prelude:: * ;
44use crossbeam_utils:: atomic:: AtomicCell ;
55use crossbeam_utils:: thread as crossbeam_thread;
6- use crossbeam:: queue:: SegQueue ;
6+ use crossbeam:: queue:: ArrayQueue ;
77use git2:: { Oid , Repository , Delta } ;
88use parking_lot:: Mutex ;
9+ use pyo3:: exceptions:: PyRuntimeError ;
910use pyo3:: prelude:: * ;
1011use std:: collections:: HashMap ;
1112use std:: path:: Path ;
@@ -124,18 +125,7 @@ fn scan_commit_oid(
124125 Ok ( ( ) )
125126}
126127
127- pub fn get_file_content (
128- repository_path : & str ,
129- file_oid : & str ,
130- ) -> Result < Vec < u8 > , git2:: Error > {
131- let git_repo = Repository :: open ( repository_path) ?;
132- let oid = Oid :: from_str ( file_oid) ?;
133- let blob = git_repo. find_blob ( oid) ?;
134-
135- Ok ( blob. content ( ) . to_vec ( ) )
136- }
137-
138- fn get_oids (
128+ fn get_commit_oids (
139129 repository_path : & str ,
140130 branch_glob_pattern : & str ,
141131 from_timestamp : i64 ,
@@ -166,40 +156,46 @@ pub fn scan_repository(
166156 from_timestamp : i64 ,
167157 rules_manager : & rules_manager:: RulesManager ,
168158 output_matches : Arc < Mutex < Vec < HashMap < & str , String > > > > ,
169- ) -> Result < ( ) , PyErr > {
170- let oids_queue = Arc :: new ( SegQueue :: new ( ) ) ;
171- match get_oids (
159+ ) -> PyResult < ( ) > {
160+ let commit_oids_queue;
161+
162+ match get_commit_oids (
172163 repository_path,
173164 branch_glob_pattern,
174165 from_timestamp
175166 ) {
176- Ok ( oids) => {
177- for oid in oids {
178- oids_queue. push ( oid) ;
167+ Ok ( commit_oids) => {
168+ if commit_oids. is_empty ( ) {
169+ return Ok ( ( ) ) ;
170+ }
171+
172+ commit_oids_queue = ArrayQueue :: new ( commit_oids. len ( ) ) ;
173+ for commit_oid in commit_oids {
174+ commit_oids_queue. push ( commit_oid) . unwrap ( ) ;
179175 }
180176 } ,
181177 Err ( error) => {
182- return Err ( pyo3 :: exceptions :: PyRuntimeError :: new_err ( error. to_string ( ) ) )
178+ return Err ( PyRuntimeError :: new_err ( error. to_string ( ) ) )
183179 } ,
184180 }
185181
186- py. check_signals ( ) ?;
187-
188182 let mut py_signal_error: PyResult < ( ) > = Ok ( ( ) ) ;
189183
190184 let should_stop = AtomicCell :: new ( false ) ;
185+ let number_of_cores = std:: thread:: available_parallelism ( ) . unwrap ( ) . get ( ) ;
186+
191187 crossbeam_thread:: scope (
192188 |scope| {
193- for _ in 0 ..num_cpus :: get ( ) {
189+ for _ in 0 ..number_of_cores {
194190 scope. spawn (
195191 |_| {
196192 if let Ok ( git_repo) = Repository :: open ( repository_path) {
197193 while !should_stop. load ( ) {
198- if let Some ( oid ) = oids_queue . pop ( ) {
194+ if let Some ( commit_oid ) = commit_oids_queue . pop ( ) {
199195 scan_commit_oid (
200196 & should_stop,
201197 & git_repo,
202- & oid ,
198+ & commit_oid ,
203199 rules_manager,
204200 output_matches. clone ( ) ,
205201 ) . unwrap_or ( ( ) ) ;
@@ -212,7 +208,7 @@ pub fn scan_repository(
212208 ) ;
213209 }
214210
215- while !oids_queue . is_empty ( ) {
211+ while !commit_oids_queue . is_empty ( ) {
216212 py_signal_error = py. check_signals ( ) ;
217213 if py_signal_error. is_err ( ) {
218214 should_stop. store ( true ) ;
@@ -223,7 +219,7 @@ pub fn scan_repository(
223219 thread:: sleep ( time:: Duration :: from_millis ( 100 ) ) ;
224220 }
225221 }
226- ) . unwrap_or ( ( ) ) ;
222+ ) . unwrap_or_default ( ) ;
227223
228224 py_signal_error?;
229225
0 commit comments