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
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
//! File collection and discovery for the analyze command.
//!
//! Handles full, incremental, and `--since` file collection modes,
//! including cached finding retrieval for unchanged files.
//!
//! The engine stage `engine/stages/collect.rs` uses `collect_file_list`.
//! The remaining functions are legacy from the old `run()` pipeline.
#![allow(dead_code)]

use crate::config::{glob_match, ExcludeConfig};
use crate::detectors::IncrementalCache;
use crate::models::Finding;

use anyhow::{Context, Result};
use console::style;
use ignore::{WalkBuilder, WalkState};
use indicatif::{ProgressBar, ProgressStyle};
use std::path::{Path, PathBuf};
use std::sync::Arc;

/// Supported file extensions for analysis
pub(crate) const SUPPORTED_EXTENSIONS: &[&str] = &[
    "py", "pyi", // Python
    "ts", "tsx", // TypeScript
    "js", "jsx", "mjs",  // JavaScript
    "rs",   // Rust
    "go",   // Go
    "java", // Java
    "c", "h", // C
    "cpp", "hpp", "cc", // C++
    "cs", // C#
    "kt", "kts",   // Kotlin
    "rb",    // Ruby
    "php",   // PHP
    "swift", // Swift
];

/// Result of file collection phase
pub(super) struct FileCollectionResult {
    pub all_files: Vec<PathBuf>,
    pub files_to_parse: Vec<PathBuf>,
    pub cached_findings: Vec<Finding>,
}

/// Maximum file size to accept for analysis (2MB, matching parser guardrail).
const MAX_ANALYSIS_FILE_BYTES: u64 = 2 * 1024 * 1024;

/// Validate a file for analysis: reject symlinks, out-of-boundary paths, and oversized files.
///
/// Returns `Some(canonical_path)` if the file passes all checks, `None` otherwise.
fn validate_file(path: &Path, repo_canonical: &Path) -> Option<PathBuf> {
    // 1. Reject symlinks
    match std::fs::symlink_metadata(path) {
        Ok(meta) => {
            if meta.file_type().is_symlink() {
                tracing::warn!("Skipping symlink: {}", path.display());
                return None;
            }
            // 2. Check file size
            if meta.len() > MAX_ANALYSIS_FILE_BYTES {
                tracing::warn!(
                    "Skipping oversized file: {} ({:.1}MB exceeds {}MB limit)",
                    path.display(),
                    meta.len() as f64 / (1024.0 * 1024.0),
                    MAX_ANALYSIS_FILE_BYTES / (1024 * 1024),
                );
                return None;
            }
        }
        Err(e) => {
            tracing::warn!("Cannot stat file {}: {}", path.display(), e);
            return None;
        }
    }

    // 3. Canonicalize and check boundary
    match path.canonicalize() {
        Ok(canonical) => {
            if !canonical.starts_with(repo_canonical) {
                tracing::warn!(
                    "Skipping file outside repository boundary: {} (resolves to {})",
                    path.display(),
                    canonical.display(),
                );
                return None;
            }
            Some(canonical)
        }
        Err(e) => {
            tracing::warn!("Cannot canonicalize {}: {}", path.display(), e);
            None
        }
    }
}

/// Quick file list collection (no git, no incremental checking) for cache validation
pub fn collect_file_list(repo_path: &Path, exclude: &ExcludeConfig) -> Result<Vec<PathBuf>> {
    let repo_canonical = repo_path.canonicalize().with_context(|| {
        format!("Cannot canonicalize repository path: {}", repo_path.display())
    })?;
    let effective = exclude.effective_patterns();
    let mut files = Vec::new();

    let walker = WalkBuilder::new(repo_path)
        .hidden(true)
        .git_ignore(true)
        .git_global(false)
        .git_exclude(true)
        .build();

    for entry in walker.filter_map(|e| e.ok()) {
        let path = entry.path();
        if !path.is_file() {
            continue;
        }
        let Some(ext) = path.extension().and_then(|e| e.to_str()) else {
            continue;
        };
        if !SUPPORTED_EXTENSIONS.contains(&ext) {
            continue;
        }

        // Skip files matching exclusion patterns
        if let Ok(rel) = path.strip_prefix(repo_path) {
            let rel_str = rel.to_string_lossy();
            if effective.iter().any(|p| glob_match(p, &rel_str)) {
                continue;
            }
        }
        if let Some(validated) = validate_file(path, &repo_canonical) {
            files.push(validated);
        }
    }

    // Sort for deterministic ordering — WalkBuilder does not guarantee
    // consistent order across runs on the same filesystem.
    files.sort();

    Ok(files)
}

/// Collect files for analysis based on mode (full, incremental, or since)
pub(super) fn collect_files_for_analysis(
    repo_path: &Path,
    since: &Option<String>,
    is_incremental_mode: bool,
    incremental_cache: &mut IncrementalCache,
    multi: &indicatif::MultiProgress,
    spinner_style: &ProgressStyle,
    exclude: &ExcludeConfig,
) -> Result<FileCollectionResult> {
    let walk_spinner = multi.add(ProgressBar::new_spinner());
    walk_spinner.set_style(spinner_style.clone());

    let (all_files, files_to_parse, cached_findings) = if let Some(ref commit) = since {
        // --since mode: only analyze files changed since specified commit
        walk_spinner.set_message(format!("Finding files changed since {}...", commit));
        walk_spinner.enable_steady_tick(std::time::Duration::from_millis(100));

        let changed = get_changed_files_since(repo_path, commit)?;
        let all = collect_source_files(repo_path, exclude)?;

        walk_spinner.finish_with_message(format!(
            "{}Found {} changed files (since {}) out of {} total",
            style("✓ ").green(),
            style(changed.len()).cyan(),
            style(commit).yellow(),
            style(all.len()).dim()
        ));

        let cached = get_cached_findings_for_unchanged(&all, &changed, incremental_cache);
        (all, changed, cached)
    } else if is_incremental_mode {
        // --incremental mode: only analyze files changed since last run
        walk_spinner.set_message("Discovering source files (incremental mode)...");
        walk_spinner.enable_steady_tick(std::time::Duration::from_millis(100));

        let all = collect_source_files(repo_path, exclude)?;
        let changed = incremental_cache.changed_files(&all);
        let cache_stats = incremental_cache.stats();

        walk_spinner.finish_with_message(format!(
            "{}Found {} changed files out of {} total ({} cached)",
            style("✓ ").green(),
            style(changed.len()).cyan(),
            style(all.len()).dim(),
            style(cache_stats.cached_files).dim()
        ));

        let cached = get_cached_findings_for_unchanged(&all, &changed, incremental_cache);
        (all, changed, cached)
    } else {
        // Full mode: analyze all files
        walk_spinner.set_message("Discovering source files...");
        walk_spinner.enable_steady_tick(std::time::Duration::from_millis(100));

        let files = collect_source_files(repo_path, exclude)?;
        walk_spinner.finish_with_message(format!(
            "{}Found {} source files",
            style("✓ ").green(),
            style(files.len()).cyan()
        ));

        (files.clone(), files, Vec::new())
    };

    Ok(FileCollectionResult {
        all_files,
        files_to_parse,
        cached_findings,
    })
}

/// Walk source files and simultaneously stream them to a channel for immediate parsing.
///
/// This enables walk+parse overlap: parser threads can begin work on early files
/// while the walker is still discovering later files. The returned `Vec<PathBuf>`
/// contains all discovered files (needed by downstream consumers like incremental
/// cache validation, detector file provider, and scoring).
///
/// The sender is dropped when the walk completes, signaling the channel's end.
/// Walk files, sending to channel for parsing and optionally publishing the
/// collected file list early via `early_files` so other threads can start
/// work before the parse pipeline finishes.
pub(crate) fn walk_files_to_channel(
    repo_path: &Path,
    exclude: &ExcludeConfig,
    sender: crossbeam_channel::Sender<PathBuf>,
    early_files: Option<Arc<std::sync::OnceLock<Vec<PathBuf>>>>,
) -> Result<Vec<PathBuf>> {
    let repo_canonical = repo_path.canonicalize().with_context(|| {
        format!(
            "Cannot canonicalize repository path: {}",
            repo_path.display()
        )
    })?;
    let effective = exclude.effective_patterns();

    let mut builder = WalkBuilder::new(repo_path);
    builder
        .hidden(true)
        .git_ignore(true)
        .git_global(true)
        .git_exclude(true)
        .require_git(false)
        .add_custom_ignore_filename(".repotoireignore");

    let files = std::sync::Mutex::new(Vec::new());

    builder.build_parallel().run(|| {
        let files = &files;
        let repo_canonical = &repo_canonical;
        let effective = &effective;
        let repo_path_ref = repo_path;
        Box::new(move |entry| {
            let entry = match entry {
                Ok(e) => e,
                Err(_) => return WalkState::Continue,
            };
            let path = entry.path();
            if !path.is_file() {
                return WalkState::Continue;
            }
            let Some(ext) = path.extension().and_then(|e| e.to_str()) else {
                return WalkState::Continue;
            };
            if !SUPPORTED_EXTENSIONS.contains(&ext) {
                return WalkState::Continue;
            }
            // Skip files matching exclusion patterns
            if let Ok(rel) = path.strip_prefix(repo_path_ref) {
                let rel_str = rel.to_string_lossy();
                if effective.iter().any(|p| glob_match(p, &rel_str)) {
                    return WalkState::Continue;
                }
            }
            if let Some(validated) = validate_file(path, repo_canonical) {
                if let Ok(mut f) = files.lock() {
                    f.push(validated);
                }
            }
            WalkState::Continue
        })
    });

    // Parallel walk returns files in arbitrary order; sort for deterministic results.
    // Sorting BEFORE sending to channel ensures the parse pipeline receives files
    // in a consistent order, producing deterministic graph node indices.
    let mut files = files.into_inner().expect("walk mutex poisoned");
    files.sort();

    // Publish file list early so other threads can start work
    if let Some(early) = early_files {
        let _ = early.set(files.clone());
    }

    // Send sorted files to parse channel
    for file in &files {
        let _ = sender.send(file.clone());
    }
    // Drop sender to signal channel end
    drop(sender);

    Ok(files)
}

/// Collect all source files in the repository, respecting .gitignore
fn collect_source_files(repo_path: &Path, exclude: &ExcludeConfig) -> Result<Vec<PathBuf>> {
    let repo_canonical = repo_path.canonicalize().with_context(|| {
        format!("Cannot canonicalize repository path: {}", repo_path.display())
    })?;
    let effective = exclude.effective_patterns();

    let mut builder = WalkBuilder::new(repo_path);
    builder
        .hidden(true)
        .git_ignore(true)
        .git_global(true)
        .git_exclude(true)
        .require_git(false)
        .add_custom_ignore_filename(".repotoireignore");

    let files = std::sync::Mutex::new(Vec::new());

    builder.build_parallel().run(|| {
        let files = &files;
        let repo_canonical = &repo_canonical;
        let effective = &effective;
        let repo_path_ref = repo_path;
        Box::new(move |entry| {
            let entry = match entry {
                Ok(e) => e,
                Err(_) => return WalkState::Continue,
            };
            let path = entry.path();
            if !path.is_file() {
                return WalkState::Continue;
            }
            let Some(ext) = path.extension().and_then(|e| e.to_str()) else {
                return WalkState::Continue;
            };
            if !SUPPORTED_EXTENSIONS.contains(&ext) {
                return WalkState::Continue;
            }
            // Skip files matching exclusion patterns
            if let Ok(rel) = path.strip_prefix(repo_path_ref) {
                let rel_str = rel.to_string_lossy();
                if effective.iter().any(|p| glob_match(p, &rel_str)) {
                    return WalkState::Continue;
                }
            }
            if let Some(validated) = validate_file(path, repo_canonical) {
                if let Ok(mut f) = files.lock() {
                    f.push(validated);
                }
            }
            WalkState::Continue
        })
    });

    // Parallel walk returns files in arbitrary order; sort for deterministic results
    let mut files = files.into_inner().expect("walk mutex poisoned");
    files.sort();
    Ok(files)
}

/// Get files changed since a specific git commit
fn get_changed_files_since(repo_path: &Path, since: &str) -> Result<Vec<PathBuf>> {
    use std::process::Command;

    // Sanitize: reject values that look like flags to prevent git flag injection (#49)
    if since.starts_with('-') {
        anyhow::bail!(
            "Invalid --since value '{}': must be a commit hash, branch name, or tag (cannot start with '-')",
            since
        );
    }

    let repo_canonical = repo_path.canonicalize().with_context(|| {
        format!("Cannot canonicalize repository path: {}", repo_path.display())
    })?;

    let output = Command::new("git")
        .args(["diff", "--name-only", since, "HEAD"])
        .current_dir(repo_path)
        .output()
        .with_context(|| format!("Failed to run git diff since '{}'", since))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        anyhow::bail!("git diff failed: {}", stderr.trim());
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    let mut files: Vec<PathBuf> = stdout
        .lines()
        .filter(|l| !l.is_empty())
        .filter_map(|l| {
            let joined = repo_path.join(l);
            if joined.exists() {
                validate_file(&joined, &repo_canonical)
            } else {
                None
            }
        })
        .collect();

    // Also get untracked files
    let untracked = Command::new("git")
        .args(["ls-files", "--others", "--exclude-standard"])
        .current_dir(repo_path)
        .output();

    let untracked_files = untracked.ok()
        .filter(|out| out.status.success())
        .map(|out| String::from_utf8_lossy(&out.stdout).to_string())
        .unwrap_or_default();
    for line in untracked_files.lines().filter(|l| !l.is_empty()) {
        let path = repo_path.join(line);
        if !path.exists() { continue; }
        let Some(validated) = validate_file(&path, &repo_canonical) else { continue; };
        if !files.contains(&validated) {
            files.push(validated);
        }
    }

    files.retain(|p| {
        p.extension()
            .and_then(|e| e.to_str())
            .map(|ext| SUPPORTED_EXTENSIONS.contains(&ext))
            .unwrap_or(false)
    });

    Ok(files)
}

/// Get cached findings for unchanged files
fn get_cached_findings_for_unchanged(
    all_files: &[PathBuf],
    changed_files: &[PathBuf],
    incremental_cache: &IncrementalCache,
) -> Vec<Finding> {
    let unchanged: Vec<_> = all_files
        .iter()
        .filter(|f| !changed_files.contains(f))
        .collect();

    let mut cached = Vec::new();
    for file in unchanged {
        cached.extend(incremental_cache.cached_findings(file));
    }
    cached
}

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

    #[test]
    fn test_validate_file_accepts_normal_file() {
        let dir = TempDir::new().expect("create temp dir");
        let file = dir.path().join("test.py");
        fs::write(&file, "print('hello')").expect("write test file");
        let repo_canonical = dir.path().canonicalize().expect("canonicalize path");
        assert!(validate_file(&file, &repo_canonical).is_some());
    }

    #[test]
    fn test_validate_file_rejects_nonexistent() {
        let dir = TempDir::new().expect("create temp dir");
        let repo_canonical = dir.path().canonicalize().expect("canonicalize path");
        let fake = dir.path().join("nope.py");
        assert!(validate_file(&fake, &repo_canonical).is_none());
    }

    #[test]
    fn test_validate_file_rejects_oversized() {
        let dir = TempDir::new().expect("create temp dir");
        let file = dir.path().join("big.py");
        let data = vec![b'x'; 2 * 1024 * 1024 + 1];
        fs::write(&file, &data).expect("write oversized file");
        let repo_canonical = dir.path().canonicalize().expect("canonicalize path");
        assert!(validate_file(&file, &repo_canonical).is_none());
    }

    #[test]
    fn test_validate_file_rejects_symlink() {
        let dir = TempDir::new().expect("create temp dir");
        let real = dir.path().join("real.py");
        fs::write(&real, "x = 1").expect("write real file");
        let link = dir.path().join("link.py");

        #[cfg(unix)]
        {
            std::os::unix::fs::symlink(&real, &link).expect("create symlink");
            let repo_canonical = dir.path().canonicalize().expect("canonicalize path");
            assert!(validate_file(&link, &repo_canonical).is_none());
        }
    }

    #[test]
    fn test_validate_file_rejects_outside_boundary() {
        let parent = TempDir::new().expect("create temp dir");
        let repo = parent.path().join("repo");
        fs::create_dir(&repo).expect("create repo dir");
        let outside = parent.path().join("secret.py");
        fs::write(&outside, "password = 'hunter2'").expect("write outside file");

        let repo_canonical = repo.canonicalize().expect("canonicalize path");
        let traversal_path = repo.join("..").join("secret.py");
        assert!(validate_file(&traversal_path, &repo_canonical).is_none());
    }
}