Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify fmt-check #21458

Merged
merged 7 commits into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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..."
Expand Down Expand Up @@ -311,30 +320,14 @@ 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

.PHONY: checks-frontend
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
Expand Down
6 changes: 3 additions & 3 deletions build/code-batch-process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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...)))
Expand Down
7 changes: 1 addition & 6 deletions build/codeformat/formatimports.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package codeformat
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"sort"
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down