mirror of
https://github.com/go-gitea/gitea.git
synced 2026-12-04 15:54:57 +00:00
refactor: remove duplicated and dead Go code (#40244)
Pure refactors with no behavior change, mostly reusing existing helpers and dropping duplicated or dead code. Net −222 lines. - Reuse existing helpers instead of inlined copies: `getIssueIndex`, `NewLabel`, `ToCommitUser`, `GetPackageFileDescriptors`, `util.SplitTrimSpace` - Merge copy-pasted blocks in review comment deletion, project deletion and the pre/post-receive hook templates - Drop dead code: an error `toStringKeys` never returned, impossible `bytes.Buffer` write errors, redundant nil-map branches, a `base64` wrapper - Use stdlib where it fits: `strings.Cut`, `bytes.IndexAny`, `cmp.Compare`, `min`/`max`, `slices.Concat`, map lookups instead of key scans
This commit is contained in:
1 parent
a5201dc427
commit
7e18e81c7d
23 files changed
+90
-312
No files matched your search
@@ -7,7 +7,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"gitea.dev/models/db"
|
||||
issues_model "gitea.dev/models/issues"
|
||||
@@ -162,12 +161,7 @@ func (actions ActionList) LoadIssues(ctx context.Context) error {
|
||||
issueNum := 0
|
||||
for _, action := range actions {
|
||||
if action.IsIssueEvent() {
|
||||
infos := action.GetIssueInfos()
|
||||
if len(infos) == 0 {
|
||||
continue
|
||||
}
|
||||
index, _ := strconv.ParseInt(infos[0], 10, 64)
|
||||
if index > 0 {
|
||||
if index := action.getIssueIndex(); index > 0 {
|
||||
conditions = conditions.Or(builder.Eq{
|
||||
"repo_id": action.RepoID,
|
||||
"`index`": index,
|
||||
|
||||
@@ -251,18 +251,8 @@ func (s AccessTokenScope) parse() (accessTokenScopeBitmap, error) {
|
||||
// The following is the more performant equivalent of 'for _, v := range strings.Split(remainingScope, ",")' as this is hot code
|
||||
remainingScopes := string(s)
|
||||
for len(remainingScopes) > 0 {
|
||||
i := strings.IndexByte(remainingScopes, ',')
|
||||
var v string
|
||||
if i < 0 {
|
||||
v = remainingScopes
|
||||
remainingScopes = ""
|
||||
} else if i+1 >= len(remainingScopes) {
|
||||
v = remainingScopes[:i]
|
||||
remainingScopes = ""
|
||||
} else {
|
||||
v = remainingScopes[:i]
|
||||
remainingScopes = remainingScopes[i+1:]
|
||||
}
|
||||
v, remainingScopes, _ = strings.Cut(remainingScopes, ",")
|
||||
singleScope := AccessTokenScope(v)
|
||||
if singleScope == "" {
|
||||
continue
|
||||
|
||||
@@ -244,13 +244,7 @@ func NewLabel(ctx context.Context, l *Label) error {
|
||||
func NewLabels(ctx context.Context, labels ...*Label) error {
|
||||
return db.WithTx(ctx, func(ctx context.Context) error {
|
||||
for _, l := range labels {
|
||||
color, err := label.NormalizeColor(l.Color)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
l.Color = color
|
||||
|
||||
if err := db.Insert(ctx, l); err != nil {
|
||||
if err := NewLabel(ctx, l); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
+8
-28
@@ -1005,34 +1005,14 @@ func DeleteReview(ctx context.Context, r *Review) error {
|
||||
return errors.New("review request can not be deleted using this method")
|
||||
}
|
||||
|
||||
opts := FindCommentsOptions{
|
||||
Type: CommentTypeCode,
|
||||
IssueID: r.IssueID,
|
||||
ReviewID: r.ID,
|
||||
}
|
||||
|
||||
if _, err := db.Delete[Comment](ctx, opts); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
opts = FindCommentsOptions{
|
||||
Type: CommentTypeReview,
|
||||
IssueID: r.IssueID,
|
||||
ReviewID: r.ID,
|
||||
}
|
||||
|
||||
if _, err := db.Delete[Comment](ctx, opts); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
opts = FindCommentsOptions{
|
||||
Type: CommentTypeDismissReview,
|
||||
IssueID: r.IssueID,
|
||||
ReviewID: r.ID,
|
||||
}
|
||||
|
||||
if _, err := db.Delete[Comment](ctx, opts); err != nil {
|
||||
return err
|
||||
for _, commentType := range [...]CommentType{CommentTypeCode, CommentTypeReview, CommentTypeDismissReview} {
|
||||
if _, err := db.Delete[Comment](ctx, FindCommentsOptions{
|
||||
Type: commentType,
|
||||
IssueID: r.IssueID,
|
||||
ReviewID: r.ID,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := db.DeleteByID[Review](ctx, r.ID); err != nil {
|
||||
|
||||
@@ -87,15 +87,7 @@ func SearchPackages(ctx context.Context, opts *PackageSearchOptions) ([]*package
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pfds := make([]*packages.PackageFileDescriptor, 0, len(pkgFiles))
|
||||
for _, pf := range pkgFiles {
|
||||
pfd, err := packages.GetPackageFileDescriptor(ctx, pf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pfds = append(pfds, pfd)
|
||||
}
|
||||
return pfds, nil
|
||||
return packages.GetPackageFileDescriptors(ctx, pkgFiles)
|
||||
}
|
||||
|
||||
// GetDistributions gets all available distributions
|
||||
|
||||
@@ -240,23 +240,10 @@ func finalProcessRepoUnitPermission(user *user_model.User, perm *Permission) {
|
||||
origPermUnits := perm.units
|
||||
perm.units = make([]*repo_model.RepoUnit, 0, len(perm.units))
|
||||
for _, u := range origPermUnits {
|
||||
shouldKeep := false
|
||||
for t := range perm.unitsMode {
|
||||
if shouldKeep = u.Type == t; shouldKeep {
|
||||
break
|
||||
}
|
||||
}
|
||||
for t := range perm.anonymousAccessMode {
|
||||
if shouldKeep = shouldKeep || u.Type == t; shouldKeep {
|
||||
break
|
||||
}
|
||||
}
|
||||
for t := range perm.everyoneAccessMode {
|
||||
if shouldKeep = shouldKeep || u.Type == t; shouldKeep {
|
||||
break
|
||||
}
|
||||
}
|
||||
if shouldKeep {
|
||||
_, hasUnitMode := perm.unitsMode[u.Type]
|
||||
_, hasAnonymousMode := perm.anonymousAccessMode[u.Type]
|
||||
_, hasEveryoneMode := perm.everyoneAccessMode[u.Type]
|
||||
if hasUnitMode || hasAnonymousMode || hasEveryoneMode {
|
||||
perm.units = append(perm.units, u)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -459,9 +459,6 @@ func DeleteProjectByRepoID(ctx context.Context, repoID int64) error {
|
||||
if _, err := db.GetEngine(ctx).Exec("DELETE FROM project_board WHERE project_board.id IN (SELECT project_board.id FROM project_board INNER JOIN project WHERE project.id = project_board.project_id AND project.repo_id = ?)", repoID); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := db.GetEngine(ctx).Table("project").Where("repo_id = ? ", repoID).Delete(&Project{}); err != nil {
|
||||
return err
|
||||
}
|
||||
case setting.Database.Type.IsPostgreSQL():
|
||||
if _, err := db.GetEngine(ctx).Exec("DELETE FROM project_issue USING project WHERE project.id = project_issue.project_id AND project.repo_id = ? ", repoID); err != nil {
|
||||
return err
|
||||
@@ -469,9 +466,6 @@ func DeleteProjectByRepoID(ctx context.Context, repoID int64) error {
|
||||
if _, err := db.GetEngine(ctx).Exec("DELETE FROM project_board USING project WHERE project.id = project_board.project_id AND project.repo_id = ? ", repoID); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := db.GetEngine(ctx).Table("project").Where("repo_id = ? ", repoID).Delete(&Project{}); err != nil {
|
||||
return err
|
||||
}
|
||||
default:
|
||||
if _, err := db.GetEngine(ctx).Exec("DELETE project_issue FROM project_issue INNER JOIN project ON project.id = project_issue.project_id WHERE project.repo_id = ? ", repoID); err != nil {
|
||||
return err
|
||||
@@ -479,10 +473,10 @@ func DeleteProjectByRepoID(ctx context.Context, repoID int64) error {
|
||||
if _, err := db.GetEngine(ctx).Exec("DELETE project_board FROM project_board INNER JOIN project ON project.id = project_board.project_id WHERE project.repo_id = ? ", repoID); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := db.GetEngine(ctx).Table("project").Where("repo_id = ? ", repoID).Delete(&Project{}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := db.GetEngine(ctx).Table("project").Where("repo_id = ? ", repoID).Delete(&Project{}); err != nil {
|
||||
return err
|
||||
}
|
||||
return updateRepositoryProjectCount(ctx, repoID)
|
||||
}
|
||||
+6
-21
@@ -17,9 +17,7 @@ import (
|
||||
|
||||
func getHookTemplates() (hookNames, hookTpls, giteaHookTpls []string) {
|
||||
hookNames = []string{"pre-receive", "update", "post-receive"}
|
||||
hookTpls = []string{
|
||||
// for pre-receive
|
||||
fmt.Sprintf(`#!/usr/bin/env %s
|
||||
receiveHookTpl := fmt.Sprintf(`#!/usr/bin/env %s
|
||||
# AUTO GENERATED BY GITEA, DO NOT MODIFY
|
||||
data=$(cat)
|
||||
exitcodes=""
|
||||
@@ -35,7 +33,10 @@ done
|
||||
for i in ${exitcodes}; do
|
||||
[ ${i} -eq 0 ] || exit ${i}
|
||||
done
|
||||
`, setting.ScriptType),
|
||||
`, setting.ScriptType)
|
||||
hookTpls = []string{
|
||||
// for pre-receive
|
||||
receiveHookTpl,
|
||||
|
||||
// for update
|
||||
fmt.Sprintf(`#!/usr/bin/env %s
|
||||
@@ -56,23 +57,7 @@ done
|
||||
`, setting.ScriptType),
|
||||
|
||||
// for post-receive
|
||||
fmt.Sprintf(`#!/usr/bin/env %s
|
||||
# AUTO GENERATED BY GITEA, DO NOT MODIFY
|
||||
data=$(cat)
|
||||
exitcodes=""
|
||||
hookname=$(basename $0)
|
||||
GIT_DIR=${GIT_DIR:-$(dirname $0)/..}
|
||||
|
||||
for hook in ${GIT_DIR}/hooks/${hookname}.d/*; do
|
||||
test -x "${hook}" && test -f "${hook}" || continue
|
||||
echo "${data}" | "${hook}"
|
||||
exitcodes="${exitcodes} $?"
|
||||
done
|
||||
|
||||
for i in ${exitcodes}; do
|
||||
[ ${i} -eq 0 ] || exit ${i}
|
||||
done
|
||||
`, setting.ScriptType),
|
||||
receiveHookTpl,
|
||||
}
|
||||
|
||||
giteaHookTpls = []string{
|
||||
|
||||
@@ -23,16 +23,12 @@ func CamoEncode(link string) string {
|
||||
|
||||
mac := hmac.New(sha1.New, []byte(setting.Camo.HMACKey))
|
||||
_, _ = mac.Write([]byte(link)) // hmac does not return errors
|
||||
macSum := b64encode(mac.Sum(nil))
|
||||
encodedURL := b64encode([]byte(link))
|
||||
macSum := base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
|
||||
encodedURL := base64.RawURLEncoding.EncodeToString([]byte(link))
|
||||
|
||||
return strings.TrimSuffix(setting.Camo.ServerURL, "/") + "/" + macSum + "/" + encodedURL
|
||||
}
|
||||
|
||||
func b64encode(data []byte) string {
|
||||
return strings.TrimRight(base64.URLEncoding.EncodeToString(data), "=")
|
||||
}
|
||||
|
||||
func camoHandleLink(link string) string {
|
||||
if setting.Camo.Enabled {
|
||||
lnkURL, err := url.Parse(link)
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
package markdown
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/yuin/goldmark/ast"
|
||||
"github.com/yuin/goldmark/text"
|
||||
)
|
||||
@@ -18,14 +20,7 @@ func (g *ASTTransformer) transformFencedCodeblock(v *ast.FencedCodeBlock, reader
|
||||
return
|
||||
}
|
||||
info := v.Info.Segment.Value(reader.Source())
|
||||
newEnd := -1
|
||||
for i, b := range info {
|
||||
if b == ' ' || b == ',' || b == ':' {
|
||||
newEnd = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if newEnd != -1 {
|
||||
if newEnd := bytes.IndexAny(info, " ,:"); newEnd != -1 {
|
||||
start := v.Info.Segment.Start
|
||||
v.Info = ast.NewTextSegment(text.NewSegment(start, start+newEnd))
|
||||
}
|
||||
|
||||
@@ -61,10 +61,7 @@ func validate(bs []byte, datatype any, isJSON bool) error {
|
||||
return err
|
||||
}
|
||||
if !isJSON {
|
||||
v, err = toStringKeys(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v = toStringKeys(v)
|
||||
}
|
||||
|
||||
var schemaFilename string
|
||||
@@ -88,30 +85,23 @@ func validate(bs []byte, datatype any, isJSON bool) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func toStringKeys(val any) (any, error) {
|
||||
var err error
|
||||
func toStringKeys(val any) any {
|
||||
switch val := val.(type) {
|
||||
case map[string]any:
|
||||
m := make(map[string]any)
|
||||
for k, v := range val {
|
||||
m[k], err = toStringKeys(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m[k] = toStringKeys(v)
|
||||
}
|
||||
return m, nil
|
||||
return m
|
||||
case []any:
|
||||
l := make([]any, len(val))
|
||||
for i, v := range val {
|
||||
l[i], err = toStringKeys(v)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l[i] = toStringKeys(v)
|
||||
}
|
||||
return l, nil
|
||||
return l
|
||||
case time.Time:
|
||||
return val.Format(time.RFC3339), nil
|
||||
return val.Format(time.RFC3339)
|
||||
default:
|
||||
return val, nil
|
||||
return val
|
||||
}
|
||||
}
|
||||
@@ -39,14 +39,8 @@ func GetContext(ctx context.Context) *Context {
|
||||
if pCtx, ok := ctx.(*Context); ok {
|
||||
return pCtx
|
||||
}
|
||||
pCtxInterface := ctx.Value(ProcessContextKey)
|
||||
if pCtxInterface == nil {
|
||||
return nil
|
||||
}
|
||||
if pCtx, ok := pCtxInterface.(*Context); ok {
|
||||
return pCtx
|
||||
}
|
||||
return nil
|
||||
pCtx, _ := ctx.Value(ProcessContextKey).(*Context)
|
||||
return pCtx
|
||||
}
|
||||
|
||||
// GetPID returns the PID for this context
|
||||
|
||||
@@ -17,28 +17,16 @@ type languageResponse []*repo_model.LanguageStat
|
||||
|
||||
func (l languageResponse) MarshalJSON() ([]byte, error) {
|
||||
var buf bytes.Buffer
|
||||
if _, err := buf.WriteString("{"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buf.WriteString("{")
|
||||
for i, lang := range l {
|
||||
if i > 0 {
|
||||
if _, err := buf.WriteString(","); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if _, err := buf.WriteString(strconv.Quote(lang.Language)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := buf.WriteString(":"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := buf.WriteString(strconv.FormatInt(lang.Size, 10)); err != nil {
|
||||
return nil, err
|
||||
buf.WriteString(",")
|
||||
}
|
||||
buf.WriteString(strconv.Quote(lang.Language))
|
||||
buf.WriteString(":")
|
||||
buf.WriteString(strconv.FormatInt(lang.Size, 10))
|
||||
}
|
||||
if _, err := buf.WriteString("}"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buf.WriteString("}")
|
||||
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gitea.dev/models/auth"
|
||||
"gitea.dev/models/db"
|
||||
@@ -18,6 +17,7 @@ import (
|
||||
"gitea.dev/modules/log"
|
||||
"gitea.dev/modules/setting"
|
||||
"gitea.dev/modules/templates"
|
||||
"gitea.dev/modules/util"
|
||||
"gitea.dev/modules/web"
|
||||
auth_service "gitea.dev/services/auth"
|
||||
"gitea.dev/services/auth/source/ldap"
|
||||
@@ -175,15 +175,6 @@ func parseOAuth2Config(form forms.AuthenticationForm) *oauth2.Source {
|
||||
EmailURL: form.Oauth2EmailURL,
|
||||
Tenant: form.Oauth2Tenant,
|
||||
}
|
||||
} else {
|
||||
customURLMapping = nil
|
||||
}
|
||||
var scopes []string
|
||||
for s := range strings.SplitSeq(form.Oauth2Scopes, ",") {
|
||||
s = strings.TrimSpace(s)
|
||||
if s != "" {
|
||||
scopes = append(scopes, s)
|
||||
}
|
||||
}
|
||||
|
||||
return &oauth2.Source{
|
||||
@@ -193,7 +184,7 @@ func parseOAuth2Config(form forms.AuthenticationForm) *oauth2.Source {
|
||||
OpenIDConnectAutoDiscoveryURL: form.OpenIDConnectAutoDiscoveryURL,
|
||||
CustomURLMapping: customURLMapping,
|
||||
IconURL: form.Oauth2IconURL,
|
||||
Scopes: scopes,
|
||||
Scopes: util.SplitTrimSpace(form.Oauth2Scopes, ","),
|
||||
RequiredClaimName: form.Oauth2RequiredClaimName,
|
||||
RequiredClaimValue: form.Oauth2RequiredClaimValue,
|
||||
GroupClaimName: form.Oauth2GroupClaimName,
|
||||
|
||||
@@ -381,19 +381,14 @@ func ViewProject(ctx *context.Context) {
|
||||
}
|
||||
|
||||
// Get the exclusive scope for every label ID
|
||||
labelExclusiveScopes := make([]string, 0, len(preparedLabelFilter.SelectedLabelIDs))
|
||||
for _, labelID := range preparedLabelFilter.SelectedLabelIDs {
|
||||
foundExclusiveScope := false
|
||||
labelExclusiveScopes := make([]string, len(preparedLabelFilter.SelectedLabelIDs))
|
||||
for i, labelID := range preparedLabelFilter.SelectedLabelIDs {
|
||||
for _, label := range labels {
|
||||
if label.ID == labelID || label.ID == -labelID {
|
||||
labelExclusiveScopes = append(labelExclusiveScopes, label.ExclusiveScope())
|
||||
foundExclusiveScope = true
|
||||
labelExclusiveScopes[i] = label.ExclusiveScope()
|
||||
break
|
||||
}
|
||||
}
|
||||
if !foundExclusiveScope {
|
||||
labelExclusiveScopes = append(labelExclusiveScopes, "")
|
||||
}
|
||||
}
|
||||
|
||||
for _, l := range labels {
|
||||
|
||||
@@ -148,17 +148,6 @@ func ScopedWorkflows(ctx *context.Context) {
|
||||
ctx.HTML(http.StatusOK, swCtx.Template)
|
||||
}
|
||||
|
||||
// parsePatternLines splits a textarea value into trimmed, non-empty status-check patterns (one per line).
|
||||
func parsePatternLines(raw string) []string {
|
||||
var patterns []string
|
||||
for line := range strings.SplitSeq(raw, "\n") {
|
||||
if p := strings.TrimSpace(line); p != "" {
|
||||
patterns = append(patterns, p)
|
||||
}
|
||||
}
|
||||
return patterns
|
||||
}
|
||||
|
||||
// deriveScopedStatusContexts returns the commit-status contexts a scoped workflow is expected to post on a consumer:
|
||||
// "<source FullName>: <display> / <job> (<event>)" for each parsed job (matrix-expanded, matching run creation) and triggering event.
|
||||
// Job names that depend on run-context expressions cannot resolve here (no run context) and appear as authored; a glob pattern still matches them.
|
||||
@@ -328,7 +317,7 @@ func ScopedWorkflowSetRequired(ctx *context.Context) {
|
||||
}
|
||||
configs := make(map[string]*actions_model.ScopedWorkflowConfig)
|
||||
for _, workflowID := range ctx.FormStrings("workflow_ids") {
|
||||
patterns := parsePatternLines(ctx.FormString("required_patterns[" + workflowID + "]"))
|
||||
patterns := util.SplitTrimSpace(ctx.FormString("required_patterns["+workflowID+"]"), "\n")
|
||||
required := requiredSet.Contains(workflowID)
|
||||
if required && len(patterns) == 0 {
|
||||
ctx.JSONError(ctx.Tr("actions.scoped_workflows.required.patterns_empty"))
|
||||
|
||||
@@ -53,19 +53,14 @@ func PrepareFilterIssueLabels(ctx *context.Context, repoID int64, owner *user_mo
|
||||
}
|
||||
|
||||
// Get the exclusive scope for every label ID
|
||||
labelExclusiveScopes := make([]string, 0, len(ret.SelectedLabelIDs))
|
||||
for _, labelID := range ret.SelectedLabelIDs {
|
||||
foundExclusiveScope := false
|
||||
labelExclusiveScopes := make([]string, len(ret.SelectedLabelIDs))
|
||||
for i, labelID := range ret.SelectedLabelIDs {
|
||||
for _, label := range allLabels {
|
||||
if label.ID == labelID || label.ID == -labelID {
|
||||
labelExclusiveScopes = append(labelExclusiveScopes, label.ExclusiveScope())
|
||||
foundExclusiveScope = true
|
||||
labelExclusiveScopes[i] = label.ExclusiveScope()
|
||||
break
|
||||
}
|
||||
}
|
||||
if !foundExclusiveScope {
|
||||
labelExclusiveScopes = append(labelExclusiveScopes, "")
|
||||
}
|
||||
}
|
||||
|
||||
for _, l := range allLabels {
|
||||
|
||||
+4
-11
@@ -183,18 +183,11 @@ func (s *SSPI) newUser(ctx context.Context, username string, cfg *sspi.Source) (
|
||||
// (eg. "DOMAIN\user" becomes "user"), and removes the UPN suffix (domain name) and separator
|
||||
// from UPNs (eg. "user@domain.local" becomes "user")
|
||||
func stripDomainNames(username string) string {
|
||||
if strings.Contains(username, "\\") {
|
||||
parts := strings.SplitN(username, "\\", 2)
|
||||
if len(parts) > 1 {
|
||||
username = parts[1]
|
||||
}
|
||||
} else if strings.Contains(username, "@") {
|
||||
parts := strings.Split(username, "@")
|
||||
if len(parts) > 1 {
|
||||
username = parts[0]
|
||||
}
|
||||
if _, after, ok := strings.Cut(username, "\\"); ok {
|
||||
return after
|
||||
}
|
||||
return username
|
||||
before, _, _ := strings.Cut(username, "@")
|
||||
return before
|
||||
}
|
||||
|
||||
func replaceSeparators(username string, cfg *sspi.Source) string {
|
||||
|
||||
@@ -92,15 +92,7 @@ func ToCommit(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Rep
|
||||
|
||||
// Retrieve author and committer information
|
||||
|
||||
var cacheAuthor *user_model.User
|
||||
var ok bool
|
||||
if userCache == nil {
|
||||
cacheAuthor = (*user_model.User)(nil)
|
||||
ok = false
|
||||
} else {
|
||||
cacheAuthor, ok = userCache[commit.Author.Email]
|
||||
}
|
||||
|
||||
cacheAuthor, ok := userCache[commit.Author.Email]
|
||||
if ok {
|
||||
apiAuthor = ToUser(ctx, cacheAuthor, nil)
|
||||
} else {
|
||||
@@ -115,14 +107,7 @@ func ToCommit(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Rep
|
||||
}
|
||||
}
|
||||
|
||||
var cacheCommitter *user_model.User
|
||||
if userCache == nil {
|
||||
cacheCommitter = (*user_model.User)(nil)
|
||||
ok = false
|
||||
} else {
|
||||
cacheCommitter, ok = userCache[commit.Committer.Email]
|
||||
}
|
||||
|
||||
cacheCommitter, ok := userCache[commit.Committer.Email]
|
||||
if ok {
|
||||
apiCommitter = ToUser(ctx, cacheCommitter, nil)
|
||||
} else {
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gitea.dev/modules/git"
|
||||
api "gitea.dev/modules/structs"
|
||||
)
|
||||
@@ -13,22 +11,10 @@ import (
|
||||
// ToWikiCommit convert a git commit into a WikiCommit
|
||||
func ToWikiCommit(commit *git.Commit) *api.WikiCommit {
|
||||
return &api.WikiCommit{
|
||||
ID: commit.ID.String(),
|
||||
Author: &api.CommitUser{
|
||||
Identity: api.Identity{
|
||||
Name: commit.Author.Name,
|
||||
Email: commit.Author.Email,
|
||||
},
|
||||
Date: commit.Author.When.UTC().Format(time.RFC3339),
|
||||
},
|
||||
Committer: &api.CommitUser{
|
||||
Identity: api.Identity{
|
||||
Name: commit.Committer.Name,
|
||||
Email: commit.Committer.Email,
|
||||
},
|
||||
Date: commit.Committer.When.UTC().Format(time.RFC3339),
|
||||
},
|
||||
Message: commit.MessageUTF8(),
|
||||
ID: commit.ID.String(),
|
||||
Author: ToCommitUser(commit.Author),
|
||||
Committer: ToCommitUser(commit.Committer),
|
||||
Message: commit.MessageUTF8(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
package arch
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
@@ -30,19 +31,12 @@ func parseEVR(evr string) (epoch, version, release string) {
|
||||
}
|
||||
|
||||
func compareSegments(a, b []string) int {
|
||||
lenA, lenB := len(a), len(b)
|
||||
l := min(lenA, lenB)
|
||||
for i := range l {
|
||||
for i := range min(len(a), len(b)) {
|
||||
if r := compare(a[i], b[i]); r != 0 {
|
||||
return r
|
||||
}
|
||||
}
|
||||
if lenA == lenB {
|
||||
return 0
|
||||
} else if l == lenA {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
return cmp.Compare(len(a), len(b))
|
||||
}
|
||||
|
||||
func compare(a, b string) int {
|
||||
@@ -56,14 +50,7 @@ func compare(a, b string) int {
|
||||
if aNumeric && bNumeric {
|
||||
aInt, _ := strconv.Atoi(a)
|
||||
bInt, _ := strconv.Atoi(b)
|
||||
switch {
|
||||
case aInt < bInt:
|
||||
return -1
|
||||
case aInt > bInt:
|
||||
return 1
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
return cmp.Compare(aInt, bInt)
|
||||
}
|
||||
|
||||
if aNumeric {
|
||||
|
||||
@@ -63,18 +63,10 @@ func (graph *Graph) AddGlyph(row, column int, flowID int64, color int, glyph byt
|
||||
}
|
||||
flow.AddGlyph(row, column, glyph)
|
||||
|
||||
if row < graph.MinRow {
|
||||
graph.MinRow = row
|
||||
}
|
||||
if row > graph.MaxRow {
|
||||
graph.MaxRow = row
|
||||
}
|
||||
if column < graph.MinColumn {
|
||||
graph.MinColumn = column
|
||||
}
|
||||
if column > graph.MaxColumn {
|
||||
graph.MaxColumn = column
|
||||
}
|
||||
graph.MinRow = min(graph.MinRow, row)
|
||||
graph.MaxRow = max(graph.MaxRow, row)
|
||||
graph.MinColumn = min(graph.MinColumn, column)
|
||||
graph.MaxColumn = max(graph.MaxColumn, column)
|
||||
}
|
||||
|
||||
// AddCommit adds a commit at row, column on flowID with the provided data
|
||||
@@ -174,18 +166,10 @@ func (flow *Flow) Color16() int {
|
||||
|
||||
// AddGlyph adds glyph at row and column
|
||||
func (flow *Flow) AddGlyph(row, column int, glyph byte) {
|
||||
if row < flow.MinRow {
|
||||
flow.MinRow = row
|
||||
}
|
||||
if row > flow.MaxRow {
|
||||
flow.MaxRow = row
|
||||
}
|
||||
if column < flow.MinColumn {
|
||||
flow.MinColumn = column
|
||||
}
|
||||
if column > flow.MaxColumn {
|
||||
flow.MaxColumn = column
|
||||
}
|
||||
flow.MinRow = min(flow.MinRow, row)
|
||||
flow.MaxRow = max(flow.MaxRow, row)
|
||||
flow.MinColumn = min(flow.MinColumn, column)
|
||||
flow.MaxColumn = max(flow.MaxColumn, column)
|
||||
|
||||
flow.Glyphs = append(flow.Glyphs, Glyph{
|
||||
row,
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -329,13 +330,6 @@ func testViewRepoDirectoryReadme(t *testing.T) {
|
||||
{Path: "docs/README", Content: "This is docs/README"},
|
||||
}
|
||||
|
||||
combineFiles := func(slices ...[]git.FastImportFile) (res []git.FastImportFile) {
|
||||
for _, s := range slices {
|
||||
res = append(res, s...)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
err := git.ForceFastImport(t.Context(), repo56.CodeStorageRepo(), []git.FastImportCommit{
|
||||
{Ref: "refs/heads/master", Message: "init master", Files: []git.FastImportFile{{Path: "README.md", Content: "The cake is a lie."}}},
|
||||
{Ref: "refs/heads/txt", Message: "init txt", Files: []git.FastImportFile{{Path: "README.txt", Content: "My spoon is too big."}}},
|
||||
@@ -364,18 +358,18 @@ func testViewRepoDirectoryReadme(t *testing.T) {
|
||||
{Mode: symlink, Path: "trampoline", Content: "README.md"},
|
||||
}},
|
||||
{Ref: "refs/heads/sp-ace", Message: "init sp-ace", Files: []git.FastImportFile{{Path: "read me", Content: "The cake is a lie."}}},
|
||||
{Ref: "refs/heads/fallbacks", Message: "init fallbacks", Files: combineFiles(allGitea, allGithub, allRoot, allDocs)},
|
||||
{Ref: "refs/heads/fallbacks2", Message: "init fallbacks2", Files: combineFiles(allGitea[1:], allGithub, allRoot, allDocs)},
|
||||
{Ref: "refs/heads/fallbacks3", Message: "init fallbacks3", Files: combineFiles(allGitea[2:], allGithub, allRoot, allDocs)},
|
||||
{Ref: "refs/heads/fallbacks4", Message: "init fallbacks4", Files: combineFiles(allGithub, allRoot, allDocs)},
|
||||
{Ref: "refs/heads/fallbacks5", Message: "init fallbacks5", Files: combineFiles(allGithub[1:], allRoot, allDocs)},
|
||||
{Ref: "refs/heads/fallbacks6", Message: "init fallbacks6", Files: combineFiles(allGithub[2:], allRoot, allDocs)},
|
||||
{Ref: "refs/heads/fallbacks7", Message: "init fallbacks7", Files: combineFiles(allRoot, allDocs)},
|
||||
{Ref: "refs/heads/fallbacks8", Message: "init fallbacks8", Files: combineFiles(allRoot[1:], allDocs)},
|
||||
{Ref: "refs/heads/fallbacks9", Message: "init fallbacks9", Files: combineFiles(allRoot[2:], allDocs)},
|
||||
{Ref: "refs/heads/fallbacks10", Message: "init fallbacks10", Files: combineFiles(allDocs)},
|
||||
{Ref: "refs/heads/fallbacks11", Message: "init fallbacks11", Files: combineFiles(allDocs[1:])},
|
||||
{Ref: "refs/heads/fallbacks12", Message: "init fallbacks12", Files: combineFiles(allDocs[2:])},
|
||||
{Ref: "refs/heads/fallbacks", Message: "init fallbacks", Files: slices.Concat(allGitea, allGithub, allRoot, allDocs)},
|
||||
{Ref: "refs/heads/fallbacks2", Message: "init fallbacks2", Files: slices.Concat(allGitea[1:], allGithub, allRoot, allDocs)},
|
||||
{Ref: "refs/heads/fallbacks3", Message: "init fallbacks3", Files: slices.Concat(allGitea[2:], allGithub, allRoot, allDocs)},
|
||||
{Ref: "refs/heads/fallbacks4", Message: "init fallbacks4", Files: slices.Concat(allGithub, allRoot, allDocs)},
|
||||
{Ref: "refs/heads/fallbacks5", Message: "init fallbacks5", Files: slices.Concat(allGithub[1:], allRoot, allDocs)},
|
||||
{Ref: "refs/heads/fallbacks6", Message: "init fallbacks6", Files: slices.Concat(allGithub[2:], allRoot, allDocs)},
|
||||
{Ref: "refs/heads/fallbacks7", Message: "init fallbacks7", Files: slices.Concat(allRoot, allDocs)},
|
||||
{Ref: "refs/heads/fallbacks8", Message: "init fallbacks8", Files: slices.Concat(allRoot[1:], allDocs)},
|
||||
{Ref: "refs/heads/fallbacks9", Message: "init fallbacks9", Files: slices.Concat(allRoot[2:], allDocs)},
|
||||
{Ref: "refs/heads/fallbacks10", Message: "init fallbacks10", Files: slices.Concat(allDocs)},
|
||||
{Ref: "refs/heads/fallbacks11", Message: "init fallbacks11", Files: slices.Concat(allDocs[1:])},
|
||||
{Ref: "refs/heads/fallbacks12", Message: "init fallbacks12", Files: slices.Concat(allDocs[2:])},
|
||||
{Ref: "refs/heads/fallbacks-broken-symlinks", Message: "init fallbacks-broken-symlinks", Files: []git.FastImportFile{
|
||||
{Mode: symlink, Path: ".gitea/README.md", Content: "non-existent-file"},
|
||||
{Mode: symlink, Path: ".github/README.md", Content: "non-existent-file"},
|
||||
|
||||
Reference in new issue
Block a user