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: allow globs in skip option #457

Merged
merged 4 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master (unreleased)

- fix: allow globs in skip option ([#457](https://github.com/evilmartians/lefthook/pull/457)) by @mrexox
- deps: dependencies update (March 2023) ([#455](https://github.com/evilmartians/lefthook/pull/455)) by @mrexox
- fix: don't fail on missing config file ([#450](https://github.com/evilmartians/lefthook/pull/450)) by @mrexox

## 1.3.6 (2024-03-16)
Expand Down
17 changes: 16 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ pre-commit:

### `skip`

You can skip all or specific commands and scripts using `skip` option. You can also skip when merging, rebasing, or being on a specific branch.
You can skip all or specific commands and scripts using `skip` option. You can also skip when merging, rebasing, or being on a specific branch. Globs are available for branches.

**Example**

Expand Down Expand Up @@ -679,6 +679,21 @@ pre-commit:
run: yarn test
```

Skipping hook for all `dev/*` branches:

```yml
# lefthook.yml

pre-commit:
skip:
- ref: dev/*
commands:
lint:
run: yarn lint
text:
run: yarn test
```

**Notes**

Always skipping is useful when you have a `lefthook-local.yml` config and you don't want to run some commands locally. So you just overwrite the `skip` option for them to be `true`.
Expand Down
14 changes: 12 additions & 2 deletions internal/config/skip.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package config

import "github.com/evilmartians/lefthook/internal/git"
import (
"github.com/gobwas/glob"

"github.com/evilmartians/lefthook/internal/git"
)

func isSkip(gitState git.State, value interface{}) bool {
switch typedValue := value.(type) {
Expand All @@ -16,7 +20,13 @@ func isSkip(gitState git.State, value interface{}) bool {
return true
}
case map[string]interface{}:
if typedState["ref"].(string) == gitState.Branch {
ref := typedState["ref"].(string)
if ref == gitState.Branch {
return true
}

g := glob.MustCompile(ref)
if g.Match(gitState.Branch) {
return true
}
}
Expand Down
83 changes: 83 additions & 0 deletions internal/config/skip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package config

import (
"testing"

"github.com/evilmartians/lefthook/internal/git"
)

func TestIsSkip(t *testing.T) {
for _, tt := range [...]struct {
name string
skip interface{}
state git.State
skipped bool
}{
{
name: "when true",
skip: true,
state: git.State{},
skipped: true,
},
{
name: "when false",
skip: false,
state: git.State{},
skipped: false,
},
{
name: "when merge",
skip: "merge",
state: git.State{Step: "merge"},
skipped: true,
},
{
name: "when rebase (but want merge)",
skip: "merge",
state: git.State{Step: "rebase"},
skipped: false,
},
{
name: "when rebase",
skip: []interface{}{"rebase"},
state: git.State{Step: "rebase"},
skipped: true,
},
{
name: "when rebase (but want merge)",
skip: []interface{}{"merge"},
state: git.State{Step: "rebase"},
skipped: false,
},
{
name: "when branch",
skip: []interface{}{map[string]interface{}{"ref": "feat/skipme"}},
state: git.State{Branch: "feat/skipme"},
skipped: true,
},
{
name: "when branch doesn't match",
skip: []interface{}{map[string]interface{}{"ref": "feat/skipme"}},
state: git.State{Branch: "feat/important"},
skipped: false,
},
{
name: "when branch glob",
skip: []interface{}{map[string]interface{}{"ref": "feat/*"}},
state: git.State{Branch: "feat/important"},
skipped: true,
},
{
name: "when branch glob doesn't match",
skip: []interface{}{map[string]interface{}{"ref": "feat/*"}},
state: git.State{Branch: "feat"},
skipped: false,
},
} {
t.Run(tt.name, func(t *testing.T) {
if isSkip(tt.state, tt.skip) != tt.skipped {
t.Errorf("Expected: %v, Was %v", tt.skipped, !tt.skipped)
}
})
}
}