From 47b878858ada27fc4c74eeadcc1e467d2da90e04 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 17 Sep 2023 16:24:40 +0800 Subject: [PATCH] Search branches (#27055) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolve #25233 图片 图片 --- models/git/branch_list.go | 4 ++++ options/locale/locale_en-US.ini | 1 + routers/web/repo/branch.go | 5 ++++- services/repository/branch.go | 3 ++- templates/repo/branch/list.tmpl | 15 +++++++++++++-- 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/models/git/branch_list.go b/models/git/branch_list.go index 131a149782..b5c1301a1d 100644 --- a/models/git/branch_list.go +++ b/models/git/branch_list.go @@ -70,6 +70,7 @@ type FindBranchOptions struct { ExcludeBranchNames []string IsDeletedBranch util.OptionalBool OrderBy string + Keyword string } func (opts *FindBranchOptions) Cond() builder.Cond { @@ -84,6 +85,9 @@ func (opts *FindBranchOptions) Cond() builder.Cond { if !opts.IsDeletedBranch.IsNone() { cond = cond.And(builder.Eq{"is_deleted": opts.IsDeletedBranch.IsTrue()}) } + if opts.Keyword != "" { + cond = cond.And(builder.Like{"name", opts.Keyword}) + } return cond } diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index c38c9d9e46..e11d5167aa 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -2515,6 +2515,7 @@ branch.default_deletion_failed = Branch "%s" is the default branch. It cannot be branch.restore = Restore Branch "%s" branch.download = Download Branch "%s" branch.rename = Rename Branch "%s" +branch.search = Search Branch branch.included_desc = This branch is part of the default branch branch.included = Included branch.create_new_branch = Create branch from branch: diff --git a/routers/web/repo/branch.go b/routers/web/repo/branch.go index f5831df28f..e0e27fd482 100644 --- a/routers/web/repo/branch.go +++ b/routers/web/repo/branch.go @@ -51,7 +51,9 @@ func Branches(ctx *context.Context) { } pageSize := setting.Git.BranchesRangeSize - defaultBranch, branches, branchesCount, err := repo_service.LoadBranches(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, util.OptionalBoolNone, page, pageSize) + kw := ctx.FormString("q") + + defaultBranch, branches, branchesCount, err := repo_service.LoadBranches(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, util.OptionalBoolNone, kw, page, pageSize) if err != nil { ctx.ServerError("LoadBranches", err) return @@ -73,6 +75,7 @@ func Branches(ctx *context.Context) { commitStatus[commitID] = git_model.CalcCommitStatus(cs) } + ctx.Data["Keyword"] = kw ctx.Data["Branches"] = branches ctx.Data["CommitStatus"] = commitStatus ctx.Data["CommitStatuses"] = commitStatuses diff --git a/services/repository/branch.go b/services/repository/branch.go index 620e0b6c9f..011dc5568e 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -66,7 +66,7 @@ type Branch struct { } // LoadBranches loads branches from the repository limited by page & pageSize. -func LoadBranches(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, isDeletedBranch util.OptionalBool, page, pageSize int) (*Branch, []*Branch, int64, error) { +func LoadBranches(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, isDeletedBranch util.OptionalBool, keyword string, page, pageSize int) (*Branch, []*Branch, int64, error) { defaultDBBranch, err := git_model.GetBranch(ctx, repo.ID, repo.DefaultBranch) if err != nil { return nil, nil, 0, err @@ -79,6 +79,7 @@ func LoadBranches(ctx context.Context, repo *repo_model.Repository, gitRepo *git Page: page, PageSize: pageSize, }, + Keyword: keyword, } totalNumOfBranches, err := git_model.CountBranches(ctx, branchOpts) diff --git a/templates/repo/branch/list.tmpl b/templates/repo/branch/list.tmpl index d2535e1e30..f309b1c60a 100644 --- a/templates/repo/branch/list.tmpl +++ b/templates/repo/branch/list.tmpl @@ -70,9 +70,20 @@ {{end}} {{if .Branches}} -

- {{.locale.Tr "repo.branches"}} +

+
+ {{.locale.Tr "repo.branches"}} +
+
+
+ + +
+

+