From 9602a9b50bf43e3cb1644df28777e4c9ca5f5c5d Mon Sep 17 00:00:00 2001 From: silverwind Date: Sun, 6 Feb 2022 07:43:50 +0100 Subject: [PATCH 1/6] 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`. --- services/mailer/mail_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/services/mailer/mail_test.go b/services/mailer/mail_test.go index 6c800e457b0c..ba82fc6ff845 100644 --- a/services/mailer/mail_test.go +++ b/services/mailer/mail_test.go @@ -17,6 +17,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/setting" + "github.com/stretchr/testify/assert" ) From 5b924d9cdad2d422bd45dbff149a2840071e18c6 Mon Sep 17 00:00:00 2001 From: silverwind Date: Sun, 6 Feb 2022 08:11:28 +0100 Subject: [PATCH 2/6] 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: https://github.com/go-gitea/gitea/pull/18633 --- Makefile | 5 ++--- services/mailer/mail_test.go | 1 + 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 88e08c1e7064..85ea94dfcbe9 100644 --- a/Makefile +++ b/Makefile @@ -284,9 +284,8 @@ errcheck: @errcheck $(GO_PACKAGES) .PHONY: fmt-check -fmt-check: - # get all go files and run gitea-fmt (with gofmt) on them - @diff=$$($(GO) run build/code-batch-process.go gitea-fmt -s -d '{file-list}'); \ +fmt-check: fmt + @diff=$$(git diff $(GO_DIRS)); \ if [ -n "$$diff" ]; then \ echo "Please run 'make fmt' and commit the result:"; \ echo "$${diff}"; \ diff --git a/services/mailer/mail_test.go b/services/mailer/mail_test.go index 6c800e457b0c..ba82fc6ff845 100644 --- a/services/mailer/mail_test.go +++ b/services/mailer/mail_test.go @@ -17,6 +17,7 @@ import ( "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/setting" + "github.com/stretchr/testify/assert" ) From 32b84e0ccd48eb94146c9575aa73f8589752bbf3 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 6 Feb 2022 11:02:54 +0000 Subject: [PATCH 3/6] 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 --- Makefile | 14 ++++++++------ build/code-batch-process.go | 10 ++++------ build/codeformat/formatimports.go | 31 ++++++++++++++++++++++++++----- go.mod | 1 + go.sum | 2 ++ 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 85ea94dfcbe9..9c319da2bd88 100644 --- a/Makefile +++ b/Makefile @@ -231,13 +231,11 @@ clean: .PHONY: fmt fmt: - @echo "Running gitea-fmt(with gofmt)..." - @$(GO) run build/code-batch-process.go gitea-fmt -s -w '{file-list}' - @echo "Running gofumpt" @hash gofumpt > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ $(GO) install mvdan.cc/gofumpt@latest; \ fi - @gofumpt -w -l -extra -lang 1.16 . + @echo "Running gitea-fmt (with gofumpt)..." + @$(GO) run build/code-batch-process.go gitea-fmt -w '{file-list}' .PHONY: vet vet: @@ -284,8 +282,12 @@ errcheck: @errcheck $(GO_PACKAGES) .PHONY: fmt-check -fmt-check: fmt - @diff=$$(git diff $(GO_DIRS)); \ +fmt-check: + @hash gofumpt > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ + $(GO) install mvdan.cc/gofumpt@latest; \ + fi + # get all go files and run gitea-fmt (with gofmt) on them + @diff=$$($(GO) run build/code-batch-process.go gitea-fmt -d '{file-list}'); \ if [ -n "$$diff" ]; then \ echo "Please run 'make fmt' and commit the result:"; \ echo "$${diff}"; \ diff --git a/build/code-batch-process.go b/build/code-batch-process.go index 02f54b9c0af2..0f1090b5bfd2 100644 --- a/build/code-batch-process.go +++ b/build/code-batch-process.go @@ -229,9 +229,9 @@ func containsString(a []string, s string) bool { return false } -func giteaFormatGoImports(files []string) error { +func giteaFormatGoImports(files []string, changedFiles, writeFile, outputDiff bool) error { for _, file := range files { - if err := codeformat.FormatGoImports(file); err != nil { + if err := codeformat.FormatGoImports(file, changedFiles, writeFile, outputDiff); err != nil { log.Printf("failed to format go imports: %s, err=%v", file, err) return err } @@ -267,10 +267,8 @@ func main() { logVerbose("batch cmd: %s %v", subCmd, substArgs) switch subCmd { case "gitea-fmt": - if containsString(subArgs, "-w") { - cmdErrors = append(cmdErrors, giteaFormatGoImports(files)) - } - cmdErrors = append(cmdErrors, passThroughCmd("gofmt", substArgs)) + cmdErrors = append(cmdErrors, giteaFormatGoImports(files, containsString(subArgs, "-l"), containsString(subArgs, "-w"), containsString(subArgs, "-d"))) + cmdErrors = append(cmdErrors, passThroughCmd("gofumpt", append([]string{"-extra", "-lang", "1.16"}, substArgs...))) case "misspell": cmdErrors = append(cmdErrors, passThroughCmd("misspell", substArgs)) default: diff --git a/build/codeformat/formatimports.go b/build/codeformat/formatimports.go index fedc5cc0909c..3b634f57c4c5 100644 --- a/build/codeformat/formatimports.go +++ b/build/codeformat/formatimports.go @@ -7,10 +7,15 @@ package codeformat import ( "bytes" "errors" + "fmt" "io" "os" "sort" "strings" + + "github.com/hexops/gotextdiff" + "github.com/hexops/gotextdiff/myers" + "github.com/hexops/gotextdiff/span" ) var importPackageGroupOrders = map[string]int{ @@ -158,7 +163,7 @@ func formatGoImports(contentBytes []byte) ([]byte, error) { } // FormatGoImports format the imports by our rules (see unit tests) -func FormatGoImports(file string) error { +func FormatGoImports(file string, changedFiles, writeFile, outputDiff bool) error { f, err := os.Open(file) if err != nil { return err @@ -181,11 +186,27 @@ func FormatGoImports(file string) error { if bytes.Equal(contentBytes, formattedBytes) { return nil } - f, err = os.OpenFile(file, os.O_TRUNC|os.O_WRONLY, 0o644) - if err != nil { + + if changedFiles { + fmt.Println(file) + } + + if writeFile { + f, err = os.OpenFile(file, os.O_TRUNC|os.O_WRONLY, 0o644) + if err != nil { + return err + } + defer f.Close() + _, err = f.Write(formattedBytes) return err } - defer f.Close() - _, err = f.Write(formattedBytes) + + if outputDiff { + edits := myers.ComputeEdits(span.URIFromPath("a/"+file), string(contentBytes), string(formattedBytes)) + diff := fmt.Sprint(gotextdiff.ToUnified("a/"+file, "b/"+file, string(contentBytes), edits)) + + fmt.Println(diff) + } + return err } diff --git a/go.mod b/go.mod index 1e1203049584..64158c5d79d9 100644 --- a/go.mod +++ b/go.mod @@ -62,6 +62,7 @@ require ( github.com/hashicorp/go-retryablehttp v0.7.0 // indirect github.com/hashicorp/go-version v1.3.1 github.com/hashicorp/golang-lru v0.5.4 + github.com/hexops/gotextdiff v1.0.3 github.com/huandu/xstrings v1.3.2 github.com/jaytaylor/html2text v0.0.0-20200412013138-3577fbdbcff7 github.com/json-iterator/go v1.1.11 diff --git a/go.sum b/go.sum index cb816f8b83c1..e7cbaf7f858b 100644 --- a/go.sum +++ b/go.sum @@ -651,6 +651,8 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= +github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= +github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw= github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= From c7703305b9be5cf8dd7907ee85411e62e051cdbd Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 6 Feb 2022 11:55:54 +0000 Subject: [PATCH 4/6] as per silverwind Signed-off-by: Andrew Thornton --- build/codeformat/formatimports.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/build/codeformat/formatimports.go b/build/codeformat/formatimports.go index 3b634f57c4c5..d689dc8ea347 100644 --- a/build/codeformat/formatimports.go +++ b/build/codeformat/formatimports.go @@ -163,7 +163,7 @@ func formatGoImports(contentBytes []byte) ([]byte, error) { } // FormatGoImports format the imports by our rules (see unit tests) -func FormatGoImports(file string, changedFiles, writeFile, outputDiff bool) error { +func FormatGoImports(file string, doChangedFiles, doWriteFile, doOutputDiff bool) error { f, err := os.Open(file) if err != nil { return err @@ -187,11 +187,11 @@ func FormatGoImports(file string, changedFiles, writeFile, outputDiff bool) erro return nil } - if changedFiles { + if doChangedFiles { fmt.Println(file) } - if writeFile { + if doWriteFile { f, err = os.OpenFile(file, os.O_TRUNC|os.O_WRONLY, 0o644) if err != nil { return err @@ -201,7 +201,7 @@ func FormatGoImports(file string, changedFiles, writeFile, outputDiff bool) erro return err } - if outputDiff { + if doOutputDiff { edits := myers.ComputeEdits(span.URIFromPath("a/"+file), string(contentBytes), string(formattedBytes)) diff := fmt.Sprint(gotextdiff.ToUnified("a/"+file, "b/"+file, string(contentBytes), edits)) From a4d45479ad12856662fa0ffd4da1f540e5a14312 Mon Sep 17 00:00:00 2001 From: zeripath Date: Sun, 6 Feb 2022 12:08:41 +0000 Subject: [PATCH 5/6] Apply suggestions from code review --- build/code-batch-process.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build/code-batch-process.go b/build/code-batch-process.go index 0f1090b5bfd2..53e762f1bd99 100644 --- a/build/code-batch-process.go +++ b/build/code-batch-process.go @@ -229,9 +229,9 @@ func containsString(a []string, s string) bool { return false } -func giteaFormatGoImports(files []string, changedFiles, writeFile, outputDiff bool) error { +func giteaFormatGoImports(files []string, hasChangedFiles, doWriteFile, doOutputDiff bool) error { for _, file := range files { - if err := codeformat.FormatGoImports(file, changedFiles, writeFile, outputDiff); err != nil { + if err := codeformat.FormatGoImports(file, hasChangedFiles, doWriteFile, doOutputDiff); err != nil { log.Printf("failed to format go imports: %s, err=%v", file, err) return err } From 5f951f8830dfbc039308119cddb4f356798c7470 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 6 Feb 2022 15:09:01 +0000 Subject: [PATCH 6/6] use -l instead of -d for fmt-check Signed-off-by: Andrew Thornton --- Makefile | 2 +- build/code-batch-process.go | 9 ++++++--- build/codeformat/formatimports.go | 13 +------------ go.mod | 1 - go.sum | 2 -- 5 files changed, 8 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index 9c319da2bd88..e3c948e33eb3 100644 --- a/Makefile +++ b/Makefile @@ -287,7 +287,7 @@ fmt-check: $(GO) install mvdan.cc/gofumpt@latest; \ fi # get all go files and run gitea-fmt (with gofmt) on them - @diff=$$($(GO) run build/code-batch-process.go gitea-fmt -d '{file-list}'); \ + @diff=$$($(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}"; \ diff --git a/build/code-batch-process.go b/build/code-batch-process.go index 53e762f1bd99..8139fe762375 100644 --- a/build/code-batch-process.go +++ b/build/code-batch-process.go @@ -229,9 +229,9 @@ func containsString(a []string, s string) bool { return false } -func giteaFormatGoImports(files []string, hasChangedFiles, doWriteFile, doOutputDiff bool) error { +func giteaFormatGoImports(files []string, hasChangedFiles, doWriteFile bool) error { for _, file := range files { - if err := codeformat.FormatGoImports(file, hasChangedFiles, doWriteFile, doOutputDiff); err != nil { + if err := codeformat.FormatGoImports(file, hasChangedFiles, doWriteFile); err != nil { log.Printf("failed to format go imports: %s, err=%v", file, err) return err } @@ -267,7 +267,10 @@ func main() { logVerbose("batch cmd: %s %v", subCmd, substArgs) switch subCmd { case "gitea-fmt": - cmdErrors = append(cmdErrors, giteaFormatGoImports(files, containsString(subArgs, "-l"), containsString(subArgs, "-w"), containsString(subArgs, "-d"))) + 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, passThroughCmd("gofumpt", append([]string{"-extra", "-lang", "1.16"}, substArgs...))) case "misspell": cmdErrors = append(cmdErrors, passThroughCmd("misspell", substArgs)) diff --git a/build/codeformat/formatimports.go b/build/codeformat/formatimports.go index d689dc8ea347..5d051b272615 100644 --- a/build/codeformat/formatimports.go +++ b/build/codeformat/formatimports.go @@ -12,10 +12,6 @@ import ( "os" "sort" "strings" - - "github.com/hexops/gotextdiff" - "github.com/hexops/gotextdiff/myers" - "github.com/hexops/gotextdiff/span" ) var importPackageGroupOrders = map[string]int{ @@ -163,7 +159,7 @@ func formatGoImports(contentBytes []byte) ([]byte, error) { } // FormatGoImports format the imports by our rules (see unit tests) -func FormatGoImports(file string, doChangedFiles, doWriteFile, doOutputDiff bool) error { +func FormatGoImports(file string, doChangedFiles, doWriteFile bool) error { f, err := os.Open(file) if err != nil { return err @@ -201,12 +197,5 @@ func FormatGoImports(file string, doChangedFiles, doWriteFile, doOutputDiff bool return err } - if doOutputDiff { - edits := myers.ComputeEdits(span.URIFromPath("a/"+file), string(contentBytes), string(formattedBytes)) - diff := fmt.Sprint(gotextdiff.ToUnified("a/"+file, "b/"+file, string(contentBytes), edits)) - - fmt.Println(diff) - } - return err } diff --git a/go.mod b/go.mod index 64158c5d79d9..1e1203049584 100644 --- a/go.mod +++ b/go.mod @@ -62,7 +62,6 @@ require ( github.com/hashicorp/go-retryablehttp v0.7.0 // indirect github.com/hashicorp/go-version v1.3.1 github.com/hashicorp/golang-lru v0.5.4 - github.com/hexops/gotextdiff v1.0.3 github.com/huandu/xstrings v1.3.2 github.com/jaytaylor/html2text v0.0.0-20200412013138-3577fbdbcff7 github.com/json-iterator/go v1.1.11 diff --git a/go.sum b/go.sum index e7cbaf7f858b..cb816f8b83c1 100644 --- a/go.sum +++ b/go.sum @@ -651,8 +651,6 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I= github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= -github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= -github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/huandu/xstrings v1.3.2 h1:L18LIDzqlW6xN2rEkpdV8+oL/IXWJ1APd+vsdYy4Wdw= github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=