use findbranch to search branch

This commit is contained in:
yp05327 2023-07-20 05:19:56 +00:00
parent e74709ed15
commit d93b2bcc80
10 changed files with 57 additions and 24 deletions

View File

@ -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)
}

View File

@ -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
}

View File

@ -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 {

View File

@ -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,

View File

@ -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

View File

@ -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

View File

@ -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,
},

View File

@ -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,
},

View File

@ -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,
},

View File

@ -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,