Skip to content

Commit 0647df3

Browse files
silverwindlunnyzeripathwxiaoguang
authored
Simplify fmt-check (#21458)
`fmt-check` now simply does `fmt` before and relies on `git diff` like other checks like 'tidy-check' already do, so we can remove the argument in the tool that handles printing changed files. Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: zeripath <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent 9fb251f commit 0647df3

File tree

4 files changed

+21
-35
lines changed

4 files changed

+21
-35
lines changed

.golangci.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ linters:
1212
- dupl
1313
#- gocyclo # The cyclomatic complexety of a lot of functions is too high, we should refactor those another time.
1414
- gofmt
15-
- misspell
1615
- gocritic
1716
- bidichk
1817
- ineffassign
@@ -148,9 +147,6 @@ issues:
148147
- path: models/issue_comment_list.go
149148
linters:
150149
- dupl
151-
- linters:
152-
- misspell
153-
text: '`Unknwon` is a misspelling of `Unknown`'
154150
- path: models/update.go
155151
linters:
156152
- unused

Makefile

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ TEST_TAGS ?= sqlite sqlite_unlock_notify
130130
TAR_EXCLUDES := .git data indexers queues log node_modules $(EXECUTABLE) $(FOMANTIC_WORK_DIR)/node_modules $(DIST) $(MAKE_EVIDENCE_DIR) $(AIR_TMP_DIR) $(GO_LICENSE_TMP_DIR)
131131

132132
GO_DIRS := cmd tests models modules routers build services tools
133+
WEB_DIRS := web_src/js web_src/less
133134

134135
GO_SOURCES := $(wildcard *.go)
135136
GO_SOURCES += $(shell find $(GO_DIRS) -type f -name "*.go" -not -path modules/options/bindata.go -not -path modules/public/bindata.go -not -path modules/templates/bindata.go)
@@ -263,11 +264,24 @@ clean:
263264

264265
.PHONY: fmt
265266
fmt:
266-
@MISSPELL_PACKAGE=$(MISSPELL_PACKAGE) GOFUMPT_PACKAGE=$(GOFUMPT_PACKAGE) $(GO) run build/code-batch-process.go gitea-fmt -w '{file-list}'
267+
GOFUMPT_PACKAGE=$(GOFUMPT_PACKAGE) $(GO) run build/code-batch-process.go gitea-fmt -w '{file-list}'
267268
$(eval TEMPLATES := $(shell find templates -type f -name '*.tmpl'))
268269
@# strip whitespace after '{{' and before `}}` unless there is only whitespace before it
269270
@$(SED_INPLACE) -e 's/{{[ ]\{1,\}/{{/g' -e '/^[ ]\{1,\}}}/! s/[ ]\{1,\}}}/}}/g' $(TEMPLATES)
270271

272+
.PHONY: fmt-check
273+
fmt-check: fmt
274+
@diff=$$(git diff $(GO_SOURCES) templates $(WEB_DIRS)); \
275+
if [ -n "$$diff" ]; then \
276+
echo "Please run 'make fmt' and commit the result:"; \
277+
echo "$${diff}"; \
278+
exit 1; \
279+
fi
280+
281+
.PHONY: misspell-check
282+
misspell-check:
283+
go run $(MISSPELL_PACKAGE) -error -i unknwon $(GO_DIRS) $(WEB_DIRS)
284+
271285
.PHONY: vet
272286
vet:
273287
@echo "Running go vet..."
@@ -311,30 +325,14 @@ errcheck:
311325
@echo "Running errcheck..."
312326
$(GO) run $(ERRCHECK_PACKAGE) $(GO_PACKAGES)
313327

314-
.PHONY: fmt-check
315-
fmt-check:
316-
@# get all go files and run gitea-fmt (with gofmt) on them
317-
@diff=$$(MISSPELL_PACKAGE=$(MISSPELL_PACKAGE) GOFUMPT_PACKAGE=$(GOFUMPT_PACKAGE) $(GO) run build/code-batch-process.go gitea-fmt -l '{file-list}'); \
318-
if [ -n "$$diff" ]; then \
319-
echo "Please run 'make fmt' and commit the result:"; \
320-
echo "$${diff}"; \
321-
exit 1; \
322-
fi
323-
@diff2=$$(git diff templates); \
324-
if [ -n "$$diff2" ]; then \
325-
echo "Please run 'make fmt' and commit the result:"; \
326-
echo "$${diff2}"; \
327-
exit 1; \
328-
fi
329-
330328
.PHONY: checks
331329
checks: checks-frontend checks-backend
332330

333331
.PHONY: checks-frontend
334332
checks-frontend: lockfile-check svg-check
335333

336334
.PHONY: checks-backend
337-
checks-backend: tidy-check swagger-check swagger-validate
335+
checks-backend: tidy-check swagger-check fmt-check misspell-check swagger-validate
338336

339337
.PHONY: lint
340338
lint: lint-frontend lint-backend

build/code-batch-process.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
// Windows has a limitation for command line arguments, the size can not exceed 32KB.
23-
// So we have to feed the files to some tools (like gofmt/misspell) batch by batch
23+
// So we have to feed the files to some tools (like gofmt) batch by batch
2424

2525
// We also introduce a `gitea-fmt` command, it does better import formatting than gofmt/goimports. `gitea-fmt` calls `gofmt` internally.
2626

@@ -195,7 +195,6 @@ Options:
195195
196196
Commands:
197197
%[1]s gofmt ...
198-
%[1]s misspell ...
199198
200199
Arguments:
201200
{file-list} the file list
@@ -239,9 +238,9 @@ func containsString(a []string, s string) bool {
239238
return false
240239
}
241240

242-
func giteaFormatGoImports(files []string, hasChangedFiles, doWriteFile bool) error {
241+
func giteaFormatGoImports(files []string, doWriteFile bool) error {
243242
for _, file := range files {
244-
if err := codeformat.FormatGoImports(file, hasChangedFiles, doWriteFile); err != nil {
243+
if err := codeformat.FormatGoImports(file, doWriteFile); err != nil {
245244
log.Printf("failed to format go imports: %s, err=%v", file, err)
246245
return err
247246
}
@@ -280,10 +279,8 @@ func main() {
280279
if containsString(subArgs, "-d") {
281280
log.Print("the -d option is not supported by gitea-fmt")
282281
}
283-
cmdErrors = append(cmdErrors, giteaFormatGoImports(files, containsString(subArgs, "-l"), containsString(subArgs, "-w")))
282+
cmdErrors = append(cmdErrors, giteaFormatGoImports(files, containsString(subArgs, "-w")))
284283
cmdErrors = append(cmdErrors, passThroughCmd("go", append([]string{"run", os.Getenv("GOFUMPT_PACKAGE"), "-extra", "-lang", getGoVersion()}, substArgs...)))
285-
case "misspell":
286-
cmdErrors = append(cmdErrors, passThroughCmd("go", append([]string{"run", os.Getenv("MISSPELL_PACKAGE")}, substArgs...)))
287284
default:
288285
log.Fatalf("unknown cmd: %s %v", subCmd, subArgs)
289286
}

build/codeformat/formatimports.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package codeformat
77
import (
88
"bytes"
99
"errors"
10-
"fmt"
1110
"io"
1211
"os"
1312
"sort"
@@ -159,7 +158,7 @@ func formatGoImports(contentBytes []byte) ([]byte, error) {
159158
}
160159

161160
// FormatGoImports format the imports by our rules (see unit tests)
162-
func FormatGoImports(file string, doChangedFiles, doWriteFile bool) error {
161+
func FormatGoImports(file string, doWriteFile bool) error {
163162
f, err := os.Open(file)
164163
if err != nil {
165164
return err
@@ -183,10 +182,6 @@ func FormatGoImports(file string, doChangedFiles, doWriteFile bool) error {
183182
return nil
184183
}
185184

186-
if doChangedFiles {
187-
fmt.Println(file)
188-
}
189-
190185
if doWriteFile {
191186
f, err = os.OpenFile(file, os.O_TRUNC|os.O_WRONLY, 0o644)
192187
if err != nil {

0 commit comments

Comments
 (0)