guardy 0.2.4

Fast, secure git hooks in Rust with secret scanning and protected file synchronization
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
// All filtering now handled through cached filters in Scanner struct and collect_file_paths
use std::{
    path::{Path, PathBuf},
    sync::Arc,
};

use anyhow::{Context, Result};
use ignore::{WalkBuilder, gitignore::GitignoreBuilder};

use super::types::{ScanStats, Scanner, SecretMatch};
use crate::config::CONFIG;

/// Execution mode determined by file count and configuration
#[derive(Debug, Clone, Copy, PartialEq)]
enum RunMode {
    Sequential,
    Parallel { workers: usize },
}

/// Check if a file should be ignored according to .guardyignore patterns
fn should_ignore_file(path: &Path) -> bool {
    // Find the repository root (where .guardyignore should be)
    let current_dir = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
    let guardyignore_path = current_dir.join(".guardyignore");

    tracing::trace!(
        "Checking .guardyignore for: {} (current_dir: {}, guardyignore_exists: {})",
        path.display(),
        current_dir.display(),
        guardyignore_path.exists()
    );

    if !guardyignore_path.exists() {
        return false;
    }

    // Build gitignore matcher from .guardyignore file
    let mut builder = GitignoreBuilder::new(&current_dir);
    if let Some(e) = builder.add(&guardyignore_path) {
        tracing::warn!("Failed to load .guardyignore: {}", e);
        return false;
    }

    let gitignore = match builder.build() {
        Ok(gi) => gi,
        Err(e) => {
            tracing::warn!("Failed to build gitignore matcher: {}", e);
            return false;
        }
    };

    // Check if path matches any ignore patterns
    let matched = gitignore.matched(path, path.is_dir());
    let is_ignored = matched.is_ignore();

    tracing::trace!(
        "Path {} matched: {:?}, is_ignored: {}",
        path.display(),
        matched,
        is_ignored
    );

    is_ignored
}

// ============================================================================
// IMPORTANT: All scanner types should be defined in types.rs, not here!
// This keeps the codebase modular and prevents type duplication.
// Only implement behavior (impl blocks) in this file.
// ============================================================================

impl Scanner {
    pub fn new() -> Result<Self> {
        // Initialize filters once from CONFIG for reuse throughout scanning
        let scanner_config = &CONFIG.scanner;
        let binary_filter = std::sync::Arc::new(super::filters::directory::BinaryFilter::new(
            !scanner_config.include_binary,
        ));
        let path_filter = std::sync::Arc::new(super::filters::directory::PathFilter::new(
            scanner_config.ignore_paths.clone(),
        ));
        let size_filter = std::sync::Arc::new(super::filters::directory::SizeFilter::new(
            scanner_config.max_file_size_mb,
        ));

        // Initialize content filters for optimization
        let prefilter = std::sync::Arc::new(super::filters::content::ContextPrefilter::new());
        let regex_executor = std::sync::Arc::new(super::filters::content::RegexExecutor::new());
        let comment_filter = std::sync::Arc::new(super::filters::content::CommentFilter::new());

        // Initialize pattern library statistics
        let pattern_lib = super::static_data::patterns::get_pattern_library();
        super::counters::set_pattern_stats(pattern_lib.count(), pattern_lib.keywords().len());

        Ok(Scanner {
            binary_filter,
            path_filter,
            size_filter,
            prefilter,
            regex_executor,
            comment_filter,
        })
    }

    // All filters are initialized from GUARDY_CONFIG and cached for performance
    // These Arc-wrapped filters are reused throughout the scanning process

    // Config parsing functions removed - now using GUARDY_CONFIG directly

    /// Build a WalkBuilder with common directory filtering logic and statistics tracking
    pub(crate) fn build_directory_walker(&self, path: &Path) -> WalkBuilder {
        let mut builder = WalkBuilder::new(path);
        builder
            .follow_links(CONFIG.scanner.follow_symlinks)
            .git_ignore(true) // Respect .gitignore files
            .git_global(true) // Respect global gitignore
            .git_exclude(true) // Respect .git/info/exclude
            .hidden(false) // Don't ignore hidden files by default
            .parents(true) // Check parent directories for .gitignore
            .add_custom_ignore_filename(".guardyignore"); // Respect .guardyignore files (hierarchical)

        // Create fast directory filtering using static data
        let path_filter = self.path_filter.clone();

        builder.filter_entry(move |entry| {
            // FIRST: Fast directory filtering by name (prevents descent)
            if entry.file_type().is_some_and(|ft| ft.is_dir())
                && let Some(dir_name) = entry.file_name().to_str()
                && super::static_data::directory_patterns::SKIP_DIRECTORIES_SET.contains(dir_name)
            {
                tracing::trace!("Skipping directory: {}", entry.path().display());
                super::counters::COUNT_DISCOVERY_PATH_FILTERED
                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                return false; // Don't descend into this directory
            }

            // SECOND: Apply PathFilter for file patterns (user-configurable)
            // Only check files, not directories (directories already handled above)
            if entry.file_type().is_some_and(|ft| ft.is_file()) {
                use super::filters::Filter;
                match path_filter.filter(entry.path()) {
                    Ok(super::filters::FilterDecision::Skip(reason)) => {
                        tracing::trace!("Skipped file {}: {}", entry.path().display(), reason);
                        super::counters::COUNT_DISCOVERY_PATH_FILTERED
                            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                        return false;
                    }
                    Ok(super::filters::FilterDecision::Process) => {
                        tracing::trace!("Allowed file {}", entry.path().display());
                    }
                    Err(e) => {
                        tracing::trace!("Path filter failed for {}: {}", entry.path().display(), e);
                    }
                }
            }

            true // Allow everything else
        });

        builder
    }

    /// Scan paths (files and directories) using appropriate execution strategy
    pub fn scan(&self, paths: &[PathBuf]) -> Result<ScanStats> {
        let start_time = std::time::Instant::now();

        // Parse output configuration flags
        let show_matches = CONFIG.scanner.show;
        let show_sensitive = CONFIG.scanner.sensitive;

        // 1. Collect all file paths from all input paths FIRST
        // We need the file count to determine execution mode
        let mut all_files = Vec::new();
        for path in paths {
            if path.is_file() {
                // Check .guardyignore patterns for directly specified files
                if should_ignore_file(path) {
                    tracing::debug!("Ignoring file per .guardyignore: {}", path.display());
                    super::counters::COUNT_DISCOVERY_PATH_FILTERED
                        .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                    continue;
                }
                all_files.push(path.clone());
            } else if path.is_dir() {
                let mut dir_files = self.collect_files_from_directory(path)?;
                all_files.append(&mut dir_files);
            } else {
                eprintln!("Warning: Path not found: {}", path.display());
            }
        }

        // 2. Determine execution mode based on file count and configuration
        let run_mode = if all_files.len() <= 3 {
            // Always use sequential for very small file counts (like old version)
            RunMode::Sequential
        } else {
            // For larger file counts, calculate optimal worker count
            let system = system_profile::SystemProfile::get();
            let thread_percentage = crate::config::CONFIG.scanner.thread_percentage as usize;
            let max_threads = crate::config::CONFIG.scanner.max_threads as usize;
            let max_workers = system.calculate_workers_with_limit(thread_percentage, max_threads);
            let optimal_workers = system.adapt_workers_for_workload(all_files.len(), max_workers);
            RunMode::Parallel {
                workers: optimal_workers,
            }
        };

        tracing::debug!(
            "Execution mode: {:?} for {} files",
            run_mode,
            all_files.len()
        );

        // 3. Initialize progress display (show for all modes when TTY enabled)
        let show_progress = CONFIG.scanner.tty;
        let progress = super::progress::ProgressDisplay::new(show_progress);
        let progress_handle = if show_progress {
            Some(progress.start_background_updates())
        } else {
            None
        };

        // 4. Initialize report writer (only if --report flag is provided)
        let (report_writer, report_handle) = if let Some(report_path) = &CONFIG.scanner.report {
            let writer = super::reports::ReportWriter::new(report_path)?;
            let handle = writer.get_atomic_handle();
            (Some(writer), Some(handle))
        } else {
            (None, None)
        };

        if all_files.is_empty() {
            let empty_stats = ScanStats {
                files_scanned: 0,
                files_skipped: 0,
                processing_errors: 0,
                total_matches: 0,
                scan_duration_ms: start_time.elapsed().as_millis() as u64,
            };

            if show_progress {
                progress.finish(&empty_stats);
                if let Some(handle) = progress_handle {
                    handle.join().ok();
                }
            }

            return Ok(empty_stats);
        }

        // 5. Execute based on the determined run mode
        match run_mode {
            RunMode::Sequential => {
                tracing::debug!("Using sequential execution for {} files", all_files.len());

                for file_path in all_files {
                    match self.process_file(
                        Arc::new(file_path),
                        show_matches,
                        show_sensitive,
                        report_handle.as_ref(),
                    ) {
                        Ok(_) => {
                            super::counters::FILES_PROCESSED
                                .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                        }
                        Err(_) => {
                            super::counters::COUNT_PROCESSING_FAILED
                                .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                        }
                    }
                }
            }
            RunMode::Parallel { workers } => {
                tracing::debug!(
                    "Using parallel execution with {} workers for {} files",
                    workers,
                    all_files.len()
                );

                // Process files in parallel with streaming output
                super::parallel::run(
                    all_files,
                    {
                        let scanner = self.clone();
                        let report_handle_clone = report_handle.clone();
                        move |file_path: &PathBuf| {
                            // Design decision: We create Arc<PathBuf> here rather than during file collection
                            // because many files get filtered out (binary, size, path filters) before reaching
                            // workers. Creating Arc only for files we actually process avoids wasted allocations.
                            // All matches from this file will share this single Arc, saving memory.
                            match scanner.process_file(
                                Arc::new(file_path.clone()),
                                show_matches,
                                show_sensitive,
                                report_handle_clone.as_ref(),
                            ) {
                                Ok(_) => {
                                    super::counters::FILES_PROCESSED
                                        .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                                }
                                Err(_) => {
                                    super::counters::COUNT_PROCESSING_FAILED
                                        .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                                }
                            }
                        }
                    },
                    workers,
                )?;
            }
        }

        // 3. Finalize report writer (write suffix and close file)
        if let Some(writer) = report_writer {
            writer.finalize()?;
        }

        // 4. Get final stats from atomic counters
        let (
            files_processed,
            binary_filtered,
            size_filtered,
            path_filtered,
            processing_failed,
            matches_found,
        ) = super::counters::get_counts();
        let total_files_skipped = binary_filtered + size_filtered + path_filtered;

        let final_stats = ScanStats {
            files_scanned: files_processed,
            files_skipped: total_files_skipped,
            processing_errors: processing_failed,
            total_matches: matches_found,
            scan_duration_ms: start_time.elapsed().as_millis() as u64,
        };

        // 5. Show filter statistics for debugging
        self.show_filter_stats();

        // 7. Finish progress display
        if show_progress {
            progress.finish(&final_stats);
            if let Some(handle) = progress_handle {
                handle.join().ok(); // Wait for progress thread to finish
            }
        }

        Ok(final_stats)
    }

    /// Collect all files from a directory with filtering
    fn collect_files_from_directory(&self, path: &Path) -> Result<Vec<PathBuf>> {
        let walker = self.build_directory_walker(path).build();
        let mut file_paths = Vec::new();

        for entry in walker {
            match entry {
                Ok(entry) => {
                    if entry.file_type().is_some_and(|ft| ft.is_file()) {
                        let file_path = entry.path();

                        // Apply size filter
                        if self.apply_size_filter(file_path).is_err() {
                            continue;
                        }

                        // Apply binary filter
                        if self.apply_binary_filter(file_path).is_err() {
                            continue;
                        }

                        file_paths.push(file_path.to_path_buf());
                    }
                }
                Err(e) => {
                    tracing::debug!("Directory walk error: {}", e);
                }
            }
        }

        Ok(file_paths)
    }

    /// Process a single file with conditional output
    fn process_file(
        &self,
        file_path: Arc<PathBuf>,
        show_matches: bool,
        show_sensitive: bool,
        report_handle: Option<&super::reports::AtomicHandle>,
    ) -> Result<()> {
        let file_start = std::time::Instant::now();

        // Get memory usage before processing this file
        let memory_before = if let Ok(status) = std::fs::read_to_string("/proc/self/status") {
            status
                .lines()
                .find(|l| l.starts_with("VmRSS:"))
                .and_then(|line| line.split_whitespace().nth(1))
                .and_then(|val| val.parse::<u64>().ok())
                .unwrap_or(0)
        } else {
            0
        };

        // Get file size
        let file_size = std::fs::metadata(file_path.as_ref())
            .map(|m| m.len())
            .unwrap_or(0);

        // Get current thread info (approximate worker ID)
        let thread_name = std::thread::current()
            .name()
            .unwrap_or("unknown")
            .to_string();
        // Read file content into Arc<String> to share across filters without copying
        let content = Arc::new(
            std::fs::read_to_string(file_path.as_ref())
                .with_context(|| format!("Failed to read file: {}", file_path.display()))?,
        );

        // Check for guardy:ignore-file directive
        if content
            .lines()
            .take(10)
            .any(|line| line.contains("guardy:ignore-file"))
        {
            tracing::debug!(
                "Skipping file {} due to guardy:ignore-file directive",
                file_path.display()
            );
            return Ok(());
        }

        // Scan content for matches - pass Arc<String> through
        let matches = self.scan_content(content.clone(), file_path.clone())?;

        if !matches.is_empty() {
            // Update matches counter
            super::counters::MATCHES_FOUND
                .fetch_add(matches.len(), std::sync::atomic::Ordering::Relaxed);

            // Conditionally stream to stdout based on --show flag
            if show_matches {
                // Use file_path from the first match - all matches share the same Arc<PathBuf>
                let mut output = format!("\n📄 {}\n", matches[0].file_path.display());
                for secret_match in &matches {
                    let content = if show_sensitive {
                        secret_match.line_content.trim()
                    } else {
                        "[REDACTED]"
                    };
                    output.push_str(&format!(
                        "  🔍 Line {}:{} {} {}\n",
                        secret_match.line_number,
                        secret_match.start_pos,
                        secret_match.pattern.name,
                        content
                    ));
                }
                print!("{output}");
            }

            // Conditionally stream to report file based on --report flag
            if let Some(handle) = report_handle {
                handle.write_matches(&matches, !show_sensitive)?;
            }
        }

        // Get memory usage after processing this file
        let memory_after = if let Ok(status) = std::fs::read_to_string("/proc/self/status") {
            status
                .lines()
                .find(|l| l.starts_with("VmRSS:"))
                .and_then(|line| line.split_whitespace().nth(1))
                .and_then(|val| val.parse::<u64>().ok())
                .unwrap_or(0)
        } else {
            0
        };

        let memory_delta = memory_after as i64 - memory_before as i64;

        // Log if this file caused significant memory increase (>10MB) - only at debug level
        if !(-10_000..=10_000).contains(&memory_delta) {
            tracing::debug!(
                "🚨 MEMORY: {} processed {} ({} bytes) - Memory: {} KB → {} KB (Δ{:+} KB) matches: {}",
                thread_name,
                file_path.display(),
                file_size,
                memory_before,
                memory_after,
                memory_delta,
                matches.len()
            );
        }

        // Explicitly drop matches vector to free memory immediately
        drop(matches);
        // Explicitly drop file content Arc to free memory immediately
        drop(content);

        let file_duration = file_start.elapsed();
        tracing::trace!(
            "Processed file {} in {}",
            file_path.display(),
            crate::shared::time::format_time(file_duration)
        );

        Ok(())
    }

    /// Apply size filter to a file path
    fn apply_size_filter(&self, file_path: &Path) -> Result<()> {
        use super::filters::Filter;
        match self.size_filter.filter(file_path) {
            Ok(super::filters::FilterDecision::Skip(reason)) => {
                tracing::trace!(
                    "Filter '{}' skipped {}: {}",
                    self.size_filter.name(),
                    file_path.display(),
                    reason
                );
                super::counters::COUNT_DISCOVERY_SIZE_FILTERED
                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                Err(anyhow::anyhow!("File skipped by size filter"))
            }
            Ok(super::filters::FilterDecision::Process) => {
                tracing::trace!(
                    "Filter '{}' allowed {}",
                    self.size_filter.name(),
                    file_path.display()
                );
                Ok(())
            }
            Err(e) => {
                tracing::trace!("Size filter failed for {}: {}", file_path.display(), e);
                Ok(()) // Continue processing on filter error
            }
        }
    }

    /// Apply binary filter to a file path
    fn apply_binary_filter(&self, file_path: &Path) -> Result<()> {
        use super::filters::Filter;
        match self.binary_filter.filter(file_path) {
            Ok(super::filters::FilterDecision::Skip(reason)) => {
                tracing::trace!(
                    "Filter '{}' skipped {}: {}",
                    self.binary_filter.name(),
                    file_path.display(),
                    reason
                );
                super::counters::COUNT_DISCOVERY_BINARY_FILTERED
                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                Err(anyhow::anyhow!("File skipped by binary filter"))
            }
            Ok(super::filters::FilterDecision::Process) => {
                tracing::trace!(
                    "Filter '{}' allowed {}",
                    self.binary_filter.name(),
                    file_path.display()
                );
                Ok(())
            }
            Err(e) => {
                tracing::trace!("Binary filter failed for {}: {}", file_path.display(), e);
                Ok(()) // Continue processing on filter error
            }
        }
    }

    /// Scan entire file content for secrets using optimized filter pipeline
    fn scan_content(
        &self,
        content: Arc<String>,
        file_path: Arc<PathBuf>,
    ) -> Result<Vec<SecretMatch>> {
        use super::filters::{
            Filter,
            content::{CommentFilterInput, RegexInput},
        };

        // Step 1: Aho-Corasick prefilter - eliminates ~85% of patterns before regex execution
        let prefilter_start = std::time::Instant::now();
        let active_patterns = self
            .prefilter
            .filter(&content)
            .context("Prefilter failed")?;
        let prefilter_duration = prefilter_start.elapsed();

        if active_patterns.is_empty() {
            tracing::trace!(
                "No active patterns for file {}, skipping regex execution",
                file_path.display()
            );
            return Ok(Vec::new());
        }

        tracing::trace!(
            "Filter '{}' found {} active patterns for file {} in {}",
            self.prefilter.name(),
            active_patterns.len(),
            file_path.display(),
            crate::shared::time::format_time(prefilter_duration)
        );

        // Step 2: Regex execution on pre-filtered patterns (~15% of original)
        let regex_input = RegexInput {
            content: content.clone(),     // Cheap Arc clone, no copy
            file_path: file_path.clone(), // Cheap Arc clone
            active_patterns,
        };

        let regex_start = std::time::Instant::now();
        let matches = self
            .regex_executor
            .filter(&regex_input)
            .context("Regex execution failed")?;
        let regex_duration = regex_start.elapsed();

        if matches.is_empty() {
            return Ok(Vec::new());
        }

        tracing::trace!(
            "Filter '{}' found {} matches for file {} in {}",
            self.regex_executor.name(),
            matches.len(),
            file_path.display(),
            crate::shared::time::format_time(regex_duration)
        );

        // Step 3: Apply entropy analysis if enabled
        let mut filtered_matches = Vec::new();
        if CONFIG.scanner.enable_entropy_analysis {
            for mut secret_match in matches {
                // Calculate and store entropy
                secret_match.entropy = super::entropy::calculate_randomness_probability(
                    secret_match.matched_text.as_bytes(),
                );

                // Use pattern-specific threshold or default
                let threshold = secret_match
                    .pattern
                    .entropy_threshold
                    .unwrap_or(CONFIG.scanner.entropy_threshold);

                // Use the comprehensive entropy analysis function
                if super::entropy::is_likely_secret(secret_match.matched_text.as_bytes(), threshold)
                {
                    filtered_matches.push(secret_match);
                } else {
                    tracing::trace!(
                        "Match '{}' [{}] failed entropy analysis (prob: {:.2e} < threshold: \
                         {:.2e}) in file {} at line {}",
                        secret_match.matched_text,
                        secret_match.pattern.name,
                        secret_match.entropy,
                        threshold,
                        file_path.display(),
                        secret_match.line_number
                    );
                }
            }
        } else {
            filtered_matches = matches;
        }

        if filtered_matches.is_empty() {
            return Ok(Vec::new());
        }

        // Step 4: Comment filter for guardy:ignore directives
        let comment_input = CommentFilterInput {
            matches: filtered_matches,
            file_content: content.clone(), // Cheap Arc clone, no copy
        };

        let comment_start = std::time::Instant::now();
        let final_matches = self
            .comment_filter
            .filter(&comment_input)
            .context("Comment filter failed")?;
        let comment_duration = comment_start.elapsed();

        tracing::trace!(
            "Filter '{}' processed {} matches for file {} in {}",
            self.comment_filter.name(),
            final_matches.len(),
            file_path.display(),
            crate::shared::time::format_time(comment_duration)
        );

        Ok(final_matches)
    }

    /// Display filter statistics for debugging and performance monitoring
    fn show_filter_stats(&self) {
        use super::filters::Filter;

        tracing::trace!("Filter Configuration and Statistics:");

        // Size filter stats
        for (key, value) in self.size_filter.get_stats() {
            tracing::trace!("  Size Filter - {}: {}", key, value);
        }

        // Binary filter stats
        for (key, value) in self.binary_filter.get_stats() {
            tracing::trace!("  Binary Filter - {}: {}", key, value);
        }

        // Content filter stats
        for (key, value) in self.prefilter.get_stats() {
            tracing::trace!("  Prefilter - {}: {}", key, value);
        }

        for (key, value) in self.regex_executor.get_stats() {
            tracing::trace!("  Regex Executor - {}: {}", key, value);
        }

        for (key, value) in self.comment_filter.get_stats() {
            tracing::trace!("  Comment Filter - {}: {}", key, value);
        }

        // Pattern library stats
        let (patterns_count, keywords_count) = super::counters::get_pattern_stats();
        tracing::trace!("  Pattern Library - Patterns loaded: {}", patterns_count);
        tracing::trace!("  Pattern Library - Keywords loaded: {}", keywords_count);
    }
}

#[cfg(test)]
mod tests {
    use std::fs;

    use tempfile::TempDir;

    use super::*;

    #[test]
    fn test_scanner_creation() {
        let scanner = Scanner::new();
        assert!(scanner.is_ok());
    }

    #[test]
    fn test_file_scanning() {
        let temp_dir = TempDir::new().unwrap();
        let test_file = temp_dir.path().join("test_secrets.txt");

        // Read test content from encrypted fixtures
        let fixtures_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("tests/fixtures/test_secrets.txt");
        let test_content = fs::read_to_string(fixtures_path)
            .expect("Failed to read test fixtures - ensure git-crypt is unlocked");

        fs::write(&test_file, test_content).unwrap();

        let scanner = Scanner::new().unwrap();
        let stats = scanner.scan(&[test_file]).unwrap();

        // Should find some secrets but filter out obvious fake ones with entropy analysis
        assert!(stats.total_matches > 0);

        // With streaming output, matches are printed during scanning
        println!(
            "Scan completed - found {} total matches",
            stats.total_matches
        );
    }

    // Removed test_scan_directory - was causing CI timeouts and will be replaced by scan2
    // implementation
}