Skip to content

Commit 334707f

Browse files
author
Gusted
authored
Don't error when branch's commit doesn't exist (#19547)
* Don't error when branch's commit doesn't exist - If one of the branches no longer exists, don't throw an error, it's possible that the branch was destroyed during the process. Simply skip it and disregard it. - Resolves #19541 * Don't send empty objects * Use more minimal approach
1 parent 53829b8 commit 334707f

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

routers/api/v1/repo/branch.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,15 @@ func ListBranches(ctx *context.APIContext) {
259259
return
260260
}
261261

262-
apiBranches := make([]*api.Branch, len(branches))
262+
apiBranches := make([]*api.Branch, 0, len(branches))
263263
for i := range branches {
264264
c, err := branches[i].GetCommit()
265265
if err != nil {
266+
// Skip if this branch doesn't exist anymore.
267+
if git.IsErrNotExist(err) {
268+
totalNumOfBranches--
269+
continue
270+
}
266271
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
267272
return
268273
}
@@ -271,11 +276,12 @@ func ListBranches(ctx *context.APIContext) {
271276
ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
272277
return
273278
}
274-
apiBranches[i], err = convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin())
279+
apiBranch, err := convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin())
275280
if err != nil {
276281
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
277282
return
278283
}
284+
apiBranches = append(apiBranches, apiBranch)
279285
}
280286

281287
ctx.SetLinkHeader(totalNumOfBranches, listOptions.PageSize)

0 commit comments

Comments
 (0)