Skip to content

Commit a5d81d9

Browse files
silverwindclaudewxiaoguangGiteaBot
authored
perf: replace goheader linter with custom check (go-gitea#37599)
Replace the [slow `goheader` linter](denis-tingaikin/go-header#70) with a custom check. Local go lint time is down from 247s to 32s. 6 new files that were previously undetected because of `//go:build ignore` are fixed. The exit code of the make target preserves the golangci-lint exit code, if present. Also refactors and consolidates the linting targets. Signed-off-by: silverwind <me@silverwind.io> Signed-off-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Claude (Opus 4.7) <noreply@anthropic.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Giteabot <teabot@gitea.io>
1 parent 73c0239 commit a5d81d9

9 files changed

Lines changed: 112 additions & 65 deletions

File tree

.github/workflows/cache-seeder.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ jobs:
5454
matrix:
5555
include:
5656
- { job: lint-backend, tags: "bindata", target: "lint-backend" }
57-
- { job: lint-go-windows, tags: "bindata", target: "lint-go-windows" }
58-
- { job: lint-go-gogit, tags: "bindata gogit", target: "lint-go" }
5957
steps:
6058
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
6159
- uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0

.github/workflows/pull-compliance.yml

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -64,48 +64,6 @@ jobs:
6464
- if: needs.files-changed.outputs.actions == 'true'
6565
run: make lint-actions
6666

67-
lint-go-windows:
68-
if: needs.files-changed.outputs.backend == 'true' || needs.files-changed.outputs.actions == 'true'
69-
needs: files-changed
70-
runs-on: ubuntu-latest
71-
steps:
72-
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
73-
- uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
74-
with:
75-
go-version-file: go.mod
76-
check-latest: true
77-
cache: false
78-
- uses: ./.github/actions/go-cache
79-
with:
80-
cache-name: lint-go-windows
81-
lint-cache: "true"
82-
- run: make deps-backend deps-tools
83-
- run: make lint-go-windows
84-
env:
85-
TAGS: bindata
86-
GOOS: windows
87-
GOARCH: amd64
88-
89-
lint-go-gogit:
90-
if: needs.files-changed.outputs.backend == 'true' || needs.files-changed.outputs.actions == 'true'
91-
needs: files-changed
92-
runs-on: ubuntu-latest
93-
steps:
94-
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
95-
- uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
96-
with:
97-
go-version-file: go.mod
98-
check-latest: true
99-
cache: false
100-
- uses: ./.github/actions/go-cache
101-
with:
102-
cache-name: lint-go-gogit
103-
lint-cache: "true"
104-
- run: make deps-backend deps-tools
105-
- run: make lint-go
106-
env:
107-
TAGS: bindata gogit
108-
10967
checks-backend:
11068
if: needs.files-changed.outputs.backend == 'true' || needs.files-changed.outputs.actions == 'true'
11169
needs: files-changed

.golangci.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ linters:
1313
- forbidigo
1414
- gocheckcompilerdirectives
1515
- gocritic
16-
- goheader
1716
- govet
1817
- ineffassign
1918
- mirror
@@ -118,11 +117,6 @@ linters:
118117
enable:
119118
- nilness
120119
- unusedwrite
121-
goheader:
122-
values:
123-
regexp:
124-
HEADER: '((Copyright [^\n]+|All rights reserved\.)\n)*Copyright \d{4} (The (Gogs|Gitea) Authors|Gitea Authors|Gitea)\.( All rights reserved\.)?(\n(Copyright [^\n]+|All rights reserved\.))*\nSPDX-License-Identifier: [\w.-]+'
125-
template: '{{ HEADER }}'
126120
exclusions:
127121
generated: lax
128122
presets:

Makefile

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,18 +332,11 @@ lint-spell-fix: ## lint spelling and fix issues
332332

333333
.PHONY: lint-go
334334
lint-go: ## lint go files
335-
$(GO) run $(GOLANGCI_LINT_PACKAGE) run
335+
GO=$(GO) GOLANGCI_LINT_PACKAGE=$(GOLANGCI_LINT_PACKAGE) $(GO) run ./tools/lint-go-all.go
336336

337337
.PHONY: lint-go-fix
338338
lint-go-fix: ## lint go files and fix issues
339-
$(GO) run $(GOLANGCI_LINT_PACKAGE) run --fix
340-
341-
# workaround step for the lint-go-windows CI task because 'go run' can not
342-
# have distinct GOOS/GOARCH for its build and run steps
343-
.PHONY: lint-go-windows
344-
lint-go-windows:
345-
@GOOS= GOARCH= $(GO) install $(GOLANGCI_LINT_PACKAGE)
346-
golangci-lint run
339+
GO=$(GO) GOLANGCI_LINT_PACKAGE=$(GOLANGCI_LINT_PACKAGE) $(GO) run ./tools/lint-go-all.go --fix
347340

348341
.PHONY: lint-editorconfig
349342
lint-editorconfig:

build/generate-gitignores.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Copyright 2020 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
14
//go:build ignore
25

36
package main

models/db/driver_sqlite_mattn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
//go:build sqlite_mattn && sqlite_unlock_notify
2-
31
// Copyright 2026 The Gitea Authors. All rights reserved.
42
// SPDX-License-Identifier: MIT
53

4+
//go:build sqlite_mattn && sqlite_unlock_notify
5+
66
package db
77

88
import (

models/db/driver_sqlite_modernc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
//go:build !sqlite_mattn
2-
31
// Copyright 2026 The Gitea Authors. All rights reserved.
42
// SPDX-License-Identifier: MIT
53

4+
//go:build !sqlite_mattn
5+
66
// modernc driver is chosen as the default one (compared to mattn, ncruces)
77
// * mattn was used as default, but it requires CGO
88
// * the CI times are almost the same for these three (race detector must be disabled)

modules/auth/pam/pam_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
//go:build pam
2-
31
// Copyright 2021 The Gitea Authors. All rights reserved.
42
// SPDX-License-Identifier: MIT
53

4+
//go:build pam
5+
66
package pam
77

88
import (

tools/lint-go-all.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2026 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"io"
9+
"io/fs"
10+
"os"
11+
"os/exec"
12+
"path/filepath"
13+
"regexp"
14+
"strings"
15+
)
16+
17+
func lintGoHeader() bool {
18+
headerRE := regexp.MustCompile(`^(// (Copyright [^\n]+|All rights reserved\.)\n)*// Copyright \d{4} (The Gogs Authors|The Gitea Authors|Gitea Authors|Gitea)\.( All rights reserved\.)?\n(// (Copyright [^\n]+|All rights reserved\.)\n)*// SPDX-License-Identifier: [\w.-]+`)
19+
generatedRE := regexp.MustCompile(`(?m)^// (Code|This file is) [Gg]enerated.*DO NOT EDIT`)
20+
skipDirs := map[string]bool{
21+
".git": true,
22+
".venv": true,
23+
"node_modules": true,
24+
"public": true,
25+
"vendor": true,
26+
"web_src": true,
27+
}
28+
root, bad := ".", 0
29+
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
30+
if err != nil {
31+
return err
32+
}
33+
if d.IsDir() {
34+
if rel, _ := filepath.Rel(root, path); skipDirs[filepath.ToSlash(rel)] {
35+
return fs.SkipDir
36+
}
37+
return nil
38+
}
39+
if !strings.HasSuffix(path, ".go") {
40+
return nil
41+
}
42+
f, err := os.Open(path)
43+
if err != nil {
44+
return err
45+
}
46+
data, err := io.ReadAll(io.LimitReader(f, 512))
47+
_ = f.Close()
48+
if err != nil {
49+
return err
50+
}
51+
if generatedRE.Match(data) {
52+
return nil
53+
}
54+
if !headerRE.Match(data) {
55+
_, _ = fmt.Fprintf(os.Stderr, "%s: missing or invalid copyright header\n", path)
56+
bad++
57+
}
58+
return nil
59+
})
60+
if err != nil {
61+
_, _ = fmt.Fprintln(os.Stderr, err)
62+
}
63+
return err == nil && bad == 0
64+
}
65+
66+
func runCmd(env []string, name string, args []string) bool {
67+
cmd := exec.Command(name, args...)
68+
cmd.Env = append(os.Environ(), env...)
69+
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
70+
if err := cmd.Run(); err != nil {
71+
_, _ = fmt.Fprintln(os.Stderr, err)
72+
return false
73+
}
74+
return true
75+
}
76+
77+
func main() {
78+
// 'go run' can not have distinct GOOS/GOARCH for its build and run steps,
79+
// so install a pre-compiled binary and run it for different target platforms.
80+
_, _ = os.Unsetenv("GOOS"), os.Unsetenv("GOARCH")
81+
82+
envGolangciLintPackage := os.Getenv("GOLANGCI_LINT_PACKAGE")
83+
envGo := os.Getenv("GO")
84+
if envGo == "" || envGolangciLintPackage == "" {
85+
_, _ = fmt.Fprintln(os.Stderr, "Environment variables GO and GOLANGCI_LINT_PACKAGE must be set")
86+
os.Exit(1)
87+
}
88+
if !runCmd(nil, envGo, []string{"install", envGolangciLintPackage}) {
89+
os.Exit(1)
90+
}
91+
92+
_, _ = fmt.Fprintln(os.Stdout, "lint go header ...")
93+
succeed := lintGoHeader()
94+
_, _ = fmt.Fprintln(os.Stdout, "lint for linux ...")
95+
succeed = runCmd([]string{"GOOS=linux", "TAGS=bindata"}, "golangci-lint", append([]string{"run"}, os.Args[1:]...)) && succeed
96+
_, _ = fmt.Fprintln(os.Stdout, "lint for windows ...")
97+
succeed = runCmd([]string{"GOOS=windows", "TAGS=gogit"}, "golangci-lint", append([]string{"run"}, os.Args[1:]...)) && succeed
98+
if !succeed {
99+
os.Exit(1)
100+
}
101+
}

0 commit comments

Comments
 (0)