@@ -252,47 +252,50 @@ func ListBranches(ctx *context.APIContext) {
252252 // "200":
253253 // "$ref": "#/responses/BranchList"
254254
255- if ctx .Repo .Repository .IsEmpty || ctx .Repo .GitRepo == nil {
256- ctx .JSON (http .StatusOK , & []* api.Branch {})
257- return
258- }
255+ var totalNumOfBranches int64
256+ var apiBranches []* api.Branch
259257
260258 listOptions := utils .GetListOptions (ctx )
261- skip , _ := listOptions .GetStartEnd ()
262- branches , totalNumOfBranches , err := ctx .Repo .GitRepo .GetBranches (skip , listOptions .PageSize )
263- if err != nil {
264- ctx .Error (http .StatusInternalServerError , "GetBranches" , err )
265- return
266- }
267259
268- apiBranches := make ([]* api.Branch , 0 , len (branches ))
269- for i := range branches {
270- c , err := branches [i ].GetCommit ()
271- if err != nil {
272- // Skip if this branch doesn't exist anymore.
273- if git .IsErrNotExist (err ) {
274- totalNumOfBranches --
275- continue
276- }
277- ctx .Error (http .StatusInternalServerError , "GetCommit" , err )
278- return
279- }
280- branchProtection , err := git_model .GetProtectedBranchBy (ctx , ctx .Repo .Repository .ID , branches [i ].Name )
260+ if ! ctx .Repo .Repository .IsEmpty && ctx .Repo .GitRepo != nil {
261+ skip , _ := listOptions .GetStartEnd ()
262+ branches , total , err := ctx .Repo .GitRepo .GetBranches (skip , listOptions .PageSize )
281263 if err != nil {
282- ctx .Error (http .StatusInternalServerError , "GetBranchProtection " , err )
264+ ctx .Error (http .StatusInternalServerError , "GetBranches " , err )
283265 return
284266 }
285- apiBranch , err := convert .ToBranch (ctx .Repo .Repository , branches [i ], c , branchProtection , ctx .Doer , ctx .Repo .IsAdmin ())
286- if err != nil {
287- ctx .Error (http .StatusInternalServerError , "convert.ToBranch" , err )
288- return
267+
268+ apiBranches = make ([]* api.Branch , 0 , len (branches ))
269+ for i := range branches {
270+ c , err := branches [i ].GetCommit ()
271+ if err != nil {
272+ // Skip if this branch doesn't exist anymore.
273+ if git .IsErrNotExist (err ) {
274+ total --
275+ continue
276+ }
277+ ctx .Error (http .StatusInternalServerError , "GetCommit" , err )
278+ return
279+ }
280+ branchProtection , err := git_model .GetProtectedBranchBy (ctx , ctx .Repo .Repository .ID , branches [i ].Name )
281+ if err != nil {
282+ ctx .Error (http .StatusInternalServerError , "GetProtectedBranchBy" , err )
283+ return
284+ }
285+ apiBranch , err := convert .ToBranch (ctx .Repo .Repository , branches [i ], c , branchProtection , ctx .Doer , ctx .Repo .IsAdmin ())
286+ if err != nil {
287+ ctx .Error (http .StatusInternalServerError , "convert.ToBranch" , err )
288+ return
289+ }
290+ apiBranches = append (apiBranches , apiBranch )
289291 }
290- apiBranches = append (apiBranches , apiBranch )
292+
293+ totalNumOfBranches = total
291294 }
292295
293296 ctx .SetLinkHeader (totalNumOfBranches , listOptions .PageSize )
294297 ctx .SetTotalCountHeader (int64 (totalNumOfBranches ))
295- ctx .JSON (http .StatusOK , & apiBranches )
298+ ctx .JSON (http .StatusOK , apiBranches )
296299}
297300
298301// GetBranchProtection gets a branch protection
0 commit comments