Skip to content

Commit

Permalink
feat: add assert_lefthook_installed option (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox authored Jul 31, 2023
1 parent 620d928 commit 82af441
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 23 deletions.
11 changes: 9 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# Configure lefthook.yml

- [Top level options](#top-level-options)
- [`assert_lefthook_installed`](#assert_lefthook_installed)
- [`colors`](#colors)
- [`yellow`](#colors)
- [`green`](#colors)
- [`cyan`](#colors)
- [`gray`](#colors)
- [`red`](#colors)
- [`no_tty`](#no_tty)
- [`extends`](#extends)
- [`min_version`](#min_version)
- [`no_tty`](#no_tty)
- [`rc`](#rc)
- [`skip_output`](#skip_output)
- [`source_dir`](#source_dir)
- [`source_dir_local`](#source_dir_local)
- [`rc`](#rc)
- [`remote` (Beta :test_tube:)](#remote)
- [`git_url`](#git_url)
- [`ref`](#ref)
Expand Down Expand Up @@ -59,6 +60,12 @@

These options are not related to git hooks, and they only control lefthook behavior.

### `assert_lefthook_installed`

**Default: `false`**

When set to `true`, fail (with exit status 1) if `lefthook` executable can't be found in $PATH, under node_modules/, as a Ruby gem, or other supported method. This makes sure git hook won't omit `lefthook` rules if `lefthook` ever was installed.

### `colors`

**Default: `true`**
Expand Down
19 changes: 10 additions & 9 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ import (
const dumpIndent = 2

type Config struct {
MinVersion string `mapstructure:"min_version,omitempty"`
SourceDir string `mapstructure:"source_dir"`
SourceDirLocal string `mapstructure:"source_dir_local"`
Rc string `mapstructure:"rc,omitempty"`
SkipOutput []string `mapstructure:"skip_output,omitempty"`
Extends []string `mapstructure:"extends,omitempty"`
NoTTY bool `mapstructure:"no_tty,omitempty"`
Colors interface{} `mapstructure:"colors,omitempty"`
Remote *Remote `mapstructure:"remote,omitempty" `
MinVersion string `mapstructure:"min_version,omitempty"`
SourceDir string `mapstructure:"source_dir"`
SourceDirLocal string `mapstructure:"source_dir_local"`
Rc string `mapstructure:"rc,omitempty"`
SkipOutput []string `mapstructure:"skip_output,omitempty"`
Extends []string `mapstructure:"extends,omitempty"`
NoTTY bool `mapstructure:"no_tty,omitempty"`
AssertLefthookInstalled bool `mapstructure:"assert_lefthook_installed,omitempty"`
Colors interface{} `mapstructure:"colors,omitempty"`
Remote *Remote `mapstructure:"remote,omitempty"`

Hooks map[string]*Hook `mapstructure:"-"`
}
Expand Down
2 changes: 1 addition & 1 deletion internal/lefthook/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (l *Lefthook) Add(args *AddArgs) error {
return err
}

err = l.addHook(args.Hook, "")
err = l.addHook(args.Hook, "", false)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/lefthook/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ func (l *Lefthook) createHooksIfNeeded(cfg *config.Config, force bool) error {
return err
}

if err = l.addHook(hook, cfg.Rc); err != nil {
if err = l.addHook(hook, cfg.Rc, cfg.AssertLefthookInstalled); err != nil {
return err
}
}

if err = l.addHook(config.GhostHookName, cfg.Rc); err != nil {
if err = l.addHook(config.GhostHookName, cfg.Rc, cfg.AssertLefthookInstalled); err != nil {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions internal/lefthook/lefthook.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ func (l *Lefthook) cleanHook(hook string, force bool) error {
}

// Creates a hook file using hook template.
func (l *Lefthook) addHook(hook, rc string) error {
func (l *Lefthook) addHook(hook, rc string, assertLefthookInstalled bool) error {
hookPath := filepath.Join(l.repo.HooksPath, hook)
return afero.WriteFile(
l.Fs, hookPath, templates.Hook(hook, rc), hookFileMode,
l.Fs, hookPath, templates.Hook(hook, rc, assertLefthookInstalled), hookFileMode,
)
}
6 changes: 6 additions & 0 deletions internal/templates/hook.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ call_lefthook()
npx @evilmartians/lefthook "$@"
else
echo "Can't find lefthook in PATH"
{{- if .AssertLefthookInstalled}}
echo "ERROR: Operation is aborted due to lefthook settings."
echo "Make sure lefthook is available in your environment and re-try."
echo "To skip these checks use --no-verify git argument or set LEFTHOOK=0 env variable."
exit 1
{{- end}}
fi
}

Expand Down
16 changes: 9 additions & 7 deletions internal/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ const checksumFormat = "%s %d\n"
var templatesFS embed.FS

type hookTmplData struct {
HookName string
Extension string
Rc string
HookName string
Extension string
Rc string
AssertLefthookInstalled bool
}

func Hook(hookName, rc string) []byte {
func Hook(hookName, rc string, assertLefthookInstalled bool) []byte {
buf := &bytes.Buffer{}
t := template.Must(template.ParseFS(templatesFS, "hook.tmpl"))
err := t.Execute(buf, hookTmplData{
HookName: hookName,
Extension: getExtension(),
Rc: rc,
HookName: hookName,
Extension: getExtension(),
Rc: rc,
AssertLefthookInstalled: assertLefthookInstalled,
})
if err != nil {
panic(err)
Expand Down

0 comments on commit 82af441

Please sign in to comment.