Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/inspect-cli/src/commands/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use sem_core::git::types::DiffScope;

use crate::formatters;
use crate::OutputFormat;
use inspect_core::analyze::{analyze_with_options, AnalyzeOptions};
use inspect_core::analyze::{analyze_with_options, retain_entity_reviews, AnalyzeOptions};
use inspect_core::types::RiskLevel;

#[derive(Args)]
Expand Down Expand Up @@ -48,7 +48,7 @@ pub fn run(args: DiffArgs) {
// Filter by min risk if specified
if let Some(ref min) = args.min_risk {
let min_level = parse_risk_level(min);
result.entity_reviews.retain(|r| r.risk_level >= min_level);
retain_entity_reviews(&mut result, |r| r.risk_level >= min_level);
}

match args.format {
Expand Down
104 changes: 98 additions & 6 deletions crates/inspect-cli/src/commands/file.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::path::PathBuf;
use std::path::{Component, Path, PathBuf};

use clap::Args;
use sem_core::git::types::DiffScope;

use crate::formatters;
use crate::OutputFormat;
use inspect_core::analyze::analyze;
use inspect_core::analyze::{analyze, retain_entity_reviews};
use inspect_core::types::RiskLevel;

#[derive(Args)]
Expand Down Expand Up @@ -38,10 +38,12 @@ pub fn run(args: FileArgs) {

match analyze(&repo, scope) {
Ok(mut result) => {
let target_path = repo_relative_path(&repo, &args.path);

// Filter to only the specified file
result
.entity_reviews
.retain(|r| r.file_path.ends_with(&args.path));
retain_entity_reviews(&mut result, |r| {
normalize_path(Path::new(&r.file_path)) == target_path
});

if let Some(ref min) = args.min_risk {
let min_level = match min.to_lowercase().as_str() {
Expand All @@ -50,7 +52,7 @@ pub fn run(args: FileArgs) {
"medium" => RiskLevel::Medium,
_ => RiskLevel::Low,
};
result.entity_reviews.retain(|r| r.risk_level >= min_level);
retain_entity_reviews(&mut result, |r| r.risk_level >= min_level);
}

match args.format {
Expand All @@ -65,3 +67,93 @@ pub fn run(args: FileArgs) {
}
}
}

fn repo_relative_path(repo: &Path, input: &str) -> String {
let path = Path::new(input);
let absolute_path = if path.is_absolute() {
path.to_path_buf()
} else {
repo.join(path)
};
let canonical_path = canonicalize_existing_path(&absolute_path);

if let Ok(relative_path) = canonical_path.strip_prefix(repo) {
normalize_path(relative_path)
} else if let Ok(relative_path) = absolute_path.strip_prefix(repo) {
normalize_path(relative_path)
} else {
normalize_path(path)
}
}

fn normalize_path(path: &Path) -> String {
let mut parts = Vec::new();
for component in path.components() {
match component {
Component::CurDir => {}
Component::ParentDir => {
if parts.last().is_some_and(|part| part != "..") {
parts.pop();
} else {
parts.push("..".into());
}
}
Component::Normal(part) => {
parts.push(part.to_string_lossy().into_owned());
}
Component::Prefix(_) | Component::RootDir => {}
}
}
parts.join("/")
}

fn canonicalize_existing_path(path: &Path) -> PathBuf {
if let Ok(canonical_path) = path.canonicalize() {
return canonical_path;
}

let mut suffix = PathBuf::new();
let mut cursor = path;
while let (Some(parent), Some(name)) = (cursor.parent(), cursor.file_name()) {
suffix = Path::new(name).join(suffix);
if let Ok(canonical_parent) = parent.canonicalize() {
return canonical_parent.join(suffix);
}
cursor = parent;
}

path.to_path_buf()
}

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

#[test]
fn repo_relative_path_accepts_relative_paths() {
let repo = Path::new("/tmp/repo");

assert_eq!(repo_relative_path(repo, "src/../app.ts"), "app.ts");
}

#[test]
fn repo_relative_path_accepts_absolute_paths_under_repo() {
let repo = Path::new("/tmp/repo");

assert_eq!(
repo_relative_path(repo, "/tmp/repo/src/app.ts"),
"src/app.ts"
);
}

#[test]
fn normalize_path_preserves_leading_parent_dirs() {
assert_eq!(normalize_path(Path::new("../app.ts")), "../app.ts");
}

#[test]
fn normalize_path_collapses_nested_parent_dirs() {
assert_eq!(normalize_path(Path::new("src/../lib/../app.ts")), "app.ts");
assert_eq!(normalize_path(Path::new("src/../../app.ts")), "../app.ts");
}
}
9 changes: 3 additions & 6 deletions crates/inspect-cli/src/commands/pr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use sem_core::git::types::DiffScope;

use crate::formatters;
use crate::OutputFormat;
use inspect_core::analyze::{analyze, analyze_remote};
use inspect_core::analyze::{analyze, analyze_remote, retain_entity_reviews};
use inspect_core::github::GitHubClient;
use inspect_core::noise::is_noise_file;
use inspect_core::types::RiskLevel;
Expand Down Expand Up @@ -146,18 +146,15 @@ async fn run_remote(args: &PrArgs, remote_repo: &str) {
}
}

fn apply_filters_and_print(
result: &mut inspect_core::types::ReviewResult,
args: &PrArgs,
) {
fn apply_filters_and_print(result: &mut inspect_core::types::ReviewResult, args: &PrArgs) {
if let Some(ref min) = args.min_risk {
let min_level = match min.to_lowercase().as_str() {
"critical" => RiskLevel::Critical,
"high" => RiskLevel::High,
"medium" => RiskLevel::Medium,
_ => RiskLevel::Low,
};
result.entity_reviews.retain(|r| r.risk_level >= min_level);
retain_entity_reviews(result, |r| r.risk_level >= min_level);
}

match args.format {
Expand Down
Loading
Loading