- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1
[Refactor] Extract diff parsing into src/diff module #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            Copilot
  wants to merge
  6
  commits into
  main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
copilot/fix-54bae479-fd01-4562-8f45-56a7c3cda9e2
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            6 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      e272d7f
              
                Initial plan
              
              
                Copilot 51ab821
              
                Extract diff parsing into dedicated src/diff module
              
              
                Copilot 4206c0f
              
                Merge remote-tracking branch 'origin/main' into copilot/fix-54bae479-…
              
              
                oleander 0029022
              
                Merge remote-tracking branch 'origin/main' into copilot/fix-54bae479-…
              
              
                 b525f80
              
                Merge branch 'main' into copilot/fix-54bae479-fd01-4562-8f45-56a7c3cd…
              
              
                oleander 937db80
              
                fix: remove needless borrows in model validation test to fix CI clipp…
              
              
                Copilot File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| //! Diff processing and parsing utilities. | ||
| //! | ||
| //! This module handles parsing git diffs into structured data | ||
| //! and provides utilities for working with diff content. | ||
|  | ||
| pub mod parser; | ||
| pub mod traits; | ||
|  | ||
| pub use parser::{parse_diff, ParsedFile}; | ||
| pub use traits::{DiffDeltaPath, FilePath, Utf8String}; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,359 @@ | ||||||||||||||||||||
| //! Git diff parsing utilities. | ||||||||||||||||||||
| use anyhow::Result; | ||||||||||||||||||||
|  | ||||||||||||||||||||
| /// Represents a parsed file from a git diff | ||||||||||||||||||||
| #[derive(Debug, Clone)] | ||||||||||||||||||||
| pub struct ParsedFile { | ||||||||||||||||||||
| pub path: String, | ||||||||||||||||||||
| pub operation: String, | ||||||||||||||||||||
| pub diff_content: String | ||||||||||||||||||||
| } | ||||||||||||||||||||
|  | ||||||||||||||||||||
| /// Extracts file path from diff header parts | ||||||||||||||||||||
| /// | ||||||||||||||||||||
| /// Handles various git prefixes (a/, b/, c/, i/) and /dev/null for deleted files. | ||||||||||||||||||||
| /// | ||||||||||||||||||||
| /// # Arguments | ||||||||||||||||||||
| /// * `parts` - The whitespace-split parts from a "diff --git" line | ||||||||||||||||||||
| /// | ||||||||||||||||||||
| /// # Returns | ||||||||||||||||||||
| /// * `Option<String>` - The extracted path without prefixes, or None if parsing fails | ||||||||||||||||||||
| fn extract_file_path_from_diff_parts(parts: &[&str]) -> Option<String> { | ||||||||||||||||||||
| if parts.len() < 4 { | ||||||||||||||||||||
| return None; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|  | ||||||||||||||||||||
| // Helper to strip git prefixes (a/, b/, c/, i/) | ||||||||||||||||||||
| let strip_prefix = |s: &str| { | ||||||||||||||||||||
| s.trim_start_matches("a/") | ||||||||||||||||||||
| .trim_start_matches("b/") | ||||||||||||||||||||
| .trim_start_matches("c/") | ||||||||||||||||||||
| .trim_start_matches("i/") | ||||||||||||||||||||
| .to_string() | ||||||||||||||||||||
| }; | ||||||||||||||||||||
|  | ||||||||||||||||||||
| let new_path = strip_prefix(parts[3]); | ||||||||||||||||||||
| let old_path = strip_prefix(parts[2]); | ||||||||||||||||||||
|  | ||||||||||||||||||||
| // Prefer new path unless it's /dev/null (deleted file) | ||||||||||||||||||||
| Some(if new_path == "/dev/null" || new_path == "dev/null" { | ||||||||||||||||||||
| old_path | ||||||||||||||||||||
| } else { | ||||||||||||||||||||
| new_path | ||||||||||||||||||||
| }) | ||||||||||||||||||||
| } | ||||||||||||||||||||
|  | ||||||||||||||||||||
| /// Parse git diff into individual file changes. | ||||||||||||||||||||
| /// | ||||||||||||||||||||
| /// Handles various diff formats including: | ||||||||||||||||||||
| /// - Standard git diff output | ||||||||||||||||||||
| /// - Diffs with commit hashes | ||||||||||||||||||||
| /// - Diffs with various path prefixes (a/, b/, c/, i/) | ||||||||||||||||||||
| /// - Deleted files (/dev/null paths) | ||||||||||||||||||||
| /// | ||||||||||||||||||||
| /// # Arguments | ||||||||||||||||||||
| /// * `diff_content` - Raw git diff text | ||||||||||||||||||||
| /// | ||||||||||||||||||||
| /// # Returns | ||||||||||||||||||||
| /// * `Result<Vec<ParsedFile>>` - Parsed files or error | ||||||||||||||||||||
| pub fn parse_diff(diff_content: &str) -> Result<Vec<ParsedFile>> { | ||||||||||||||||||||
|         
                  oleander marked this conversation as resolved.
              Show resolved
            Hide resolved | ||||||||||||||||||||
| let mut files = Vec::new(); | ||||||||||||||||||||
| let mut current_file: Option<ParsedFile> = None; | ||||||||||||||||||||
| let mut current_diff = String::new(); | ||||||||||||||||||||
|  | ||||||||||||||||||||
| // Debug output | ||||||||||||||||||||
| log::debug!("Parsing diff with {} lines", diff_content.lines().count()); | ||||||||||||||||||||
|         
                  oleander marked this conversation as resolved.
              Show resolved
            Hide resolved | ||||||||||||||||||||
|  | ||||||||||||||||||||
| // Add more detailed logging for debugging | ||||||||||||||||||||
| if log::log_enabled!(log::Level::Debug) && !diff_content.is_empty() { | ||||||||||||||||||||
| // Make sure we truncate at a valid UTF-8 character boundary | ||||||||||||||||||||
| let preview = if diff_content.len() > 500 { | ||||||||||||||||||||
| let truncated_index = diff_content | ||||||||||||||||||||
| .char_indices() | ||||||||||||||||||||
| .take_while(|(i, _)| *i < 500) | ||||||||||||||||||||
| .last() | ||||||||||||||||||||
| .map(|(i, c)| i + c.len_utf8()) | ||||||||||||||||||||
| .unwrap_or(0); | ||||||||||||||||||||
|  | ||||||||||||||||||||
| format!("{}... (truncated)", &diff_content[..truncated_index]) | ||||||||||||||||||||
| } else { | ||||||||||||||||||||
| diff_content.to_string() | ||||||||||||||||||||
| }; | ||||||||||||||||||||
| log::debug!("Diff content preview: \n{preview}"); | ||||||||||||||||||||
| } | ||||||||||||||||||||
|  | ||||||||||||||||||||
| // Handle different diff formats | ||||||||||||||||||||
| let mut in_diff_section = false; | ||||||||||||||||||||
| let mut _commit_hash_line: Option<&str> = None; | ||||||||||||||||||||
|  | ||||||||||||||||||||
| // First scan to detect if this is a commit message with hash | ||||||||||||||||||||
| for line in diff_content.lines().take(3) { | ||||||||||||||||||||
| if line.len() >= 40 && line.chars().take(40).all(|c| c.is_ascii_hexdigit()) { | ||||||||||||||||||||
| _commit_hash_line = Some(line); | ||||||||||||||||||||
| break; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
|  | ||||||||||||||||||||
| 
      Comment on lines
    
      +88
     to 
      +97
    
   
     | ||||||||||||||||||||
| let mut _commit_hash_line: Option<&str> = None; | |
| // First scan to detect if this is a commit message with hash | |
| for line in diff_content.lines().take(3) { | |
| if line.len() >= 40 && line.chars().take(40).all(|c| c.is_ascii_hexdigit()) { | |
| _commit_hash_line = Some(line); | |
| break; | |
| } | |
| } | 
        
          
              
                  oleander marked this conversation as resolved.
              
          
            Show resolved
            Hide resolved
        
      
                  oleander marked this conversation as resolved.
              
          
            Show resolved
            Hide resolved
        
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Since diff::mod.rs re-exports parse_diff, prefer use ai::diff::parse_diff; to decouple the example from internal module structure and rely on the intended public API surface.