mgechev / revive

🔥 ~6x faster, stricter, configurable, extensible, and beautiful drop-in replacement for golint
https://revive.run
MIT License
4.69k stars 273 forks source link

basic - pre-commit config #916

Closed mfederowicz closed 8 months ago

mfederowicz commented 9 months ago

related with #913

basic .pre-commit-config.yaml:

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.4.0
    hooks:
      - id: end-of-file-fixer
  - repo: https://github.com/mgechev/revive
    #for start use dev tag, after development we switch to proper tags with versions 
    rev: dev-pre-commit
    hooks:
    -   id: revive

in rev field for start we can set tag/branch but one condition is required file .pre-commit-hooks.yaml must exists i this tag/branch version otherwise command: pre-commit run --all-files (this command can be used to test configured hooks on our code without commit) will return error

.pre-commit-hooks.yaml

-   id: revive
    name: revive
    description: Beautiful Go linter
    entry: revive
    language: golang

hook presented above should be more configured, because it dumps some strage errors :

revive...................................................................Failed                                                                                              
- hook id: revive                                                                                                                                                            
- exit code: 2                                                                                                                                                               

Dockerfile:1:1: invalid file Dockerfile: Dockerfile:1:1: expected 'package', found FROM                                                                                      
panic: runtime error: invalid memory address or nil pointer dereference                                                                                                      
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x576179]                                                                                                       

goroutine 105 [running]:                                                                                                                                                     
go/ast.Walk({0x8eb140?, 0xc000317160?}, {0x8edcc0?, 0x0?})                                                                                                                   
        /usr/local/go1.21.1.linux-amd64/src/go/ast/walk.go:234 +0xf39                                                                                                        
github.com/mgechev/revive/rule.pick({0x8edcc0, 0x0}, 0xc000317150, 0x0)                                                                                                      
        /home/mfederowicz/.cache/pre-commit/repop2q1usxb/rule/utils.go:113 +0xe5                                                                                             
github.com/mgechev/revive/rule.lintUnusedReceiverRule.Visit({0xc000316e70, 0xc0001cfe00, {0x861334, 0x5e}}, {0x8eda90?, 0xc00031d020})                                       
        /home/mfederowicz/.cache/pre-commit/repop2q1usxb/rule/unused-receiver.go:116 +0x185                                                                                  
go/ast.Walk({0x8eb1a0?, 0xc0001d0a00?}, {0x8eda90?, 0xc00031d020?})                                                                                                          
        /usr/local/go1.21.1.linux-amd64/src/go/ast/walk.go:51 +0x5c                                                                                                          
go/ast.walkDeclList(...)

steps to install hooks in local repo:

pre-commit install

pre-commit installed at .git/hooks/pre-commit

steps to uninstall hooks in local repo:

pre-commit uninstall

pre-commit installed at .git/hooks/pre-commit

in development stage if we dont set rev field as v1.2.3 we can see warning like below:

[WARNING] The 'rev' field of repo 'https://github.com/mgechev/revive' appears to be a mutable reference (moving tag / branch). Mutable references are never updated after first install and are not supported. See https://pre-commit.com/#using-the-latest-version-for-a-repository for more details. Hint:pre-commit autoupdateoften fixes this.

steps to test code without commit:

pre-commit run --all-files
chavacava commented 8 months ago

Thank you @mfederowicz for providing a PR. I'll will close #913 without introducing the pre-commit hook definition in revive nor defining pre-commit config.