@@ -177,23 +177,18 @@ func CreateBranch(ctx *context.APIContext) {
177177 }
178178
179179 err := repo_service .CreateNewBranch (ctx .User , ctx .Repo .Repository , opt .OldBranchName , opt .BranchName )
180-
181180 if err != nil {
182181 if models .IsErrBranchDoesNotExist (err ) {
183182 ctx .Error (http .StatusNotFound , "" , "The old branch does not exist" )
184183 }
185184 if models .IsErrTagAlreadyExists (err ) {
186185 ctx .Error (http .StatusConflict , "" , "The branch with the same tag already exists." )
187-
188186 } else if models .IsErrBranchAlreadyExists (err ) || git .IsErrPushOutOfDate (err ) {
189187 ctx .Error (http .StatusConflict , "" , "The branch already exists." )
190-
191188 } else if models .IsErrBranchNameConflict (err ) {
192189 ctx .Error (http .StatusConflict , "" , "The branch with the same name already exists." )
193-
194190 } else {
195191 ctx .Error (http .StatusInternalServerError , "CreateRepoBranch" , err )
196-
197192 }
198193 return
199194 }
@@ -263,10 +258,15 @@ func ListBranches(ctx *context.APIContext) {
263258 return
264259 }
265260
266- apiBranches := make ([]* api.Branch , len (branches ))
261+ apiBranches := make ([]* api.Branch , 0 , len (branches ))
267262 for i := range branches {
268263 c , err := branches [i ].GetCommit ()
269264 if err != nil {
265+ // Skip if this branch doesn't exist anymore.
266+ if git .IsErrNotExist (err ) {
267+ totalNumOfBranches --
268+ continue
269+ }
270270 ctx .Error (http .StatusInternalServerError , "GetCommit" , err )
271271 return
272272 }
@@ -275,11 +275,12 @@ func ListBranches(ctx *context.APIContext) {
275275 ctx .Error (http .StatusInternalServerError , "GetBranchProtection" , err )
276276 return
277277 }
278- apiBranches [ i ] , err = convert .ToBranch (ctx .Repo .Repository , branches [i ], c , branchProtection , ctx .User , ctx .Repo .IsAdmin ())
278+ apiBranch , err : = convert .ToBranch (ctx .Repo .Repository , branches [i ], c , branchProtection , ctx .User , ctx .Repo .IsAdmin ())
279279 if err != nil {
280280 ctx .Error (http .StatusInternalServerError , "convert.ToBranch" , err )
281281 return
282282 }
283+ apiBranches = append (apiBranches , apiBranch )
283284 }
284285
285286 ctx .SetLinkHeader (totalNumOfBranches , listOptions .PageSize )
@@ -532,7 +533,6 @@ func CreateBranchProtection(ctx *context.APIContext) {
532533 }
533534
534535 ctx .JSON (http .StatusCreated , convert .ToBranchProtection (bp ))
535-
536536}
537537
538538// EditBranchProtection edits a branch protection for a repo
0 commit comments