mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	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 <xiaolunwen@gmail.com> Co-authored-by: zeripath <art27@cantab.net> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
		@@ -12,7 +12,6 @@ linters:
 | 
				
			|||||||
    - dupl
 | 
					    - dupl
 | 
				
			||||||
    #- gocyclo # The cyclomatic complexety of a lot of functions is too high, we should refactor those another time.
 | 
					    #- gocyclo # The cyclomatic complexety of a lot of functions is too high, we should refactor those another time.
 | 
				
			||||||
    - gofmt
 | 
					    - gofmt
 | 
				
			||||||
    - misspell
 | 
					 | 
				
			||||||
    - gocritic
 | 
					    - gocritic
 | 
				
			||||||
    - bidichk
 | 
					    - bidichk
 | 
				
			||||||
    - ineffassign
 | 
					    - ineffassign
 | 
				
			||||||
@@ -148,9 +147,6 @@ issues:
 | 
				
			|||||||
    - path: models/issue_comment_list.go
 | 
					    - path: models/issue_comment_list.go
 | 
				
			||||||
      linters:
 | 
					      linters:
 | 
				
			||||||
        - dupl
 | 
					        - dupl
 | 
				
			||||||
    - linters:
 | 
					 | 
				
			||||||
        - misspell
 | 
					 | 
				
			||||||
      text: '`Unknwon` is a misspelling of `Unknown`'
 | 
					 | 
				
			||||||
    - path: models/update.go
 | 
					    - path: models/update.go
 | 
				
			||||||
      linters:
 | 
					      linters:
 | 
				
			||||||
        - unused
 | 
					        - unused
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										34
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								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)
 | 
					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
 | 
					GO_DIRS := cmd tests models modules routers build services tools
 | 
				
			||||||
 | 
					WEB_DIRS := web_src/js web_src/less
 | 
				
			||||||
 | 
					
 | 
				
			||||||
GO_SOURCES := $(wildcard *.go)
 | 
					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)
 | 
					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:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
.PHONY: fmt
 | 
					.PHONY: fmt
 | 
				
			||||||
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'))
 | 
						$(eval TEMPLATES := $(shell find templates -type f -name '*.tmpl'))
 | 
				
			||||||
	@# strip whitespace after '{{' and before `}}` unless there is only whitespace before it
 | 
						@# strip whitespace after '{{' and before `}}` unless there is only whitespace before it
 | 
				
			||||||
	@$(SED_INPLACE) -e 's/{{[ 	]\{1,\}/{{/g' -e '/^[ 	]\{1,\}}}/! s/[ 	]\{1,\}}}/}}/g' $(TEMPLATES)
 | 
						@$(SED_INPLACE) -e 's/{{[ 	]\{1,\}/{{/g' -e '/^[ 	]\{1,\}}}/! s/[ 	]\{1,\}}}/}}/g' $(TEMPLATES)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.PHONY: fmt-check
 | 
				
			||||||
 | 
					fmt-check: fmt
 | 
				
			||||||
 | 
						@diff=$$(git diff $(GO_SOURCES) templates $(WEB_DIRS)); \
 | 
				
			||||||
 | 
						if [ -n "$$diff" ]; then \
 | 
				
			||||||
 | 
						  echo "Please run 'make fmt' and commit the result:"; \
 | 
				
			||||||
 | 
						  echo "$${diff}"; \
 | 
				
			||||||
 | 
						  exit 1; \
 | 
				
			||||||
 | 
						fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.PHONY: misspell-check
 | 
				
			||||||
 | 
					misspell-check:
 | 
				
			||||||
 | 
						go run $(MISSPELL_PACKAGE) -error -i unknwon $(GO_DIRS) $(WEB_DIRS)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.PHONY: vet
 | 
					.PHONY: vet
 | 
				
			||||||
vet:
 | 
					vet:
 | 
				
			||||||
	@echo "Running go vet..."
 | 
						@echo "Running go vet..."
 | 
				
			||||||
@@ -311,22 +325,6 @@ errcheck:
 | 
				
			|||||||
	@echo "Running errcheck..."
 | 
						@echo "Running errcheck..."
 | 
				
			||||||
	$(GO) run $(ERRCHECK_PACKAGE) $(GO_PACKAGES)
 | 
						$(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
 | 
					.PHONY: checks
 | 
				
			||||||
checks: checks-frontend checks-backend
 | 
					checks: checks-frontend checks-backend
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -334,7 +332,7 @@ checks: checks-frontend checks-backend
 | 
				
			|||||||
checks-frontend: lockfile-check svg-check
 | 
					checks-frontend: lockfile-check svg-check
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.PHONY: checks-backend
 | 
					.PHONY: checks-backend
 | 
				
			||||||
checks-backend: tidy-check swagger-check swagger-validate
 | 
					checks-backend: tidy-check swagger-check fmt-check misspell-check swagger-validate
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.PHONY: lint
 | 
					.PHONY: lint
 | 
				
			||||||
lint: lint-frontend lint-backend
 | 
					lint: lint-frontend lint-backend
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,7 +20,7 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Windows has a limitation for command line arguments, the size can not exceed 32KB.
 | 
					// 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.
 | 
					// 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:
 | 
					Commands:
 | 
				
			||||||
  %[1]s gofmt ...
 | 
					  %[1]s gofmt ...
 | 
				
			||||||
  %[1]s misspell ...
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
Arguments:
 | 
					Arguments:
 | 
				
			||||||
  {file-list}     the file list
 | 
					  {file-list}     the file list
 | 
				
			||||||
@@ -239,9 +238,9 @@ func containsString(a []string, s string) bool {
 | 
				
			|||||||
	return false
 | 
						return false
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func giteaFormatGoImports(files []string, hasChangedFiles, doWriteFile bool) error {
 | 
					func giteaFormatGoImports(files []string, doWriteFile bool) error {
 | 
				
			||||||
	for _, file := range files {
 | 
						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)
 | 
								log.Printf("failed to format go imports: %s, err=%v", file, err)
 | 
				
			||||||
			return err
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@@ -280,10 +279,8 @@ func main() {
 | 
				
			|||||||
			if containsString(subArgs, "-d") {
 | 
								if containsString(subArgs, "-d") {
 | 
				
			||||||
				log.Print("the -d option is not supported by gitea-fmt")
 | 
									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", getGoVersion()}, substArgs...)))
 | 
								cmdErrors = append(cmdErrors, passThroughCmd("go", append([]string{"run", os.Getenv("GOFUMPT_PACKAGE"), "-extra", "-lang", getGoVersion()}, substArgs...)))
 | 
				
			||||||
		case "misspell":
 | 
					 | 
				
			||||||
			cmdErrors = append(cmdErrors, passThroughCmd("go", append([]string{"run", os.Getenv("MISSPELL_PACKAGE")}, substArgs...)))
 | 
					 | 
				
			||||||
		default:
 | 
							default:
 | 
				
			||||||
			log.Fatalf("unknown cmd: %s %v", subCmd, subArgs)
 | 
								log.Fatalf("unknown cmd: %s %v", subCmd, subArgs)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,7 +7,6 @@ package codeformat
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"bytes"
 | 
						"bytes"
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
					 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"sort"
 | 
						"sort"
 | 
				
			||||||
@@ -159,7 +158,7 @@ func formatGoImports(contentBytes []byte) ([]byte, error) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// FormatGoImports format the imports by our rules (see unit tests)
 | 
					// 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)
 | 
						f, err := os.Open(file)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
@@ -183,10 +182,6 @@ func FormatGoImports(file string, doChangedFiles, doWriteFile bool) error {
 | 
				
			|||||||
		return nil
 | 
							return nil
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if doChangedFiles {
 | 
					 | 
				
			||||||
		fmt.Println(file)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if doWriteFile {
 | 
						if doWriteFile {
 | 
				
			||||||
		f, err = os.OpenFile(file, os.O_TRUNC|os.O_WRONLY, 0o644)
 | 
							f, err = os.OpenFile(file, os.O_TRUNC|os.O_WRONLY, 0o644)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user