repotoire 0.5.3

Graph-powered code analysis CLI. 106 detectors for security, architecture, and code quality.
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
//! TRUE streaming AST processing with minimal memory footprint
//!
//! This module provides `LightweightFileInfo` - an ultra-compact representation
//! of parsed file data that holds ONLY what detectors and graph building need.
//!
//! # Key Differences from ParseResult
//!
//! ```text
//! ParseResult (10+ functions):
//! - Vec<Function> with PathBuf EACH (~200 bytes × N)
//! - Full parameter lists with strings
//! - AST metadata
//! Total: ~2-5KB per file with 10 functions
//!
//! LightweightFileInfo (10+ functions):  
//! - Single PathBuf for the file
//! - Compact function info (no PathBuf duplication)
//! - Only essential fields
//! Total: ~300-500 bytes per file with 10 functions
//! ```
//!
//! # Memory Strategy
//!
//! The key insight is that tree-sitter ASTs are HUGE (10-50MB for large files),
//! but we only need a tiny fraction of that data. This module:
//!
//! 1. Parses file with tree-sitter
//! 2. Extracts essential info immediately
//! 3. DROPS the AST before returning
//! 4. Returns compact struct suitable for collection
//!
//! This means even with 20k files, memory stays bounded.

use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::path::{Path, PathBuf};

/// Ultra-compact function info - no PathBuf, minimal strings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LightweightFunctionInfo {
    /// Function name (not qualified - saves memory)
    pub name: String,
    /// Qualified name for graph edges
    pub qualified_name: String,
    /// Start line
    pub line_start: u32,
    /// End line
    pub line_end: u32,
    /// Number of parameters (not the full list - saves memory)
    pub param_count: u8,
    /// Is async function
    pub is_async: bool,
    /// Cyclomatic complexity
    pub complexity: u16,
    /// Whether this function has decorators/annotations (1 byte flag)
    #[serde(default)]
    pub has_annotations: bool,
    /// Whether this function is exported (pub visibility)
    #[serde(default)]
    pub is_exported: bool,
    /// Maximum nesting depth within this function
    #[serde(default)]
    pub max_nesting: Option<u16>,
    /// Annotation/decorator strings (e.g. "test", "cfg(test)", "exported").
    /// Stored as comma-joined string to keep the struct compact.
    #[serde(default)]
    pub annotations_joined: Option<String>,
}

impl LightweightFunctionInfo {
    /// Lines of code for this function
    pub fn loc(&self) -> u32 {
        if self.line_end >= self.line_start {
            self.line_end - self.line_start + 1
        } else {
            1
        }
    }
}

/// Ultra-compact class info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LightweightClassInfo {
    /// Class name
    pub name: String,
    /// Qualified name for graph edges
    pub qualified_name: String,
    /// Start line
    pub line_start: u32,
    /// End line
    pub line_end: u32,
    /// Number of methods (not the full list)
    pub method_count: u16,
    /// Number of fields/attributes
    pub field_count: u16,
    /// Number of base classes
    pub base_count: u8,
}

/// Compact import info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LightweightImport {
    /// Import path
    pub path: String,
    /// Is type-only import (TypeScript)
    pub is_type_only: bool,
}

/// Compact call edge
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LightweightCall {
    /// Caller qualified name
    pub caller: String,
    /// Callee name (may not be qualified)
    pub callee: String,
}

/// Language enum for efficient storage
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[repr(u8)]
pub enum Language {
    Python = 0,
    TypeScript = 1,
    JavaScript = 2,
    Rust = 3,
    Go = 4,
    Java = 5,
    CSharp = 6,
    Kotlin = 7,
    C = 8,
    Cpp = 9,
    Ruby = 10,
    Php = 11,
    Swift = 12,
    Unknown = 255,
}

impl Language {
    pub fn from_extension(ext: &str) -> Self {
        match ext {
            "py" | "pyi" => Language::Python,
            "ts" | "tsx" => Language::TypeScript,
            "js" | "jsx" | "mjs" | "cjs" => Language::JavaScript,
            "rs" => Language::Rust,
            "go" => Language::Go,
            "java" => Language::Java,
            "cs" => Language::CSharp,
            "kt" | "kts" => Language::Kotlin,
            "c" | "h" => Language::C,
            "cpp" | "cc" | "cxx" | "hpp" | "hh" => Language::Cpp,
            "rb" => Language::Ruby,
            "php" => Language::Php,
            "swift" => Language::Swift,
            _ => Language::Unknown,
        }
    }

    pub fn from_path(path: &Path) -> Self {
        let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");

        if ext != "h" {
            return Self::from_extension(ext);
        }

        // Heuristic C/C++ header detection for .h files (#31)
        let content = match std::fs::read(path) {
            Ok(bytes) => bytes,
            Err(_) => return Language::C,
        };
        let sample = &content[..content.len().min(16 * 1024)];
        let text = String::from_utf8_lossy(sample);
        let cpp_markers = [
            "class ",
            "namespace ",
            "template<",
            "template <",
            "typename ",
            "constexpr",
            "std::",
        ];

        if cpp_markers.iter().any(|m| text.contains(m)) {
            Language::Cpp
        } else {
            Language::C
        }
    }

    pub fn as_str(&self) -> &'static str {
        match self {
            Language::Python => "Python",
            Language::TypeScript => "TypeScript",
            Language::JavaScript => "JavaScript",
            Language::Rust => "Rust",
            Language::Go => "Go",
            Language::Java => "Java",
            Language::CSharp => "C#",
            Language::Kotlin => "Kotlin",
            Language::C => "C",
            Language::Cpp => "C++",
            Language::Ruby => "Ruby",
            Language::Php => "PHP",
            Language::Swift => "Swift",
            Language::Unknown => "Unknown",
        }
    }
}

/// Ultra-lightweight file info - the ONLY struct we collect during streaming
///
/// Memory target: <500 bytes for a typical file with 10 functions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LightweightFileInfo {
    /// File path (only place PathBuf appears)
    pub path: PathBuf,
    /// Language (1 byte instead of String)
    pub language: Language,
    /// Lines of code
    pub loc: u32,
    /// Functions in this file
    pub functions: Vec<LightweightFunctionInfo>,
    /// Classes in this file
    pub classes: Vec<LightweightClassInfo>,
    /// Imports from this file
    pub imports: Vec<LightweightImport>,
    /// Call edges originating from this file
    pub calls: Vec<LightweightCall>,
    /// Functions whose address is taken (callbacks, etc.)
    pub address_taken: HashSet<String>,
    /// Trait implementations as (type_name, trait_name) pairs
    pub trait_impls: Vec<(String, String)>,
}

impl LightweightFileInfo {
    /// Create empty info for a path
    #[allow(dead_code)] // Public API
    pub fn empty(path: PathBuf, language: Language) -> Self {
        Self {
            path,
            language,
            loc: 0,
            functions: Vec::new(),
            classes: Vec::new(),
            imports: Vec::new(),
            calls: Vec::new(),
            address_taken: HashSet::new(),
            trait_impls: Vec::new(),
        }
    }

    /// Get relative path string for graph building
    pub fn relative_path(&self, repo_root: &Path) -> String {
        self.path
            .strip_prefix(repo_root)
            .unwrap_or(&self.path)
            .display()
            .to_string()
    }

    /// Check if file is empty (no functions or classes)
    #[allow(dead_code)] // Public API
    pub fn is_empty(&self) -> bool {
        self.functions.is_empty() && self.classes.is_empty()
    }

    /// Total entities count
    #[allow(dead_code)] // Public API
    pub fn entity_count(&self) -> usize {
        self.functions.len() + self.classes.len()
    }

    /// Estimated memory usage in bytes
    pub fn estimated_memory(&self) -> usize {
        let base = std::mem::size_of::<Self>();
        let path_size = self.path.as_os_str().len();
        let funcs_size = self.functions.len() * std::mem::size_of::<LightweightFunctionInfo>();
        let classes_size = self.classes.len() * std::mem::size_of::<LightweightClassInfo>();
        let imports_size: usize = self.imports.iter().map(|i| i.path.len() + 2).sum();
        let calls_size: usize = self
            .calls
            .iter()
            .map(|c| c.caller.len() + c.callee.len())
            .sum();

        base + path_size + funcs_size + classes_size + imports_size + calls_size
    }
}

/// Convert from full ParseResult to LightweightFileInfo
///
/// This is a COMPATIBILITY function for gradual migration.
/// Prefer using `parse_file_lightweight()` directly for true streaming.
impl LightweightFileInfo {
    pub fn from_parse_result(
        result: &crate::parsers::ParseResult,
        path: PathBuf,
        language: Language,
        loc: u32,
    ) -> Self {
        let functions = result
            .functions
            .iter()
            .map(|f| LightweightFunctionInfo {
                name: f.name.clone(),
                qualified_name: f.qualified_name.clone(),
                line_start: f.line_start,
                line_end: f.line_end,
                param_count: f.parameters.len().min(255) as u8,
                is_async: f.is_async,
                complexity: f.complexity.unwrap_or(1).min(65535) as u16,
                has_annotations: !f.annotations.is_empty(),
                is_exported: f.annotations.iter().any(|a| a == "exported"),
                max_nesting: f.max_nesting.map(|n| n.min(65535) as u16),
                annotations_joined: if f.annotations.is_empty() {
                    None
                } else {
                    Some(f.annotations.join(","))
                },
            })
            .collect();

        let classes = result
            .classes
            .iter()
            .map(|c| LightweightClassInfo {
                name: c.name.clone(),
                qualified_name: c.qualified_name.clone(),
                line_start: c.line_start,
                line_end: c.line_end,
                method_count: c.methods.len().min(65535) as u16,
                field_count: c.field_count.min(65535) as u16,
                base_count: c.bases.len().min(255) as u8,
            })
            .collect();

        let imports = result
            .imports
            .iter()
            .map(|i| LightweightImport {
                path: i.path.clone(),
                is_type_only: i.is_type_only,
            })
            .collect();

        let calls = result
            .calls
            .iter()
            .map(|(caller, callee)| LightweightCall {
                caller: caller.clone(),
                callee: callee.clone(),
            })
            .collect();

        Self {
            path,
            language,
            loc,
            functions,
            classes,
            imports,
            calls,
            address_taken: result.address_taken.clone(),
            trait_impls: result.trait_impls.clone(),
        }
    }
}

/// Statistics from lightweight parsing
#[derive(Debug, Clone, Default)]
pub struct LightweightParseStats {
    #[allow(dead_code)] // Included in stats
    pub total_files: usize,
    pub parsed_files: usize,
    #[allow(dead_code)] // Included in stats
    pub skipped_files: usize,
    pub total_functions: usize,
    pub total_classes: usize,
    pub total_imports: usize,
    pub total_calls: usize,
    pub parse_errors: usize,
    pub estimated_memory_bytes: usize,
}

impl LightweightParseStats {
    /// Add stats from a single file
    pub fn add_file(&mut self, info: &LightweightFileInfo) {
        self.parsed_files += 1;
        self.total_functions += info.functions.len();
        self.total_classes += info.classes.len();
        self.total_imports += info.imports.len();
        self.total_calls += info.calls.len();
        self.estimated_memory_bytes += info.estimated_memory();
    }

    /// Human-readable memory estimate
    #[allow(dead_code)] // Public API
    pub fn memory_human(&self) -> String {
        let bytes = self.estimated_memory_bytes;
        if bytes < 1024 {
            format!("{} B", bytes)
        } else if bytes < 1024 * 1024 {
            format!("{:.1} KB", bytes as f64 / 1024.0)
        } else {
            format!("{:.1} MB", bytes as f64 / (1024.0 * 1024.0))
        }
    }
}

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

    #[test]
    fn test_language_from_extension() {
        assert_eq!(Language::from_extension("py"), Language::Python);
        assert_eq!(Language::from_extension("ts"), Language::TypeScript);
        assert_eq!(Language::from_extension("rs"), Language::Rust);
        assert_eq!(Language::from_extension("go"), Language::Go);
        assert_eq!(Language::from_extension("xyz"), Language::Unknown);
    }

    #[test]
    fn test_lightweight_function_loc() {
        let func = LightweightFunctionInfo {
            name: "test".to_string(),
            qualified_name: "mod::test:1".to_string(),
            line_start: 10,
            line_end: 20,
            param_count: 2,
            is_async: false,
            complexity: 5,
            has_annotations: false,
            is_exported: false,
            max_nesting: None,
            annotations_joined: None,
        };
        assert_eq!(func.loc(), 11);
    }

    #[test]
    fn test_language_from_path_header_heuristic() {
        let dir = tempfile::tempdir().expect("should create temp dir");
        let h = dir.path().join("x.h");
        std::fs::write(&h, "namespace n { class A {}; }").expect("should write test header");
        assert_eq!(Language::from_path(&h), Language::Cpp);

        std::fs::write(&h, "#ifndef X_H\nint sum(int a, int b);\n#endif").expect("should write C header");
        assert_eq!(Language::from_path(&h), Language::C);
    }

    fn test_estimated_memory() {
        let info = LightweightFileInfo::empty(PathBuf::from("test.py"), Language::Python);
        let mem = info.estimated_memory();
        assert!(mem > 0);
        // Empty file should be very small
        assert!(mem < 500);
    }
}