From 51d024fd4d94cf0242510c1db45303a7f14cdf92 Mon Sep 17 00:00:00 2001 From: silverwind Date: Fri, 14 Oct 2022 21:44:41 +0200 Subject: [PATCH 1/4] Simplify fmt-check fmt-check now simly 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. --- Makefile | 27 ++++++++++----------------- build/code-batch-process.go | 6 +++--- build/codeformat/formatimports.go | 7 +------ 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index a8bb260921293..5fe5ee3d206a3 100644 --- a/Makefile +++ b/Makefile @@ -268,6 +268,15 @@ fmt: @# strip whitespace after '{{' and before `}}` unless there is only whitespace before it @$(SED_INPLACE) -e 's/{{[ ]\{1,\}/{{/g' -e '/^[ ]\{1,\}}}/! s/[ ]\{1,\}}}/}}/g' $(TEMPLATES) +.PHONY: fmt-check +fmt-check: fmt + @diff=$$(git diff $(GO_SOURCES) templates); \ + if [ -n "$$diff" ]; then \ + echo "Please run 'make fmt' and commit the result:"; \ + echo "$${diff}"; \ + exit 1; \ + fi + .PHONY: vet vet: @echo "Running go vet..." @@ -311,22 +320,6 @@ errcheck: @echo "Running errcheck..." $(GO) run $(ERRCHECK_PACKAGE) $(GO_PACKAGES) -.PHONY: fmt-check -fmt-check: - @# get all go files and run gitea-fmt (with gofmt) on them - @diff=$$(MISSPELL_PACKAGE=$(MISSPELL_PACKAGE) GOFUMPT_PACKAGE=$(GOFUMPT_PACKAGE) $(GO) run build/code-batch-process.go gitea-fmt -l '{file-list}'); \ - if [ -n "$$diff" ]; then \ - echo "Please run 'make fmt' and commit the result:"; \ - echo "$${diff}"; \ - exit 1; \ - fi - @diff2=$$(git diff templates); \ - if [ -n "$$diff2" ]; then \ - echo "Please run 'make fmt' and commit the result:"; \ - echo "$${diff2}"; \ - exit 1; \ - fi - .PHONY: checks checks: checks-frontend checks-backend @@ -334,7 +327,7 @@ checks: checks-frontend checks-backend checks-frontend: lockfile-check svg-check .PHONY: checks-backend -checks-backend: tidy-check swagger-check swagger-validate +checks-backend: tidy-check swagger-check fmt-check swagger-validate .PHONY: lint lint: lint-frontend lint-backend diff --git a/build/code-batch-process.go b/build/code-batch-process.go index 24b13470ab459..057a33052714e 100644 --- a/build/code-batch-process.go +++ b/build/code-batch-process.go @@ -228,9 +228,9 @@ func containsString(a []string, s string) bool { return false } -func giteaFormatGoImports(files []string, hasChangedFiles, doWriteFile bool) error { +func giteaFormatGoImports(files []string, doWriteFile bool) error { for _, file := range files { - if err := codeformat.FormatGoImports(file, hasChangedFiles, doWriteFile); err != nil { + if err := codeformat.FormatGoImports(file, doWriteFile); err != nil { log.Printf("failed to format go imports: %s, err=%v", file, err) return err } @@ -269,7 +269,7 @@ func main() { if containsString(subArgs, "-d") { log.Print("the -d option is not supported by gitea-fmt") } - cmdErrors = append(cmdErrors, giteaFormatGoImports(files, containsString(subArgs, "-l"), containsString(subArgs, "-w"))) + cmdErrors = append(cmdErrors, giteaFormatGoImports(files, containsString(subArgs, "-w"))) cmdErrors = append(cmdErrors, passThroughCmd("go", append([]string{"run", os.Getenv("GOFUMPT_PACKAGE"), "-extra", "-lang", "1.17"}, substArgs...))) case "misspell": cmdErrors = append(cmdErrors, passThroughCmd("go", append([]string{"run", os.Getenv("MISSPELL_PACKAGE")}, substArgs...))) diff --git a/build/codeformat/formatimports.go b/build/codeformat/formatimports.go index 5d051b2726153..1076e3a0d1cd2 100644 --- a/build/codeformat/formatimports.go +++ b/build/codeformat/formatimports.go @@ -7,7 +7,6 @@ package codeformat import ( "bytes" "errors" - "fmt" "io" "os" "sort" @@ -159,7 +158,7 @@ func formatGoImports(contentBytes []byte) ([]byte, error) { } // FormatGoImports format the imports by our rules (see unit tests) -func FormatGoImports(file string, doChangedFiles, doWriteFile bool) error { +func FormatGoImports(file string, doWriteFile bool) error { f, err := os.Open(file) if err != nil { return err @@ -183,10 +182,6 @@ func FormatGoImports(file string, doChangedFiles, doWriteFile bool) error { return nil } - if doChangedFiles { - fmt.Println(file) - } - if doWriteFile { f, err = os.OpenFile(file, os.O_TRUNC|os.O_WRONLY, 0o644) if err != nil { From f0488aa95bf50f63e3a5c8bd55a629416dfa0c21 Mon Sep 17 00:00:00 2001 From: silverwind Date: Sat, 15 Oct 2022 12:21:34 +0200 Subject: [PATCH 2/4] Remove misspell from golangci and code-batch-format and run it directly --- .golangci.yml | 4 ---- Makefile | 4 +++- build/code-batch-process.go | 5 +---- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 0e796a2016b0a..99133badd9b86 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -12,7 +12,6 @@ linters: - dupl #- gocyclo # The cyclomatic complexety of a lot of functions is too high, we should refactor those another time. - gofmt - - misspell - gocritic - bidichk - ineffassign @@ -148,9 +147,6 @@ issues: - path: models/issue_comment_list.go linters: - dupl - - linters: - - misspell - text: '`Unknwon` is a misspelling of `Unknown`' - path: models/update.go linters: - unused diff --git a/Makefile b/Makefile index 5fe5ee3d206a3..5c00382798692 100644 --- a/Makefile +++ b/Makefile @@ -130,6 +130,7 @@ TEST_TAGS ?= sqlite sqlite_unlock_notify 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) GO_DIRS := cmd tests models modules routers build services tools +WEB_DIRS := web_src/js web_src/less GO_SOURCES := $(wildcard *.go) 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,10 +264,11 @@ clean: .PHONY: fmt fmt: - @MISSPELL_PACKAGE=$(MISSPELL_PACKAGE) GOFUMPT_PACKAGE=$(GOFUMPT_PACKAGE) $(GO) run build/code-batch-process.go gitea-fmt -w '{file-list}' + GOFUMPT_PACKAGE=$(GOFUMPT_PACKAGE) $(GO) run build/code-batch-process.go gitea-fmt -w '{file-list}' $(eval TEMPLATES := $(shell find templates -type f -name '*.tmpl')) @# strip whitespace after '{{' and before `}}` unless there is only whitespace before it @$(SED_INPLACE) -e 's/{{[ ]\{1,\}/{{/g' -e '/^[ ]\{1,\}}}/! s/[ ]\{1,\}}}/}}/g' $(TEMPLATES) + go run $(MISSPELL_PACKAGE) -w -i unknwon $(GO_DIRS) $(WEB_DIRS) .PHONY: fmt-check fmt-check: fmt diff --git a/build/code-batch-process.go b/build/code-batch-process.go index 057a33052714e..f64376d3842c0 100644 --- a/build/code-batch-process.go +++ b/build/code-batch-process.go @@ -20,7 +20,7 @@ import ( ) // Windows has a limitation for command line arguments, the size can not exceed 32KB. -// So we have to feed the files to some tools (like gofmt/misspell) batch by batch +// So we have to feed the files to some tools (like gofmt) batch by batch // We also introduce a `gitea-fmt` command, it does better import formatting than gofmt/goimports. `gitea-fmt` calls `gofmt` internally. @@ -195,7 +195,6 @@ Options: Commands: %[1]s gofmt ... - %[1]s misspell ... Arguments: {file-list} the file list @@ -271,8 +270,6 @@ func main() { } cmdErrors = append(cmdErrors, giteaFormatGoImports(files, containsString(subArgs, "-w"))) cmdErrors = append(cmdErrors, passThroughCmd("go", append([]string{"run", os.Getenv("GOFUMPT_PACKAGE"), "-extra", "-lang", "1.17"}, substArgs...))) - case "misspell": - cmdErrors = append(cmdErrors, passThroughCmd("go", append([]string{"run", os.Getenv("MISSPELL_PACKAGE")}, substArgs...))) default: log.Fatalf("unknown cmd: %s %v", subCmd, subArgs) } From ce25a28ed23ff8d532308447c823c9d0981a6ee6 Mon Sep 17 00:00:00 2001 From: silverwind Date: Sat, 15 Oct 2022 12:22:52 +0200 Subject: [PATCH 3/4] add WEB_DIRS to diff --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5c00382798692..91cad36849a51 100644 --- a/Makefile +++ b/Makefile @@ -272,7 +272,7 @@ fmt: .PHONY: fmt-check fmt-check: fmt - @diff=$$(git diff $(GO_SOURCES) templates); \ + @diff=$$(git diff $(GO_SOURCES) templates $(WEB_DIRS)); \ if [ -n "$$diff" ]; then \ echo "Please run 'make fmt' and commit the result:"; \ echo "$${diff}"; \ From 29a46711de14eeea3486d12ee75eaf855e71b61b Mon Sep 17 00:00:00 2001 From: silverwind Date: Sat, 15 Oct 2022 12:25:49 +0200 Subject: [PATCH 4/4] add misspell-check and remove it from fmt --- Makefile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 91cad36849a51..5170436d839f1 100644 --- a/Makefile +++ b/Makefile @@ -268,7 +268,6 @@ fmt: $(eval TEMPLATES := $(shell find templates -type f -name '*.tmpl')) @# strip whitespace after '{{' and before `}}` unless there is only whitespace before it @$(SED_INPLACE) -e 's/{{[ ]\{1,\}/{{/g' -e '/^[ ]\{1,\}}}/! s/[ ]\{1,\}}}/}}/g' $(TEMPLATES) - go run $(MISSPELL_PACKAGE) -w -i unknwon $(GO_DIRS) $(WEB_DIRS) .PHONY: fmt-check fmt-check: fmt @@ -279,6 +278,10 @@ fmt-check: fmt exit 1; \ fi +.PHONY: misspell-check +misspell-check: + go run $(MISSPELL_PACKAGE) -error -i unknwon $(GO_DIRS) $(WEB_DIRS) + .PHONY: vet vet: @echo "Running go vet..." @@ -329,7 +332,7 @@ checks: checks-frontend checks-backend checks-frontend: lockfile-check svg-check .PHONY: checks-backend -checks-backend: tidy-check swagger-check fmt-check swagger-validate +checks-backend: tidy-check swagger-check fmt-check misspell-check swagger-validate .PHONY: lint lint: lint-frontend lint-backend