Skip to content

Commit 2f0e79e

Browse files
HesterGsilverwindwxiaoguang
authored
Use frontend fetch for branch dropdown component (#25719)
- Send request to get branch/tag list, use loading icon when waiting for response. - Only fetch when the first time branch/tag list shows. - For backend, removed assignment to `ctx.Data["Branches"]` and `ctx.Data["Tags"]` from `context/repo.go` and passed these data wherever needed. - Changed some `v-if` to `v-show` and used native `svg` as mentioned in #25719 (comment) to improve perfomance when there are a lot of branches. - Places Used the dropdown component: Repo Home Page <img width="1429" alt="Screen Shot 2023-07-06 at 12 17 51" src="https://github.com/go-gitea/gitea/assets/17645053/6accc7b6-8d37-4e88-ae1a-bd2b3b927ea0"> Commits Page <img width="1431" alt="Screen Shot 2023-07-06 at 12 18 34" src="https://github.com/go-gitea/gitea/assets/17645053/2d0bf306-d1e2-45a8-a784-bc424879f537"> Specific commit -> operations -> cherry-pick <img width="758" alt="Screen Shot 2023-07-06 at 12 23 28" src="https://github.com/go-gitea/gitea/assets/17645053/1e557948-3881-4e45-a625-8ef36d45ae2d"> Release Page <img width="1433" alt="Screen Shot 2023-07-06 at 12 25 05" src="https://github.com/go-gitea/gitea/assets/17645053/3ec82af1-15a4-4162-a50b-04a9502161bb"> - Demo https://github.com/go-gitea/gitea/assets/17645053/d45d266b-3eb0-465a-82f9-57f78dc5f9f3 - Note: UI of dropdown menu could be improved in another PR as it should apply to more dropdown menus. Fix #14180 --------- Co-authored-by: silverwind <[email protected]> Co-authored-by: wxiaoguang <[email protected]>
1 parent dbbae67 commit 2f0e79e

File tree

13 files changed

+218
-66
lines changed

13 files changed

+218
-66
lines changed

modules/context/repo.go

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -660,13 +660,6 @@ func RepoAssignment(ctx *Context) context.CancelFunc {
660660
return cancel
661661
}
662662

663-
tags, err := repo_model.GetTagNamesByRepoID(ctx, ctx.Repo.Repository.ID)
664-
if err != nil {
665-
ctx.ServerError("GetTagNamesByRepoID", err)
666-
return cancel
667-
}
668-
ctx.Data["Tags"] = tags
669-
670663
branchOpts := git_model.FindBranchOptions{
671664
RepoID: ctx.Repo.Repository.ID,
672665
IsDeletedBranch: util.OptionalBoolFalse,
@@ -680,7 +673,7 @@ func RepoAssignment(ctx *Context) context.CancelFunc {
680673
return cancel
681674
}
682675

683-
// non empty repo should have at least 1 branch, so this repository's branches haven't been synced yet
676+
// non-empty repo should have at least 1 branch, so this repository's branches haven't been synced yet
684677
if branchesTotal == 0 { // fallback to do a sync immediately
685678
branchesTotal, err = repo_module.SyncRepoBranches(ctx, ctx.Repo.Repository.ID, 0)
686679
if err != nil {
@@ -689,24 +682,19 @@ func RepoAssignment(ctx *Context) context.CancelFunc {
689682
}
690683
}
691684

692-
// FIXME: use paganation and async loading
693-
branchOpts.ExcludeBranchNames = []string{ctx.Repo.Repository.DefaultBranch}
694-
brs, err := git_model.FindBranchNames(ctx, branchOpts)
695-
if err != nil {
696-
ctx.ServerError("GetBranches", err)
697-
return cancel
698-
}
699-
// always put default branch on the top
700-
ctx.Data["Branches"] = append(branchOpts.ExcludeBranchNames, brs...)
701685
ctx.Data["BranchesCount"] = branchesTotal
702686

703-
// If not branch selected, try default one.
704-
// If default branch doesn't exist, fall back to some other branch.
687+
// If no branch is set in the request URL, try to guess a default one.
705688
if len(ctx.Repo.BranchName) == 0 {
706689
if len(ctx.Repo.Repository.DefaultBranch) > 0 && gitRepo.IsBranchExist(ctx.Repo.Repository.DefaultBranch) {
707690
ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch
708-
} else if len(brs) > 0 {
709-
ctx.Repo.BranchName = brs[0]
691+
} else {
692+
ctx.Repo.BranchName, _ = gitRepo.GetDefaultBranch()
693+
if ctx.Repo.BranchName == "" {
694+
// If it still can't get a default branch, fall back to default branch from setting.
695+
// Something might be wrong. Either site admin should fix the repo sync or Gitea should fix a potential bug.
696+
ctx.Repo.BranchName = setting.Repository.DefaultBranch
697+
}
710698
}
711699
ctx.Repo.RefName = ctx.Repo.BranchName
712700
}

routers/web/repo/compare.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,12 @@ func CompareDiff(ctx *context.Context) {
754754
}
755755
ctx.Data["HeadBranches"] = headBranches
756756

757+
// For compare repo branches
758+
PrepareBranchList(ctx)
759+
if ctx.Written() {
760+
return
761+
}
762+
757763
headTags, err := repo_model.GetTagNamesByRepoID(ctx, ci.HeadRepo.ID)
758764
if err != nil {
759765
ctx.ServerError("GetTagNamesByRepoID", err)

routers/web/repo/issue.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -785,18 +785,10 @@ func RetrieveRepoMetas(ctx *context.Context, repo *repo_model.Repository, isPull
785785
return nil
786786
}
787787

788-
brs, err := git_model.FindBranchNames(ctx, git_model.FindBranchOptions{
789-
RepoID: ctx.Repo.Repository.ID,
790-
ListOptions: db.ListOptions{
791-
ListAll: true,
792-
},
793-
IsDeletedBranch: util.OptionalBoolFalse,
794-
})
795-
if err != nil {
796-
ctx.ServerError("GetBranches", err)
788+
PrepareBranchList(ctx)
789+
if ctx.Written() {
797790
return nil
798791
}
799-
ctx.Data["Branches"] = brs
800792

801793
// Contains true if the user can create issue dependencies
802794
ctx.Data["CanCreateIssueDependencies"] = ctx.Repo.CanCreateIssueDependencies(ctx.Doer, isPull)
@@ -921,6 +913,13 @@ func NewIssue(ctx *context.Context) {
921913

922914
RetrieveRepoMetas(ctx, ctx.Repo.Repository, false)
923915

916+
tags, err := repo_model.GetTagNamesByRepoID(ctx, ctx.Repo.Repository.ID)
917+
if err != nil {
918+
ctx.ServerError("GetTagNamesByRepoID", err)
919+
return
920+
}
921+
ctx.Data["Tags"] = tags
922+
924923
_, templateErrs := issue_service.GetTemplatesFromDefaultBranch(ctx.Repo.Repository, ctx.Repo.GitRepo)
925924
if errs := setTemplateIfExists(ctx, issueTemplateKey, IssueTemplateCandidates); len(errs) > 0 {
926925
for k, v := range errs {
@@ -1918,6 +1917,19 @@ func ViewIssue(ctx *context.Context) {
19181917
ctx.Data["ShouldShowCommentType"] = func(commentType issues_model.CommentType) bool {
19191918
return hiddenCommentTypes == nil || hiddenCommentTypes.Bit(int(commentType)) == 0
19201919
}
1920+
// For sidebar
1921+
PrepareBranchList(ctx)
1922+
1923+
if ctx.Written() {
1924+
return
1925+
}
1926+
1927+
tags, err := repo_model.GetTagNamesByRepoID(ctx, ctx.Repo.Repository.ID)
1928+
if err != nil {
1929+
ctx.ServerError("GetTagNamesByRepoID", err)
1930+
return
1931+
}
1932+
ctx.Data["Tags"] = tags
19211933

19221934
ctx.HTML(http.StatusOK, tplIssueView)
19231935
}

routers/web/repo/pull.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,11 @@ func ViewPullCommits(ctx *context.Context) {
729729
ctx.Data["HasIssuesOrPullsWritePermission"] = ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)
730730
ctx.Data["IsIssuePoster"] = ctx.IsSigned && issue.IsPoster(ctx.Doer.ID)
731731

732+
// For PR commits page
733+
PrepareBranchList(ctx)
734+
if ctx.Written() {
735+
return
736+
}
732737
getBranchData(ctx, issue)
733738
ctx.HTML(http.StatusOK, tplPullCommits)
734739
}
@@ -893,6 +898,11 @@ func ViewPullFiles(ctx *context.Context) {
893898
ctx.Data["HasIssuesOrPullsWritePermission"] = ctx.Repo.CanWriteIssuesOrPulls(issue.IsPull)
894899

895900
ctx.Data["IsAttachmentEnabled"] = setting.Attachment.Enabled
901+
// For files changed page
902+
PrepareBranchList(ctx)
903+
if ctx.Written() {
904+
return
905+
}
896906
upload.AddUploadContext(ctx, "comment")
897907

898908
ctx.HTML(http.StatusOK, tplPullFiles)

routers/web/repo/release.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,20 @@ func NewRelease(ctx *context.Context) {
352352
ctx.Data["Assignees"] = MakeSelfOnTop(ctx, assigneeUsers)
353353

354354
upload.AddUploadContext(ctx, "release")
355+
356+
// For New Release page
357+
PrepareBranchList(ctx)
358+
if ctx.Written() {
359+
return
360+
}
361+
362+
tags, err := repo_model.GetTagNamesByRepoID(ctx, ctx.Repo.Repository.ID)
363+
if err != nil {
364+
ctx.ServerError("GetTagNamesByRepoID", err)
365+
return
366+
}
367+
ctx.Data["Tags"] = tags
368+
355369
ctx.HTML(http.StatusOK, tplReleaseNew)
356370
}
357371

@@ -361,6 +375,13 @@ func NewReleasePost(ctx *context.Context) {
361375
ctx.Data["Title"] = ctx.Tr("repo.release.new_release")
362376
ctx.Data["PageIsReleaseList"] = true
363377

378+
tags, err := repo_model.GetTagNamesByRepoID(ctx, ctx.Repo.Repository.ID)
379+
if err != nil {
380+
ctx.ServerError("GetTagNamesByRepoID", err)
381+
return
382+
}
383+
ctx.Data["Tags"] = tags
384+
364385
if ctx.HasError() {
365386
ctx.HTML(http.StatusOK, tplReleaseNew)
366387
return

routers/web/repo/repo.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,3 +622,64 @@ func SearchRepo(ctx *context.Context) {
622622
Data: results,
623623
})
624624
}
625+
626+
type branchTagSearchResponse struct {
627+
Results []string `json:"results"`
628+
}
629+
630+
// GetBranchesList get branches for current repo'
631+
func GetBranchesList(ctx *context.Context) {
632+
branchOpts := git_model.FindBranchOptions{
633+
RepoID: ctx.Repo.Repository.ID,
634+
IsDeletedBranch: util.OptionalBoolFalse,
635+
ListOptions: db.ListOptions{
636+
ListAll: true,
637+
},
638+
}
639+
branches, err := git_model.FindBranchNames(ctx, branchOpts)
640+
if err != nil {
641+
ctx.JSON(http.StatusInternalServerError, err)
642+
return
643+
}
644+
resp := &branchTagSearchResponse{}
645+
// always put default branch on the top if it exists
646+
if util.SliceContains(branches, ctx.Repo.Repository.DefaultBranch) {
647+
branches = util.SliceRemoveAll(branches, ctx.Repo.Repository.DefaultBranch)
648+
branches = append([]string{ctx.Repo.Repository.DefaultBranch}, branches...)
649+
}
650+
resp.Results = branches
651+
ctx.JSON(http.StatusOK, resp)
652+
}
653+
654+
// GetTagList get tag list for current repo
655+
func GetTagList(ctx *context.Context) {
656+
tags, err := repo_model.GetTagNamesByRepoID(ctx, ctx.Repo.Repository.ID)
657+
if err != nil {
658+
ctx.JSON(http.StatusInternalServerError, err)
659+
return
660+
}
661+
resp := &branchTagSearchResponse{}
662+
resp.Results = tags
663+
ctx.JSON(http.StatusOK, resp)
664+
}
665+
666+
func PrepareBranchList(ctx *context.Context) {
667+
branchOpts := git_model.FindBranchOptions{
668+
RepoID: ctx.Repo.Repository.ID,
669+
IsDeletedBranch: util.OptionalBoolFalse,
670+
ListOptions: db.ListOptions{
671+
ListAll: true,
672+
},
673+
}
674+
brs, err := git_model.FindBranchNames(ctx, branchOpts)
675+
if err != nil {
676+
ctx.ServerError("GetBranches", err)
677+
return
678+
}
679+
// always put default branch on the top if it exists
680+
if util.SliceContains(brs, ctx.Repo.Repository.DefaultBranch) {
681+
brs = util.SliceRemoveAll(brs, ctx.Repo.Repository.DefaultBranch)
682+
brs = append([]string{ctx.Repo.Repository.DefaultBranch}, brs...)
683+
}
684+
ctx.Data["Branches"] = brs
685+
}

routers/web/repo/setting/protected_branch.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"code.gitea.io/gitea/modules/log"
2222
"code.gitea.io/gitea/modules/setting"
2323
"code.gitea.io/gitea/modules/web"
24+
"code.gitea.io/gitea/routers/web/repo"
2425
"code.gitea.io/gitea/services/forms"
2526
pull_service "code.gitea.io/gitea/services/pull"
2627
"code.gitea.io/gitea/services/repository"
@@ -44,6 +45,11 @@ func ProtectedBranchRules(ctx *context.Context) {
4445
}
4546
ctx.Data["ProtectedBranches"] = rules
4647

48+
repo.PrepareBranchList(ctx)
49+
if ctx.Written() {
50+
return
51+
}
52+
4753
ctx.HTML(http.StatusOK, tplBranches)
4854
}
4955

@@ -52,6 +58,11 @@ func SetDefaultBranchPost(ctx *context.Context) {
5258
ctx.Data["Title"] = ctx.Tr("repo.settings.branches.update_default_branch")
5359
ctx.Data["PageIsSettingsBranches"] = true
5460

61+
repo.PrepareBranchList(ctx)
62+
if ctx.Written() {
63+
return
64+
}
65+
5566
repo := ctx.Repo.Repository
5667

5768
switch ctx.FormString("action") {

routers/web/web.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,7 @@ func registerRoutes(m *web.Route) {
10941094
}, context.RepoRef(), canEnableEditor, context.RepoMustNotBeArchived())
10951095

10961096
m.Group("/branches", func() {
1097+
m.Get("/list", repo.GetBranchesList)
10971098
m.Group("/_new", func() {
10981099
m.Post("/branch/*", context.RepoRefByType(context.RepoRefBranch), repo.CreateBranch)
10991100
m.Post("/tag/*", context.RepoRefByType(context.RepoRefTag), repo.CreateBranch)
@@ -1108,6 +1109,7 @@ func registerRoutes(m *web.Route) {
11081109
m.Group("/{username}/{reponame}", func() {
11091110
m.Group("/tags", func() {
11101111
m.Get("", repo.TagsList)
1112+
m.Get("/list", repo.GetTagList)
11111113
m.Get(".rss", feedEnabled, repo.TagsListFeedRSS)
11121114
m.Get(".atom", feedEnabled, repo.TagsListFeedAtom)
11131115
}, ctxDataSet("EnableFeed", setting.Other.EnableFeed),

templates/repo/branch_dropdown.tmpl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
'tagName': {{.root.TagName}},
4545
'branchName': {{.root.BranchName}},
4646
'noTag': {{.noTag}},
47-
'branches': {{.root.Branches}},
48-
'tags': {{.root.Tags}},
4947
'defaultBranch': {{$defaultBranch}},
5048
'enableFeed': {{.root.EnableFeed}},
5149
'rssURLPrefix': '{{$.root.RepoLink}}/rss/branch/',

web_src/css/repo.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3359,3 +3359,7 @@ tbody.commit-list {
33593359
font-size: 18px;
33603360
margin-left: 4px;
33613361
}
3362+
3363+
#cherry-pick-modal .scrolling.menu {
3364+
max-height: 200px;
3365+
}

0 commit comments

Comments
 (0)