mirror of https://github.com/go-gitea/gitea
use findbranch to search branch
This commit is contained in:
parent
e74709ed15
commit
d93b2bcc80
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
|
|
|
@ -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,
|
||||
},
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue