diff options
author | KN4CK3R <admin@oldschoolhack.me> | 2022-10-12 07:18:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-12 13:18:26 +0800 |
commit | 0e57ff7eee4ac71d923f970d15889ad4d50f97a9 (patch) | |
tree | e5a92c55af5366924bd40ae14b4bf12842239193 /services | |
parent | e84558b0931309cf1f4f2767bc47296483b9b3e1 (diff) | |
download | gitea-0e57ff7eee4ac71d923f970d15889ad4d50f97a9.tar.gz gitea-0e57ff7eee4ac71d923f970d15889ad4d50f97a9.zip |
Add generic set type (#21408)
This PR adds a generic set type to get rid of maps used as sets.
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Diffstat (limited to 'services')
-rw-r--r-- | services/issue/commit.go | 6 | ||||
-rw-r--r-- | services/mailer/mail_comment.go | 5 | ||||
-rw-r--r-- | services/mailer/mail_issue.go | 16 | ||||
-rw-r--r-- | services/pull/patch.go | 17 | ||||
-rw-r--r-- | services/pull/pull.go | 9 | ||||
-rw-r--r-- | services/release/release.go | 7 | ||||
-rw-r--r-- | services/repository/adopt.go | 7 |
7 files changed, 33 insertions, 34 deletions
diff --git a/services/issue/commit.go b/services/issue/commit.go index 0d04de81bc..c8cfa6cc8a 100644 --- a/services/issue/commit.go +++ b/services/issue/commit.go @@ -18,6 +18,7 @@ import ( access_model "code.gitea.io/gitea/models/perm/access" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/references" "code.gitea.io/gitea/modules/repository" @@ -111,7 +112,7 @@ func UpdateIssuesCommit(doer *user_model.User, repo *repo_model.Repository, comm Action references.XRefAction } - refMarked := make(map[markKey]bool) + refMarked := make(container.Set[markKey]) var refRepo *repo_model.Repository var refIssue *issues_model.Issue var err error @@ -144,10 +145,9 @@ func UpdateIssuesCommit(doer *user_model.User, repo *repo_model.Repository, comm } key := markKey{ID: refIssue.ID, Action: ref.Action} - if refMarked[key] { + if !refMarked.Add(key) { continue } - refMarked[key] = true // FIXME: this kind of condition is all over the code, it should be consolidated in a single place canclose := perm.IsAdmin() || perm.IsOwner() || perm.CanWriteIssuesOrPulls(refIssue.IsPull) || refIssue.PosterID == doer.ID diff --git a/services/mailer/mail_comment.go b/services/mailer/mail_comment.go index 2dab673b4e..af07821c29 100644 --- a/services/mailer/mail_comment.go +++ b/services/mailer/mail_comment.go @@ -10,6 +10,7 @@ import ( activities_model "code.gitea.io/gitea/models/activities" issues_model "code.gitea.io/gitea/models/issues" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" ) @@ -46,8 +47,8 @@ func MailMentionsComment(ctx context.Context, pr *issues_model.PullRequest, c *i return nil } - visited := make(map[int64]bool, len(mentions)+1) - visited[c.Poster.ID] = true + visited := make(container.Set[int64], len(mentions)+1) + visited.Add(c.Poster.ID) if err = mailIssueCommentBatch( &mailCommentContext{ Context: ctx, diff --git a/services/mailer/mail_issue.go b/services/mailer/mail_issue.go index ec6ddcf14e..15bfa4af41 100644 --- a/services/mailer/mail_issue.go +++ b/services/mailer/mail_issue.go @@ -14,6 +14,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" ) @@ -89,11 +90,11 @@ func mailIssueCommentToParticipants(ctx *mailCommentContext, mentions []*user_mo unfiltered = append(ids, unfiltered...) } - visited := make(map[int64]bool, len(unfiltered)+len(mentions)+1) + visited := make(container.Set[int64], len(unfiltered)+len(mentions)+1) // Avoid mailing the doer if ctx.Doer.EmailNotificationsPreference != user_model.EmailNotificationsAndYourOwn { - visited[ctx.Doer.ID] = true + visited.Add(ctx.Doer.ID) } // =========== Mentions =========== @@ -106,9 +107,7 @@ func mailIssueCommentToParticipants(ctx *mailCommentContext, mentions []*user_mo if err != nil { return fmt.Errorf("GetIssueWatchersIDs(%d): %v", ctx.Issue.ID, err) } - for _, i := range ids { - visited[i] = true - } + visited.AddMultiple(ids...) unfilteredUsers, err := user_model.GetMaileableUsersByIDs(unfiltered, false) if err != nil { @@ -121,7 +120,7 @@ func mailIssueCommentToParticipants(ctx *mailCommentContext, mentions []*user_mo return nil } -func mailIssueCommentBatch(ctx *mailCommentContext, users []*user_model.User, visited map[int64]bool, fromMention bool) error { +func mailIssueCommentBatch(ctx *mailCommentContext, users []*user_model.User, visited container.Set[int64], fromMention bool) error { checkUnit := unit.TypeIssues if ctx.Issue.IsPull { checkUnit = unit.TypePullRequests @@ -142,13 +141,10 @@ func mailIssueCommentBatch(ctx *mailCommentContext, users []*user_model.User, vi } // if we have already visited this user we exclude them - if _, ok := visited[user.ID]; ok { + if !visited.Add(user.ID) { continue } - // now mark them as visited - visited[user.ID] = true - // test if this user is allowed to see the issue/pull if !access_model.CheckRepoUnitUser(ctx, ctx.Issue.Repo, user, checkUnit) { continue diff --git a/services/pull/patch.go b/services/pull/patch.go index 32895b2e78..dafd577069 100644 --- a/services/pull/patch.go +++ b/services/pull/patch.go @@ -17,6 +17,7 @@ import ( "code.gitea.io/gitea/models" issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/unit" + "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" @@ -409,7 +410,7 @@ func checkConflicts(ctx context.Context, pr *issues_model.PullRequest, gitRepo * const appliedPatchPrefix = "Applied patch to '" const withConflicts = "' with conflicts." - conflictMap := map[string]bool{} + conflicts := make(container.Set[string]) // Now scan the output from the command scanner := bufio.NewScanner(stderrReader) @@ -418,7 +419,7 @@ func checkConflicts(ctx context.Context, pr *issues_model.PullRequest, gitRepo * if strings.HasPrefix(line, prefix) { conflict = true filepath := strings.TrimSpace(strings.Split(line[len(prefix):], ":")[0]) - conflictMap[filepath] = true + conflicts.Add(filepath) } else if is3way && line == threewayFailed { conflict = true } else if strings.HasPrefix(line, errorPrefix) { @@ -427,7 +428,7 @@ func checkConflicts(ctx context.Context, pr *issues_model.PullRequest, gitRepo * if strings.HasSuffix(line, suffix) { filepath := strings.TrimSpace(strings.TrimSuffix(line[len(errorPrefix):], suffix)) if filepath != "" { - conflictMap[filepath] = true + conflicts.Add(filepath) } break } @@ -436,18 +437,18 @@ func checkConflicts(ctx context.Context, pr *issues_model.PullRequest, gitRepo * conflict = true filepath := strings.TrimPrefix(strings.TrimSuffix(line, withConflicts), appliedPatchPrefix) if filepath != "" { - conflictMap[filepath] = true + conflicts.Add(filepath) } } // only list 10 conflicted files - if len(conflictMap) >= 10 { + if len(conflicts) >= 10 { break } } - if len(conflictMap) > 0 { - pr.ConflictedFiles = make([]string, 0, len(conflictMap)) - for key := range conflictMap { + if len(conflicts) > 0 { + pr.ConflictedFiles = make([]string, 0, len(conflicts)) + for key := range conflicts { pr.ConflictedFiles = append(pr.ConflictedFiles, key) } } diff --git a/services/pull/pull.go b/services/pull/pull.go index 103fdc340d..9de7cb5d4f 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -20,6 +20,7 @@ import ( issues_model "code.gitea.io/gitea/models/issues" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/json" @@ -640,7 +641,7 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ posterSig := pr.Issue.Poster.NewGitSig().String() - authorsMap := map[string]bool{} + uniqueAuthors := make(container.Set[string]) authors := make([]string, 0, len(commits)) stringBuilder := strings.Builder{} @@ -687,9 +688,8 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ } authorString := commit.Author.String() - if !authorsMap[authorString] && authorString != posterSig { + if uniqueAuthors.Add(authorString) && authorString != posterSig { authors = append(authors, authorString) - authorsMap[authorString] = true } } @@ -709,9 +709,8 @@ func GetSquashMergeCommitMessages(ctx context.Context, pr *issues_model.PullRequ } for _, commit := range commits { authorString := commit.Author.String() - if !authorsMap[authorString] && authorString != posterSig { + if uniqueAuthors.Add(authorString) && authorString != posterSig { authors = append(authors, authorString) - authorsMap[authorString] = true } } skip += limit diff --git a/services/release/release.go b/services/release/release.go index ae610b0e3c..187ebeb486 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -15,6 +15,7 @@ import ( git_model "code.gitea.io/gitea/models/git" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" @@ -209,7 +210,7 @@ func UpdateRelease(doer *user_model.User, gitRepo *git.Repository, rel *repo_mod return fmt.Errorf("AddReleaseAttachments: %v", err) } - deletedUUIDsMap := make(map[string]bool) + deletedUUIDs := make(container.Set[string]) if len(delAttachmentUUIDs) > 0 { // Check attachments attachments, err := repo_model.GetAttachmentsByUUIDs(ctx, delAttachmentUUIDs) @@ -220,7 +221,7 @@ func UpdateRelease(doer *user_model.User, gitRepo *git.Repository, rel *repo_mod if attach.ReleaseID != rel.ID { return errors.New("delete attachement of release permission denied") } - deletedUUIDsMap[attach.UUID] = true + deletedUUIDs.Add(attach.UUID) } if _, err := repo_model.DeleteAttachments(ctx, attachments, false); err != nil { @@ -245,7 +246,7 @@ func UpdateRelease(doer *user_model.User, gitRepo *git.Repository, rel *repo_mod } for uuid, newName := range editAttachments { - if !deletedUUIDsMap[uuid] { + if !deletedUUIDs.Contains(uuid) { if err = repo_model.UpdateAttachmentByUUID(ctx, &repo_model.Attachment{ UUID: uuid, Name: newName, diff --git a/services/repository/adopt.go b/services/repository/adopt.go index 74876d8e76..9e04c15977 100644 --- a/services/repository/adopt.go +++ b/services/repository/adopt.go @@ -14,6 +14,7 @@ import ( "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" @@ -257,12 +258,12 @@ func checkUnadoptedRepositories(userName string, repoNamesToCheck []string, unad if len(repos) == len(repoNamesToCheck) { return nil } - repoNames := make(map[string]bool, len(repos)) + repoNames := make(container.Set[string], len(repos)) for _, repo := range repos { - repoNames[repo.LowerName] = true + repoNames.Add(repo.LowerName) } for _, repoName := range repoNamesToCheck { - if _, ok := repoNames[repoName]; !ok { + if !repoNames.Contains(repoName) { unadopted.add(filepath.Join(userName, repoName)) } } |