mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 00:20:25 +08:00 
			
		
		
		
	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>
This commit is contained in:
		@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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,
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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,
 | 
			
		||||
 
 | 
			
		||||
@@ -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))
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user