aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2022-12-04 16:57:17 +0800
committerGitHub <noreply@github.com>2022-12-04 08:57:17 +0000
commit36cbaec54cb4640e23a1edb3ba6d488867f73302 (patch)
treec1190caf2dfbd1dd7531863e1cd86fa38ee97478
parent9eb9cf5153160e38a25dbbdeeb223fac769879db (diff)
downloadgitea-36cbaec54cb4640e23a1edb3ba6d488867f73302.tar.gz
gitea-36cbaec54cb4640e23a1edb3ba6d488867f73302.zip
Fix ListBranches to handle empty case (#21921)
Fix #21910 Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
-rw-r--r--routers/api/v1/repo/branch.go60
1 files changed, 34 insertions, 26 deletions
diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go
index f5b1eb1c42..3060cf2406 100644
--- a/routers/api/v1/repo/branch.go
+++ b/routers/api/v1/repo/branch.go
@@ -251,42 +251,50 @@ func ListBranches(ctx *context.APIContext) {
// "200":
// "$ref": "#/responses/BranchList"
+ var totalNumOfBranches int
+ var apiBranches []*api.Branch
+
listOptions := utils.GetListOptions(ctx)
- skip, _ := listOptions.GetStartEnd()
- branches, totalNumOfBranches, err := ctx.Repo.GitRepo.GetBranches(skip, listOptions.PageSize)
- if err != nil {
- ctx.Error(http.StatusInternalServerError, "GetBranches", err)
- return
- }
- apiBranches := make([]*api.Branch, 0, len(branches))
- for i := range branches {
- c, err := branches[i].GetCommit()
+ if !ctx.Repo.Repository.IsEmpty && ctx.Repo.GitRepo != nil {
+ skip, _ := listOptions.GetStartEnd()
+ branches, total, err := ctx.Repo.GitRepo.GetBranches(skip, listOptions.PageSize)
if err != nil {
- // Skip if this branch doesn't exist anymore.
- if git.IsErrNotExist(err) {
- totalNumOfBranches--
- continue
- }
- ctx.Error(http.StatusInternalServerError, "GetCommit", err)
+ ctx.Error(http.StatusInternalServerError, "GetBranches", err)
return
}
- branchProtection, err := git_model.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branches[i].Name)
- if err != nil {
- ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
- return
- }
- 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
+
+ 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) {
+ total--
+ continue
+ }
+ ctx.Error(http.StatusInternalServerError, "GetCommit", err)
+ return
+ }
+ branchProtection, err := git_model.GetProtectedBranchBy(ctx, ctx.Repo.Repository.ID, branches[i].Name)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "GetProtectedBranchBy", err)
+ return
+ }
+ 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
+ }
+ apiBranches = append(apiBranches, apiBranch)
}
- apiBranches = append(apiBranches, apiBranch)
+
+ totalNumOfBranches = total
}
ctx.SetLinkHeader(totalNumOfBranches, listOptions.PageSize)
ctx.SetTotalCountHeader(int64(totalNumOfBranches))
- ctx.JSON(http.StatusOK, &apiBranches)
+ ctx.JSON(http.StatusOK, apiBranches)
}
// GetBranchProtection gets a branch protection