oh-my-posh/src/segments/git_windows.go
Piotr Kalinowski a2533c8f5b fix(git): better cross-platform path resolution and tests
The simple filepath.IsAbs() is not enough on Windows, where the relative
path may start with a separator and should then be taken relative to the
volume name.

So, introduce resolveGitPath() helper function that will do the right
thing on both Windows and other systems. As a bonus, it returns paths
converted to slashes for ease of use with the rest of the git segment:
- Git for Windows uses only slashes.
- Slashes do work as a path separator on Windows anyway.
- Some tests in git segment still use (and rightfully so) slashes.

Add tests for resolveGitPath() on both types of systems and fix
TestEnableInWorktree using a system-dependent testing constant as a root
path.
2022-08-03 18:20:00 +02:00

23 lines
595 B
Go

package segments
import "path/filepath"
// resolveGitPath resolves path relative to base.
func resolveGitPath(base, path string) string {
if len(path) == 0 {
return base
}
if filepath.IsAbs(path) {
return path
}
// Note that git on Windows uses slashes exclusively. And it's okay
// because Windows actually accepts both directory separators. More
// importantly, however, parts of the git segment depend on those
// slashes.
if path[0] == '/' {
// path is a disk-relative path.
return filepath.VolumeName(base) + path
}
return filepath.ToSlash(filepath.Join(base, path))
}