From c2fa9c308f5cdb08dd84fb8ec6623a57e75d5152 Mon Sep 17 00:00:00 2001
From: Gergely Nagy <forgejo@gergo.csillger.hu>
Date: Sat, 13 Jan 2024 21:05:21 +0100
Subject: [PATCH] services: Gracefully handle missing branches

When loading branches, if loading one fails, log an error, and ignore
the branch, rather than returning and causing an internal server error.

Ideally, we would only ignore the error if it was caused by a missing
branch, and do it silently, like the respective API endpoint does.
However, veryfing that at this place is not very practical, so for the
time being, ignore any and all branch loading errors.

Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
---
 services/repository/branch.go | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/services/repository/branch.go b/services/repository/branch.go
index 472e3521b8..567104d29f 100644
--- a/services/repository/branch.go
+++ b/services/repository/branch.go
@@ -100,7 +100,13 @@ func LoadBranches(ctx context.Context, repo *repo_model.Repository, gitRepo *git
 	for i := range dbBranches {
 		branch, err := loadOneBranch(ctx, repo, dbBranches[i], &rules, repoIDToRepo, repoIDToGitRepo)
 		if err != nil {
-			return nil, nil, 0, fmt.Errorf("loadOneBranch: %v", err)
+			log.Error("loadOneBranch() on repo #%d, branch '%s' failed: %v", repo.ID, dbBranches[i].Name, err)
+
+			// TODO: Ideally, we would only do this if the branch doesn't exist
+			// anymore. That is not practical to check here currently, so we do
+			// this for all kinds of errors.
+			totalNumOfBranches--
+			continue
 		}
 
 		branches = append(branches, branch)