Skip to content

Commit 0d196e2

Browse files
author
Gusted
authored
Don't error when branch's commit doesn't exist (#19547) (#19548)
- Backport #19547 - 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
1 parent b86606f commit 0d196e2

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

routers/api/v1/repo/branch.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)