Skip to content

Commit

Permalink
*: enable gci (pingcap#35823)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkingrei authored Jul 4, 2022
1 parent aec4349 commit e50b943
Show file tree
Hide file tree
Showing 76 changed files with 268 additions and 222 deletions.
16 changes: 16 additions & 0 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,14 @@ def go_deps():
sum = "h1:0rkFMAbn5KBKNpJyHQ6Prb95vIKanmAe62KxsrN+sqA=",
version = "v0.0.0-20171016134553-529a34b1c186",
)
go_repository(
name = "com_github_daixiang0_gci",
build_file_proto_mode = "disable",
importpath = "github.com/daixiang0/gci",
sum = "h1:+EZ83znNs73C9ZBTM7xhNagMP6gJs5wlptiFiuce5BM=",
version = "v0.3.4",
)

go_repository(
name = "com_github_danjacques_gofslock",
build_file_proto_mode = "disable_global",
Expand Down Expand Up @@ -1250,6 +1258,14 @@ def go_deps():
sum = "h1:5IcZpTvzydCQeHzK4Ef/D5rrSqwxob0t8PQPMybUNFM=",
version = "v1.1.2",
)
go_repository(
name = "com_github_hexops_gotextdiff",
build_file_proto_mode = "disable",
importpath = "github.com/hexops/gotextdiff",
sum = "h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=",
version = "v1.0.3",
)

go_repository(
name = "com_github_hpcloud_tail",
build_file_proto_mode = "disable_global",
Expand Down
7 changes: 3 additions & 4 deletions br/pkg/lightning/backend/kv/sql2kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ import (
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/parser/mysql" //nolint: goimports
// Import tidb/planner/core to initialize expression.RewriteAstExpr
_ "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/table/tables"
Expand All @@ -42,9 +44,6 @@ import (
"github.com/pingcap/tidb/util/chunk"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

// Import tidb/planner/core to initialize expression.RewriteAstExpr
_ "github.com/pingcap/tidb/planner/core"
)

var ExtraHandleColumnInfo = model.NewExtraHandleColInfo()
Expand Down
3 changes: 1 addition & 2 deletions br/pkg/lightning/backend/local/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ import (
"github.com/cockroachdb/pebble"
"github.com/cockroachdb/pebble/sstable"
"github.com/google/uuid"
"github.com/pingcap/tidb/br/pkg/lightning/backend"
"github.com/pingcap/tidb/br/pkg/lightning/log"
"github.com/stretchr/testify/require"

"github.com/pingcap/tidb/br/pkg/lightning/backend"
)

func TestIngestSSTWithClosedEngine(t *testing.T) {
Expand Down
7 changes: 3 additions & 4 deletions br/pkg/lightning/errormanager/errormanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ import (
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/pingcap/errors"
"go.uber.org/multierr"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"

"github.com/pingcap/tidb/br/pkg/lightning/common"
"github.com/pingcap/tidb/br/pkg/lightning/config"
"github.com/pingcap/tidb/br/pkg/lightning/log"
"github.com/pingcap/tidb/br/pkg/redact"
"github.com/pingcap/tidb/br/pkg/utils"
"go.uber.org/multierr"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
)

const (
Expand Down
5 changes: 2 additions & 3 deletions br/pkg/lightning/errormanager/errormanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ import (
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/pingcap/tidb/br/pkg/lightning/config"
"github.com/pingcap/tidb/br/pkg/lightning/log"
"github.com/pingcap/tidb/br/pkg/utils"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"

"github.com/pingcap/tidb/br/pkg/lightning/config"
"github.com/pingcap/tidb/br/pkg/utils"
)

func TestInit(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions br/pkg/lightning/mydump/charset_convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ import (
"unicode/utf8"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/br/pkg/lightning/config"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/simplifiedchinese"

"github.com/pingcap/tidb/br/pkg/lightning/config"
)

// CharsetConvertor is used to convert a character set to utf8mb4 encoding.
Expand Down
5 changes: 2 additions & 3 deletions br/pkg/lightning/mydump/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
"strings"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/br/pkg/lightning/config"
"github.com/pingcap/tidb/br/pkg/lightning/log"
"github.com/pingcap/tidb/util/filter"
"github.com/pingcap/tidb/util/slice"
"go.uber.org/zap"

"github.com/pingcap/tidb/br/pkg/lightning/config"
"github.com/pingcap/tidb/br/pkg/lightning/log"
)

type SourceType int
Expand Down
7 changes: 3 additions & 4 deletions br/pkg/lightning/restore/restore_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ import (
"github.com/DATA-DOG/go-sqlmock"
"github.com/golang/mock/gomock"
"github.com/pingcap/errors"
filter "github.com/pingcap/tidb/util/table-filter"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/pingcap/tidb/br/pkg/lightning/backend"
"github.com/pingcap/tidb/br/pkg/lightning/checkpoints"
"github.com/pingcap/tidb/br/pkg/lightning/config"
Expand All @@ -39,6 +35,9 @@ import (
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
tmock "github.com/pingcap/tidb/util/mock"
filter "github.com/pingcap/tidb/util/table-filter"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

type restoreSchemaSuite struct {
Expand Down
3 changes: 1 addition & 2 deletions br/pkg/restore/rawkv_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import (
berrors "github.com/pingcap/tidb/br/pkg/errors"
"github.com/pingcap/tidb/br/pkg/restore"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/util/codec"
"github.com/stretchr/testify/require"
"github.com/tikv/client-go/v2/rawkv"

"github.com/pingcap/tidb/util/codec"
)

// fakeRawkvClient is a mock for rawkv.client
Expand Down
5 changes: 2 additions & 3 deletions br/pkg/storage/azblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ import (
"path"
"strings"

"github.com/google/uuid"
"github.com/spf13/pflag"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/google/uuid"
"github.com/pingcap/errors"
backuppb "github.com/pingcap/kvproto/pkg/brpb"
"github.com/pingcap/log"
berrors "github.com/pingcap/tidb/br/pkg/errors"
"github.com/spf13/pflag"
"go.uber.org/zap"
)

Expand Down
1 change: 0 additions & 1 deletion br/pkg/storage/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"

"github.com/pingcap/errors"

berrors "github.com/pingcap/tidb/br/pkg/errors"
)

Expand Down
5 changes: 2 additions & 3 deletions br/pkg/storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ import (
"github.com/pingcap/errors"
backuppb "github.com/pingcap/kvproto/pkg/brpb"
"github.com/pingcap/log"
"github.com/spf13/pflag"
"go.uber.org/zap"

berrors "github.com/pingcap/tidb/br/pkg/errors"
"github.com/pingcap/tidb/br/pkg/logutil"
"github.com/spf13/pflag"
"go.uber.org/zap"
)

const (
Expand Down
5 changes: 2 additions & 3 deletions br/pkg/stream/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import (

"github.com/gogo/protobuf/proto"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"go.uber.org/zap"

backuppb "github.com/pingcap/kvproto/pkg/brpb"
"github.com/pingcap/log"
berrors "github.com/pingcap/tidb/br/pkg/errors"
"github.com/pingcap/tidb/br/pkg/redact"
"github.com/pingcap/tidb/kv"
clientv3 "go.etcd.io/etcd/client/v3"
"go.uber.org/zap"
)

// MetaDataClient is the client for operations over metadata.
Expand Down
3 changes: 1 addition & 2 deletions br/pkg/stream/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import (
"net/url"
"testing"

berrors "github.com/pingcap/tidb/br/pkg/errors"

"github.com/pingcap/errors"
"github.com/pingcap/log"
berrors "github.com/pingcap/tidb/br/pkg/errors"
"github.com/pingcap/tidb/br/pkg/logutil"
"github.com/pingcap/tidb/br/pkg/storage"
"github.com/pingcap/tidb/br/pkg/stream"
Expand Down
1 change: 0 additions & 1 deletion br/pkg/task/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

"github.com/pingcap/errors"
backuppb "github.com/pingcap/kvproto/pkg/brpb"

"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/tidb/br/pkg/conn"
"github.com/pingcap/tidb/br/pkg/pdutil"
Expand Down
1 change: 1 addition & 0 deletions build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ nogo(
"//build/linter/durationcheck:durationcheck",
"//build/linter/exportloopref:exportloopref",
"//build/linter/gofmt:gofmt",
"//build/linter/gci:gci",
"//build/linter/ineffassign:ineffassign",
"//build/linter/misspell:misspell",
"//build/linter/prealloc:prealloc",
Expand Down
13 changes: 13 additions & 0 deletions build/linter/gci/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "gci",
srcs = ["analysis.go"],
importpath = "github.com/pingcap/tidb/build/linter/gci",
visibility = ["//visibility:public"],
deps = [
"@com_github_daixiang0_gci//pkg/configuration",
"@com_github_daixiang0_gci//pkg/gci",
"@org_golang_x_tools//go/analysis",
],
)
65 changes: 65 additions & 0 deletions build/linter/gci/analysis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package gci

import (
"fmt"
"sync"

"github.com/daixiang0/gci/pkg/configuration"
"github.com/daixiang0/gci/pkg/gci"
"golang.org/x/tools/go/analysis"
)

// Analyzer is the analyzer struct of gci.
var Analyzer = &analysis.Analyzer{
Name: "gci",
Doc: "Gci controls golang package import order and makes it always deterministic.",
Run: run,
}

func run(pass *analysis.Pass) (any, error) {
fileNames := make([]string, 0, len(pass.Files))
for _, f := range pass.Files {
pos := pass.Fset.PositionFor(f.Pos(), false)
fileNames = append(fileNames, pos.Filename)
}
var rawCfg gci.GciStringConfiguration
rawCfg.Cfg = configuration.FormatterConfiguration{
NoInlineComments: false,
NoPrefixComments: false,
Debug: false,
}
cfg, _ := rawCfg.Parse()
var diffs []string
var lock sync.Mutex
err := gci.DiffFormattedFilesToArray(fileNames, *cfg, &diffs, &lock)
if err != nil {
return nil, err
}

for _, diff := range diffs {
if diff == "" {
continue
}

pass.Report(analysis.Diagnostic{
Pos: 1,
Message: fmt.Sprintf("\n%s", diff),
})
}

return nil, nil
}
12 changes: 12 additions & 0 deletions build/nogo_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@
".*failpoint_binding__.go$": "ignore generated code"
}
},
"gci": {
"exclude_files": {
"/external/": "no need to vet third party code",
".*_generated\\.go$": "ignore generated code",
"/cgo/": "ignore cgo code",
".*\\.pb\\.go$": "generated code",
"/rules_go_work-*": "ignore generated code",
".*test_/testmain\\.go$": "ignore generated code",
".*failpoint_binding__.go$": "ignore generated code",
"util/printer/printer.go": "ignore util/printer code"
}
},
"httpresponse": {
"exclude_files": {
"/external/": "no need to vet third party code",
Expand Down
1 change: 0 additions & 1 deletion cmd/benchraw/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/pingcap/tidb/parser/terror"
"github.com/tikv/client-go/v2/config"
"github.com/tikv/client-go/v2/rawkv"

"go.uber.org/zap"
)

Expand Down
5 changes: 2 additions & 3 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/ddl/util"
ddlutil "github.com/pingcap/tidb/ddl/util"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/errno"
"github.com/pingcap/tidb/infoschema"
Expand Down Expand Up @@ -726,7 +725,7 @@ func TestCancelJobByErrorCountLimit(t *testing.T) {

limit := variable.GetDDLErrorCountLimit()
tk.MustExec("set @@global.tidb_ddl_error_count_limit = 16")
err := ddlutil.LoadDDLVars(tk.Session())
err := util.LoadDDLVars(tk.Session())
require.NoError(t, err)
defer tk.MustExec(fmt.Sprintf("set @@global.tidb_ddl_error_count_limit = %d", limit))

Expand All @@ -744,7 +743,7 @@ func TestTruncateTableUpdateSchemaVersionErr(t *testing.T) {

limit := variable.GetDDLErrorCountLimit()
tk.MustExec("set @@global.tidb_ddl_error_count_limit = 5")
err := ddlutil.LoadDDLVars(tk.Session())
err := util.LoadDDLVars(tk.Session())
require.NoError(t, err)
defer tk.MustExec(fmt.Sprintf("set @@global.tidb_ddl_error_count_limit = %d", limit))

Expand Down
4 changes: 1 addition & 3 deletions ddl/table_split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ import (
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/testkit"

"github.com/tikv/client-go/v2/tikv"

"github.com/stretchr/testify/require"
"github.com/tikv/client-go/v2/tikv"
)

func TestTableSplit(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion distsql/select_result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/pingcap/tidb/util/execdetails"
"github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tipb/go-tipb"

"github.com/stretchr/testify/require"
)

Expand Down
Loading

0 comments on commit e50b943

Please sign in to comment.