Normalize a tracked path from the TREE table to a relative PathBuf with forward slashes, handling absolute paths and platform differences.
(path: &str, repo_root: &Path)
| 584 | /// Normalize a tracked path from the TREE table to a relative PathBuf |
| 585 | /// with forward slashes, handling absolute paths and platform differences. |
| 586 | fn normalize_tracked_path(path: &str, repo_root: &Path) -> PathBuf { |
| 587 | let path_buf = PathBuf::from(path); |
| 588 | |
| 589 | let stripped = if path_buf.is_absolute() { |
| 590 | if let Ok(rel) = path_buf.strip_prefix(repo_root) { |
| 591 | rel.to_path_buf() |
| 592 | } else if let Ok(canonical_root) = repo_root.canonicalize() { |
| 593 | if let Ok(rel) = path_buf.strip_prefix(&canonical_root) { |
| 594 | rel.to_path_buf() |
| 595 | } else { |
| 596 | path_buf |
| 597 | } |
| 598 | } else { |
| 599 | path_buf |
| 600 | } |
| 601 | } else { |
| 602 | path_buf |
| 603 | }; |
| 604 | |
| 605 | // Normalize to forward slashes for cross-platform consistency |
| 606 | if cfg!(windows) || stripped.to_string_lossy().contains('\\') { |
| 607 | PathBuf::from(stripped.to_string_lossy().replace('\\', "/")) |
| 608 | } else { |
| 609 | stripped |
| 610 | } |
| 611 | } |