@@ -252,42 +252,50 @@ func ListBranches(ctx *context.APIContext) {
252252 // "200":
253253 // "$ref": "#/responses/BranchList"
254254
255+ var totalNumOfBranches int
256+ var apiBranches []* api.Branch
257+
255258 listOptions := utils .GetListOptions (ctx )
256- skip , _ := listOptions .GetStartEnd ()
257- branches , totalNumOfBranches , err := ctx .Repo .GitRepo .GetBranches (skip , listOptions .PageSize )
258- if err != nil {
259- ctx .Error (http .StatusInternalServerError , "GetBranches" , err )
260- return
261- }
262259
263- apiBranches := make ([] * api. Branch , 0 , len ( branches ))
264- for i := range branches {
265- c , err := branches [ i ]. GetCommit ( )
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 )
266263 if err != nil {
267- // Skip if this branch doesn't exist anymore.
268- if git .IsErrNotExist (err ) {
269- totalNumOfBranches --
270- continue
271- }
272- ctx .Error (http .StatusInternalServerError , "GetCommit" , err )
264+ ctx .Error (http .StatusInternalServerError , "GetBranches" , err )
273265 return
274266 }
275- branchProtection , err := git_model .GetProtectedBranchBy (ctx , ctx .Repo .Repository .ID , branches [i ].Name )
276- if err != nil {
277- ctx .Error (http .StatusInternalServerError , "GetBranchProtection" , err )
278- return
279- }
280- apiBranch , err := convert .ToBranch (ctx .Repo .Repository , branches [i ], c , branchProtection , ctx .Doer , ctx .Repo .IsAdmin ())
281- if err != nil {
282- ctx .Error (http .StatusInternalServerError , "convert.ToBranch" , err )
283- 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 )
284291 }
285- apiBranches = append (apiBranches , apiBranch )
292+
293+ totalNumOfBranches = total
286294 }
287295
288296 ctx .SetLinkHeader (totalNumOfBranches , listOptions .PageSize )
289297 ctx .SetTotalCountHeader (int64 (totalNumOfBranches ))
290- ctx .JSON (http .StatusOK , & apiBranches )
298+ ctx .JSON (http .StatusOK , apiBranches )
291299}
292300
293301// GetBranchProtection gets a branch protection
0 commit comments