Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use env. variable to overwrite .githooks name ⚓ #117

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion githooks/hooks/githooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -139,18 +140,37 @@ 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 dir = path.Join(repoDir, "githooks"); cm.IsDirectory(dir) {
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) {
return
Expand Down