Skip to content

Commit

Permalink
fix: Prioritize $HOME when Cygwin or msys2 is detected
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Feb 14, 2024
1 parent 6788c54 commit ccde612
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
3 changes: 1 addition & 2 deletions internal/chezmoi/abspath.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package chezmoi

import (
"fmt"
"os"
"path"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -176,7 +175,7 @@ func (p *AbsPath) UnmarshalText(text []byte) error {

// HomeDirAbsPath returns the user's home directory as an AbsPath.
func HomeDirAbsPath() (AbsPath, error) {
userHomeDir, err := os.UserHomeDir()
userHomeDir, err := UserHomeDir()
if err != nil {
return EmptyAbsPath, err
}
Expand Down
6 changes: 6 additions & 0 deletions internal/chezmoi/chezmoi_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package chezmoi

import (
"io/fs"
"os"

"golang.org/x/sys/unix"
)
Expand All @@ -26,6 +27,11 @@ func IsExecutable(fileInfo fs.FileInfo) bool {
return fileInfo.Mode().Perm()&0o111 != 0
}

// UserHomeDir on UNIX returns the value of os.UserHomeDir.
func UserHomeDir() (string, error) {
return os.UserHomeDir()
}

// isPrivate returns if fileInfo is private.
func isPrivate(fileInfo fs.FileInfo) bool {
return fileInfo.Mode().Perm()&0o77 == 0
Expand Down
11 changes: 11 additions & 0 deletions internal/chezmoi/chezmoi_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ func IsExecutable(fileInfo fs.FileInfo) bool {
})
}

// UserHomeDir on Windows returns the value of $HOME if it is set and either
// Cygwin or msys2 is detected, otherwise it falls back to os.UserHomeDir.
func UserHomeDir() (string, error) {
if os.Getenv("CYGWIN") != "" || os.Getenv("MSYSTEM") != "" {
if userHomeDir := os.Getenv("HOME"); userHomeDir != "" {
return userHomeDir, nil
}
}
return os.UserHomeDir()
}

// isPrivate returns false on Windows.
func isPrivate(fileInfo fs.FileInfo) bool {
return false
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ var (

// newConfig creates a new Config with the given options.
func newConfig(options ...configOption) (*Config, error) {
userHomeDir, err := os.UserHomeDir()
userHomeDir, err := chezmoi.UserHomeDir()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/upgradecmd_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func getUpgradeMethod(fileSystem vfs.Stater, executableAbsPath chezmoi.AbsPath)

// If the executable is in the user's home directory, then always use
// replace-executable.
switch userHomeDir, err := os.UserHomeDir(); {
switch userHomeDir, err := chezmoi.UserHomeDir(); {
case errors.Is(err, fs.ErrNotExist):
case err != nil:
return "", err
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/upgradecmd_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func getUpgradeMethod(fileSystem vfs.Stater, executableAbsPath chezmoi.AbsPath)

// If the executable is in the user's home directory, then always use
// replace-executable.
switch userHomeDir, err := os.UserHomeDir(); {
switch userHomeDir, err := chezmoi.UserHomeDir(); {
case errors.Is(err, fs.ErrNotExist):
case err != nil:
return "", err
Expand Down

0 comments on commit ccde612

Please sign in to comment.