fix: catch and fix more lint problems (#37674)

Changes are done by "make lint-go-fix"
This commit is contained in:
wxiaoguang
2026-05-13 15:00:41 +08:00
committed by GitHub
parent ffd5e0698b
commit 3738809219
7 changed files with 28 additions and 25 deletions
+1 -2
View File
@@ -30,9 +30,8 @@ jobs:
cache-name: lint-backend cache-name: lint-backend
lint-cache: "true" lint-cache: "true"
- run: make deps-backend deps-tools - 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 - run: make lint-backend
env:
TAGS: bindata
lint-on-demand: lint-on-demand:
needs: files-changed needs: files-changed
+4 -6
View File
@@ -7,6 +7,7 @@ package git
import ( import (
"context" "context"
"maps"
"path" "path"
"github.com/emirpasic/gods/trees/binaryheap" "github.com/emirpasic/gods/trees/binaryheap"
@@ -47,9 +48,7 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, repoLink string, commit *
return nil, nil, err return nil, nil, err
} }
for k, v := range revs2 { maps.Copy(revs, revs2)
revs[k] = v
}
} }
} else { } else {
revs, err = GetLastCommitForPaths(ctx, nil, c, treePath, entryPaths) revs, err = GetLastCommitForPaths(ctx, nil, c, treePath, entryPaths)
@@ -201,7 +200,7 @@ heaploop:
// Load the parent commits for the one we are currently examining // Load the parent commits for the one we are currently examining
numParents := current.commit.NumParents() numParents := current.commit.NumParents()
var parents []cgobject.CommitNode var parents []cgobject.CommitNode
for i := 0; i < numParents; i++ { for i := range numParents {
parent, err := current.commit.ParentNode(i) parent, err := current.commit.ParentNode(i)
if err != nil { if err != nil {
break break
@@ -273,9 +272,8 @@ heaploop:
if len(newRemainingPaths) == 0 { if len(newRemainingPaths) == 0 {
break break
} else {
remainingPaths = newRemainingPaths
} }
remainingPaths = newRemainingPaths
} }
} }
} }
+8 -7
View File
@@ -9,6 +9,7 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"strings"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
@@ -30,7 +31,7 @@ func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note)
} }
remainingCommitID := commitID remainingCommitID := commitID
path := "" var path strings.Builder
currentTree, err := notes.Tree.gogitTreeObject() currentTree, err := notes.Tree.gogitTreeObject()
if err != nil { if err != nil {
return fmt.Errorf("unable to get tree object for notes commit %q: %w", notes.ID.String(), err) 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 { for len(remainingCommitID) > 2 {
file, err = currentTree.File(remainingCommitID) file, err = currentTree.File(remainingCommitID)
if err == nil { if err == nil {
path += remainingCommitID path.WriteString(remainingCommitID)
break break
} }
if err == object.ErrFileNotFound { if err == object.ErrFileNotFound {
currentTree, err = currentTree.Tree(remainingCommitID[0:2]) currentTree, err = currentTree.Tree(remainingCommitID[0:2])
path += remainingCommitID[0:2] + "/" path.WriteString(remainingCommitID[0:2] + "/")
remainingCommitID = remainingCommitID[2:] remainingCommitID = remainingCommitID[2:]
} }
if err != nil { if err != nil {
if err == object.ErrDirectoryNotFound { 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) log.Error("Unable to find git note corresponding to the commit %q. Error: %v", commitID, err)
return err return err
@@ -83,12 +84,12 @@ func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note)
return err return err
} }
lastCommits, err := GetLastCommitForPaths(ctx, nil, commitNode, "", []string{path}) lastCommits, err := GetLastCommitForPaths(ctx, nil, commitNode, "", []string{path.String()})
if err != nil { 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 return err
} }
note.Commit = lastCommits[path] note.Commit = lastCommits[path.String()]
return nil return nil
} }
+2
View File
@@ -43,6 +43,8 @@ func parseTreeEntries(data []byte, ptree *Tree) ([]*TreeEntry, error) {
return entries, nil 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) { func catBatchParseTreeEntries(objectFormat ObjectFormat, ptree *Tree, rd BufferedReader, sz int64) ([]*TreeEntry, error) {
entries := make([]*TreeEntry, 0, 10) entries := make([]*TreeEntry, 0, 10)
+5 -5
View File
@@ -17,21 +17,21 @@ import (
) )
// CommitNodeIndex returns the index for walking commit graph // CommitNodeIndex returns the index for walking commit graph
func (r *Repository) CommitNodeIndex() (cgobject.CommitNodeIndex, *os.File) { func (repo *Repository) CommitNodeIndex() (cgobject.CommitNodeIndex, *os.File) {
indexPath := filepath.Join(r.Path, "objects", "info", "commit-graph") indexPath := filepath.Join(repo.Path, "objects", "info", "commit-graph")
file, err := os.Open(indexPath) file, err := os.Open(indexPath)
if err == nil { if err == nil {
var index commitgraph.Index var index commitgraph.Index
index, err = commitgraph.OpenFileIndex(file) index, err = commitgraph.OpenFileIndex(file)
if err == nil { if err == nil {
return cgobject.NewGraphCommitNodeIndex(index, r.gogitRepo.Storer), file return cgobject.NewGraphCommitNodeIndex(index, repo.gogitRepo.Storer), file
} }
} }
if !os.IsNotExist(err) { 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
} }
+2 -2
View File
@@ -21,7 +21,7 @@ func TestEntryGogit(t *testing.T) {
EntryModeTree: filemode.Dir, EntryModeTree: filemode.Dir,
} }
for emode, fmode := range cases { for emode, fmode := range cases {
assert.EqualValues(t, fmode, entryModeToGogitFileMode(emode)) assert.Equal(t, fmode, entryModeToGogitFileMode(emode))
assert.EqualValues(t, emode, gogitFileModeToEntryMode(fmode)) assert.Equal(t, emode, gogitFileModeToEntryMode(fmode))
} }
} }
+6 -3
View File
@@ -92,9 +92,12 @@ func main() {
_, _ = fmt.Fprintln(os.Stdout, "lint go header ...") _, _ = fmt.Fprintln(os.Stdout, "lint go header ...")
succeed := lintGoHeader() succeed := lintGoHeader()
_, _ = fmt.Fprintln(os.Stdout, "lint for linux ...") _, _ = fmt.Fprintln(os.Stdout, "lint for linux ...")
succeed = runCmd([]string{"GOOS=linux", "TAGS=bindata"}, "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
_, _ = fmt.Fprintln(os.Stdout, "lint for windows ...") if os.Getenv("CI") != "" {
succeed = runCmd([]string{"GOOS=windows", "TAGS=gogit"}, "golangci-lint", append([]string{"run"}, os.Args[1:]...)) && succeed // 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 { if !succeed {
os.Exit(1) os.Exit(1)
} }