feat: add hk (hk.jdx.dev) as supported git hook manager
This commit is contained in:
parent
5a07c92db4
commit
eafff366b4
3 changed files with 334 additions and 0 deletions
|
|
@ -44,6 +44,10 @@ var hookManagerConfigs = []hookManagerConfig{
|
|||
{"pre-commit", []string{".pre-commit-config.yaml", ".pre-commit-config.yml"}},
|
||||
{"overcommit", []string{".overcommit.yml"}},
|
||||
{"yorkie", []string{".yorkie"}},
|
||||
{"hk", []string{
|
||||
"hk.pkl", ".config/hk.pkl",
|
||||
"hk.local.pkl", ".config/hk.local.pkl",
|
||||
}},
|
||||
{"simple-git-hooks", []string{
|
||||
".simple-git-hooks.cjs", ".simple-git-hooks.js",
|
||||
"simple-git-hooks.cjs", "simple-git-hooks.js",
|
||||
|
|
@ -97,6 +101,7 @@ type hookManagerPattern struct {
|
|||
// hookManagerPatterns identifies which hook manager installed a git hook (in priority order).
|
||||
// Note: prek must come before pre-commit since prek hooks may also contain "pre-commit" in paths.
|
||||
var hookManagerPatterns = []hookManagerPattern{
|
||||
{"hk", regexp.MustCompile(`(?i)\bhk\s+run\b`)},
|
||||
{"lefthook", regexp.MustCompile(`(?i)lefthook`)},
|
||||
{"husky", regexp.MustCompile(`(?i)(\.husky|husky\.sh)`)},
|
||||
// prek (https://prek.j178.dev) - faster Rust-based pre-commit alternative
|
||||
|
|
@ -469,6 +474,62 @@ func CheckHuskyBdIntegration(path string) *HookIntegrationStatus {
|
|||
return status
|
||||
}
|
||||
|
||||
// hkConfigFiles lists hk config files (derived from hookManagerConfigs).
|
||||
var hkConfigFiles = []string{"hk.pkl", ".config/hk.pkl", "hk.local.pkl", ".config/hk.local.pkl"}
|
||||
|
||||
// CheckHkBdIntegration checks hk Pkl config for bd integration.
|
||||
// hk uses Pkl configuration where hooks are nested under hooks { ["hook-name"] { steps { ... } } }.
|
||||
// See https://hk.jdx.dev/ for config format.
|
||||
func CheckHkBdIntegration(path string) *HookIntegrationStatus {
|
||||
// Find first existing config file
|
||||
var configPath string
|
||||
for _, name := range hkConfigFiles {
|
||||
p := filepath.Join(path, name)
|
||||
if _, err := os.Stat(p); err == nil {
|
||||
configPath = p
|
||||
break
|
||||
}
|
||||
}
|
||||
if configPath == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
content, err := os.ReadFile(configPath) // #nosec G304 - path is validated
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
contentStr := string(content)
|
||||
|
||||
status := &HookIntegrationStatus{
|
||||
Manager: "hk",
|
||||
Configured: false,
|
||||
}
|
||||
|
||||
// For each recommended hook, check two things:
|
||||
// 1. Is the hook section present? (e.g., ["pre-commit"] appears in config)
|
||||
// 2. Does "bd hooks run <hookname>" appear in the config?
|
||||
// Since bd hooks run commands include the hook name, we can match them directly.
|
||||
for _, hookName := range recommendedBdHooks {
|
||||
sectionPattern := regexp.MustCompile(`\[\s*"` + regexp.QuoteMeta(hookName) + `"\s*\]`)
|
||||
hasBdRun := regexp.MustCompile(`\bbd\s+hooks\s+run\s+` + regexp.QuoteMeta(hookName) + `\b`)
|
||||
|
||||
sectionExists := sectionPattern.MatchString(contentStr)
|
||||
bdRunExists := hasBdRun.MatchString(contentStr)
|
||||
|
||||
if bdRunExists {
|
||||
status.HooksWithBd = append(status.HooksWithBd, hookName)
|
||||
status.Configured = true
|
||||
} else if sectionExists {
|
||||
status.HooksWithoutBd = append(status.HooksWithoutBd, hookName)
|
||||
} else {
|
||||
status.HooksNotInConfig = append(status.HooksNotInConfig, hookName)
|
||||
}
|
||||
}
|
||||
|
||||
return status
|
||||
}
|
||||
|
||||
// checkManagerBdIntegration checks a specific manager for bd integration.
|
||||
func checkManagerBdIntegration(name, path string) *HookIntegrationStatus {
|
||||
switch name {
|
||||
|
|
@ -476,6 +537,8 @@ func checkManagerBdIntegration(name, path string) *HookIntegrationStatus {
|
|||
return CheckLefthookBdIntegration(path)
|
||||
case "husky":
|
||||
return CheckHuskyBdIntegration(path)
|
||||
case "hk":
|
||||
return CheckHkBdIntegration(path)
|
||||
case "pre-commit", "prek":
|
||||
// prek uses the same config format as pre-commit
|
||||
status := CheckPrecommitBdIntegration(path)
|
||||
|
|
|
|||
|
|
@ -80,6 +80,31 @@ func TestDetectExternalHookManagers(t *testing.T) {
|
|||
},
|
||||
expected: []string{"pre-commit"},
|
||||
},
|
||||
{
|
||||
name: "hk.pkl",
|
||||
setup: func(dir string) error {
|
||||
return os.WriteFile(filepath.Join(dir, "hk.pkl"), []byte("hooks {\n}\n"), 0644)
|
||||
},
|
||||
expected: []string{"hk"},
|
||||
},
|
||||
{
|
||||
name: ".config/hk.pkl",
|
||||
setup: func(dir string) error {
|
||||
configDir := filepath.Join(dir, ".config")
|
||||
if err := os.MkdirAll(configDir, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(filepath.Join(configDir, "hk.pkl"), []byte("hooks {\n}\n"), 0644)
|
||||
},
|
||||
expected: []string{"hk"},
|
||||
},
|
||||
{
|
||||
name: "hk.local.pkl",
|
||||
setup: func(dir string) error {
|
||||
return os.WriteFile(filepath.Join(dir, "hk.local.pkl"), []byte("hooks {\n}\n"), 0644)
|
||||
},
|
||||
expected: []string{"hk"},
|
||||
},
|
||||
{
|
||||
name: ".overcommit.yml",
|
||||
setup: func(dir string) error {
|
||||
|
|
@ -295,6 +320,193 @@ pre-commit:
|
|||
}
|
||||
}
|
||||
|
||||
func TestCheckHkBdIntegration(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
configFile string
|
||||
configContent string
|
||||
expectNil bool
|
||||
expectConfigured bool
|
||||
expectHooksWithBd []string
|
||||
expectHooksWithoutBd []string
|
||||
expectNotInConfig []string
|
||||
}{
|
||||
{
|
||||
name: "no config",
|
||||
expectNil: true,
|
||||
},
|
||||
{
|
||||
name: "all recommended hooks with bd",
|
||||
configFile: "hk.pkl",
|
||||
configContent: `amends "package://github.com/jdx/hk/releases/download/v1.36.0/hk@1.36.0#/Config.pkl"
|
||||
|
||||
hooks {
|
||||
["pre-commit"] {
|
||||
steps {
|
||||
["bd-pre-commit"] {
|
||||
check = "bd hooks run pre-commit"
|
||||
}
|
||||
}
|
||||
}
|
||||
["post-merge"] {
|
||||
steps {
|
||||
["bd-post-merge"] {
|
||||
check = "bd hooks run post-merge"
|
||||
}
|
||||
}
|
||||
}
|
||||
["pre-push"] {
|
||||
steps {
|
||||
["bd-pre-push"] {
|
||||
check = "bd hooks run pre-push \"$@\""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
expectConfigured: true,
|
||||
expectHooksWithBd: []string{"pre-commit", "post-merge", "pre-push"},
|
||||
},
|
||||
{
|
||||
name: "partial bd integration",
|
||||
configFile: "hk.pkl",
|
||||
configContent: `hooks {
|
||||
["pre-commit"] {
|
||||
steps {
|
||||
["bd-pre-commit"] {
|
||||
check = "bd hooks run pre-commit"
|
||||
}
|
||||
["lint"] {
|
||||
glob = "*.go"
|
||||
check = "golangci-lint run {{files}}"
|
||||
}
|
||||
}
|
||||
}
|
||||
["post-merge"] {
|
||||
steps {
|
||||
["notify"] {
|
||||
check = "echo merged"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
expectConfigured: true,
|
||||
expectHooksWithBd: []string{"pre-commit"},
|
||||
expectHooksWithoutBd: []string{"post-merge"},
|
||||
expectNotInConfig: []string{"pre-push"},
|
||||
},
|
||||
{
|
||||
name: "no bd at all",
|
||||
configFile: "hk.pkl",
|
||||
configContent: `hooks {
|
||||
["pre-commit"] {
|
||||
steps {
|
||||
["lint"] {
|
||||
glob = "*.go"
|
||||
check = "golangci-lint run {{files}}"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
expectConfigured: false,
|
||||
expectHooksWithoutBd: []string{"pre-commit"},
|
||||
expectNotInConfig: []string{"post-merge", "pre-push"},
|
||||
},
|
||||
{
|
||||
name: ".config/hk.pkl location",
|
||||
configFile: ".config/hk.pkl",
|
||||
configContent: `hooks {
|
||||
["pre-commit"] {
|
||||
steps {
|
||||
["bd-sync"] {
|
||||
check = "bd hooks run pre-commit"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
expectConfigured: true,
|
||||
expectHooksWithBd: []string{"pre-commit"},
|
||||
expectNotInConfig: []string{"post-merge", "pre-push"},
|
||||
},
|
||||
{
|
||||
name: "hk.local.pkl only",
|
||||
configFile: "hk.local.pkl",
|
||||
configContent: `hooks {
|
||||
["pre-commit"] {
|
||||
steps {
|
||||
["bd-sync"] {
|
||||
check = "bd hooks run pre-commit"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`,
|
||||
expectConfigured: true,
|
||||
expectHooksWithBd: []string{"pre-commit"},
|
||||
expectNotInConfig: []string{"post-merge", "pre-push"},
|
||||
},
|
||||
{
|
||||
name: "empty config",
|
||||
configFile: "hk.pkl",
|
||||
configContent: ``,
|
||||
expectConfigured: false,
|
||||
expectNotInConfig: []string{"pre-commit", "post-merge", "pre-push"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
|
||||
if tt.configFile != "" {
|
||||
configPath := filepath.Join(dir, tt.configFile)
|
||||
if err := os.MkdirAll(filepath.Dir(configPath), 0755); err != nil {
|
||||
t.Fatalf("failed to create config dir: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(configPath, []byte(tt.configContent), 0644); err != nil {
|
||||
t.Fatalf("failed to write config: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
status := CheckHkBdIntegration(dir)
|
||||
|
||||
if tt.expectNil {
|
||||
if status != nil {
|
||||
t.Errorf("expected nil status, got %+v", status)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if status == nil {
|
||||
t.Fatal("expected non-nil status")
|
||||
}
|
||||
|
||||
if status.Manager != "hk" {
|
||||
t.Errorf("Manager: expected 'hk', got %q", status.Manager)
|
||||
}
|
||||
|
||||
if status.Configured != tt.expectConfigured {
|
||||
t.Errorf("Configured: expected %v, got %v", tt.expectConfigured, status.Configured)
|
||||
}
|
||||
|
||||
if !slicesEqual(status.HooksWithBd, tt.expectHooksWithBd) {
|
||||
t.Errorf("HooksWithBd: expected %v, got %v", tt.expectHooksWithBd, status.HooksWithBd)
|
||||
}
|
||||
|
||||
if !slicesEqual(status.HooksWithoutBd, tt.expectHooksWithoutBd) {
|
||||
t.Errorf("HooksWithoutBd: expected %v, got %v", tt.expectHooksWithoutBd, status.HooksWithoutBd)
|
||||
}
|
||||
|
||||
if !slicesEqual(status.HooksNotInConfig, tt.expectNotInConfig) {
|
||||
t.Errorf("HooksNotInConfig: expected %v, got %v", tt.expectNotInConfig, status.HooksNotInConfig)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckHuskyBdIntegration(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
@ -571,6 +783,11 @@ func TestDetectActiveHookManager(t *testing.T) {
|
|||
hookContent string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "hk signature",
|
||||
hookContent: "#!/bin/sh\ntest \"${HK:-1}\" = \"0\" || exec hk run pre-commit \"$@\"\n",
|
||||
expected: "hk",
|
||||
},
|
||||
{
|
||||
name: "lefthook signature",
|
||||
hookContent: "#!/bin/sh\n# lefthook\nexec lefthook run pre-commit\n",
|
||||
|
|
@ -1028,6 +1245,16 @@ func TestCheckManagerBdIntegration(t *testing.T) {
|
|||
expectNil: false,
|
||||
expectManager: "husky",
|
||||
},
|
||||
{
|
||||
name: "hk integration",
|
||||
managerName: "hk",
|
||||
setup: func(dir string) error {
|
||||
config := "hooks {\n [\"pre-commit\"] {\n steps {\n [\"bd\"] {\n check = \"bd hooks run pre-commit\"\n }\n }\n }\n}\n"
|
||||
return os.WriteFile(filepath.Join(dir, "hk.pkl"), []byte(config), 0644)
|
||||
},
|
||||
expectNil: false,
|
||||
expectManager: "hk",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
|
|
|||
|
|
@ -84,6 +84,50 @@ See [PROTECTED_BRANCHES.md](PROTECTED_BRANCHES.md) for complete setup guide, tro
|
|||
|
||||
## Git Hooks
|
||||
|
||||
### External Hook Manager Support
|
||||
|
||||
bd detects and integrates with these external git hook managers:
|
||||
|
||||
- **[lefthook](https://lefthook.dev/)** — YAML/TOML/JSON config
|
||||
- **[husky](https://typicode.github.io/husky/)** — `.husky/` directory scripts
|
||||
- **[pre-commit](https://pre-commit.com/)** — `.pre-commit-config.yaml`
|
||||
- **[prek](https://prek.j178.dev/)** — Rust-based pre-commit alternative (same config)
|
||||
- **[hk](https://hk.jdx.dev/)** — Fast hook manager using Pkl config
|
||||
- **[overcommit](https://github.com/sds/overcommit)** — Ruby-based (detection only)
|
||||
- **[simple-git-hooks](https://github.com/toplenboren/simple-git-hooks)** — Lightweight JS (detection only)
|
||||
|
||||
When an external hook manager is detected, `bd hooks install` uses `--chain` to preserve existing hooks.
|
||||
|
||||
#### hk Integration Example
|
||||
|
||||
Add bd hooks to your `hk.pkl`:
|
||||
|
||||
```pkl
|
||||
hooks {
|
||||
["pre-commit"] {
|
||||
steps {
|
||||
["bd-pre-commit"] {
|
||||
check = "bd hooks run pre-commit"
|
||||
}
|
||||
}
|
||||
}
|
||||
["post-merge"] {
|
||||
steps {
|
||||
["bd-post-merge"] {
|
||||
check = "bd hooks run post-merge"
|
||||
}
|
||||
}
|
||||
}
|
||||
["pre-push"] {
|
||||
steps {
|
||||
["bd-pre-push"] {
|
||||
check = "bd hooks run pre-push \"$@\""
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Installation
|
||||
|
||||
```bash
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue