Skip to content

Commit

Permalink
fix: Windows path errors ⚓
Browse files Browse the repository at this point in the history
  • Loading branch information
gabyx committed Jan 24, 2024
1 parent 1e6253e commit 0e89c59
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 74 deletions.
12 changes: 2 additions & 10 deletions githooks/apps/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"strings"
"time"

"github.com/mitchellh/go-homedir"
"github.com/pbenner/threadpool"
"github.com/pkg/math"
)
Expand Down Expand Up @@ -196,15 +195,8 @@ func setupSettings(repoPath string) (HookSettings, UISettings) {
func getInstallDir(gitx *git.Context) string {
installDir := hooks.GetInstallDir(gitx)

setDefault := func() {
usr, err := homedir.Dir()
cm.AssertNoErrorPanic(err, "Could not get home directory.")
usr = filepath.ToSlash(usr)
installDir = path.Join(usr, hooks.HooksDirName)
}

if strs.IsEmpty(installDir) {
setDefault()
installDir, _ = hooks.GetDefaultInstallDir()
} else if exists, err := cm.IsPathExisting(installDir); !exists {

log.AssertNoError(err,
Expand All @@ -214,7 +206,7 @@ func getInstallDir(gitx *git.Context) string {
"Install directory at '%s' is missing.",
installDir)

setDefault()
installDir, _ = hooks.GetDefaultInstallDir()

log.WarnF(
"Falling back to default directory at '%s'.\n"+
Expand Down
47 changes: 0 additions & 47 deletions githooks/cmd/common/install/load-install.go

This file was deleted.

5 changes: 3 additions & 2 deletions githooks/cmd/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,10 @@ func setupSettings(
// an install directory set (from --prefix)
if strs.IsNotEmpty(args.InstallPrefix) {
var err error
installPrefix, err := cm.ReplaceTilde(filepath.ToSlash(args.InstallPrefix))
args.InstallPrefix, err = cm.ReplaceTilde(filepath.ToSlash(args.InstallPrefix))
log.AssertNoErrorPanic(err, "Could not replace '~' character in path.")
installDirRaw = path.Join(installPrefix, ".githooks")

installDirRaw = path.Join(args.InstallPrefix, ".githooks")
installDir = os.ExpandEnv(installDirRaw)

} else {
Expand Down
3 changes: 1 addition & 2 deletions githooks/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/gabyx/githooks/githooks/build"
ccm "github.com/gabyx/githooks/githooks/cmd/common"
inst "github.com/gabyx/githooks/githooks/cmd/common/install"
"github.com/gabyx/githooks/githooks/cmd/config"
"github.com/gabyx/githooks/githooks/cmd/disable"
"github.com/gabyx/githooks/githooks/cmd/exec"
Expand Down Expand Up @@ -44,7 +43,7 @@ func NewSettings(
log.AssertNoError(git.SanitizeOsEnv(), "Could not sanitize OS environment.")
gitx := git.NewCtx()

installDir, installDirRaw := inst.LoadInstallDir(log, gitx)
installDir, installDirRaw := hooks.LoadInstallDir(log, gitx)

promptx, err = prompt.CreateContext(log, false, false)
log.AssertNoErrorF(err, "Prompt setup failed -> using fallback.")
Expand Down
19 changes: 9 additions & 10 deletions githooks/cmd/uninstaller/uninstaller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ func defineArguments(cmd *cobra.Command, vi *viper.Viper) {
setupMockFlags(cmd, vi)
}

func setupSettings(log cm.ILogContext, gitx *git.Context, args *Arguments) (Settings, UISettings) {
func setupSettings(log cm.ILogContext, ctx *ccm.CmdContext, args *Arguments) (Settings, UISettings) {

var promptx prompt.IContext
var err error
gitx := ctx.GitX

log.AssertNoErrorPanic(err, "Could not get current working directory.")

Expand All @@ -89,24 +90,22 @@ func setupSettings(log cm.ILogContext, gitx *git.Context, args *Arguments) (Sett
log.AssertNoErrorF(err, "Prompt setup failed -> using fallback.")
}

installDir, _ := install.LoadInstallDir(log, gitx)

// Safety check.
log.PanicIfF(!strings.Contains(installDir, ".githooks"),
log.PanicIfF(!strings.Contains(ctx.InstallDir, ".githooks"),
"Uninstall path at '%s' needs to contain '.githooks'.")

// Remove temporary directory if existing
tempDir, err := hooks.CleanTemporaryDir(installDir)
tempDir, err := hooks.CleanTemporaryDir(ctx.InstallDir)
log.AssertNoErrorPanicF(err,
"Could not clean temporary directory in '%s'", installDir)
"Could not clean temporary directory in '%s'", ctx.InstallDir)

lfsHooksCache, err := hooks.NewLFSHooksCache(hooks.GetTemporaryDir(installDir))
lfsHooksCache, err := hooks.NewLFSHooksCache(hooks.GetTemporaryDir(ctx.InstallDir))
log.AssertNoErrorPanicF(err, "Could not create LFS hooks cache.")

return Settings{
Gitx: gitx,
InstallDir: installDir,
CloneDir: hooks.GetReleaseCloneDir(installDir),
InstallDir: ctx.InstallDir,
CloneDir: hooks.GetReleaseCloneDir(ctx.InstallDir),
TempDir: tempDir,
UninstalledGitDirs: make(UninstallSet, 10), // nolint: gomnd
LFSHooksCache: lfsHooksCache},
Expand Down Expand Up @@ -384,7 +383,7 @@ func runUninstall(ctx *ccm.CmdContext, vi *viper.Viper) {

log.DebugF("Arguments: %+v", args)

settings, uiSettings := setupSettings(log, ctx.GitX, &args)
settings, uiSettings := setupSettings(log, ctx, &args)

if !args.InternalPostDispatch {
if isDispatched := runDispatchedInstall(log, &settings, &args); isDispatched {
Expand Down
51 changes: 48 additions & 3 deletions githooks/hooks/githooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
cm "github.com/gabyx/githooks/githooks/common"
"github.com/gabyx/githooks/githooks/git"
strs "github.com/gabyx/githooks/githooks/strings"
"github.com/mitchellh/go-homedir"
)

// HooksDirName denotes the directory name used for repository specific hooks.
Expand Down Expand Up @@ -162,23 +163,67 @@ func GetSharedGithooksDir(repoDir string) (dir string) {
return
}

// LoadInstallDir loads the install directory and uses a default if
// it does not exist.
func LoadInstallDir(log cm.ILogContext, gitx *git.Context) (installDir string, installDirRaw string) {

installDir, installDirRaw = GetInstallDirWithRaw(gitx)

if !cm.IsDirectory(installDir) {

if strs.IsNotEmpty(installDir) {
log.WarnF("Install directory '%s' does not exist.\n"+
"Githooks installation is corrupt!\n"+
"Using default location '~/.githooks'.", installDir)
}

installDir, installDirRaw = GetDefaultInstallDir()
}

return
}

// / Get the default install dir.
func GetDefaultInstallDir() (installDir, installDirRaw string) {
home := filepath.ToSlash(os.Getenv("HOME"))

if exists, _ := cm.IsPathExisting(home); !exists {
// Home variable does not exist. Use the lib to detect it, as fallback.
var err error
home, err = homedir.Dir()
home = filepath.ToSlash(home)
cm.AssertNoErrorPanic(err, "Could not get home directory.")

installDir = path.Join(home, HooksDirName)
installDirRaw = installDir

} else {
// Home env. variable exists use this one.
installDir = path.Join(home, HooksDirName)
installDirRaw = path.Join("$HOME", HooksDirName)
}

return
}

// GetInstallDir returns the Githooks install directory.
func GetInstallDir(gitx *git.Context) string {
raw := filepath.ToSlash(gitx.GetConfig(GitCKInstallDir, git.GlobalScope))
raw := gitx.GetConfig(GitCKInstallDir, git.GlobalScope)

return os.ExpandEnv(raw)
return filepath.ToSlash(os.ExpandEnv(raw))
}

// GetInstallDirWithRaw returns the Githooks install directory,
// but also with unexpanded env. variables form.
func GetInstallDirWithRaw(gitx *git.Context) (string, string) {
raw := filepath.ToSlash(gitx.GetConfig(GitCKInstallDir, git.GlobalScope))

return os.ExpandEnv(raw), raw
return filepath.ToSlash(os.ExpandEnv(raw)), raw
}

// SetInstallDir sets the global Githooks install directory.
func SetInstallDir(gitx *git.Context, path string) error {
cm.DebugAssertF(path == filepath.ToSlash(path), "Filepath must be '/' delimted.")
return gitx.SetConfig(GitCKInstallDir, path, git.GlobalScope)
}

Expand Down

0 comments on commit 0e89c59

Please sign in to comment.