@@ -3,9 +3,10 @@ use crate::rules_manager;
3
3
use chrono:: prelude:: * ;
4
4
use crossbeam_utils:: atomic:: AtomicCell ;
5
5
use crossbeam_utils:: thread as crossbeam_thread;
6
- use crossbeam:: queue:: SegQueue ;
6
+ use crossbeam:: queue:: ArrayQueue ;
7
7
use git2:: { Oid , Repository , Delta } ;
8
8
use parking_lot:: Mutex ;
9
+ use pyo3:: exceptions:: PyRuntimeError ;
9
10
use pyo3:: prelude:: * ;
10
11
use std:: collections:: HashMap ;
11
12
use std:: path:: Path ;
@@ -124,18 +125,7 @@ fn scan_commit_oid(
124
125
Ok ( ( ) )
125
126
}
126
127
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 (
139
129
repository_path : & str ,
140
130
branch_glob_pattern : & str ,
141
131
from_timestamp : i64 ,
@@ -166,40 +156,46 @@ pub fn scan_repository(
166
156
from_timestamp : i64 ,
167
157
rules_manager : & rules_manager:: RulesManager ,
168
158
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 (
172
163
repository_path,
173
164
branch_glob_pattern,
174
165
from_timestamp
175
166
) {
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 ( ) ;
179
175
}
180
176
} ,
181
177
Err ( error) => {
182
- return Err ( pyo3 :: exceptions :: PyRuntimeError :: new_err ( error. to_string ( ) ) )
178
+ return Err ( PyRuntimeError :: new_err ( error. to_string ( ) ) )
183
179
} ,
184
180
}
185
181
186
- py. check_signals ( ) ?;
187
-
188
182
let mut py_signal_error: PyResult < ( ) > = Ok ( ( ) ) ;
189
183
190
184
let should_stop = AtomicCell :: new ( false ) ;
185
+ let number_of_cores = std:: thread:: available_parallelism ( ) . unwrap ( ) . get ( ) ;
186
+
191
187
crossbeam_thread:: scope (
192
188
|scope| {
193
- for _ in 0 ..num_cpus :: get ( ) {
189
+ for _ in 0 ..number_of_cores {
194
190
scope. spawn (
195
191
|_| {
196
192
if let Ok ( git_repo) = Repository :: open ( repository_path) {
197
193
while !should_stop. load ( ) {
198
- if let Some ( oid ) = oids_queue . pop ( ) {
194
+ if let Some ( commit_oid ) = commit_oids_queue . pop ( ) {
199
195
scan_commit_oid (
200
196
& should_stop,
201
197
& git_repo,
202
- & oid ,
198
+ & commit_oid ,
203
199
rules_manager,
204
200
output_matches. clone ( ) ,
205
201
) . unwrap_or ( ( ) ) ;
@@ -212,7 +208,7 @@ pub fn scan_repository(
212
208
) ;
213
209
}
214
210
215
- while !oids_queue . is_empty ( ) {
211
+ while !commit_oids_queue . is_empty ( ) {
216
212
py_signal_error = py. check_signals ( ) ;
217
213
if py_signal_error. is_err ( ) {
218
214
should_stop. store ( true ) ;
@@ -223,7 +219,7 @@ pub fn scan_repository(
223
219
thread:: sleep ( time:: Duration :: from_millis ( 100 ) ) ;
224
220
}
225
221
}
226
- ) . unwrap_or ( ( ) ) ;
222
+ ) . unwrap_or_default ( ) ;
227
223
228
224
py_signal_error?;
229
225
0 commit comments