|
1 | | -use super::smart_iter::SmartIter; |
| 1 | +use super::{smart_iter::SmartIter, types::Tag}; |
2 | 2 | use crate::utils::error::{Error, Result}; |
3 | 3 | use ignore::overrides::{Override, OverrideBuilder}; |
4 | 4 | use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; |
@@ -32,8 +32,8 @@ pub fn find_owners_for_file<'a>( |
32 | 32 | .ok_or_else(|| Error::new("file path has no parent directory"))?; |
33 | 33 |
|
34 | 34 | // CodeownersEntry candidates |
35 | | - let mut candidates = entries |
36 | | - .smart_iter(3) |
| 35 | + let mut candidates: Vec<_> = entries |
| 36 | + .iter() |
37 | 37 | .filter_map(|entry| { |
38 | 38 | let codeowners_dir = match entry.source_file.parent() { |
39 | 39 | Some(dir) => dir, |
@@ -402,3 +402,92 @@ mod tests { |
402 | 402 | assert_eq!(result[0].identifier, "@rust-team"); |
403 | 403 | } |
404 | 404 | } |
| 405 | + |
| 406 | +/// Find both owners and tags for a specific file based on all parsed CODEOWNERS entries |
| 407 | +pub fn find_owners_and_tags_for_file( |
| 408 | + file_path: &Path, entries: &[CodeownersEntry], |
| 409 | +) -> Result<(Vec<Owner>, Vec<Tag>)> { |
| 410 | + let target_dir = file_path.parent().ok_or_else(|| { |
| 411 | + std::io::Error::new( |
| 412 | + std::io::ErrorKind::InvalidInput, |
| 413 | + "file path has no parent directory", |
| 414 | + ) |
| 415 | + })?; |
| 416 | + |
| 417 | + let mut candidates: Vec<_> = entries |
| 418 | + .iter() |
| 419 | + .filter_map(|entry| { |
| 420 | + let codeowners_dir = match entry.source_file.parent() { |
| 421 | + Some(dir) => dir, |
| 422 | + None => { |
| 423 | + eprintln!( |
| 424 | + "CODEOWNERS entry has no parent directory: {}", |
| 425 | + entry.source_file.display() |
| 426 | + ); |
| 427 | + return None; |
| 428 | + } |
| 429 | + }; |
| 430 | + |
| 431 | + // Check if the CODEOWNERS directory is an ancestor of the target directory |
| 432 | + if !target_dir.starts_with(codeowners_dir) { |
| 433 | + return None; |
| 434 | + } |
| 435 | + |
| 436 | + // Calculate the depth as the number of components in the relative path from codeowners_dir to target_dir |
| 437 | + let rel_path = match target_dir.strip_prefix(codeowners_dir) { |
| 438 | + Ok(p) => p, |
| 439 | + Err(_) => return None, // Should not happen due to starts_with check |
| 440 | + }; |
| 441 | + let depth = rel_path.components().count(); |
| 442 | + |
| 443 | + // Check if the pattern matches the target file |
| 444 | + let matches = { |
| 445 | + let mut builder = OverrideBuilder::new(codeowners_dir); |
| 446 | + if let Err(e) = builder.add(&entry.pattern) { |
| 447 | + eprintln!( |
| 448 | + "Invalid pattern '{}' in {}: {}", |
| 449 | + entry.pattern, |
| 450 | + entry.source_file.display(), |
| 451 | + e |
| 452 | + ); |
| 453 | + return None; |
| 454 | + } |
| 455 | + let over: Override = match builder.build() { |
| 456 | + Ok(o) => o, |
| 457 | + Err(e) => { |
| 458 | + eprintln!( |
| 459 | + "Failed to build override for pattern '{}': {}", |
| 460 | + entry.pattern, e |
| 461 | + ); |
| 462 | + return None; |
| 463 | + } |
| 464 | + }; |
| 465 | + over.matched(file_path, false).is_whitelist() |
| 466 | + }; |
| 467 | + |
| 468 | + if matches { Some((entry, depth)) } else { None } |
| 469 | + }) |
| 470 | + .collect(); |
| 471 | + |
| 472 | + // Sort the candidates by depth, source file, and line number |
| 473 | + candidates.sort_unstable_by(|a, b| { |
| 474 | + let a_entry = a.0; |
| 475 | + let a_depth = a.1; |
| 476 | + let b_entry = b.0; |
| 477 | + let b_depth = b.1; |
| 478 | + |
| 479 | + // Primary sort by depth (ascending) |
| 480 | + a_depth |
| 481 | + .cmp(&b_depth) |
| 482 | + // Then by source file (to group entries from the same CODEOWNERS file) |
| 483 | + .then_with(|| a_entry.source_file.cmp(&b_entry.source_file)) |
| 484 | + // Then by line number (descending) to prioritize later entries in the same file |
| 485 | + .then_with(|| b_entry.line_number.cmp(&a_entry.line_number)) |
| 486 | + }); |
| 487 | + |
| 488 | + // Extract both owners and tags from the highest priority entry, if any |
| 489 | + Ok(candidates |
| 490 | + .first() |
| 491 | + .map(|(entry, _)| (entry.owners.clone(), entry.tags.clone())) |
| 492 | + .unwrap_or_default()) |
| 493 | +} |
0 commit comments