From 2aafa03c94d6cdc1181aed37e6a6a77fddf61650 Mon Sep 17 00:00:00 2001 From: Gusted Date: Fri, 29 Apr 2022 03:04:05 +0200 Subject: [PATCH 1/3] 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 --- routers/api/v1/repo/branch.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 794d367b53e73..8afc5fa2fa710 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -260,22 +260,29 @@ func ListBranches(ctx *context.APIContext) { } apiBranches := make([]*api.Branch, len(branches)) - for i := range branches { - c, err := branches[i].GetCommit() + idx := 0 + for _, branch := range branches { + c, err := branch.GetCommit() if err != nil { + // Skip if this branch doesn't exist anymore. + if git.IsErrNotExist(err) { + totalNumOfBranches-- + continue + } ctx.Error(http.StatusInternalServerError, "GetCommit", err) return } - branchProtection, err := models.GetProtectedBranchBy(ctx.Repo.Repository.ID, branches[i].Name) + branchProtection, err := models.GetProtectedBranchBy(ctx.Repo.Repository.ID, branch.Name) if err != nil { ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err) return } - apiBranches[i], err = convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin()) + apiBranches[idx], err = convert.ToBranch(ctx.Repo.Repository, branch, c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin()) if err != nil { ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err) return } + idx++ } ctx.SetLinkHeader(totalNumOfBranches, listOptions.PageSize) From 949c848f9db4278c6e7b7b8b3b801d58cb7dfb6d Mon Sep 17 00:00:00 2001 From: Gusted Date: Fri, 29 Apr 2022 03:13:19 +0200 Subject: [PATCH 2/3] Don't send empty objects --- routers/api/v1/repo/branch.go | 1 + 1 file changed, 1 insertion(+) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 8afc5fa2fa710..aab99099f28fc 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -284,6 +284,7 @@ func ListBranches(ctx *context.APIContext) { } idx++ } + apiBranches = apiBranches[:idx] ctx.SetLinkHeader(totalNumOfBranches, listOptions.PageSize) ctx.SetTotalCountHeader(int64(totalNumOfBranches)) From 5eeff422786440090f59520a9dacc837dbe9c442 Mon Sep 17 00:00:00 2001 From: Gusted Date: Fri, 29 Apr 2022 03:24:12 +0200 Subject: [PATCH 3/3] Use more minimal approach --- routers/api/v1/repo/branch.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index aab99099f28fc..c030a896a792d 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -259,10 +259,9 @@ func ListBranches(ctx *context.APIContext) { return } - apiBranches := make([]*api.Branch, len(branches)) - idx := 0 - for _, branch := range branches { - c, err := branch.GetCommit() + apiBranches := make([]*api.Branch, 0, len(branches)) + for i := range branches { + c, err := branches[i].GetCommit() if err != nil { // Skip if this branch doesn't exist anymore. if git.IsErrNotExist(err) { @@ -272,19 +271,18 @@ func ListBranches(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "GetCommit", err) return } - branchProtection, err := models.GetProtectedBranchBy(ctx.Repo.Repository.ID, branch.Name) + branchProtection, err := models.GetProtectedBranchBy(ctx.Repo.Repository.ID, branches[i].Name) if err != nil { ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err) return } - apiBranches[idx], err = convert.ToBranch(ctx.Repo.Repository, branch, c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin()) + apiBranch, err := convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.Doer, ctx.Repo.IsAdmin()) if err != nil { ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err) return } - idx++ + apiBranches = append(apiBranches, apiBranch) } - apiBranches = apiBranches[:idx] ctx.SetLinkHeader(totalNumOfBranches, listOptions.PageSize) ctx.SetTotalCountHeader(int64(totalNumOfBranches))