Skip to content

Commit

Permalink
chore: Tidy up absolute path handling
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Dec 19, 2021
1 parent 68fff1c commit 7f07025
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 42 deletions.
33 changes: 13 additions & 20 deletions internal/chezmoi/path_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ import (
// NewAbsPathFromExtPath returns a new AbsPath by converting extPath to use
// slashes, performing tilde expansion, and making the path absolute.
func NewAbsPathFromExtPath(extPath string, homeDirAbsPath AbsPath) (AbsPath, error) {
tildeSlashPath := expandTilde(filepath.ToSlash(extPath), homeDirAbsPath)
if filepath.IsAbs(tildeSlashPath) {
return NewAbsPath(tildeSlashPath), nil
}
slashPathAbsPath, err := filepath.Abs(tildeSlashPath)
if err != nil {
return EmptyAbsPath, err
switch {
case extPath == "~":
return homeDirAbsPath, nil
case strings.HasPrefix(extPath, "~/"):
return homeDirAbsPath.JoinString(extPath[2:]), nil
case filepath.IsAbs(extPath):
return NewAbsPath(extPath), nil
default:
absPath, err := filepath.Abs(extPath)
if err != nil {
return EmptyAbsPath, err
}
return NewAbsPath(absPath), nil
}
return NewAbsPath(slashPathAbsPath), nil
}

// NormalizePath returns path normalized. On non-Windows systems, normalized
Expand All @@ -32,18 +37,6 @@ func NormalizePath(path string) (AbsPath, error) {
return NewAbsPath(absPath), nil
}

// expandTilde expands a leading tilde in path.
func expandTilde(path string, homeDirAbsPath AbsPath) string {
switch {
case path == "~":
return homeDirAbsPath.String()
case strings.HasPrefix(path, "~/"):
return homeDirAbsPath.JoinString(path[2:]).String()
default:
return path
}
}

// normalizeLinkname returns linkname normalized. On non-Windows systems, it
// returns linkname unchanged.
func normalizeLinkname(linkname string) string {
Expand Down
35 changes: 13 additions & 22 deletions internal/chezmoi/path_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ import (
// slashes, performing tilde expansion, making the path absolute, and converting
// the volume name to uppercase.
func NewAbsPathFromExtPath(extPath string, homeDirAbsPath AbsPath) (AbsPath, error) {
slashTildePath := filepath.ToSlash(expandTilde(extPath, homeDirAbsPath))
if filepath.IsAbs(slashTildePath) {
return NewAbsPath(volumeNameToUpper(slashTildePath)), nil
}
tildeAbsPath, err := filepath.Abs(slashTildePath)
if err != nil {
return EmptyAbsPath, err
switch {
case extPath == "~":
return homeDirAbsPath, nil
case len(extPath) >= 2 && extPath[0] == '~' && isSlash(extPath[1]):
return homeDirAbsPath.JoinString(filepath.ToSlash(extPath[2:])), nil
case filepath.IsAbs(extPath):
return NewAbsPath(volumeNameToUpper(extPath)).ToSlash(), nil
default:
extPath, err := filepath.Abs(extPath)
if err != nil {
return EmptyAbsPath, err
}
return NewAbsPath(volumeNameToUpper(extPath)).ToSlash(), nil
}
// filepath.Abs on Windows converts forward slashes to backslashes so we
// have to call filepath.ToSlash again.
return NewAbsPath(filepath.ToSlash(volumeNameToUpper(tildeAbsPath))), nil
}

// NormalizePath returns path normalized. On Windows, normalized paths are
Expand All @@ -36,18 +39,6 @@ func NormalizePath(path string) (AbsPath, error) {
return NewAbsPath(path).ToSlash(), nil
}

// expandTilde expands a leading tilde in path.
func expandTilde(path string, homeDirAbsPath AbsPath) string {
switch {
case path == "~":
return homeDirAbsPath.String()
case len(path) >= 2 && path[0] == '~' && isSlash(path[1]):
return homeDirAbsPath.JoinString(path[2:]).String()
default:
return path
}
}

// normalizeLinkname returns linkname normalized. On Windows, backslashes are
// converted to forward slashes and if linkname is an absolute path then the
// volume name is converted to uppercase.
Expand Down

0 comments on commit 7f07025

Please sign in to comment.