Skip to content

Commit 1f35435

Browse files
authored
1 parent 71e4740 commit 1f35435

File tree

190 files changed

+369
-696
lines changed

Some content is hidden

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

190 files changed

+369
-696
lines changed

cmd/cert.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ func runCert(_ context.Context, c *cli.Command) error {
156156
BasicConstraintsValid: true,
157157
}
158158

159-
hosts := strings.Split(c.String("host"), ",")
160-
for _, h := range hosts {
159+
hosts := strings.SplitSeq(c.String("host"), ",")
160+
for h := range hosts {
161161
if ip := net.ParseIP(h); ip != nil {
162162
template.IPAddresses = append(template.IPAddresses, ip)
163163
} else {

cmd/dump_repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@ func runDumpRepository(ctx context.Context, cmd *cli.Command) error {
137137
opts.PullRequests = true
138138
opts.ReleaseAssets = true
139139
} else {
140-
units := strings.Split(cmd.String("units"), ",")
141-
for _, unit := range units {
140+
units := strings.SplitSeq(cmd.String("units"), ",")
141+
for unit := range units {
142142
switch strings.ToLower(strings.TrimSpace(unit)) {
143143
case "":
144144
continue

cmd/hook.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ func hookPrintResult(output, isCreate bool, branch, url string) {
480480
func pushOptions() map[string]string {
481481
opts := make(map[string]string)
482482
if pushCount, err := strconv.Atoi(os.Getenv(private.GitPushOptionCount)); err == nil {
483-
for idx := 0; idx < pushCount; idx++ {
483+
for idx := range pushCount {
484484
opt := os.Getenv(fmt.Sprintf("GIT_PUSH_OPTION_%d", idx))
485485
kv := strings.SplitN(opt, "=", 2)
486486
if len(kv) == 2 {
@@ -732,7 +732,7 @@ func readPktLine(ctx context.Context, in *bufio.Reader, requestType pktLineType)
732732

733733
// read prefix
734734
lengthBytes := make([]byte, 4)
735-
for i := 0; i < 4; i++ {
735+
for i := range 4 {
736736
lengthBytes[i], err = in.ReadByte()
737737
if err != nil {
738738
return nil, fail(ctx, "Protocol: stdin error", "Pkt-Line: read stdin failed : %v", err)

contrib/backport/backport.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ func determineRemote(ctx context.Context, forkUser string) (string, string, erro
337337
fmt.Fprintf(os.Stderr, "Unable to list git remotes:\n%s\n", string(out))
338338
return "", "", fmt.Errorf("unable to determine forked remote: %w", err)
339339
}
340-
lines := strings.Split(string(out), "\n")
341-
for _, line := range lines {
340+
lines := strings.SplitSeq(string(out), "\n")
341+
for line := range lines {
342342
fields := strings.Split(line, "\t")
343343
name, remote := fields[0], fields[1]
344344
// only look at pushers
@@ -356,12 +356,12 @@ func determineRemote(ctx context.Context, forkUser string) (string, string, erro
356356
if !strings.Contains(remote, forkUser) {
357357
continue
358358
}
359-
if strings.HasPrefix(remote, "[email protected]:") {
360-
forkUser = strings.TrimPrefix(remote, "[email protected]:")
361-
} else if strings.HasPrefix(remote, "https://github.com/") {
362-
forkUser = strings.TrimPrefix(remote, "https://github.com/")
363-
} else if strings.HasPrefix(remote, "https://www.github.com/") {
364-
forkUser = strings.TrimPrefix(remote, "https://www.github.com/")
359+
if after, ok := strings.CutPrefix(remote, "[email protected]:"); ok {
360+
forkUser = after
361+
} else if after, ok := strings.CutPrefix(remote, "https://github.com/"); ok {
362+
forkUser = after
363+
} else if after, ok := strings.CutPrefix(remote, "https://www.github.com/"); ok {
364+
forkUser = after
365365
} else if forkUser == "" {
366366
return "", "", fmt.Errorf("unable to extract forkUser from remote %s: %s", name, remote)
367367
}

models/actions/status.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package actions
55

66
import (
7+
"slices"
8+
79
"code.gitea.io/gitea/modules/translation"
810

911
runnerv1 "code.gitea.io/actions-proto-go/runner/v1"
@@ -88,12 +90,7 @@ func (s Status) IsBlocked() bool {
8890

8991
// In returns whether s is one of the given statuses
9092
func (s Status) In(statuses ...Status) bool {
91-
for _, v := range statuses {
92-
if s == v {
93-
return true
94-
}
95-
}
96-
return false
93+
return slices.Contains(statuses, s)
9794
}
9895

9996
func (s Status) AsResult() runnerv1.Result {

models/activities/action.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"net/url"
1111
"path"
12+
"slices"
1213
"strconv"
1314
"strings"
1415
"time"
@@ -125,12 +126,7 @@ func (at ActionType) String() string {
125126
}
126127

127128
func (at ActionType) InActions(actions ...string) bool {
128-
for _, action := range actions {
129-
if action == at.String() {
130-
return true
131-
}
132-
}
133-
return false
129+
return slices.Contains(actions, at.String())
134130
}
135131

136132
// Action represents user operation type and other information to

models/activities/notification_list.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,7 @@ func (nl NotificationList) LoadRepos(ctx context.Context) (repo_model.Repository
208208
repos := make(map[int64]*repo_model.Repository, len(repoIDs))
209209
left := len(repoIDs)
210210
for left > 0 {
211-
limit := db.DefaultMaxInSize
212-
if left < limit {
213-
limit = left
214-
}
211+
limit := min(left, db.DefaultMaxInSize)
215212
rows, err := db.GetEngine(ctx).
216213
In("id", repoIDs[:limit]).
217214
Rows(new(repo_model.Repository))
@@ -282,10 +279,7 @@ func (nl NotificationList) LoadIssues(ctx context.Context) ([]int, error) {
282279
issues := make(map[int64]*issues_model.Issue, len(issueIDs))
283280
left := len(issueIDs)
284281
for left > 0 {
285-
limit := db.DefaultMaxInSize
286-
if left < limit {
287-
limit = left
288-
}
282+
limit := min(left, db.DefaultMaxInSize)
289283
rows, err := db.GetEngine(ctx).
290284
In("id", issueIDs[:limit]).
291285
Rows(new(issues_model.Issue))
@@ -377,10 +371,7 @@ func (nl NotificationList) LoadUsers(ctx context.Context) ([]int, error) {
377371
users := make(map[int64]*user_model.User, len(userIDs))
378372
left := len(userIDs)
379373
for left > 0 {
380-
limit := db.DefaultMaxInSize
381-
if left < limit {
382-
limit = left
383-
}
374+
limit := min(left, db.DefaultMaxInSize)
384375
rows, err := db.GetEngine(ctx).
385376
In("id", userIDs[:limit]).
386377
Rows(new(user_model.User))
@@ -428,10 +419,7 @@ func (nl NotificationList) LoadComments(ctx context.Context) ([]int, error) {
428419
comments := make(map[int64]*issues_model.Comment, len(commentIDs))
429420
left := len(commentIDs)
430421
for left > 0 {
431-
limit := db.DefaultMaxInSize
432-
if left < limit {
433-
limit = left
434-
}
422+
limit := min(left, db.DefaultMaxInSize)
435423
rows, err := db.GetEngine(ctx).
436424
In("id", commentIDs[:limit]).
437425
Rows(new(issues_model.Comment))

models/activities/repo_activity.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,7 @@ func GetActivityStatsTopAuthors(ctx context.Context, repo *repo_model.Repository
139139
return v[i].Commits > v[j].Commits
140140
})
141141

142-
cnt := count
143-
if cnt > len(v) {
144-
cnt = len(v)
145-
}
142+
cnt := min(count, len(v))
146143

147144
return v[:cnt], nil
148145
}

models/auth/access_token_scope.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,7 @@ func GetRequiredScopes(level AccessTokenScopeLevel, scopeCategories ...AccessTok
213213

214214
// ContainsCategory checks if a list of categories contains a specific category
215215
func ContainsCategory(categories []AccessTokenScopeCategory, category AccessTokenScopeCategory) bool {
216-
for _, c := range categories {
217-
if c == category {
218-
return true
219-
}
220-
}
221-
return false
216+
return slices.Contains(categories, category)
222217
}
223218

224219
// GetScopeLevelFromAccessMode converts permission access mode to scope level

models/auth/oauth2.go

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

1718
"code.gitea.io/gitea/models/db"
@@ -511,12 +512,7 @@ func (grant *OAuth2Grant) IncreaseCounter(ctx context.Context) error {
511512

512513
// ScopeContains returns true if the grant scope contains the specified scope
513514
func (grant *OAuth2Grant) ScopeContains(scope string) bool {
514-
for _, currentScope := range strings.Split(grant.Scope, " ") {
515-
if scope == currentScope {
516-
return true
517-
}
518-
}
519-
return false
515+
return slices.Contains(strings.Split(grant.Scope, " "), scope)
520516
}
521517

522518
// SetNonce updates the current nonce value of a grant

models/db/context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func contextSafetyCheck(e Engine) {
6767
_ = e.SQL("SELECT 1").Iterate(&m{}, func(int, any) error {
6868
callers := make([]uintptr, 32)
6969
callerNum := runtime.Callers(1, callers)
70-
for i := 0; i < callerNum; i++ {
70+
for i := range callerNum {
7171
if funcName := runtime.FuncForPC(callers[i]).Name(); funcName == "xorm.io/xorm.(*Session).Iterate" {
7272
contextSafetyDeniedFuncPCs = append(contextSafetyDeniedFuncPCs, callers[i])
7373
}
@@ -82,7 +82,7 @@ func contextSafetyCheck(e Engine) {
8282
// it should be very fast: xxxx ns/op
8383
callers := make([]uintptr, 32)
8484
callerNum := runtime.Callers(3, callers) // skip 3: runtime.Callers, contextSafetyCheck, GetEngine
85-
for i := 0; i < callerNum; i++ {
85+
for i := range callerNum {
8686
if slices.Contains(contextSafetyDeniedFuncPCs, callers[i]) {
8787
panic(errors.New("using database context in an iterator would cause corrupted results"))
8888
}

models/db/name.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package db
55

66
import (
77
"fmt"
8+
"slices"
89
"strings"
910
"unicode/utf8"
1011

@@ -80,10 +81,8 @@ func IsUsableName(reservedNames, reservedPatterns []string, name string) error {
8081
return util.NewInvalidArgumentErrorf("name is empty")
8182
}
8283

83-
for i := range reservedNames {
84-
if name == reservedNames[i] {
85-
return ErrNameReserved{name}
86-
}
84+
if slices.Contains(reservedNames, name) {
85+
return ErrNameReserved{name}
8786
}
8887

8988
for _, pat := range reservedPatterns {

models/dbfs/dbfile.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ func (f *file) readAt(fileMeta *dbfsMeta, offset int64, p []byte) (n int, err er
4646
blobPos := int(offset % f.blockSize)
4747
blobOffset := offset - int64(blobPos)
4848
blobRemaining := int(f.blockSize) - blobPos
49-
needRead := len(p)
50-
if needRead > blobRemaining {
51-
needRead = blobRemaining
52-
}
49+
needRead := min(len(p), blobRemaining)
5350
if blobOffset+int64(blobPos)+int64(needRead) > fileMeta.FileSize {
5451
needRead = int(fileMeta.FileSize - blobOffset - int64(blobPos))
5552
}
@@ -66,14 +63,8 @@ func (f *file) readAt(fileMeta *dbfsMeta, offset int64, p []byte) (n int, err er
6663
blobData = nil
6764
}
6865

69-
canCopy := len(blobData) - blobPos
70-
if canCopy <= 0 {
71-
canCopy = 0
72-
}
73-
realRead := needRead
74-
if realRead > canCopy {
75-
realRead = canCopy
76-
}
66+
canCopy := max(len(blobData)-blobPos, 0)
67+
realRead := min(needRead, canCopy)
7768
if realRead > 0 {
7869
copy(p[:realRead], fileData.BlobData[blobPos:blobPos+realRead])
7970
}
@@ -113,10 +104,7 @@ func (f *file) Write(p []byte) (n int, err error) {
113104
blobPos := int(f.offset % f.blockSize)
114105
blobOffset := f.offset - int64(blobPos)
115106
blobRemaining := int(f.blockSize) - blobPos
116-
needWrite := len(p)
117-
if needWrite > blobRemaining {
118-
needWrite = blobRemaining
119-
}
107+
needWrite := min(len(p), blobRemaining)
120108
buf := make([]byte, f.blockSize)
121109
readBytes, err := f.readAt(fileMeta, blobOffset, buf)
122110
if err != nil && !errors.Is(err, io.EOF) {

models/git/protected_branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func (protectBranch *ProtectedBranch) GetUnprotectedFilePatterns() []glob.Glob {
246246

247247
func getFilePatterns(filePatterns string) []glob.Glob {
248248
extarr := make([]glob.Glob, 0, 10)
249-
for _, expr := range strings.Split(strings.ToLower(filePatterns), ";") {
249+
for expr := range strings.SplitSeq(strings.ToLower(filePatterns), ";") {
250250
expr = strings.TrimSpace(expr)
251251
if expr != "" {
252252
if g, err := glob.Compile(expr, '.', '/'); err != nil {

models/issues/comment.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"context"
1010
"fmt"
1111
"html/template"
12+
"slices"
1213
"strconv"
1314
"unicode/utf8"
1415

@@ -196,12 +197,7 @@ func (t CommentType) HasMailReplySupport() bool {
196197
}
197198

198199
func (t CommentType) CountedAsConversation() bool {
199-
for _, ct := range ConversationCountedCommentType() {
200-
if t == ct {
201-
return true
202-
}
203-
}
204-
return false
200+
return slices.Contains(ConversationCountedCommentType(), t)
205201
}
206202

207203
// ConversationCountedCommentType returns the comment types that are counted as a conversation
@@ -614,7 +610,7 @@ func UpdateCommentAttachments(ctx context.Context, c *Comment, uuids []string) e
614610
if err != nil {
615611
return fmt.Errorf("getAttachmentsByUUIDs [uuids: %v]: %w", uuids, err)
616612
}
617-
for i := 0; i < len(attachments); i++ {
613+
for i := range attachments {
618614
attachments[i].IssueID = c.IssueID
619615
attachments[i].CommentID = c.ID
620616
if err := repo_model.UpdateAttachment(ctx, attachments[i]); err != nil {

0 commit comments

Comments
 (0)