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)
| 553 | /// Normalize a tracked path from the TREE table to a relative PathBuf |
| 554 | /// with forward slashes, handling absolute paths and platform differences. |
| 555 | fn normalize_tracked_path(path: &str, repo_root: &Path) -> PathBuf { |
| 556 | let path_buf = PathBuf::from(path); |
| 557 | |
| 558 | let stripped = if path_buf.is_absolute() { |
| 559 | if let Ok(rel) = path_buf.strip_prefix(repo_root) { |
| 560 | rel.to_path_buf() |
| 561 | } else if let Ok(canonical_root) = repo_root.canonicalize() { |
| 562 | if let Ok(rel) = path_buf.strip_prefix(&canonical_root) { |
| 563 | rel.to_path_buf() |
| 564 | } else { |
| 565 | path_buf |
| 566 | } |
| 567 | } else { |
| 568 | path_buf |
| 569 | } |
| 570 | } else { |
| 571 | path_buf |
| 572 | }; |
| 573 | |
| 574 | // Normalize to forward slashes for cross-platform consistency |
| 575 | if cfg!(windows) || stripped.to_string_lossy().contains('\\') { |
| 576 | PathBuf::from(stripped.to_string_lossy().replace('\\', "/")) |
| 577 | } else { |
| 578 | stripped |
| 579 | } |
| 580 | } |