Skip to content

Commit 37ead26

Browse files
silverwindlunnyzeripath
authored
Fix make fmt and make fmt-check (#18633)
* Run 'make fmt' 'make fmt' currently produces this change, I'm not sure how CI did not fail on it, I made sure I have `mvdan.cc/gofumpt@latest`. * Fix 'make fmt-check' `make fmt-check` did not run all commands that `make fmt` did, resulting in missed diffs. Fix that by just depending on the `fmt` target. Includes: #18633 * Make gitea-fmt work with -l and -d and integrate gofumpt This implements -l, -w and -d with gitea-fmt and merges gofumpt. Signed-off-by: Andrew Thornton <[email protected]> * as per silverwind Signed-off-by: Andrew Thornton <[email protected]> * Apply suggestions from code review * use -l instead of -d for fmt-check Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: Lunny Xiao <[email protected]> Co-authored-by: Andrew Thornton <[email protected]>
1 parent 7b6c1f8 commit 37ead26

File tree

4 files changed

+28
-15
lines changed

4 files changed

+28
-15
lines changed

Makefile

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,11 @@ clean:
231231

232232
.PHONY: fmt
233233
fmt:
234-
@echo "Running gitea-fmt(with gofmt)..."
235-
@$(GO) run build/code-batch-process.go gitea-fmt -s -w '{file-list}'
236-
@echo "Running gofumpt"
237234
@hash gofumpt > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
238235
$(GO) install mvdan.cc/gofumpt@latest; \
239236
fi
240-
@gofumpt -w -l -extra -lang 1.16 .
237+
@echo "Running gitea-fmt (with gofumpt)..."
238+
@$(GO) run build/code-batch-process.go gitea-fmt -w '{file-list}'
241239

242240
.PHONY: vet
243241
vet:
@@ -285,8 +283,11 @@ errcheck:
285283

286284
.PHONY: fmt-check
287285
fmt-check:
286+
@hash gofumpt > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
287+
$(GO) install mvdan.cc/gofumpt@latest; \
288+
fi
288289
# get all go files and run gitea-fmt (with gofmt) on them
289-
@diff=$$($(GO) run build/code-batch-process.go gitea-fmt -s -d '{file-list}'); \
290+
@diff=$$($(GO) run build/code-batch-process.go gitea-fmt -l '{file-list}'); \
290291
if [ -n "$$diff" ]; then \
291292
echo "Please run 'make fmt' and commit the result:"; \
292293
echo "$${diff}"; \

build/code-batch-process.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,9 @@ func containsString(a []string, s string) bool {
229229
return false
230230
}
231231

232-
func giteaFormatGoImports(files []string) error {
232+
func giteaFormatGoImports(files []string, hasChangedFiles, doWriteFile bool) error {
233233
for _, file := range files {
234-
if err := codeformat.FormatGoImports(file); err != nil {
234+
if err := codeformat.FormatGoImports(file, hasChangedFiles, doWriteFile); err != nil {
235235
log.Printf("failed to format go imports: %s, err=%v", file, err)
236236
return err
237237
}
@@ -267,10 +267,11 @@ func main() {
267267
logVerbose("batch cmd: %s %v", subCmd, substArgs)
268268
switch subCmd {
269269
case "gitea-fmt":
270-
if containsString(subArgs, "-w") {
271-
cmdErrors = append(cmdErrors, giteaFormatGoImports(files))
270+
if containsString(subArgs, "-d") {
271+
log.Print("the -d option is not supported by gitea-fmt")
272272
}
273-
cmdErrors = append(cmdErrors, passThroughCmd("gofmt", substArgs))
273+
cmdErrors = append(cmdErrors, giteaFormatGoImports(files, containsString(subArgs, "-l"), containsString(subArgs, "-w")))
274+
cmdErrors = append(cmdErrors, passThroughCmd("gofumpt", append([]string{"-extra", "-lang", "1.16"}, substArgs...)))
274275
case "misspell":
275276
cmdErrors = append(cmdErrors, passThroughCmd("misspell", substArgs))
276277
default:

build/codeformat/formatimports.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package codeformat
77
import (
88
"bytes"
99
"errors"
10+
"fmt"
1011
"io"
1112
"os"
1213
"sort"
@@ -158,7 +159,7 @@ func formatGoImports(contentBytes []byte) ([]byte, error) {
158159
}
159160

160161
// FormatGoImports format the imports by our rules (see unit tests)
161-
func FormatGoImports(file string) error {
162+
func FormatGoImports(file string, doChangedFiles, doWriteFile bool) error {
162163
f, err := os.Open(file)
163164
if err != nil {
164165
return err
@@ -181,11 +182,20 @@ func FormatGoImports(file string) error {
181182
if bytes.Equal(contentBytes, formattedBytes) {
182183
return nil
183184
}
184-
f, err = os.OpenFile(file, os.O_TRUNC|os.O_WRONLY, 0o644)
185-
if err != nil {
185+
186+
if doChangedFiles {
187+
fmt.Println(file)
188+
}
189+
190+
if doWriteFile {
191+
f, err = os.OpenFile(file, os.O_TRUNC|os.O_WRONLY, 0o644)
192+
if err != nil {
193+
return err
194+
}
195+
defer f.Close()
196+
_, err = f.Write(formattedBytes)
186197
return err
187198
}
188-
defer f.Close()
189-
_, err = f.Write(formattedBytes)
199+
190200
return err
191201
}

services/mailer/mail_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"code.gitea.io/gitea/models/unittest"
1818
user_model "code.gitea.io/gitea/models/user"
1919
"code.gitea.io/gitea/modules/setting"
20+
2021
"github.com/stretchr/testify/assert"
2122
)
2223

0 commit comments

Comments
 (0)