Skip to content

Commit a19447a

Browse files
authored
migrate from com.* to alternatives (#14103)
* remove github.com/unknwon/com from models * dont use "com.ToStr()" * replace "com.ToStr" with "fmt.Sprint" where its easy to do * more refactor * fix test * just "proxy" Copy func for now * as per @lunny
1 parent 04ae0f2 commit a19447a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+231
-221
lines changed

cmd/serv.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626

2727
"github.com/dgrijalva/jwt-go"
2828
"github.com/kballard/go-shellquote"
29-
"github.com/unknwon/com"
3029
"github.com/urfave/cli"
3130
)
3231

@@ -105,7 +104,10 @@ func runServ(c *cli.Context) error {
105104
if len(keys) != 2 || keys[0] != "key" {
106105
fail("Key ID format error", "Invalid key argument: %s", c.Args()[0])
107106
}
108-
keyID := com.StrTo(keys[1]).MustInt64()
107+
keyID, err := strconv.ParseInt(keys[1], 10, 64)
108+
if err != nil {
109+
fail("Key ID format error", "Invalid key argument: %s", c.Args()[1])
110+
}
109111

110112
cmd := os.Getenv("SSH_ORIGINAL_COMMAND")
111113
if len(cmd) == 0 {

contrib/pr/checkout.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"github.com/go-git/go-git/v5/config"
3838
"github.com/go-git/go-git/v5/plumbing"
3939
context2 "github.com/gorilla/context"
40-
"github.com/unknwon/com"
4140
"xorm.io/xorm"
4241
)
4342

@@ -111,7 +110,7 @@ func runPR() {
111110
models.LoadFixtures()
112111
util.RemoveAll(setting.RepoRootPath)
113112
util.RemoveAll(models.LocalCopyPath())
114-
com.CopyDir(path.Join(curDir, "integrations/gitea-repositories-meta"), setting.RepoRootPath)
113+
util.CopyDir(path.Join(curDir, "integrations/gitea-repositories-meta"), setting.RepoRootPath)
115114

116115
log.Printf("[PR] Setting up router\n")
117116
//routers.GlobalInit()

integrations/git_helper_for_declarative_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"code.gitea.io/gitea/modules/util"
2525

2626
"github.com/stretchr/testify/assert"
27-
"github.com/unknwon/com"
2827
)
2928

3029
func withKeyFile(t *testing.T, keyname string, callback func(string)) {
@@ -112,7 +111,9 @@ func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bo
112111
func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
113112
return func(t *testing.T) {
114113
assert.NoError(t, git.CloneWithArgs(context.Background(), u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{}))
115-
assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
114+
exist, err := util.IsExist(filepath.Join(dstLocalPath, "README.md"))
115+
assert.NoError(t, err)
116+
assert.True(t, exist)
116117
}
117118
}
118119

@@ -122,7 +123,9 @@ func doGitCloneFail(u *url.URL) func(*testing.T) {
122123
assert.NoError(t, err)
123124
defer util.RemoveAll(tmpDir)
124125
assert.Error(t, git.Clone(u.String(), tmpDir, git.CloneRepoOptions{}))
125-
assert.False(t, com.IsExist(filepath.Join(tmpDir, "README.md")))
126+
exist, err := util.IsExist(filepath.Join(tmpDir, "README.md"))
127+
assert.NoError(t, err)
128+
assert.False(t, exist)
126129
}
127130
}
128131

integrations/integration_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"github.com/PuerkitoBio/goquery"
3838
"github.com/go-chi/chi"
3939
"github.com/stretchr/testify/assert"
40-
"github.com/unknwon/com"
4140
)
4241

4342
var c chi.Router
@@ -231,8 +230,7 @@ func prepareTestEnv(t testing.TB, skip ...int) func() {
231230
assert.NoError(t, models.LoadFixtures())
232231
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
233232

234-
assert.NoError(t, com.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"),
235-
setting.RepoRootPath))
233+
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
236234
return deferFn
237235
}
238236

@@ -473,6 +471,5 @@ func resetFixtures(t *testing.T) {
473471
assert.NoError(t, queue.GetManager().FlushAll(context.Background(), -1))
474472
assert.NoError(t, models.LoadFixtures())
475473
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
476-
assert.NoError(t, com.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"),
477-
setting.RepoRootPath))
474+
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
478475
}

integrations/migration-test/migration_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"code.gitea.io/gitea/modules/util"
2828

2929
"github.com/stretchr/testify/assert"
30-
"github.com/unknwon/com"
3130
"xorm.io/xorm"
3231
)
3332

@@ -60,7 +59,7 @@ func initMigrationTest(t *testing.T) func() {
6059

6160
assert.True(t, len(setting.RepoRootPath) != 0)
6261
assert.NoError(t, util.RemoveAll(setting.RepoRootPath))
63-
assert.NoError(t, com.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
62+
assert.NoError(t, util.CopyDir(path.Join(filepath.Dir(setting.AppPath), "integrations/gitea-repositories-meta"), setting.RepoRootPath))
6463

6564
setting.CheckLFSVersion()
6665
setting.InitDBConfig()

models/action.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"code.gitea.io/gitea/modules/setting"
1919
"code.gitea.io/gitea/modules/timeutil"
2020

21-
"github.com/unknwon/com"
2221
"xorm.io/builder"
2322
)
2423

@@ -266,7 +265,7 @@ func (a *Action) GetIssueInfos() []string {
266265
// GetIssueTitle returns the title of first issue associated
267266
// with the action.
268267
func (a *Action) GetIssueTitle() string {
269-
index := com.StrTo(a.GetIssueInfos()[0]).MustInt64()
268+
index, _ := strconv.ParseInt(a.GetIssueInfos()[0], 10, 64)
270269
issue, err := GetIssueByIndex(a.RepoID, index)
271270
if err != nil {
272271
log.Error("GetIssueByIndex: %v", err)
@@ -278,7 +277,7 @@ func (a *Action) GetIssueTitle() string {
278277
// GetIssueContent returns the content of first issue associated with
279278
// this action.
280279
func (a *Action) GetIssueContent() string {
281-
index := com.StrTo(a.GetIssueInfos()[0]).MustInt64()
280+
index, _ := strconv.ParseInt(a.GetIssueInfos()[0], 10, 64)
282281
issue, err := GetIssueByIndex(a.RepoID, index)
283282
if err != nil {
284283
log.Error("GetIssueByIndex: %v", err)

models/admin.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"code.gitea.io/gitea/modules/storage"
1313
"code.gitea.io/gitea/modules/timeutil"
1414
"code.gitea.io/gitea/modules/util"
15-
16-
"github.com/unknwon/com"
1715
)
1816

1917
//NoticeType describes the notice type
@@ -36,7 +34,7 @@ type Notice struct {
3634

3735
// TrStr returns a translation format string.
3836
func (n *Notice) TrStr() string {
39-
return "admin.notices.type_" + com.ToStr(n.Type)
37+
return fmt.Sprintf("admin.notices.type_%d", n.Type)
4038
}
4139

4240
// CreateNotice creates new system notice.

models/branches.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"code.gitea.io/gitea/modules/util"
1717

1818
"github.com/gobwas/glob"
19-
"github.com/unknwon/com"
2019
)
2120

2221
// ProtectedBranch struct
@@ -483,7 +482,7 @@ func updateTeamWhitelist(repo *Repository, currentWhitelist, newWhitelist []int6
483482

484483
whitelist = make([]int64, 0, len(teams))
485484
for i := range teams {
486-
if com.IsSliceContainsInt64(newWhitelist, teams[i].ID) {
485+
if util.IsInt64InSlice(teams[i].ID, newWhitelist) {
487486
whitelist = append(whitelist, teams[i].ID)
488487
}
489488
}

models/issue.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"code.gitea.io/gitea/modules/timeutil"
2121
"code.gitea.io/gitea/modules/util"
2222

23-
"github.com/unknwon/com"
2423
"xorm.io/builder"
2524
"xorm.io/xorm"
2625
)
@@ -386,7 +385,7 @@ func (issue *Issue) State() api.StateType {
386385

387386
// HashTag returns unique hash tag for issue.
388387
func (issue *Issue) HashTag() string {
389-
return "issue-" + com.ToStr(issue.ID)
388+
return fmt.Sprintf("issue-%d", issue.ID)
390389
}
391390

392391
// IsPoster returns true if given user by ID is the poster.
@@ -1374,7 +1373,8 @@ func parseCountResult(results []map[string][]byte) int64 {
13741373
return 0
13751374
}
13761375
for _, result := range results[0] {
1377-
return com.StrTo(string(result)).MustInt64()
1376+
c, _ := strconv.ParseInt(string(result), 10, 64)
1377+
return c
13781378
}
13791379
return 0
13801380
}

models/issue_comment.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"code.gitea.io/gitea/modules/structs"
2323
"code.gitea.io/gitea/modules/timeutil"
2424

25-
"github.com/unknwon/com"
2625
"xorm.io/builder"
2726
"xorm.io/xorm"
2827
)
@@ -367,7 +366,7 @@ func (c *Comment) HashTag() string {
367366

368367
// EventTag returns unique event hash tag for comment.
369368
func (c *Comment) EventTag() string {
370-
return "event-" + com.ToStr(c.ID)
369+
return fmt.Sprintf("event-%d", c.ID)
371370
}
372371

373372
// LoadLabel if comment.Type is CommentTypeLabel, then load Label

models/issue_xref.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"code.gitea.io/gitea/modules/log"
1111
"code.gitea.io/gitea/modules/references"
1212

13-
"github.com/unknwon/com"
1413
"xorm.io/xorm"
1514
)
1615

@@ -324,7 +323,7 @@ func (comment *Comment) RefIssueIdent() string {
324323
return ""
325324
}
326325
// FIXME: check this name for cross-repository references (#7901 if it gets merged)
327-
return "#" + com.ToStr(comment.RefIssue.Index)
326+
return fmt.Sprintf("#%d", comment.RefIssue.Index)
328327
}
329328

330329
// __________ .__ .__ __________ __

models/login_source.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"fmt"
1313
"net/smtp"
1414
"net/textproto"
15+
"strconv"
1516
"strings"
1617

1718
"code.gitea.io/gitea/modules/auth/ldap"
@@ -20,8 +21,8 @@ import (
2021
"code.gitea.io/gitea/modules/log"
2122
"code.gitea.io/gitea/modules/setting"
2223
"code.gitea.io/gitea/modules/timeutil"
24+
"code.gitea.io/gitea/modules/util"
2325

24-
"github.com/unknwon/com"
2526
"xorm.io/xorm"
2627
"xorm.io/xorm/convert"
2728
)
@@ -180,7 +181,9 @@ func Cell2Int64(val xorm.Cell) int64 {
180181
switch (*val).(type) {
181182
case []uint8:
182183
log.Trace("Cell2Int64 ([]uint8): %v", *val)
183-
return com.StrTo(string((*val).([]uint8))).MustInt64()
184+
185+
v, _ := strconv.ParseInt(string((*val).([]uint8)), 10, 64)
186+
return v
184187
}
185188
return (*val).(int64)
186189
}
@@ -200,7 +203,7 @@ func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) {
200203
case LoginSSPI:
201204
source.Cfg = new(SSPIConfig)
202205
default:
203-
panic("unrecognized login source type: " + com.ToStr(*val))
206+
panic(fmt.Sprintf("unrecognized login source type: %v", *val))
204207
}
205208
}
206209
}
@@ -610,7 +613,7 @@ func LoginViaSMTP(user *User, login, password string, sourceID int64, cfg *SMTPC
610613
idx := strings.Index(login, "@")
611614
if idx == -1 {
612615
return nil, ErrUserNotExist{0, login, 0}
613-
} else if !com.IsSliceContainsStr(strings.Split(cfg.AllowedDomains, ","), login[idx+1:]) {
616+
} else if !util.IsStringInSlice(login[idx+1:], strings.Split(cfg.AllowedDomains, ","), true) {
614617
return nil, ErrUserNotExist{0, login, 0}
615618
}
616619
}

models/oauth2_application.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import (
1414
"code.gitea.io/gitea/modules/secret"
1515
"code.gitea.io/gitea/modules/setting"
1616
"code.gitea.io/gitea/modules/timeutil"
17+
"code.gitea.io/gitea/modules/util"
1718

1819
"github.com/dgrijalva/jwt-go"
1920
uuid "github.com/google/uuid"
20-
"github.com/unknwon/com"
2121
"golang.org/x/crypto/bcrypt"
2222
"xorm.io/xorm"
2323
)
@@ -62,7 +62,7 @@ func (app *OAuth2Application) LoadUser() (err error) {
6262

6363
// ContainsRedirectURI checks if redirectURI is allowed for app
6464
func (app *OAuth2Application) ContainsRedirectURI(redirectURI string) bool {
65-
return com.IsSliceContainsStr(app.RedirectURIs, redirectURI)
65+
return util.IsStringInSlice(redirectURI, app.RedirectURIs, true)
6666
}
6767

6868
// GenerateClientSecret will generate the client secret and returns the plaintext and saves the hash at the database

models/repo.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"code.gitea.io/gitea/modules/timeutil"
3535
"code.gitea.io/gitea/modules/util"
3636

37-
"github.com/unknwon/com"
3837
"xorm.io/builder"
3938
)
4039

@@ -85,7 +84,7 @@ func loadRepoConfig() {
8584
}
8685

8786
for _, f := range customFiles {
88-
if !com.IsSliceContainsStr(files, f) {
87+
if !util.IsStringInSlice(f, files, true) {
8988
files = append(files, f)
9089
}
9190
}
@@ -115,12 +114,12 @@ func loadRepoConfig() {
115114
// Filter out invalid names and promote preferred licenses.
116115
sortedLicenses := make([]string, 0, len(Licenses))
117116
for _, name := range setting.Repository.PreferredLicenses {
118-
if com.IsSliceContainsStr(Licenses, name) {
117+
if util.IsStringInSlice(name, Licenses, true) {
119118
sortedLicenses = append(sortedLicenses, name)
120119
}
121120
}
122121
for _, name := range Licenses {
123-
if !com.IsSliceContainsStr(setting.Repository.PreferredLicenses, name) {
122+
if !util.IsStringInSlice(name, setting.Repository.PreferredLicenses, true) {
124123
sortedLicenses = append(sortedLicenses, name)
125124
}
126125
}
@@ -1933,7 +1932,7 @@ func repoStatsCheck(ctx context.Context, checker *repoChecker) {
19331932
return
19341933
}
19351934
for _, result := range results {
1936-
id := com.StrTo(result["id"]).MustInt64()
1935+
id, _ := strconv.ParseInt(string(result["id"]), 10, 64)
19371936
select {
19381937
case <-ctx.Done():
19391938
log.Warn("CheckRepoStats: Cancelled before checking %s for Repo[%d]", checker.desc, id)
@@ -2001,7 +2000,7 @@ func CheckRepoStats(ctx context.Context) error {
20012000
log.Error("Select %s: %v", desc, err)
20022001
} else {
20032002
for _, result := range results {
2004-
id := com.StrTo(result["id"]).MustInt64()
2003+
id, _ := strconv.ParseInt(string(result["id"]), 10, 64)
20052004
select {
20062005
case <-ctx.Done():
20072006
log.Warn("CheckRepoStats: Cancelled during %s for repo ID %d", desc, id)
@@ -2024,7 +2023,7 @@ func CheckRepoStats(ctx context.Context) error {
20242023
log.Error("Select %s: %v", desc, err)
20252024
} else {
20262025
for _, result := range results {
2027-
id := com.StrTo(result["id"]).MustInt64()
2026+
id, _ := strconv.ParseInt(string(result["id"]), 10, 64)
20282027
select {
20292028
case <-ctx.Done():
20302029
log.Warn("CheckRepoStats: Cancelled")
@@ -2047,7 +2046,7 @@ func CheckRepoStats(ctx context.Context) error {
20472046
log.Error("Select repository count 'num_forks': %v", err)
20482047
} else {
20492048
for _, result := range results {
2050-
id := com.StrTo(result["id"]).MustInt64()
2049+
id, _ := strconv.ParseInt(string(result["id"]), 10, 64)
20512050
select {
20522051
case <-ctx.Done():
20532052
log.Warn("CheckRepoStats: Cancelled")

models/repo_unit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ package models
66

77
import (
88
"encoding/json"
9+
"fmt"
910

1011
"code.gitea.io/gitea/modules/timeutil"
1112

12-
"github.com/unknwon/com"
1313
"xorm.io/xorm"
1414
"xorm.io/xorm/convert"
1515
)
@@ -147,7 +147,7 @@ func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
147147
case UnitTypeIssues:
148148
r.Config = new(IssuesConfig)
149149
default:
150-
panic("unrecognized repo unit type: " + com.ToStr(*val))
150+
panic(fmt.Sprintf("unrecognized repo unit type: %v", *val))
151151
}
152152
}
153153
}

0 commit comments

Comments
 (0)