7
7
//! including getting changed files, comparing branches, and producing analysis results.
8
8
9
9
use anyhow:: { Context as _, Result } ;
10
+ use log:: { debug, error, info} ;
10
11
use octocrab:: params:: pulls:: MediaType :: Raw ;
11
12
use octocrab:: Octocrab ;
12
13
use regex:: Regex ;
@@ -70,7 +71,7 @@ fn analyze_annotations(
70
71
head_branch : & str ,
71
72
rules : & [ String ] ,
72
73
) -> Result < AnalysisResult > {
73
- println ! ( "Analyzing clippy annotations in {} files..." , files. len( ) ) ;
74
+ debug ! ( "Analyzing clippy annotations in {} files..." , files. len( ) ) ;
74
75
75
76
// Create a regex for matching clippy allow annotations
76
77
let rule_pattern = rules. join ( "|" ) ;
@@ -80,25 +81,22 @@ fn analyze_annotations(
80
81
) )
81
82
. context ( "Failed to compile annotation regex" ) ?;
82
83
83
- let mut base_annotations = Vec :: with_capacity ( files . len ( ) * 3 ) ;
84
- let mut head_annotations = Vec :: with_capacity ( files . len ( ) * 3 ) ;
85
- let mut changed_files = HashSet :: with_capacity ( files . len ( ) ) ;
84
+ let mut base_annotations = Vec :: new ( ) ;
85
+ let mut head_annotations = Vec :: new ( ) ;
86
+ let mut changed_files = HashSet :: new ( ) ;
86
87
87
88
// Cache for rule Rc instances to avoid duplicates
89
+ // TODO: EK - why?
88
90
let mut rule_cache = HashMap :: new ( ) ;
89
91
90
- // Process each file
91
92
for file in files {
92
93
changed_files. insert ( file. clone ( ) ) ;
93
94
94
95
// Get file content from base branch
95
96
let base_content = match get_file_content ( file, base_branch) {
96
97
Ok ( content) => content,
97
98
Err ( e) => {
98
- println ! (
99
- "Warning: Failed to get {} content from {}: {}" ,
100
- file, base_branch, e
101
- ) ;
99
+ error ! ( "Failed to get {} content from {}: {}" , file, base_branch, e) ;
102
100
String :: new ( )
103
101
}
104
102
} ;
@@ -107,10 +105,7 @@ fn analyze_annotations(
107
105
let head_content = match get_file_content ( file, head_branch) {
108
106
Ok ( content) => content,
109
107
Err ( e) => {
110
- println ! (
111
- "Warning: Failed to get {} content from {}: {}" ,
112
- file, head_branch, e
113
- ) ;
108
+ error ! ( "Failed to get {} content from {}: {}" , file, head_branch, e) ;
114
109
String :: new ( )
115
110
}
116
111
} ;
@@ -142,7 +137,7 @@ fn analyze_annotations(
142
137
let base_crate_counts = count_annotations_by_crate ( & base_annotations) ;
143
138
let head_crate_counts = count_annotations_by_crate ( & head_annotations) ;
144
139
145
- println ! (
140
+ info ! (
146
141
"Analysis complete. Found {} annotations in base branch and {} in head branch" ,
147
142
base_annotations. len( ) ,
148
143
head_annotations. len( )
@@ -166,7 +161,7 @@ async fn get_changed_files(
166
161
repo : & str ,
167
162
pr_number : u64 ,
168
163
) -> Result < Vec < String > > {
169
- println ! ( "Getting changed files from PR #{}..." , pr_number) ;
164
+ info ! ( "Getting changed files from PR #{}..." , pr_number) ;
170
165
171
166
let files = octocrab
172
167
. pulls ( owner, repo)
@@ -182,14 +177,14 @@ async fn get_changed_files(
182
177
. map ( |file| file. filename )
183
178
. collect ( ) ;
184
179
185
- println ! ( "Found {} changed Rust files" , rust_files. len( ) ) ;
180
+ info ! ( "Found {} changed Rust files" , rust_files. len( ) ) ;
186
181
187
182
Ok ( rust_files)
188
183
}
189
184
190
185
/// Get file content from a specific branch
191
186
fn get_file_content ( file : & str , branch : & str ) -> Result < String > {
192
- println ! ( "Getting content for {} from {}" , file, branch) ;
187
+ debug ! ( "Getting content for {} from {}" , file, branch) ;
193
188
194
189
let output = Command :: new ( "git" )
195
190
. args ( [ "show" , & format ! ( "{}:{}" , branch, file) ] )
@@ -316,7 +311,7 @@ fn count_annotations_by_crate(annotations: &[ClippyAnnotation]) -> HashMap<Rc<St
316
311
317
312
/// Get all Rust files in the repository
318
313
fn get_all_rust_files ( ) -> Result < Vec < String > > {
319
- println ! ( "Getting all Rust files in the repository..." ) ;
314
+ info ! ( "Getting all Rust files in the repository..." ) ;
320
315
321
316
// Use git ls-files to get all tracked Rust files
322
317
let output = Command :: new ( "git" )
@@ -333,7 +328,7 @@ fn get_all_rust_files() -> Result<Vec<String>> {
333
328
334
329
let rust_files: Vec < String > = files. lines ( ) . map ( |line| line. to_owned ( ) ) . collect ( ) ;
335
330
336
- println ! ( "Found {} Rust files in total" , rust_files. len( ) ) ;
331
+ info ! ( "Found {} Rust files in total" , rust_files. len( ) ) ;
337
332
338
333
Ok ( rust_files)
339
334
}
@@ -345,7 +340,7 @@ fn analyze_all_files_for_crates(
345
340
head_branch : & str ,
346
341
rules : & [ String ] ,
347
342
) -> Result < ( HashMap < Rc < String > , usize > , HashMap < Rc < String > , usize > ) > {
348
- println ! (
343
+ info ! (
349
344
"Analyzing all {} Rust files for crate-level statistics..." ,
350
345
files. len( )
351
346
) ;
@@ -364,35 +359,8 @@ fn analyze_all_files_for_crates(
364
359
let mut rule_cache = HashMap :: new ( ) ;
365
360
// Process each file
366
361
for file in files {
367
- // Get file content from base branch
368
- let base_content = match get_file_content ( file, base_branch) {
369
- Ok ( content) => content,
370
- Err ( e) => {
371
- // Skip errors for files that might not exist in one branch
372
- if !e. to_string ( ) . contains ( "did not match any file" ) {
373
- println ! (
374
- "Warning: Failed to get {} content from {}: {}" ,
375
- file, base_branch, e
376
- ) ;
377
- }
378
- String :: new ( )
379
- }
380
- } ;
381
-
382
- // Get file content from head branch
383
- let head_content = match get_file_content ( file, head_branch) {
384
- Ok ( content) => content,
385
- Err ( e) => {
386
- // Skip errors for files that might not exist in one branch
387
- if !e. to_string ( ) . contains ( "did not match any file" ) {
388
- println ! (
389
- "Warning: Failed to get {} content from {}: {}" ,
390
- file, head_branch, e
391
- ) ;
392
- }
393
- String :: new ( )
394
- }
395
- } ;
362
+ let base_content = get_branch_content ( file, base_branch) ;
363
+ let head_content = get_branch_content ( file, head_branch) ;
396
364
397
365
// Find annotations in base branch
398
366
find_annotations (
@@ -419,6 +387,20 @@ fn analyze_all_files_for_crates(
419
387
Ok ( ( base_crate_counts, head_crate_counts) )
420
388
}
421
389
390
+ /// Get file content from a branch, handling common errors
391
+ fn get_branch_content ( file : & str , branch : & str ) -> String {
392
+ match get_file_content ( file, branch) {
393
+ Ok ( content) => content,
394
+ Err ( e) => {
395
+ // Skip errors for files that might not exist in one branch
396
+ if !e. to_string ( ) . contains ( "did not match any file" ) {
397
+ log:: warn!( "Failed to get {} content from {}: {}" , file, branch, e) ;
398
+ }
399
+ String :: new ( )
400
+ }
401
+ }
402
+ }
403
+
422
404
#[ cfg( test) ]
423
405
mod tests {
424
406
use super :: * ;
0 commit comments