@@ -2,6 +2,8 @@ package internal
22
33import (
44 "context"
5+ "crypto/sha256"
6+ "encoding/base64"
57 "fmt"
68 "io"
79 "os"
@@ -12,6 +14,8 @@ import (
1214 "time"
1315 "unicode"
1416
17+ "golang.org/x/mod/sumdb/dirhash"
18+
1519 "github.com/golangci/golangci-lint/v2/pkg/logutils"
1620)
1721
@@ -173,13 +177,17 @@ func (b Builder) goModTidy(ctx context.Context) error {
173177}
174178
175179func (b Builder ) goBuild (ctx context.Context , binaryName string ) error {
180+ version , err := b .createVersion (b .cfg .Version )
181+ if err != nil {
182+ return fmt .Errorf ("custom version: %w" , err )
183+ }
184+
185+ b .log .Infof ("version: %s" , version )
186+
176187 //nolint:gosec // the variable is sanitized.
177188 cmd := exec .CommandContext (ctx , "go" , "build" ,
178189 "-ldflags" ,
179- fmt .Sprintf (
180- "-s -w -X 'main.version=%s-custom-gcl' -X 'main.date=%s'" ,
181- sanitizeVersion (b .cfg .Version ), time .Now ().UTC ().String (),
182- ),
190+ fmt .Sprintf ("-s -w -X 'main.version=%s' -X 'main.date=%s'" , version , time .Now ().UTC ().String ()),
183191 "-o" , binaryName ,
184192 "./cmd/golangci-lint" ,
185193 )
@@ -241,6 +249,30 @@ func (b Builder) getBinaryName() string {
241249 return name
242250}
243251
252+ func (b Builder ) createVersion (orig string ) (string , error ) {
253+ hash := sha256 .New ()
254+
255+ for _ , plugin := range b .cfg .Plugins {
256+ if plugin .Path == "" {
257+ continue
258+ }
259+
260+ dh , err := dirhash .HashDir (plugin .Path , "" , dirhash .DefaultHash )
261+ if err != nil {
262+ return "" , fmt .Errorf ("hash plugin directory: %w" , err )
263+ }
264+
265+ b .log .Infof ("%s: %s" , plugin .Path , dh )
266+
267+ hash .Write ([]byte (dh ))
268+ }
269+
270+ return fmt .Sprintf ("%s-custom-gcl-%s" ,
271+ sanitizeVersion (orig ),
272+ sanitizeVersion (base64 .URLEncoding .EncodeToString (hash .Sum (nil ))),
273+ ), nil
274+ }
275+
244276func sanitizeVersion (v string ) string {
245277 fn := func (c rune ) bool {
246278 return ! unicode .IsLetter (c ) && ! unicode .IsNumber (c ) && c != '.' && c != '/'
0 commit comments