Skip to content

Commit

Permalink
Merge pull request #562 from kachick/faster-local-githooks
Browse files Browse the repository at this point in the history
Make faster local githooks
  • Loading branch information
kachick authored Apr 11, 2024
2 parents afa2455 + 1154e09 commit 4a96e17
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

set -euxo pipefail

makers fmt
makers lint
makers --loglevel=error fmt
makers --loglevel=error lint
32 changes: 26 additions & 6 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,39 @@ script = [
"nix flake update --commit-lock-file",
]

[tasks.build-lint]
condition.files_modified = { input = ["./cmd/lint/*.go"], output = ["./dist/lint"] }
command = 'go'
args = ['build', '-v', '-o', 'dist', './cmd/lint']

[tasks.lint]
dependencies = ['build']
dependencies = ['build-lint']
command = 'dist/lint'

[tasks.lint-all]
dependencies = ['build-lint']
command = 'dist/lint'
args = ['-all']

[tasks.build-fmt]
condition.files_modified = { input = ["./cmd/fmt/*.go"], output = ["./dist/fmt"] }
command = 'go'
args = ['build', '-v', '-o', 'dist', './cmd/fmt']

[tasks.fmt-all]
dependencies = ['build-fmt']
command = './dist/fmt'
args = ['-all']

[tasks.fmt]
dependencies = ['build']
command = 'dist/fmt'
dependencies = ['build-fmt']
command = './dist/fmt'

[tasks.check]
dependencies = [
'build',
'test',
'lint',
'lint-all',
'build-go-race-as-the-race-test',
]

Expand Down Expand Up @@ -112,10 +132,10 @@ dependencies = [
'deps',
'build',
# 'test', # Needs home-manager, so needless in Nix-CI
'lint',
'lint-all',
'test-go',
'build-go-race-as-the-race-test',
'fmt',
'fmt-all',
]

[tasks.build-container]
Expand Down
22 changes: 17 additions & 5 deletions cmd/fmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
package main

import (
"github.com/kachick/dotfiles/internal/constants"
"flag"

"github.com/kachick/dotfiles/internal/fileutils"
"github.com/kachick/dotfiles/internal/runner"
)

func main() {
allFlag := flag.Bool("all", false, "includes heavy linters")

flag.Parse()

walker := fileutils.GetWalker()

bashPaths := walker.GetAllBash()
Expand All @@ -18,10 +23,17 @@ func main() {
cmds := runner.Commands{
{Path: "dprint", Args: []string{"fmt"}},
{Path: "shfmt", Args: append([]string{"--language-dialect", "bash", "--write"}, bashPaths...)},
// nix fmt doesn't respect .gitignore, without paths, .direnv included: https://github.com/NixOS/nixfmt/issues/151
{Path: "nix", Args: append([]string{"fmt"}, nixPaths...)},
{Path: "typos", Args: append(constants.GetTyposTargetedRoots(), "--write-changes")},
{Path: "go", Args: []string{"fmt", "./..."}},
}

// Editing them basically will be fmt in editor integrations
if *allFlag {
cmds = append(cmds, runner.Commands{
// nix fmt doesn't respect .gitignore, without paths, .direnv included: https://github.com/NixOS/nixfmt/issues/151
// nixfmt-rfc-style looks like using cache, if update with empty commit, it takes longtime
{Path: "nix", Args: append([]string{"fmt"}, nixPaths...)},
// go fmt looks like caching, but refreshed in each commit, and it takes bit longer than others
{Path: "go", Args: []string{"fmt", "./..."}},
}...)
}

cmds.ParallelRun()
Expand Down
14 changes: 12 additions & 2 deletions cmd/lint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
package main

import (
"flag"

"github.com/kachick/dotfiles/internal/constants"
"github.com/kachick/dotfiles/internal/fileutils"
"github.com/kachick/dotfiles/internal/runner"
)

func main() {
allFlag := flag.Bool("all", false, "includes heavy linters")

flag.Parse()

walker := fileutils.GetWalker()

bashPaths := walker.GetAllBash()
Expand All @@ -19,9 +25,13 @@ func main() {
{Path: "shellcheck", Args: bashPaths},
// nix fmt doesn't have check option: https://github.com/NixOS/nix/issues/6918, so do not include here
{Path: "typos", Args: constants.GetTyposTargetedRoots()},
{Path: "gitleaks", Args: []string{"detect"}},
// No git makes 4x+ faster
{Path: "gitleaks", Args: []string{"detect", "--no-git"}},
{Path: "go", Args: []string{"vet", "./..."}},
{Path: "trivy", Args: []string{"config", "--exit-code", "1", "."}},
}

if *allFlag {
cmds = append(cmds, runner.Cmd{Path: "trivy", Args: []string{"config", "--exit-code", "1", "."}})
}

cmds.ParallelRun()
Expand Down
4 changes: 3 additions & 1 deletion dprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
},
"excludes": [
".git",
"**/*-lock.json"
".direnv",
"dist",
"dependencies"
],
"plugins": [
"https://plugins.dprint.dev/json-0.19.2.wasm",
Expand Down
8 changes: 4 additions & 4 deletions internal/runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func (cmds Commands) ParallelRun() {
wg.Add(1)
go func(cmd Cmd) {
defer wg.Done()
output, err := exec.Command(cmd.Path, cmd.Args...).Output()
log.Printf("%s %s\n%s\n", cmd.Path, strings.Join(cmd.Args, " "), output)
output, err := exec.Command(cmd.Path, cmd.Args...).CombinedOutput()
log.Printf("%s %s\n%s\n", cmd.Path, strings.Join(cmd.Args, " "), string(output))
if err != nil {
log.Fatalln(err)
}
Expand All @@ -32,8 +32,8 @@ func (cmds Commands) ParallelRun() {

func (cmds Commands) SequentialRun() {
for _, cmd := range cmds {
output, err := exec.Command(cmd.Path, cmd.Args...).Output()
log.Printf("%s %s\n%s\n", cmd.Path, strings.Join(cmd.Args, " "), output)
output, err := exec.Command(cmd.Path, cmd.Args...).CombinedOutput()
log.Printf("%s %s\n%s\n", cmd.Path, strings.Join(cmd.Args, " "), string(output))
if err != nil {
log.Fatalln(err)
}
Expand Down

0 comments on commit 4a96e17

Please sign in to comment.