loregrep 0.6.0

Repository indexing library for AI coding assistants. Tree-sitter parsing, fast in-memory indexing, and tool APIs for LLM integration.
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
use anyhow::{Context, Result};
use globset::{Glob, GlobSet, GlobSetBuilder};
use ignore::{Walk, WalkBuilder};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Instant;
use tracing::{info, warn};

use crate::analyzers::registry::RegistryHandle;
use crate::internal::config::FileScanningConfig;

#[derive(Clone)]
pub struct FileFilters {
    include_globs: GlobSet,
    exclude_globs: GlobSet,
    max_file_size: u64,
}

#[derive(Clone)]
pub struct LanguageDetector {
    rust_extensions: GlobSet,
    python_extensions: GlobSet,
    typescript_extensions: GlobSet,
    javascript_extensions: GlobSet,
    go_extensions: GlobSet,
}

#[derive(Debug, Clone)]
pub struct ScanConfig {
    pub follow_symlinks: bool,
    pub max_depth: Option<u32>,
    pub parallel: bool,
}

pub struct ScanResult {
    /// The analysis root, canonicalized (absolute, symlinks resolved). This is
    /// THE root every `relative_path` below is anchored at, and the value the
    /// index records as its `scan_root`.
    pub root: PathBuf,
    pub files: Vec<DiscoveredFile>,
    pub total_files_found: usize,
    pub total_files_filtered: usize,
    pub scan_duration: std::time::Duration,
    pub languages_found: std::collections::HashMap<String, usize>,
}

#[derive(Debug, Clone)]
pub struct DiscoveredFile {
    /// Absolute, canonical-root-anchored path. Use this to READ the file.
    pub path: PathBuf,
    pub language: String,
    pub size: u64,
    /// The file's path relative to the canonical analysis root. This — not
    /// [`DiscoveredFile::path`] — is the file's identity everywhere downstream;
    /// see [`DiscoveredFile::index_path`].
    pub relative_path: PathBuf,
}

impl DiscoveredFile {
    /// The file's identity in the index: its root-relative path, normalized.
    ///
    /// One canonicalization at the scanner boundary plus this one derivation is
    /// what makes the emitted path independent of how `--path` was spelled and
    /// of where the caller's shell stood (F3/F4/K1/K9).
    pub fn index_path(&self) -> crate::storage::graph::IndexPath {
        crate::storage::graph::IndexPath::new(&self.relative_path.to_string_lossy())
    }
}

/// Resolve an analysis root to ONE canonical absolute path (symlinks resolved).
///
/// Every spelling of the same directory — `.`, `./src/..`, an absolute path, a
/// `../..` climb, a symlink to it — collapses here, before anything derived from
/// it (index keys, cache header, emitted paths) exists. Falls back to the input
/// joined onto the cwd when the path does not exist, so a bad root still
/// produces a clear downstream error rather than a panic.
pub fn canonical_root<P: AsRef<Path>>(path: P) -> PathBuf {
    let path = path.as_ref();
    std::fs::canonicalize(path).unwrap_or_else(|_| {
        if path.is_absolute() {
            path.to_path_buf()
        } else {
            std::env::current_dir()
                .unwrap_or_else(|_| PathBuf::from("."))
                .join(path)
        }
    })
}

#[derive(Clone)]
pub struct RepositoryScanner {
    filters: FileFilters,
    language_detector: LanguageDetector,
    config: ScanConfig,
    scanning_config: FileScanningConfig,
    /// Optional handle into the analyzer registry. When present, language
    /// labeling is driven by the registered analyzers' extensions (so adding a
    /// language needs no change here). When absent, the built-in
    /// `LanguageDetector` fallback is used.
    registry: Option<RegistryHandle>,
}

impl FileFilters {
    pub fn new(config: &FileScanningConfig) -> Result<Self> {
        let mut include_builder = GlobSetBuilder::new();
        for pattern in &config.include_patterns {
            let glob = Glob::new(pattern)
                .with_context(|| format!("Invalid include pattern: {}", pattern))?;
            include_builder.add(glob);
        }

        let mut exclude_builder = GlobSetBuilder::new();
        for pattern in &config.exclude_patterns {
            let glob = Glob::new(pattern)
                .with_context(|| format!("Invalid exclude pattern: {}", pattern))?;
            exclude_builder.add(glob);
        }

        Ok(Self {
            include_globs: include_builder.build()?,
            exclude_globs: exclude_builder.build()?,
            max_file_size: config.max_file_size,
        })
    }

    pub fn should_include(&self, path: &Path, size: u64) -> bool {
        // Check file size first (quick check)
        if size > self.max_file_size {
            return false;
        }

        let path_str = path.to_string_lossy().to_string();

        // If explicitly excluded, reject
        if self.exclude_globs.is_match(&path_str) {
            return false;
        }

        // If include patterns are specified, file must match at least one
        if self.include_globs.len() > 0 {
            self.include_globs.is_match(&path_str)
        } else {
            // No include patterns means include everything (except excluded)
            true
        }
    }
}

impl LanguageDetector {
    pub fn new() -> Result<Self> {
        let rust_globs = Self::build_globset(&["*.rs"])?;
        let python_globs = Self::build_globset(&["*.py", "*.pyi"])?;
        let typescript_globs = Self::build_globset(&["*.ts", "*.tsx"])?;
        let javascript_globs = Self::build_globset(&["*.js", "*.jsx", "*.mjs", "*.cjs"])?;
        let go_globs = Self::build_globset(&["*.go"])?;

        Ok(Self {
            rust_extensions: rust_globs,
            python_extensions: python_globs,
            typescript_extensions: typescript_globs,
            javascript_extensions: javascript_globs,
            go_extensions: go_globs,
        })
    }

    fn build_globset(patterns: &[&str]) -> Result<GlobSet> {
        let mut builder = GlobSetBuilder::new();
        for pattern in patterns {
            builder.add(Glob::new(pattern)?);
        }
        Ok(builder.build()?)
    }

    pub fn detect_language(&self, path: &Path) -> String {
        let path_str = path.to_string_lossy().to_string();

        if self.rust_extensions.is_match(&path_str) {
            "rust".to_string()
        } else if self.python_extensions.is_match(&path_str) {
            "python".to_string()
        } else if self.typescript_extensions.is_match(&path_str) {
            "typescript".to_string()
        } else if self.javascript_extensions.is_match(&path_str) {
            "javascript".to_string()
        } else if self.go_extensions.is_match(&path_str) {
            "go".to_string()
        } else {
            "unknown".to_string()
        }
    }
}

impl Default for ScanConfig {
    fn default() -> Self {
        Self {
            follow_symlinks: false,
            max_depth: Some(20),
            parallel: true,
        }
    }
}

impl RepositoryScanner {
    pub fn new(
        scanning_config: &FileScanningConfig,
        scan_config: Option<ScanConfig>,
    ) -> Result<Self> {
        let filters = FileFilters::new(scanning_config)?;
        let language_detector = LanguageDetector::new()?;
        let config = scan_config.unwrap_or_default();

        Ok(Self {
            filters,
            language_detector,
            config,
            scanning_config: scanning_config.clone(),
            registry: None,
        })
    }

    /// Construct a scanner whose language labeling is driven by the analyzer
    /// registry. Extensions of registered analyzers determine the language of
    /// each discovered file, so no per-language edits are needed in this module
    /// when a new analyzer is added.
    pub fn new_with_registry(
        scanning_config: &FileScanningConfig,
        registry: RegistryHandle,
        scan_config: Option<ScanConfig>,
    ) -> Result<Self> {
        let mut scanner = Self::new(scanning_config, scan_config)?;
        scanner.registry = Some(registry);
        Ok(scanner)
    }

    /// Determine the language label for a path, preferring the registry (when
    /// present) and falling back to the built-in extension detector.
    ///
    /// The registry label wins when an analyzer is registered for the file's
    /// extension (this keeps drop-in support for newly-registered languages).
    /// When the registry does not recognize the extension, we fall back to the
    /// built-in `LanguageDetector` so that files of a *known* language whose
    /// analyzer simply isn't registered (e.g. Python when only Rust is
    /// registered) are still discovered. This lets the scan loop emit its
    /// helpful "No analyzer available for '<lang>' files" guidance instead of
    /// silently dropping them at discovery. Genuinely-unknown extensions map to
    /// "unknown" through both paths and are skipped by the scan loop.
    fn resolve_language(&self, path: &Path) -> String {
        if let Some(registry) = &self.registry {
            if let Some(label) = registry.detect_language(&path.to_string_lossy()) {
                return label;
            }
        }
        self.language_detector.detect_language(path)
    }

    pub fn scan<P: AsRef<Path>>(&self, root_path: P) -> Result<ScanResult> {
        let start_time = Instant::now();
        // Canonicalize ONCE, here, before anything is derived from the root.
        // `ignore::WalkBuilder` yields `entry.path()` = the literal spelling it
        // was handed with the discovered suffix appended, and that string used to
        // become the file's identity in one hop with no normalization.
        let root_path = canonical_root(root_path);
        let root_path = root_path.as_path();

        info!("Starting repository scan at: {:?}", root_path);

        // Build the walker
        let walker = self.build_walker(root_path)?;

        // Track statistics
        let total_found = Arc::new(AtomicUsize::new(0));
        let total_filtered = Arc::new(AtomicUsize::new(0));

        // Collect files
        let mut discovered_files = Vec::new();
        let mut languages_found = std::collections::HashMap::new();

        for result in walker {
            match result {
                Ok(entry) => {
                    total_found.fetch_add(1, Ordering::Relaxed);

                    // Skip directories
                    if entry.file_type().map_or(false, |ft| ft.is_dir()) {
                        continue;
                    }

                    let path = entry.path();

                    // Get file size
                    let metadata = match entry.metadata() {
                        Ok(meta) => meta,
                        Err(e) => {
                            warn!("Failed to get metadata for {:?}: {}", path, e);
                            continue;
                        }
                    };

                    let file_size = metadata.len();

                    // Apply filters
                    if !self.filters.should_include(path, file_size) {
                        total_filtered.fetch_add(1, Ordering::Relaxed);
                        continue;
                    }

                    // Detect language (via the registry when available)
                    let language = self.resolve_language(path);

                    // Skip unknown languages for now
                    if language == "unknown" {
                        continue;
                    }

                    // Calculate relative path
                    let relative_path = path.strip_prefix(root_path).unwrap_or(path).to_path_buf();

                    let discovered_file = DiscoveredFile {
                        path: path.to_path_buf(),
                        language: language.clone(),
                        size: file_size,
                        relative_path,
                    };

                    discovered_files.push(discovered_file);
                    *languages_found.entry(language).or_insert(0) += 1;
                }
                Err(e) => {
                    warn!("Error walking directory: {}", e);
                }
            }
        }

        let scan_duration = start_time.elapsed();

        info!(
            "Repository scan completed in {:?}. Found {} files, filtered out {}",
            scan_duration,
            discovered_files.len(),
            total_filtered.load(Ordering::Relaxed)
        );

        Ok(ScanResult {
            root: root_path.to_path_buf(),
            files: discovered_files,
            total_files_found: total_found.load(Ordering::Relaxed),
            total_files_filtered: total_filtered.load(Ordering::Relaxed),
            scan_duration,
            languages_found,
        })
    }

    fn build_walker(&self, root_path: &Path) -> Result<Walk> {
        let mut builder = WalkBuilder::new(root_path);

        builder
            .follow_links(self.scanning_config.follow_symlinks)
            .git_ignore(self.scanning_config.respect_gitignore)
            .git_global(self.scanning_config.respect_gitignore)
            .git_exclude(self.scanning_config.respect_gitignore)
            .hidden(false); // Include hidden files by default

        if let Some(max_depth) = self.scanning_config.max_depth {
            builder.max_depth(Some(max_depth as usize));
        }

        // Add thread count for parallel processing
        if self.config.parallel {
            builder.threads(num_cpus::get());
        } else {
            builder.threads(1);
        }

        Ok(builder.build())
    }

    /// Quick scan that just counts files without detailed analysis
    pub fn quick_scan<P: AsRef<Path>>(
        &self,
        root_path: P,
    ) -> Result<(usize, std::collections::HashMap<String, usize>)> {
        let root_path = canonical_root(root_path);
        let walker = self.build_walker(&root_path)?;

        let mut count = 0;
        let mut languages = std::collections::HashMap::new();

        for result in walker {
            if let Ok(entry) = result {
                if entry.file_type().map_or(false, |ft| ft.is_file()) {
                    let path = entry.path();

                    if let Ok(metadata) = entry.metadata() {
                        if self.filters.should_include(path, metadata.len()) {
                            let language = self.resolve_language(path);
                            if language != "unknown" {
                                count += 1;
                                *languages.entry(language).or_insert(0) += 1;
                            }
                        }
                    }
                }
            }
        }

        Ok((count, languages))
    }

    // `newest_modified_time` used to live here, as the basis of the cache
    // freshness check. It was removed with that check: max(mtime) answers "was
    // anything edited", which is not the same question as "does the index still
    // describe this tree". A file added with a preserved older mtime is never
    // newer than the cache, and a deleted file makes nothing newer than it, so
    // both were invisible forever. Freshness is now decided by comparing the
    // discovered path set and per-file content hashes against the index
    // (`LoreGrep::is_cache_fresh`), which uses `scan` directly.

    /// Check if a path should be analyzed based on current filters
    pub fn should_analyze(&self, path: &Path) -> Result<bool> {
        let metadata = std::fs::metadata(path)
            .with_context(|| format!("Failed to get metadata for {:?}", path))?;

        let file_size = metadata.len();
        Ok(self.filters.should_include(path, file_size))
    }

    /// Get language for a specific file
    pub fn detect_file_language(&self, path: &Path) -> String {
        self.resolve_language(path)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    fn create_test_config() -> FileScanningConfig {
        FileScanningConfig {
            include_patterns: vec!["*.rs".to_string(), "*.py".to_string()],
            exclude_patterns: vec!["**/target/**".to_string(), "*.test.rs".to_string()],
            max_file_size: 1024 * 1024, // 1MB
            follow_symlinks: false,
            max_depth: Some(10),
            respect_gitignore: true,
        }
    }

    #[test]
    fn test_file_filters_creation() {
        let config = create_test_config();
        let filters = FileFilters::new(&config).unwrap();

        // Test include patterns
        assert!(filters.should_include(Path::new("main.rs"), 1000));
        assert!(filters.should_include(Path::new("script.py"), 1000));
        assert!(!filters.should_include(Path::new("main.js"), 1000));

        // Test exclude patterns
        assert!(!filters.should_include(Path::new("target/debug/main.rs"), 1000));
        assert!(!filters.should_include(Path::new("main.test.rs"), 1000));

        // Test file size limit
        assert!(!filters.should_include(Path::new("huge.rs"), 2 * 1024 * 1024));
    }

    #[test]
    fn test_language_detector() {
        let detector = LanguageDetector::new().unwrap();

        assert_eq!(detector.detect_language(Path::new("main.rs")), "rust");
        assert_eq!(detector.detect_language(Path::new("script.py")), "python");
        assert_eq!(detector.detect_language(Path::new("app.ts")), "typescript");
        assert_eq!(detector.detect_language(Path::new("app.js")), "javascript");
        assert_eq!(detector.detect_language(Path::new("main.go")), "go");
        assert_eq!(
            detector.detect_language(Path::new("unknown.txt")),
            "unknown"
        );
    }

    #[test]
    fn test_resolve_language_falls_back_for_unregistered_known_language() -> Result<()> {
        use crate::analyzers::LanguageAnalyzerRegistry;
        use crate::analyzers::registry::{DefaultLanguageRegistry, RegistryHandle};
        use crate::analyzers::rust::RustAnalyzer;

        // Registry with ONLY Rust registered.
        let mut registry = DefaultLanguageRegistry::new();
        registry.register(Box::new(RustAnalyzer::new().unwrap()))?;
        let handle = RegistryHandle::new(&registry);

        let config = create_test_config();
        let scanner = RepositoryScanner::new_with_registry(&config, handle, None)?;

        // Rust is registered -> label from registry.
        assert_eq!(scanner.detect_file_language(Path::new("main.rs")), "rust");

        // Python is a KNOWN language whose analyzer is not registered. It must
        // still be discovered (labeled "python"), not dropped as "unknown", so
        // the scan loop can emit its "no analyzer for python" guidance.
        assert_eq!(
            scanner.detect_file_language(Path::new("script.py")),
            "python"
        );

        // Genuinely-unknown extensions remain "unknown".
        assert_eq!(
            scanner.detect_file_language(Path::new("readme.txt")),
            "unknown"
        );

        Ok(())
    }

    #[test]
    fn test_repository_scanner() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let root = temp_dir.path();

        // Create test files
        fs::write(root.join("main.rs"), "fn main() {}")?;
        fs::write(root.join("lib.rs"), "pub fn test() {}")?;
        fs::write(root.join("script.py"), "print('hello')")?;
        fs::write(root.join("readme.txt"), "This is a readme")?;

        // Create subdirectory
        fs::create_dir(root.join("src"))?;
        fs::write(root.join("src/parser.rs"), "pub mod parser;")?;

        // Create excluded file
        fs::create_dir(root.join("target"))?;
        fs::write(root.join("target/main.rs"), "// generated")?;

        let config = create_test_config();
        let scan_config = ScanConfig {
            follow_symlinks: false,
            ..Default::default()
        };

        let scanner = RepositoryScanner::new(&config, Some(scan_config))?;
        let result = scanner.scan(root)?;

        // Should find main.rs, lib.rs, script.py, src/parser.rs
        // Should exclude readme.txt (not in include patterns) and target/main.rs (in exclude patterns)
        assert_eq!(result.files.len(), 4);
        assert_eq!(result.languages_found.get("rust"), Some(&3));
        assert_eq!(result.languages_found.get("python"), Some(&1));

        Ok(())
    }

    /// F3: three spellings of one root must produce one canonical root and one
    /// set of root-relative identities. Pre-fix the same file came back as
    /// `./src/x.rs`, `/abs/…/src/x.rs` or `../../src/x.rs` depending on how
    /// `--path` was typed and where the caller stood.
    #[test]
    fn every_spelling_of_one_root_yields_identical_identities() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let root = std::fs::canonicalize(temp_dir.path())?;
        fs::create_dir(root.join("src"))?;
        fs::write(root.join("src/main.rs"), "fn main() {}")?;
        fs::write(root.join("lib.rs"), "pub fn f() {}")?;

        let scanner = RepositoryScanner::new(&create_test_config(), None)?;

        let ids = |p: &Path| -> Result<Vec<String>> {
            let result = scanner.scan(p)?;
            assert_eq!(result.root, root, "root must canonicalize to one value");
            let mut v: Vec<String> = result
                .files
                .iter()
                .map(|f| f.index_path().into_string())
                .collect();
            v.sort();
            Ok(v)
        };

        let absolute = ids(&root)?;
        // A `..` climb back into the same directory.
        let climbed = ids(&root.join("src").join(".."))?;
        // A `./`-prefixed spelling.
        let dotted = ids(&PathBuf::from(format!("{}/./.", root.display())))?;

        assert_eq!(absolute, vec!["lib.rs", "src/main.rs"]);
        assert_eq!(absolute, climbed);
        assert_eq!(absolute, dotted);
        Ok(())
    }

    /// K9: a symlink to the root and the real root are one repository, so they
    /// must produce the same canonical root and the same keys.
    #[test]
    #[cfg(unix)]
    fn symlinked_root_and_real_root_agree() -> Result<()> {
        let temp_dir = TempDir::new()?;
        let real = temp_dir.path().join("real");
        fs::create_dir(&real)?;
        fs::write(real.join("a.rs"), "pub fn a() {}")?;
        let link = temp_dir.path().join("link");
        std::os::unix::fs::symlink(&real, &link)?;

        let scanner = RepositoryScanner::new(&create_test_config(), None)?;
        let via_real = scanner.scan(&real)?;
        let via_link = scanner.scan(&link)?;

        assert_eq!(via_real.root, via_link.root);
        assert_eq!(
            via_real.files[0].index_path(),
            via_link.files[0].index_path()
        );
        Ok(())
    }
}