From 5217c93fb10be9799d5a22886ea77afcaebdbe22 Mon Sep 17 00:00:00 2001 From: HesterG Date: Tue, 4 Jul 2023 11:59:49 +0800 Subject: [PATCH 01/46] save for branches --- models/git/branch_list.go | 16 ++++++ routers/web/repo/view.go | 32 +++++++++++ routers/web/web.go | 1 + templates/repo/branch_dropdown.tmpl | 1 - .../js/components/RepoBranchTagSelector.vue | 55 +++++++++++-------- 5 files changed, 82 insertions(+), 23 deletions(-) diff --git a/models/git/branch_list.go b/models/git/branch_list.go index 131a149782e27..1f36c4a775973 100644 --- a/models/git/branch_list.go +++ b/models/git/branch_list.go @@ -5,6 +5,7 @@ package git import ( "context" + "fmt" "code.gitea.io/gitea/models/db" user_model "code.gitea.io/gitea/models/user" @@ -143,3 +144,18 @@ func FindBranchesByRepoAndBranchName(ctx context.Context, repoBranches map[int64 } return branchMap, nil } + +func FindBranchesWithSearch(ctx context.Context, opts FindBranchOptions, search string) ([]string, error) { + fmt.Println(search) + branches := make([]string, 0, 30) + var prefixCond builder.Cond = builder.Like{"name", "%" + search + "%"} + sess := db.GetEngine(ctx).Select("name").Where(opts.Cond()).And(prefixCond) + if opts.PageSize > 0 && !opts.IsListAll() { + sess = db.SetSessionPagination(sess, &opts.ListOptions) + } + sess = orderByBranches(sess, opts) + if err := sess.Table("branch").Limit(30).Find(&branches); err != nil { + return nil, err + } + return branches, nil +} diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index ece2ec5416a5c..8563793abb950 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -13,6 +13,7 @@ import ( "net/http" "net/url" "path" + "sort" "strings" "time" @@ -1085,3 +1086,34 @@ func Forks(ctx *context.Context) { ctx.HTML(http.StatusOK, tplForks) } + +type branchSearchResponse struct { + Branches []string `json:"branches"` +} + +// GetBranches get branches for current repo' +func GetBranches(ctx *context.Context) { + search := strings.TrimSpace(ctx.FormString("q")) + branchOpts := git_model.FindBranchOptions{ + RepoID: ctx.Repo.Repository.ID, + IsDeletedBranch: util.OptionalBoolFalse, + ListOptions: db.ListOptions{ + ListAll: true, + }, + } + branches, err := git_model.FindBranchesWithSearch(ctx, branchOpts, search) + if err != nil { + ctx.JSON(http.StatusInternalServerError, err) + return + } + // always put default branch on the top + sort.Slice(branches, func(i, j int) bool { + if branches[i] == branches[j] { + return false + } + return branches[i] == ctx.Repo.Repository.DefaultBranch + }) + resp := &branchSearchResponse{} + resp.Branches = branches + ctx.JSON(http.StatusOK, resp) +} diff --git a/routers/web/web.go b/routers/web/web.go index 00e0ca36dab0e..7cea23659d6bd 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1368,6 +1368,7 @@ func registerRoutes(m *web.Route) { m.Group("/{username}", func() { m.Group("/{reponame}", func() { m.Get("", repo.SetEditorconfigIfExists, repo.Home) + m.Get("/branches", repo.GetBranches) }, ignSignIn, context.RepoAssignment, context.RepoRef(), context.UnitTypes()) m.Group("/{reponame}", func() { diff --git a/templates/repo/branch_dropdown.tmpl b/templates/repo/branch_dropdown.tmpl index 984899552dc0b..0a12ba93b04a1 100644 --- a/templates/repo/branch_dropdown.tmpl +++ b/templates/repo/branch_dropdown.tmpl @@ -44,7 +44,6 @@ 'tagName': {{.root.TagName}}, 'branchName': {{.root.BranchName}}, 'noTag': {{.noTag}}, - 'branches': {{.root.Branches}}, 'tags': {{.root.Tags}}, 'defaultBranch': {{$defaultBranch}}, 'enableFeed': {{.root.EnableFeed}}, diff --git a/web_src/js/components/RepoBranchTagSelector.vue b/web_src/js/components/RepoBranchTagSelector.vue index 4fc393624469d..fad0cbd808ba3 100644 --- a/web_src/js/components/RepoBranchTagSelector.vue +++ b/web_src/js/components/RepoBranchTagSelector.vue @@ -1,6 +1,6 @@