@@ -14,6 +14,7 @@ import (
1414 "net/http"
1515 "net/url"
1616 "path/filepath"
17+ "slices"
1718 "strings"
1819
1920 "code.gitea.io/gitea/models/db"
@@ -193,7 +194,7 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
193194 pathParam := ctx .PathParam ("*" )
194195 baseRepo := ctx .Repo .Repository
195196
196- ci , err := common .ParseComparePathParams (ctx , pathParam , baseRepo , ctx .Repo .GitRepo )
197+ ci , err := common .ParseComparePathParams (ctx , pathParam , baseRepo , ctx .Repo .GitRepo , ctx . Doer )
197198 if err != nil {
198199 switch {
199200 case user_model .IsErrUserNotExist (err ):
@@ -207,7 +208,6 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
207208 }
208209 return nil
209210 }
210- defer ci .Close ()
211211
212212 // remove the check when we support compare with carets
213213 if ci .CaretTimes > 0 {
@@ -245,8 +245,6 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
245245 ctx .Data ["CanWriteToHeadRepo" ] = permHead .CanWrite (unit .TypeCode )
246246 }
247247
248- // TODO: prepareRepos, branches and tags for dropdowns
249-
250248 ctx .Data ["PageIsComparePull" ] = ci .IsPull () && ctx .Repo .CanReadIssuesOrPulls (true )
251249 ctx .Data ["BaseName" ] = baseRepo .OwnerName
252250 ctx .Data ["BaseBranch" ] = ci .BaseOriRef
@@ -258,7 +256,6 @@ func ParseCompareInfo(ctx *context.Context) *common.CompareInfo {
258256 ctx .Data ["BaseIsBranch" ] = ci .BaseFullRef .IsBranch ()
259257 ctx .Data ["BaseIsTag" ] = ci .BaseFullRef .IsTag ()
260258 ctx .Data ["IsPull" ] = true
261- // ctx.Data["OwnForkRepo"] = ownForkRepo FIXME: This is not used
262259
263260 ctx .Data ["HeadRepo" ] = ci .HeadRepo
264261 ctx .Data ["BaseCompareRepo" ] = ctx .Repo .Repository
@@ -399,34 +396,91 @@ func PrepareCompareDiff(
399396 return false
400397}
401398
402- func getBranchesAndTagsForRepo (ctx gocontext.Context , repo * repo_model.Repository ) (branches , tags []string , err error ) {
403- gitRepo , err := gitrepo .OpenRepository (ctx , repo )
404- if err != nil {
405- return nil , nil , err
406- }
407- defer gitRepo .Close ()
408-
409- branches , err = git_model .FindBranchNames (ctx , git_model.FindBranchOptions {
399+ func getBranchesAndTagsForRepo (ctx gocontext.Context , repo * repo_model.Repository ) ([]string , []string , error ) {
400+ branches , err := git_model .FindBranchNames (ctx , git_model.FindBranchOptions {
410401 RepoID : repo .ID ,
411402 ListOptions : db .ListOptionsAll ,
412403 IsDeletedBranch : optional .Some (false ),
413404 })
414405 if err != nil {
415406 return nil , nil , err
416407 }
417- tags , err = gitRepo .GetTags (0 , 0 )
408+ // always put default branch on the top if it exists
409+ if slices .Contains (branches , repo .DefaultBranch ) {
410+ branches = util .SliceRemoveAll (branches , repo .DefaultBranch )
411+ branches = append ([]string {repo .DefaultBranch }, branches ... )
412+ }
413+
414+ tags , err := repo_model .GetTagNamesByRepoID (ctx , repo .ID )
418415 if err != nil {
419416 return nil , nil , err
420417 }
418+
421419 return branches , tags , nil
422420}
423421
422+ func prepareCompareRepoBranchesTagsDropdowns (ctx * context.Context , ci * common.CompareInfo ) {
423+ baseRepo := ctx .Repo .Repository
424+ // For compare repo branches
425+ baseBranches , baseTags , err := getBranchesAndTagsForRepo (ctx , baseRepo )
426+ if err != nil {
427+ ctx .ServerError ("getBranchesAndTagsForRepo" , err )
428+ return
429+ }
430+
431+ ctx .Data ["Branches" ] = baseBranches
432+ ctx .Data ["Tags" ] = baseTags
433+
434+ if ci .IsSameRepo () {
435+ ctx .Data ["HeadBranches" ] = baseBranches
436+ ctx .Data ["HeadTags" ] = baseTags
437+ } else {
438+ headBranches , headTags , err := getBranchesAndTagsForRepo (ctx , ci .HeadRepo )
439+ if err != nil {
440+ ctx .ServerError ("getBranchesAndTagsForRepo" , err )
441+ return
442+ }
443+ ctx .Data ["HeadBranches" ] = headBranches
444+ ctx .Data ["HeadTags" ] = headTags
445+ }
446+
447+ if ci .RootRepo != nil &&
448+ ci .RootRepo .ID != ci .HeadRepo .ID &&
449+ ci .RootRepo .ID != baseRepo .ID {
450+ canRead := access_model .CheckRepoUnitUser (ctx , ci .RootRepo , ctx .Doer , unit .TypeCode )
451+ if canRead {
452+ ctx .Data ["RootRepo" ] = ci .RootRepo
453+ branches , tags , err := getBranchesAndTagsForRepo (ctx , ci .RootRepo )
454+ if err != nil {
455+ ctx .ServerError ("GetBranchesForRepo" , err )
456+ return
457+ }
458+ ctx .Data ["RootRepoBranches" ] = branches
459+ ctx .Data ["RootRepoTags" ] = tags
460+ }
461+ }
462+
463+ if ci .OwnForkRepo != nil &&
464+ ci .OwnForkRepo .ID != ci .HeadRepo .ID &&
465+ ci .OwnForkRepo .ID != baseRepo .ID &&
466+ (ci .RootRepo == nil || ci .OwnForkRepo .ID != ci .RootRepo .ID ) {
467+ ctx .Data ["OwnForkRepo" ] = ci .OwnForkRepo
468+ branches , tags , err := getBranchesAndTagsForRepo (ctx , ci .OwnForkRepo )
469+ if err != nil {
470+ ctx .ServerError ("GetBranchesForRepo" , err )
471+ return
472+ }
473+ ctx .Data ["OwnForkRepoBranches" ] = branches
474+ ctx .Data ["OwnForkRepoTags" ] = tags
475+ }
476+ }
477+
424478// CompareDiff show different from one commit to another commit
425479func CompareDiff (ctx * context.Context ) {
426480 ci := ParseCompareInfo (ctx )
427481 defer func () {
428- if ci != nil && ci . HeadGitRepo != nil {
429- ci .HeadGitRepo . Close ()
482+ if ci != nil {
483+ ci .Close ()
430484 }
431485 }()
432486 if ctx .Written () {
@@ -448,43 +502,17 @@ func CompareDiff(ctx *context.Context) {
448502 return
449503 }
450504
451- baseTags , err := repo_model .GetTagNamesByRepoID (ctx , ctx .Repo .Repository .ID )
452- if err != nil {
453- ctx .ServerError ("GetTagNamesByRepoID" , err )
454- return
455- }
456- ctx .Data ["Tags" ] = baseTags
457-
458505 fileOnly := ctx .FormBool ("file-only" )
459506 if fileOnly {
460507 ctx .HTML (http .StatusOK , tplDiffBox )
461508 return
462509 }
463510
464- headBranches , err := git_model .FindBranchNames (ctx , git_model.FindBranchOptions {
465- RepoID : ci .HeadRepo .ID ,
466- ListOptions : db .ListOptionsAll ,
467- IsDeletedBranch : optional .Some (false ),
468- })
469- if err != nil {
470- ctx .ServerError ("GetBranches" , err )
471- return
472- }
473- ctx .Data ["HeadBranches" ] = headBranches
474-
475- // For compare repo branches
476- PrepareBranchList (ctx )
511+ prepareCompareRepoBranchesTagsDropdowns (ctx , ci )
477512 if ctx .Written () {
478513 return
479514 }
480515
481- headTags , err := repo_model .GetTagNamesByRepoID (ctx , ci .HeadRepo .ID )
482- if err != nil {
483- ctx .ServerError ("GetTagNamesByRepoID" , err )
484- return
485- }
486- ctx .Data ["HeadTags" ] = headTags
487-
488516 if ctx .Data ["PageIsComparePull" ] == true {
489517 pr , err := issues_model .GetUnmergedPullRequest (ctx , ci .HeadRepo .ID , ctx .Repo .Repository .ID , ci .HeadOriRef , ci .BaseOriRef , issues_model .PullRequestFlowGithub )
490518 if err != nil {
0 commit comments