From 37388092192155b8a82b8c94096903db174985ea Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 13 May 2026 15:00:41 +0800 Subject: [PATCH] fix: catch and fix more lint problems (#37674) Changes are done by "make lint-go-fix" --- .github/workflows/pull-compliance.yml | 3 +-- modules/git/commit_info_gogit.go | 10 ++++------ modules/git/notes_gogit.go | 15 ++++++++------- modules/git/parse_treeentry.go | 2 ++ modules/git/repo_commitgraph_gogit.go | 10 +++++----- modules/git/tree_entry_gogit_test.go | 4 ++-- tools/lint-go-all.go | 9 ++++++--- 7 files changed, 28 insertions(+), 25 deletions(-) diff --git a/.github/workflows/pull-compliance.yml b/.github/workflows/pull-compliance.yml index 51e1c805e6..7d61ad3a1c 100644 --- a/.github/workflows/pull-compliance.yml +++ b/.github/workflows/pull-compliance.yml @@ -30,9 +30,8 @@ jobs: cache-name: lint-backend lint-cache: "true" - run: make deps-backend deps-tools + - run: TAGS="bindata" make generate-go # lint-go also lints with "bindata" tags which requires "_bindata.go" - run: make lint-backend - env: - TAGS: bindata lint-on-demand: needs: files-changed diff --git a/modules/git/commit_info_gogit.go b/modules/git/commit_info_gogit.go index 73227347bc..6174f46f07 100644 --- a/modules/git/commit_info_gogit.go +++ b/modules/git/commit_info_gogit.go @@ -7,6 +7,7 @@ package git import ( "context" + "maps" "path" "github.com/emirpasic/gods/trees/binaryheap" @@ -47,9 +48,7 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, repoLink string, commit * return nil, nil, err } - for k, v := range revs2 { - revs[k] = v - } + maps.Copy(revs, revs2) } } else { revs, err = GetLastCommitForPaths(ctx, nil, c, treePath, entryPaths) @@ -201,7 +200,7 @@ heaploop: // Load the parent commits for the one we are currently examining numParents := current.commit.NumParents() var parents []cgobject.CommitNode - for i := 0; i < numParents; i++ { + for i := range numParents { parent, err := current.commit.ParentNode(i) if err != nil { break @@ -273,9 +272,8 @@ heaploop: if len(newRemainingPaths) == 0 { break - } else { - remainingPaths = newRemainingPaths } + remainingPaths = newRemainingPaths } } } diff --git a/modules/git/notes_gogit.go b/modules/git/notes_gogit.go index 340f4d5ccc..b3638820dd 100644 --- a/modules/git/notes_gogit.go +++ b/modules/git/notes_gogit.go @@ -9,6 +9,7 @@ import ( "context" "fmt" "io" + "strings" "code.gitea.io/gitea/modules/log" @@ -30,7 +31,7 @@ func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) } remainingCommitID := commitID - path := "" + var path strings.Builder currentTree, err := notes.Tree.gogitTreeObject() if err != nil { return fmt.Errorf("unable to get tree object for notes commit %q: %w", notes.ID.String(), err) @@ -41,17 +42,17 @@ func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) for len(remainingCommitID) > 2 { file, err = currentTree.File(remainingCommitID) if err == nil { - path += remainingCommitID + path.WriteString(remainingCommitID) break } if err == object.ErrFileNotFound { currentTree, err = currentTree.Tree(remainingCommitID[0:2]) - path += remainingCommitID[0:2] + "/" + path.WriteString(remainingCommitID[0:2] + "/") remainingCommitID = remainingCommitID[2:] } if err != nil { if err == object.ErrDirectoryNotFound { - return ErrNotExist{ID: remainingCommitID, RelPath: path} + return ErrNotExist{ID: remainingCommitID, RelPath: path.String()} } log.Error("Unable to find git note corresponding to the commit %q. Error: %v", commitID, err) return err @@ -83,12 +84,12 @@ func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note) return err } - lastCommits, err := GetLastCommitForPaths(ctx, nil, commitNode, "", []string{path}) + lastCommits, err := GetLastCommitForPaths(ctx, nil, commitNode, "", []string{path.String()}) if err != nil { - log.Error("Unable to get the commit for the path %q. Error: %v", path, err) + log.Error("Unable to get the commit for the path %q. Error: %v", path.String(), err) return err } - note.Commit = lastCommits[path] + note.Commit = lastCommits[path.String()] return nil } diff --git a/modules/git/parse_treeentry.go b/modules/git/parse_treeentry.go index 23d59c1923..4a00cc1fb1 100644 --- a/modules/git/parse_treeentry.go +++ b/modules/git/parse_treeentry.go @@ -43,6 +43,8 @@ func parseTreeEntries(data []byte, ptree *Tree) ([]*TreeEntry, error) { return entries, nil } +var _ = catBatchParseTreeEntries // bypass "unused" lint because it is only used by "nogogit" + func catBatchParseTreeEntries(objectFormat ObjectFormat, ptree *Tree, rd BufferedReader, sz int64) ([]*TreeEntry, error) { entries := make([]*TreeEntry, 0, 10) diff --git a/modules/git/repo_commitgraph_gogit.go b/modules/git/repo_commitgraph_gogit.go index c0082b62c8..aebb9981e2 100644 --- a/modules/git/repo_commitgraph_gogit.go +++ b/modules/git/repo_commitgraph_gogit.go @@ -17,21 +17,21 @@ import ( ) // CommitNodeIndex returns the index for walking commit graph -func (r *Repository) CommitNodeIndex() (cgobject.CommitNodeIndex, *os.File) { - indexPath := filepath.Join(r.Path, "objects", "info", "commit-graph") +func (repo *Repository) CommitNodeIndex() (cgobject.CommitNodeIndex, *os.File) { + indexPath := filepath.Join(repo.Path, "objects", "info", "commit-graph") file, err := os.Open(indexPath) if err == nil { var index commitgraph.Index index, err = commitgraph.OpenFileIndex(file) if err == nil { - return cgobject.NewGraphCommitNodeIndex(index, r.gogitRepo.Storer), file + return cgobject.NewGraphCommitNodeIndex(index, repo.gogitRepo.Storer), file } } if !os.IsNotExist(err) { - gitealog.Warn("Unable to read commit-graph for %s: %v", r.Path, err) + gitealog.Warn("Unable to read commit-graph for %s: %v", repo.Path, err) } - return cgobject.NewObjectCommitNodeIndex(r.gogitRepo.Storer), nil + return cgobject.NewObjectCommitNodeIndex(repo.gogitRepo.Storer), nil } diff --git a/modules/git/tree_entry_gogit_test.go b/modules/git/tree_entry_gogit_test.go index ed14b45e9e..7727b08cb9 100644 --- a/modules/git/tree_entry_gogit_test.go +++ b/modules/git/tree_entry_gogit_test.go @@ -21,7 +21,7 @@ func TestEntryGogit(t *testing.T) { EntryModeTree: filemode.Dir, } for emode, fmode := range cases { - assert.EqualValues(t, fmode, entryModeToGogitFileMode(emode)) - assert.EqualValues(t, emode, gogitFileModeToEntryMode(fmode)) + assert.Equal(t, fmode, entryModeToGogitFileMode(emode)) + assert.Equal(t, emode, gogitFileModeToEntryMode(fmode)) } } diff --git a/tools/lint-go-all.go b/tools/lint-go-all.go index b2071f1703..0193baccf2 100644 --- a/tools/lint-go-all.go +++ b/tools/lint-go-all.go @@ -92,9 +92,12 @@ func main() { _, _ = fmt.Fprintln(os.Stdout, "lint go header ...") succeed := lintGoHeader() _, _ = fmt.Fprintln(os.Stdout, "lint for linux ...") - succeed = runCmd([]string{"GOOS=linux", "TAGS=bindata"}, "golangci-lint", append([]string{"run"}, os.Args[1:]...)) && succeed - _, _ = fmt.Fprintln(os.Stdout, "lint for windows ...") - succeed = runCmd([]string{"GOOS=windows", "TAGS=gogit"}, "golangci-lint", append([]string{"run"}, os.Args[1:]...)) && succeed + succeed = runCmd([]string{"GOOS=linux", "TAGS=bindata"}, "golangci-lint", append([]string{"run", "--build-tags=linux,bindata"}, os.Args[1:]...)) && succeed + if os.Getenv("CI") != "" { + // only lint for other platforms when in CI, to keep local lint fast + _, _ = fmt.Fprintln(os.Stdout, "lint for windows ...") + succeed = runCmd([]string{"GOOS=windows", "TAGS=gogit"}, "golangci-lint", append([]string{"run", "--build-tags=windows,gogit"}, os.Args[1:]...)) && succeed + } if !succeed { os.Exit(1) }