-
Notifications
You must be signed in to change notification settings - Fork 25
feat: Add lefthook support #662
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
|
||
| package lefthook | ||
|
|
||
| // Command represents a single named command under a hook's commands: map. | ||
| type Command struct { //nolint:govet | ||
| Run string `yaml:"run"` | ||
| Tags []string `yaml:"tags,omitempty"` | ||
| Glob string `yaml:"glob,omitempty"` | ||
| Files string `yaml:"files,omitempty"` | ||
| Skip []string `yaml:"skip,omitempty"` | ||
| Only []string `yaml:"only,omitempty"` | ||
| Interactive bool `yaml:"interactive,omitempty"` | ||
| StageFixed bool `yaml:"stage_fixed,omitempty"` | ||
| Priority int `yaml:"priority,omitempty"` | ||
| } | ||
|
|
||
| // WithRun sets the shell command lefthook executes for this command. | ||
| func (c *Command) WithRun(run string) *Command { | ||
| c.Run = run | ||
|
|
||
| return c | ||
| } | ||
|
|
||
| // WithTags attaches tags used for selective hook execution (lefthook --tags ...). | ||
| func (c *Command) WithTags(tags ...string) *Command { | ||
| c.Tags = tags | ||
|
|
||
| return c | ||
| } | ||
|
|
||
| // WithGlob restricts the command to files matching the given glob. | ||
| func (c *Command) WithGlob(glob string) *Command { | ||
| c.Glob = glob | ||
|
|
||
| return c | ||
| } | ||
|
|
||
| // WithFiles overrides the default file source (e.g. "git diff --name-only ..."). | ||
| func (c *Command) WithFiles(files string) *Command { | ||
| c.Files = files | ||
|
|
||
| return c | ||
| } | ||
|
|
||
| // WithSkip lists git states or refs where the command should be skipped (e.g. "merge", "rebase"). | ||
| func (c *Command) WithSkip(skip ...string) *Command { | ||
| c.Skip = skip | ||
|
|
||
| return c | ||
| } | ||
|
|
||
| // WithOnly is the inverse of WithSkip: command runs only in the listed states. | ||
| func (c *Command) WithOnly(only ...string) *Command { | ||
| c.Only = only | ||
|
|
||
| return c | ||
| } | ||
|
|
||
| // WithInteractive marks the command as needing a TTY (stdin/stdout passthrough). | ||
| func (c *Command) WithInteractive() *Command { | ||
| c.Interactive = true | ||
|
|
||
| return c | ||
| } | ||
|
|
||
| // WithStageFixed re-stages files modified by the command (useful for formatters). | ||
| func (c *Command) WithStageFixed() *Command { | ||
| c.StageFixed = true | ||
|
|
||
| return c | ||
| } | ||
|
|
||
| // WithPriority sets the command's run order within its hook (lower runs first). | ||
| func (c *Command) WithPriority(priority int) *Command { | ||
| c.Priority = priority | ||
|
|
||
| return c | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
|
||
| package lefthook | ||
|
|
||
| // Config is a declarative description of lefthook.yml contributions, suitable | ||
| // for unmarshalling from a kres.yaml block. | ||
| // | ||
| // Hooks reuse the very same builder types (Hook, Command, Job, Group) that | ||
| // serialize lefthook.yml, so the config is authored in lefthook's native schema | ||
| // and there is a single definitive set of types for both serializing the output | ||
| // and deserializing the config. | ||
| type Config struct { | ||
| Hooks map[string]*Hook `yaml:"hooks"` | ||
| Enabled bool `yaml:"enabled"` | ||
| } | ||
|
|
||
| // Compile merges the configured hooks into the output. It is a no-op when the | ||
| // config is disabled. | ||
| func (c Config) Compile(output *Output) error { | ||
| if !c.Enabled { | ||
| return nil | ||
| } | ||
|
|
||
| for name, cfgHook := range c.Hooks { | ||
| if cfgHook == nil { | ||
| continue | ||
| } | ||
|
|
||
| hook := output.Hook(name) | ||
|
|
||
| if cfgHook.Parallel != nil { | ||
| hook.WithParallel(*cfgHook.Parallel) | ||
| } | ||
|
|
||
| if cfgHook.Piped { | ||
| hook.WithPiped(true) | ||
| } | ||
|
|
||
| for cmdName, cmd := range cfgHook.Commands { | ||
| if cmd == nil { | ||
| continue | ||
| } | ||
|
|
||
| *hook.Command(cmdName) = *cmd | ||
| } | ||
|
|
||
| for _, job := range cfgHook.Jobs { | ||
| if job == nil { | ||
| continue | ||
| } | ||
|
|
||
| // A named job wrapping a group is merged into the hook's group of | ||
| // the same name, so a config can extend the shared fix/lint groups | ||
| // emitted by the standard blocks instead of forking a new one. | ||
| if job.Group != nil && job.Name != "" { | ||
| group := hook.Group(job.Name) | ||
|
|
||
| if job.Group.Parallel != nil { | ||
| group.WithParallel(*job.Group.Parallel) | ||
| } | ||
|
|
||
| if job.Group.Piped { | ||
| group.WithPiped(true) | ||
| } | ||
|
|
||
| group.Jobs = append(group.Jobs, job.Group.Jobs...) | ||
|
|
||
| continue | ||
| } | ||
|
|
||
| hook.Jobs = append(hook.Jobs, job) | ||
| } | ||
|
majabojarska marked this conversation as resolved.
|
||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
|
||
| package lefthook | ||
|
|
||
| // Group is a container for nested Jobs with its own parallel/piped semantics. | ||
| // Used to express "run these in parallel, then those sequentially" without | ||
| // touching the hook-level execution model. | ||
| type Group struct { //nolint:govet | ||
| // Parallel is a pointer so an explicit `parallel: false` can be emitted | ||
| // (the bool zero value would otherwise be suppressed by omitempty). | ||
| Parallel *bool `yaml:"parallel,omitempty"` | ||
| Piped bool `yaml:"piped,omitempty"` | ||
| Jobs []*Job `yaml:"jobs,omitempty"` | ||
| } | ||
|
|
||
| // WithParallel toggles parallel execution of this group's jobs. | ||
| func (g *Group) WithParallel(parallel bool) *Group { | ||
| g.Parallel = ¶llel | ||
|
|
||
| return g | ||
| } | ||
|
|
||
| // WithPiped enables piped (sequential, fail-fast, stdout-chained) execution. | ||
| func (g *Group) WithPiped(piped bool) *Group { | ||
| g.Piped = piped | ||
|
|
||
| return g | ||
| } | ||
|
|
||
| // Job appends a new job to the group and returns it for further configuration. | ||
| func (g *Group) Job() *Job { | ||
| j := &Job{} | ||
|
|
||
| g.Jobs = append(g.Jobs, j) | ||
|
|
||
| return j | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.