@@ -251,42 +251,50 @@ func ListBranches(ctx *context.APIContext) {
251251 // "200":
252252 // "$ref": "#/responses/BranchList"
253253
254+ var totalNumOfBranches int
255+ var apiBranches []* api.Branch
256+
254257 listOptions := utils .GetListOptions (ctx )
255- skip , _ := listOptions .GetStartEnd ()
256- branches , totalNumOfBranches , err := ctx .Repo .GitRepo .GetBranches (skip , listOptions .PageSize )
257- if err != nil {
258- ctx .Error (http .StatusInternalServerError , "GetBranches" , err )
259- return
260- }
261258
262- apiBranches := make ([] * api. Branch , 0 , len ( branches ))
263- for i := range branches {
264- c , err := branches [ i ]. GetCommit ( )
259+ if ! ctx . Repo . Repository . IsEmpty && ctx . Repo . GitRepo != nil {
260+ skip , _ := listOptions . GetStartEnd ()
261+ branches , total , err := ctx . Repo . GitRepo . GetBranches ( skip , listOptions . PageSize )
265262 if err != nil {
266- // Skip if this branch doesn't exist anymore.
267- if git .IsErrNotExist (err ) {
268- totalNumOfBranches --
269- continue
270- }
271- ctx .Error (http .StatusInternalServerError , "GetCommit" , err )
263+ ctx .Error (http .StatusInternalServerError , "GetBranches" , err )
272264 return
273265 }
274- branchProtection , err := git_model .GetProtectedBranchBy (ctx , ctx .Repo .Repository .ID , branches [i ].Name )
275- if err != nil {
276- ctx .Error (http .StatusInternalServerError , "GetBranchProtection" , err )
277- return
278- }
279- apiBranch , err := convert .ToBranch (ctx .Repo .Repository , branches [i ], c , branchProtection , ctx .Doer , ctx .Repo .IsAdmin ())
280- if err != nil {
281- ctx .Error (http .StatusInternalServerError , "convert.ToBranch" , err )
282- return
266+
267+ apiBranches = make ([]* api.Branch , 0 , len (branches ))
268+ for i := range branches {
269+ c , err := branches [i ].GetCommit ()
270+ if err != nil {
271+ // Skip if this branch doesn't exist anymore.
272+ if git .IsErrNotExist (err ) {
273+ total --
274+ continue
275+ }
276+ ctx .Error (http .StatusInternalServerError , "GetCommit" , err )
277+ return
278+ }
279+ branchProtection , err := git_model .GetProtectedBranchBy (ctx , ctx .Repo .Repository .ID , branches [i ].Name )
280+ if err != nil {
281+ ctx .Error (http .StatusInternalServerError , "GetProtectedBranchBy" , err )
282+ return
283+ }
284+ apiBranch , err := convert .ToBranch (ctx .Repo .Repository , branches [i ], c , branchProtection , ctx .Doer , ctx .Repo .IsAdmin ())
285+ if err != nil {
286+ ctx .Error (http .StatusInternalServerError , "convert.ToBranch" , err )
287+ return
288+ }
289+ apiBranches = append (apiBranches , apiBranch )
283290 }
284- apiBranches = append (apiBranches , apiBranch )
291+
292+ totalNumOfBranches = total
285293 }
286294
287295 ctx .SetLinkHeader (totalNumOfBranches , listOptions .PageSize )
288296 ctx .SetTotalCountHeader (int64 (totalNumOfBranches ))
289- ctx .JSON (http .StatusOK , & apiBranches )
297+ ctx .JSON (http .StatusOK , apiBranches )
290298}
291299
292300// GetBranchProtection gets a branch protection
0 commit comments