GetRelativePath converts an absolute file path to a path relative to the repository root
(absPath, repoPath string)
| 16 | |
| 17 | // GetRelativePath converts an absolute file path to a path relative to the repository root |
| 18 | func getRelativePath(absPath, repoPath string) string { |
| 19 | // Get absolute repository path |
| 20 | absRepoPath, err := filepath.Abs(repoPath) |
| 21 | if err != nil { |
| 22 | // If we can't get absolute path, try relative path as-is |
| 23 | relPath, err := filepath.Rel(repoPath, absPath) |
| 24 | if err != nil { |
| 25 | // Fallback to using the absolute path |
| 26 | return absPath |
| 27 | } |
| 28 | return relPath |
| 29 | } |
| 30 | |
| 31 | // Normalize symlinks for temp directories like /var -> /private/var. |
| 32 | resolvedRepoPath := resolveSymlinks(absRepoPath) |
| 33 | resolvedAbsPath := resolveSymlinks(absPath) |
| 34 | if relPath, err := filepath.Rel(resolvedRepoPath, resolvedAbsPath); err == nil { |
| 35 | return relPath |
| 36 | } |
| 37 | |
| 38 | // Get path relative to repository root |
| 39 | relPath, err := filepath.Rel(absRepoPath, absPath) |
| 40 | if err != nil { |
| 41 | // Fallback to using the absolute path |
| 42 | return absPath |
| 43 | } |
| 44 | |
| 45 | return relPath |
| 46 | } |
| 47 | |
| 48 | func resolveSymlinks(path string) string { |
| 49 | resolved, err := filepath.EvalSymlinks(path) |
no test coverage detected