| 398 | } |
| 399 | |
| 400 | fn create_diff(filepath: &str, old_content: &str, new_content: &str) -> String { |
| 401 | let old_lines: Vec<&str> = old_content.lines().collect(); |
| 402 | let new_lines: Vec<&str> = new_content.lines().collect(); |
| 403 | |
| 404 | let mut diff = format!("--- {}\n+++ {}\n", filepath, filepath); |
| 405 | |
| 406 | let mut old_idx = 0; |
| 407 | let mut new_idx = 0; |
| 408 | |
| 409 | while old_idx < old_lines.len() || new_idx < new_lines.len() { |
| 410 | if old_idx >= old_lines.len() { |
| 411 | diff.push_str(&format!("+{}\n", new_lines[new_idx])); |
| 412 | new_idx += 1; |
| 413 | } else if new_idx >= new_lines.len() { |
| 414 | diff.push_str(&format!("-{}\n", old_lines[old_idx])); |
| 415 | old_idx += 1; |
| 416 | } else if old_lines[old_idx] == new_lines[new_idx] { |
| 417 | old_idx += 1; |
| 418 | new_idx += 1; |
| 419 | } else { |
| 420 | diff.push_str(&format!("-{}\n", old_lines[old_idx])); |
| 421 | diff.push_str(&format!("+{}\n", new_lines[new_idx])); |
| 422 | old_idx += 1; |
| 423 | new_idx += 1; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | diff |
| 428 | } |
| 429 | |
| 430 | fn normalize_line_endings(text: &str) -> String { |
| 431 | text.replace("\r\n", "\n") |