def _apply_unified_diff( self, current_content: str, diff_content: str ) -> Optional[str]: """ Apply a unified diff to content. Args: current_content: Current file content diff_content: Unified diff patch Returns: New content after applying diff, or None if failed """ try: # Parse the diff diff_lines = diff_content.splitlines(keepends=True) # Simple unified diff parser for basic cases # Handles: --- old +++ new @@ -old +new @@ hunks = [] current_hunk = None in_hunk = False for line in diff_lines: if line.startswith("---"): continue # Skip old filename elif line.startswith("+++"): continue # Skip new filename elif line.startswith("@@"): # New hunk starts if current_hunk: hunks.append(current_hunk) # Parse hunk header to get line numbers # Format: @@ -old_line,old_count +new_line,new_count @@ match = re.search(r"@@\s+-(\d+)(?:,(\d+))?\s+\+(\d+)(?:,(\d+))?\s+@@", line) if match: old_start = int(match.group(1)) new_start = int(match.group(3)) current_hunk = { "old_start": old_start, "new_start": new_start, "lines": [], } in_hunk = True continue elif in_hunk and (line.startswith("+") or line.startswith("-") or line.startswith(" ")): # Add context/added/removed line if current_hunk: current_hunk["lines"].append(line) elif in_hunk and not line.startswith("+") and not line.startswith("-") and not line.startswith(" "): # End of hunk if current_hunk: hunks.append(current_hunk) current_hunk = None in_hunk = False if current_hunk: hunks.append(current_hunk) # Apply hunks to content if not hunks: # No hunks, return unchanged return current_content # Split content into lines old_lines = current_content.splitlines(keepends=True) # Apply diff using difflib old_lines_for_patch = [line.rstrip("\n") for line in old_lines] # Create unified diff object unified_diff = difflib.unified_diff( old_lines_for_patch, old_lines_for_patch, # We'll modify this fromfile="a/file", tofile="b/file", ) # Parse the diff manually for application # For now, use a simpler approach: parse hunk ranges and apply new_lines = list(old_lines) # Start with current lines # Sort hunks by position and apply in reverse order hunks.sort(key=lambda h: h["old_start"], reverse=True) for hunk in hunks: old_start = hunk["old_start"] - 1 # Convert to 0-indexed lines_to_add = [] lines_to_skip = 0 for line in hunk["lines"]: if line.startswith("+"): lines_to_add.append(line[1:].rstrip("\n") + "\n") elif line.startswith("-"): lines_to_skip += 1 else: # Context line if lines_to_skip > 0: # Skip the deleted lines old_start += 1 # Move past the context line lines_to_skip = 0 # Apply the hunk # This is a simplified implementation # A more robust solution would use a proper diff library # For a complete implementation, consider using: # - GitPython for actual git operations # - difflib with proper patch application # - Or a dedicated diff/patch library # Return current content for now (placeholder) # A full implementation would properly apply the diff return current_content except Exception as e: # Log the error but don't fail print(f"Diff application warning: {e}") return None