@@ -72,11 +72,12 @@ func Branches(ctx *context.Context) {
7272
7373 skip := (page - 1 ) * limit
7474 log .Debug ("Branches: skip: %d limit: %d" , skip , limit )
75- branches , branchesCount := loadBranches (ctx , skip , limit )
75+ defaultBranchBranch , branches , branchesCount := loadBranches (ctx , skip , limit )
7676 if ctx .Written () {
7777 return
7878 }
7979 ctx .Data ["Branches" ] = branches
80+ ctx .Data ["DefaultBranchBranch" ] = defaultBranchBranch
8081 pager := context .NewPagination (int (branchesCount ), setting .Git .BranchesRangeSize , page , 5 )
8182 pager .SetDefaultParams (ctx )
8283 ctx .Data ["Page" ] = pager
@@ -165,25 +166,28 @@ func redirect(ctx *context.Context) {
165166
166167// loadBranches loads branches from the repository limited by page & pageSize.
167168// NOTE: May write to context on error.
168- func loadBranches (ctx * context.Context , skip , limit int ) ([]* Branch , int ) {
169+ func loadBranches (ctx * context.Context , skip , limit int ) (* Branch , []* Branch , int ) {
169170 defaultBranch , err := ctx .Repo .GitRepo .GetBranch (ctx .Repo .Repository .DefaultBranch )
170171 if err != nil {
171- log .Error ("loadBranches: get default branch: %v" , err )
172- ctx .ServerError ("GetDefaultBranch" , err )
173- return nil , 0
172+ if ! git .IsErrBranchNotExist (err ) {
173+ log .Error ("loadBranches: get default branch: %v" , err )
174+ ctx .ServerError ("GetDefaultBranch" , err )
175+ return nil , nil , 0
176+ }
177+ log .Warn ("loadBranches: missing default branch %s for %-v" , ctx .Repo .Repository .DefaultBranch , ctx .Repo .Repository )
174178 }
175179
176180 rawBranches , totalNumOfBranches , err := ctx .Repo .GitRepo .GetBranches (skip , limit )
177181 if err != nil {
178182 log .Error ("GetBranches: %v" , err )
179183 ctx .ServerError ("GetBranches" , err )
180- return nil , 0
184+ return nil , nil , 0
181185 }
182186
183187 protectedBranches , err := models .GetProtectedBranches (ctx .Repo .Repository .ID )
184188 if err != nil {
185189 ctx .ServerError ("GetProtectedBranches" , err )
186- return nil , 0
190+ return nil , nil , 0
187191 }
188192
189193 repoIDToRepo := map [int64 ]* repo_model.Repository {}
@@ -194,36 +198,40 @@ func loadBranches(ctx *context.Context, skip, limit int) ([]*Branch, int) {
194198
195199 var branches []* Branch
196200 for i := range rawBranches {
197- if rawBranches [i ].Name == defaultBranch .Name {
201+ if defaultBranch != nil && rawBranches [i ].Name == defaultBranch .Name {
198202 // Skip default branch
199203 continue
200204 }
201205
202- var branch = loadOneBranch (ctx , rawBranches [i ], protectedBranches , repoIDToRepo , repoIDToGitRepo )
206+ var branch = loadOneBranch (ctx , rawBranches [i ], defaultBranch , protectedBranches , repoIDToRepo , repoIDToGitRepo )
203207 if branch == nil {
204- return nil , 0
208+ return nil , nil , 0
205209 }
206210
207211 branches = append (branches , branch )
208212 }
209213
210- // Always add the default branch
211- log .Debug ("loadOneBranch: load default: '%s'" , defaultBranch .Name )
212- branches = append (branches , loadOneBranch (ctx , defaultBranch , protectedBranches , repoIDToRepo , repoIDToGitRepo ))
214+ var defaultBranchBranch * Branch
215+ if defaultBranch != nil {
216+ // Always add the default branch
217+ log .Debug ("loadOneBranch: load default: '%s'" , defaultBranch .Name )
218+ defaultBranchBranch = loadOneBranch (ctx , defaultBranch , defaultBranch , protectedBranches , repoIDToRepo , repoIDToGitRepo )
219+ branches = append (branches , defaultBranchBranch )
220+ }
213221
214222 if ctx .Repo .CanWrite (unit .TypeCode ) {
215223 deletedBranches , err := getDeletedBranches (ctx )
216224 if err != nil {
217225 ctx .ServerError ("getDeletedBranches" , err )
218- return nil , 0
226+ return nil , nil , 0
219227 }
220228 branches = append (branches , deletedBranches ... )
221229 }
222230
223- return branches , totalNumOfBranches
231+ return defaultBranchBranch , branches , totalNumOfBranches
224232}
225233
226- func loadOneBranch (ctx * context.Context , rawBranch * git.Branch , protectedBranches []* models.ProtectedBranch ,
234+ func loadOneBranch (ctx * context.Context , rawBranch , defaultBranch * git.Branch , protectedBranches []* models.ProtectedBranch ,
227235 repoIDToRepo map [int64 ]* repo_model.Repository ,
228236 repoIDToGitRepo map [int64 ]* git.Repository ) * Branch {
229237 log .Trace ("loadOneBranch: '%s'" , rawBranch .Name )
@@ -243,10 +251,15 @@ func loadOneBranch(ctx *context.Context, rawBranch *git.Branch, protectedBranche
243251 }
244252 }
245253
246- divergence , divergenceError := files_service .CountDivergingCommits (ctx .Repo .Repository , git .BranchPrefix + branchName )
247- if divergenceError != nil {
248- ctx .ServerError ("CountDivergingCommits" , divergenceError )
249- return nil
254+ divergence := & git.DivergeObject {
255+ Ahead : - 1 ,
256+ Behind : - 1 ,
257+ }
258+ if defaultBranch != nil {
259+ divergence , err = files_service .CountDivergingCommits (ctx .Repo .Repository , git .BranchPrefix + branchName )
260+ if err != nil {
261+ log .Error ("CountDivergingCommits" , err )
262+ }
250263 }
251264
252265 pr , err := models .GetLatestPullRequestByHeadInfo (ctx .Repo .Repository .ID , branchName )
0 commit comments