From 50792728cfba5d5dfc53922dcee8669a7a73bb4f Mon Sep 17 00:00:00 2001 From: "Nuetzi, Gabriel" Date: Tue, 27 Jun 2023 07:55:07 +0200 Subject: [PATCH 1/2] fix: Prevent hardcoding `.githooks` etc :anchor: --- githooks/hooks/githooks.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/githooks/hooks/githooks.go b/githooks/hooks/githooks.go index 3b72f4a1..db1dc71d 100644 --- a/githooks/hooks/githooks.go +++ b/githooks/hooks/githooks.go @@ -14,6 +14,7 @@ import ( // HooksDirName denotes the directory name used for repository specific hooks. const HooksDirName = ".githooks" +const HooksDirNameShared = "githooks" // GithooksWebpage is the main Githooks webpage. const GithooksWebpage = "https://github.com/gabyx/githooks" @@ -150,7 +151,7 @@ func GetSharedGithooksDir(repoDir string) (dir string) { // This is second, to allow internal development Githooks inside shared repos. // 3. Fallback to the whole repository. - if dir = path.Join(repoDir, "githooks"); cm.IsDirectory(dir) { + if dir = path.Join(repoDir, HooksDirNameShared); cm.IsDirectory(dir) { return } else if dir = GetGithooksDir(repoDir); cm.IsDirectory(dir) { return From 1b119b3d15dc306f67cabfe931dc9b4d0b38b5d4 Mon Sep 17 00:00:00 2001 From: "Nuetzi, Gabriel" Date: Tue, 27 Jun 2023 08:13:50 +0200 Subject: [PATCH 2/2] feat: Use env. variable to overwrite `.githooks` name :anchor: --- githooks/hooks/githooks.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/githooks/hooks/githooks.go b/githooks/hooks/githooks.go index db1dc71d..61ba4222 100644 --- a/githooks/hooks/githooks.go +++ b/githooks/hooks/githooks.go @@ -140,17 +140,36 @@ func CheckGithooksSetup(gitx *git.Context) (err error) { // GetGithooksDir gets the hooks directory for Githooks inside a repository (bare, non-bare). func GetGithooksDir(repoDir string) string { + + // 0. priority has ".${GITHOOKS_HOOKS_DIR_NAME}" + // 1. priority has ".githooks" + + if name := os.Getenv("GITHOOKS_HOOKS_DIR_NAME"); strs.IsNotEmpty(name) { + name = "." + strings.TrimPrefix(".", name) // nolint: gocritic + if dir := path.Join(repoDir, name); cm.IsDirectory(dir) { + return dir + } + } + return path.Join(repoDir, HooksDirName) } // GetSharedGithooksDir gets the hooks directory for Githooks inside a shared repository. func GetSharedGithooksDir(repoDir string) (dir string) { + // 0. priority has ".${GITHOOKS_HOOKS_DIR_NAME}" // 1. priority has non-dot folder 'githooks' // 2. priority is the normal '.githooks' folder. // This is second, to allow internal development Githooks inside shared repos. // 3. Fallback to the whole repository. + if name := os.Getenv("GITHOOKS_HOOKS_DIR_NAME"); strs.IsNotEmpty(name) { + name = strings.TrimPrefix(".", name) // nolint: gocritic + if dir = path.Join(repoDir, name); cm.IsDirectory(dir) { + return + } + } + if dir = path.Join(repoDir, HooksDirNameShared); cm.IsDirectory(dir) { return } else if dir = GetGithooksDir(repoDir); cm.IsDirectory(dir) {