diff --git a/models/git/branch.go b/models/git/branch.go index 59c1f48b48..c0f95e7a89 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -396,7 +396,6 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to str // doer should not be nil // TODO use options to find the branches func FindRecentlyPushedNewBranches(ctx context.Context, baseRepo *repo_model.Repository, doer *user_model.User) (BranchList, error) { - branches := make(BranchList, 0, 2) baseBranch, err := GetBranch(ctx, baseRepo.ID, baseRepo.DefaultBranch) if err != nil { return nil, err @@ -417,14 +416,16 @@ func FindRecentlyPushedNewBranches(ctx context.Context, baseRepo *repo_model.Rep builder.In("pull_request.head_repo_id", repoCond), builder.Eq{"branch.id": baseBranch.ID}, )) - cond := builder.And( - builder.Neq{"commit_id": baseBranch.CommitID}, // newly created branch have no changes, so skip them - builder.Eq{"pusher_id": doer.ID}, - builder.Eq{"is_deleted": false}, - builder.Gte{"updated_unix": time.Now().Add(-time.Hour * 6).Unix()}, - builder.In("repo_id", repoCond), - builder.NotIn("id", invalidBranchCond), - ) - err = db.GetEngine(ctx).Where(cond).OrderBy("branch.updated_unix DESC").Limit(2).Find(&branches) - return branches, err + + opts := FindBranchOptions{ + IDCond: builder.NotIn("id", invalidBranchCond), + RepoCond: builder.In("repo_id", repoCond), + CommitCond: builder.Neq{"commit_id": baseBranch.CommitID}, // newly created branch have no changes, so skip them, + PusherID: doer.ID, + IsDeletedBranch: util.OptionalBoolFalse, + OrderBy: "branch.updated_unix DESC", + } + opts.PageSize = 2 + opts.Page = 1 + return FindBranches(ctx, opts) } diff --git a/models/git/branch_list.go b/models/git/branch_list.go index a87e93c05a..b089405a3a 100644 --- a/models/git/branch_list.go +++ b/models/git/branch_list.go @@ -10,6 +10,7 @@ import ( 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/timeutil" "code.gitea.io/gitea/modules/util" "xorm.io/builder" @@ -87,24 +88,55 @@ func (branches BranchList) LoadRepo(ctx context.Context) error { type FindBranchOptions struct { db.ListOptions - RepoID int64 + IDCond builder.Cond + RepoIDs []int64 // overwrites RepoCond if the length is not 0 + RepoCond builder.Cond ExcludeBranchNames []string + CommitCond builder.Cond + PusherID int64 IsDeletedBranch util.OptionalBool + UpdatedAfterUnix timeutil.TimeStamp + UpdatedBeforeUnix timeutil.TimeStamp OrderBy string } func (opts *FindBranchOptions) Cond() builder.Cond { cond := builder.NewCond() - if opts.RepoID > 0 { - cond = cond.And(builder.Eq{"repo_id": opts.RepoID}) + if opts.IDCond != nil { + cond = cond.And(opts.IDCond) + } + + if len(opts.RepoIDs) == 1 { + opts.RepoCond = builder.Eq{"issue.repo_id": opts.RepoIDs[0]} + } else if len(opts.RepoIDs) > 1 { + opts.RepoCond = builder.In("issue.repo_id", opts.RepoIDs) + } + if opts.RepoCond != nil { + cond = cond.And(opts.RepoCond) } if len(opts.ExcludeBranchNames) > 0 { cond = cond.And(builder.NotIn("name", opts.ExcludeBranchNames)) } + + if opts.CommitCond != nil { + cond = cond.And(opts.CommitCond) + } + + if opts.PusherID > 0 { + cond = cond.And(builder.Eq{"pusher_id": opts.PusherID}) + } + if !opts.IsDeletedBranch.IsNone() { cond = cond.And(builder.Eq{"is_deleted": opts.IsDeletedBranch.IsTrue()}) } + + if opts.UpdatedAfterUnix != 0 { + cond = cond.And(builder.Gte{"updated_unix": opts.UpdatedAfterUnix}) + } + if opts.UpdatedBeforeUnix != 0 { + cond = cond.And(builder.Lte{"updated_unix": opts.UpdatedBeforeUnix}) + } return cond } diff --git a/models/git/protected_branch_list.go b/models/git/protected_branch_list.go index eeb307e245..f67bb14d17 100644 --- a/models/git/protected_branch_list.go +++ b/models/git/protected_branch_list.go @@ -55,7 +55,7 @@ func FindAllMatchedBranches(ctx context.Context, repoID int64, ruleName string) PageSize: 100, Page: page, }, - RepoID: repoID, + RepoIDs: []int64{repoID}, IsDeletedBranch: util.OptionalBoolFalse, }) if err != nil { diff --git a/modules/context/repo.go b/modules/context/repo.go index eae71cfb7b..6eb08bef58 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -668,7 +668,7 @@ func RepoAssignment(ctx *Context) context.CancelFunc { ctx.Data["Tags"] = tags branchOpts := git_model.FindBranchOptions{ - RepoID: ctx.Repo.Repository.ID, + RepoIDs: []int64{ctx.Repo.Repository.ID}, IsDeletedBranch: util.OptionalBoolFalse, ListOptions: db.ListOptions{ ListAll: true, diff --git a/modules/repository/branch.go b/modules/repository/branch.go index bffadd62f4..2c45dbb7f2 100644 --- a/modules/repository/branch.go +++ b/modules/repository/branch.go @@ -53,7 +53,7 @@ func SyncRepoBranchesWithRepo(ctx context.Context, repo *repo_model.Repository, ListOptions: db.ListOptions{ ListAll: true, }, - RepoID: repo.ID, + RepoIDs: []int64{repo.ID}, }) if err != nil { return 0, err diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 5e2c9878f0..62c7764038 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -142,7 +142,7 @@ func DeleteBranch(ctx *context.APIContext) { // check whether branches of this repository has been synced totalNumOfBranches, err := git_model.CountBranches(ctx, git_model.FindBranchOptions{ - RepoID: ctx.Repo.Repository.ID, + RepoIDs: []int64{ctx.Repo.Repository.ID}, IsDeletedBranch: util.OptionalBoolFalse, }) if err != nil { @@ -349,7 +349,7 @@ func ListBranches(ctx *context.APIContext) { branchOpts := git_model.FindBranchOptions{ ListOptions: listOptions, - RepoID: ctx.Repo.Repository.ID, + RepoIDs: []int64{ctx.Repo.Repository.ID}, IsDeletedBranch: util.OptionalBoolFalse, } var err error diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 7089c219ad..68ccfcb1a5 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -685,7 +685,7 @@ func getBranchesAndTagsForRepo(ctx gocontext.Context, repo *repo_model.Repositor defer gitRepo.Close() branches, err = git_model.FindBranchNames(ctx, git_model.FindBranchOptions{ - RepoID: repo.ID, + RepoIDs: []int64{repo.ID}, ListOptions: db.ListOptions{ ListAll: true, }, @@ -742,7 +742,7 @@ func CompareDiff(ctx *context.Context) { } headBranches, err := git_model.FindBranchNames(ctx, git_model.FindBranchOptions{ - RepoID: ci.HeadRepo.ID, + RepoIDs: []int64{ci.HeadRepo.ID}, ListOptions: db.ListOptions{ ListAll: true, }, diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index bd8959846c..603f5a3fad 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -786,7 +786,7 @@ func RetrieveRepoMetas(ctx *context.Context, repo *repo_model.Repository, isPull } brs, err := git_model.FindBranchNames(ctx, git_model.FindBranchOptions{ - RepoID: ctx.Repo.Repository.ID, + RepoIDs: []int64{ctx.Repo.Repository.ID}, ListOptions: db.ListOptions{ ListAll: true, }, diff --git a/services/repository/adopt.go b/services/repository/adopt.go index f95fb5988f..450c0371fc 100644 --- a/services/repository/adopt.go +++ b/services/repository/adopt.go @@ -149,7 +149,7 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r } branches, _ := git_model.FindBranchNames(ctx, git_model.FindBranchOptions{ - RepoID: repo.ID, + RepoIDs: []int64{repo.ID}, ListOptions: db.ListOptions{ ListAll: true, }, diff --git a/services/repository/branch.go b/services/repository/branch.go index 11a8b20531..3dd4236129 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -73,7 +73,7 @@ func LoadBranches(ctx context.Context, repo *repo_model.Repository, gitRepo *git } branchOpts := git_model.FindBranchOptions{ - RepoID: repo.ID, + RepoIDs: []int64{repo.ID}, IsDeletedBranch: isDeletedBranch, ListOptions: db.ListOptions{ Page: page,