loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Refactor some legacy code and remove unused code (#28622)

1. use slices.Contains, remove Int64sContains
2. use HashEmail, remove base.EncodeMD5
3. remove BasicAuthEncode, IsLetter

authored by

wxiaoguang and committed by
GitHub
f3999888 921df1cb

+14 -76
+5 -2
models/avatars/avatar.go
··· 5 5 6 6 import ( 7 7 "context" 8 + "crypto/md5" 9 + "encoding/hex" 8 10 "fmt" 9 11 "net/url" 10 12 "path" ··· 13 15 "sync/atomic" 14 16 15 17 "code.gitea.io/gitea/models/db" 16 - "code.gitea.io/gitea/modules/base" 17 18 "code.gitea.io/gitea/modules/cache" 18 19 "code.gitea.io/gitea/modules/log" 19 20 "code.gitea.io/gitea/modules/setting" ··· 90 91 91 92 // HashEmail hashes email address to MD5 string. https://en.gravatar.com/site/implement/hash/ 92 93 func HashEmail(email string) string { 93 - return base.EncodeMD5(strings.ToLower(strings.TrimSpace(email))) 94 + m := md5.New() 95 + _, _ = m.Write([]byte(strings.ToLower(strings.TrimSpace(email)))) 96 + return hex.EncodeToString(m.Sum(nil)) 94 97 } 95 98 96 99 // GetEmailForHash converts a provided md5sum to the email
+3 -4
models/git/protected_branch.go
··· 17 17 repo_model "code.gitea.io/gitea/models/repo" 18 18 "code.gitea.io/gitea/models/unit" 19 19 user_model "code.gitea.io/gitea/models/user" 20 - "code.gitea.io/gitea/modules/base" 21 20 "code.gitea.io/gitea/modules/log" 22 21 "code.gitea.io/gitea/modules/timeutil" 23 22 "code.gitea.io/gitea/modules/util" ··· 127 126 return writeAccess 128 127 } 129 128 130 - if base.Int64sContains(protectBranch.WhitelistUserIDs, user.ID) { 129 + if slices.Contains(protectBranch.WhitelistUserIDs, user.ID) { 131 130 return true 132 131 } 133 132 ··· 150 149 return permissionInRepo.CanWrite(unit.TypeCode) 151 150 } 152 151 153 - if base.Int64sContains(protectBranch.MergeWhitelistUserIDs, userID) { 152 + if slices.Contains(protectBranch.MergeWhitelistUserIDs, userID) { 154 153 return true 155 154 } 156 155 ··· 182 181 return writeAccess, nil 183 182 } 184 183 185 - if base.Int64sContains(protectBranch.ApprovalsWhitelistUserIDs, user.ID) { 184 + if slices.Contains(protectBranch.ApprovalsWhitelistUserIDs, user.ID) { 186 185 return true, nil 187 186 } 188 187
+2 -2
models/git/protected_tag.go
··· 6 6 import ( 7 7 "context" 8 8 "regexp" 9 + "slices" 9 10 "strings" 10 11 11 12 "code.gitea.io/gitea/models/db" 12 13 "code.gitea.io/gitea/models/organization" 13 - "code.gitea.io/gitea/modules/base" 14 14 "code.gitea.io/gitea/modules/timeutil" 15 15 16 16 "github.com/gobwas/glob" ··· 76 76 77 77 // IsUserAllowedModifyTag returns true if the user is allowed to modify the tag 78 78 func IsUserAllowedModifyTag(ctx context.Context, pt *ProtectedTag, userID int64) (bool, error) { 79 - if base.Int64sContains(pt.AllowlistUserIDs, userID) { 79 + if slices.Contains(pt.AllowlistUserIDs, userID) { 80 80 return true, nil 81 81 } 82 82
+2 -2
models/issues/review.go
··· 6 6 import ( 7 7 "context" 8 8 "fmt" 9 + "slices" 9 10 "strings" 10 11 11 12 "code.gitea.io/gitea/models/db" ··· 15 16 access_model "code.gitea.io/gitea/models/perm/access" 16 17 "code.gitea.io/gitea/models/unit" 17 18 user_model "code.gitea.io/gitea/models/user" 18 - "code.gitea.io/gitea/modules/base" 19 19 "code.gitea.io/gitea/modules/structs" 20 20 "code.gitea.io/gitea/modules/timeutil" 21 21 "code.gitea.io/gitea/modules/util" ··· 279 279 return team.UnitAccessMode(ctx, unit.TypeCode) >= perm.AccessModeWrite, nil 280 280 } 281 281 282 - return base.Int64sContains(pb.ApprovalsWhitelistTeamIDs, team.ID), nil 282 + return slices.Contains(pb.ApprovalsWhitelistTeamIDs, team.ID), nil 283 283 } 284 284 285 285 // CreateReview creates a new review based on opts
-30
modules/base/tool.go
··· 4 4 package base 5 5 6 6 import ( 7 - "crypto/md5" 8 7 "crypto/sha1" 9 8 "encoding/base64" 10 9 "encoding/hex" ··· 16 15 "strconv" 17 16 "strings" 18 17 "time" 19 - "unicode" 20 18 "unicode/utf8" 21 19 22 20 "code.gitea.io/gitea/modules/git" ··· 26 24 "github.com/dustin/go-humanize" 27 25 "github.com/minio/sha256-simd" 28 26 ) 29 - 30 - // EncodeMD5 encodes string to md5 hex value. 31 - func EncodeMD5(str string) string { 32 - m := md5.New() 33 - _, _ = m.Write([]byte(str)) 34 - return hex.EncodeToString(m.Sum(nil)) 35 - } 36 27 37 28 // EncodeSha1 string to sha1 hex value. 38 29 func EncodeSha1(str string) string { ··· 68 59 } 69 60 70 61 return auth[0], auth[1], nil 71 - } 72 - 73 - // BasicAuthEncode encode basic auth string 74 - func BasicAuthEncode(username, password string) string { 75 - return base64.StdEncoding.EncodeToString([]byte(username + ":" + password)) 76 62 } 77 63 78 64 // VerifyTimeLimitCode verify time limit code ··· 182 168 strs[i] = strconv.FormatInt(ints[i], 10) 183 169 } 184 170 return strs 185 - } 186 - 187 - // Int64sContains returns if a int64 in a slice of int64 188 - func Int64sContains(intsSlice []int64, a int64) bool { 189 - for _, c := range intsSlice { 190 - if c == a { 191 - return true 192 - } 193 - } 194 - return false 195 - } 196 - 197 - // IsLetter reports whether the rune is a letter (category L). 198 - // https://github.com/golang/go/blob/c3b4918/src/go/scanner/scanner.go#L342 199 - func IsLetter(ch rune) bool { 200 - return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' || ch >= 0x80 && unicode.IsLetter(ch) 201 171 } 202 172 203 173 // EntryIcon returns the octicon class for displaying files/directories
-35
modules/base/tool_test.go
··· 11 11 "github.com/stretchr/testify/assert" 12 12 ) 13 13 14 - func TestEncodeMD5(t *testing.T) { 15 - assert.Equal(t, 16 - "3858f62230ac3c915f300c664312c63f", 17 - EncodeMD5("foobar"), 18 - ) 19 - } 20 - 21 14 func TestEncodeSha1(t *testing.T) { 22 15 assert.Equal(t, 23 16 "8843d7f92416211de9ebb963ff4ce28125932878", ··· 50 43 51 44 _, _, err = BasicAuthDecode("invalid") 52 45 assert.Error(t, err) 53 - } 54 - 55 - func TestBasicAuthEncode(t *testing.T) { 56 - assert.Equal(t, "Zm9vOmJhcg==", BasicAuthEncode("foo", "bar")) 57 - assert.Equal(t, "MjM6IjotLS0t", BasicAuthEncode("23:\"", "----")) 58 46 } 59 47 60 48 func TestVerifyTimeLimitCode(t *testing.T) { ··· 165 153 []string{"1", "4", "16", "64", "256"}, 166 154 Int64sToStrings([]int64{1, 4, 16, 64, 256}), 167 155 ) 168 - } 169 - 170 - func TestInt64sContains(t *testing.T) { 171 - assert.True(t, Int64sContains([]int64{6, 44324, 4324, 32, 1, 2323}, 1)) 172 - assert.True(t, Int64sContains([]int64{2323}, 2323)) 173 - assert.False(t, Int64sContains([]int64{6, 44324, 4324, 32, 1, 2323}, 232)) 174 - } 175 - 176 - func TestIsLetter(t *testing.T) { 177 - assert.True(t, IsLetter('a')) 178 - assert.True(t, IsLetter('e')) 179 - assert.True(t, IsLetter('q')) 180 - assert.True(t, IsLetter('z')) 181 - assert.True(t, IsLetter('A')) 182 - assert.True(t, IsLetter('E')) 183 - assert.True(t, IsLetter('Q')) 184 - assert.True(t, IsLetter('Z')) 185 - assert.True(t, IsLetter('_')) 186 - assert.False(t, IsLetter('-')) 187 - assert.False(t, IsLetter('1')) 188 - assert.False(t, IsLetter('$')) 189 - assert.False(t, IsLetter(0x00)) 190 - assert.False(t, IsLetter(0x93)) 191 156 } 192 157 193 158 // TODO: Test EntryIcon
+2 -1
routers/web/user/setting/profile.go
··· 14 14 "path/filepath" 15 15 "strings" 16 16 17 + "code.gitea.io/gitea/models/avatars" 17 18 "code.gitea.io/gitea/models/db" 18 19 "code.gitea.io/gitea/models/organization" 19 20 repo_model "code.gitea.io/gitea/models/repo" ··· 130 131 ctxUser.UseCustomAvatar = form.Source == forms.AvatarLocal 131 132 if len(form.Gravatar) > 0 { 132 133 if form.Avatar != nil { 133 - ctxUser.Avatar = base.EncodeMD5(form.Gravatar) 134 + ctxUser.Avatar = avatars.HashEmail(form.Gravatar) 134 135 } else { 135 136 ctxUser.Avatar = "" 136 137 }