From 7636d581d9008b13a3aff6c1cd7af1ecc1c3a535 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 27 Aug 2025 22:32:22 -0700 Subject: [PATCH] Deleting branch could delete broken branch which has database record but git branch is missing (#35360) For some reasons, branches between database and git are not synced. If a branch exists in database but not in the git, it should be able to be deleted. --------- Co-authored-by: wxiaoguang --- services/repository/branch.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/services/repository/branch.go b/services/repository/branch.go index 6e0065b277..df7202227a 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -532,8 +532,8 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R // database branch record not exist or it's a deleted branch notExist := git_model.IsErrBranchNotExist(err) || rawBranch.IsDeleted - commit, err := gitRepo.GetBranchCommit(branchName) - if err != nil { + branchCommit, err := gitRepo.GetBranchCommit(branchName) + if err != nil && !errors.Is(err, util.ErrNotExist) { return err } @@ -549,6 +549,9 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R return fmt.Errorf("DeleteBranch: %v", err) } } + if branchCommit == nil { + return nil + } return gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{ Force: true, @@ -557,20 +560,24 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R return err } - objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName) + if branchCommit == nil { + return nil + } // Don't return error below this + + objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName) if err := PushUpdate( &repo_module.PushUpdateOptions{ RefFullName: git.RefNameFromBranch(branchName), - OldCommitID: commit.ID.String(), + OldCommitID: branchCommit.ID.String(), NewCommitID: objectFormat.EmptyObjectID().String(), PusherID: doer.ID, PusherName: doer.Name, RepoUserName: repo.OwnerName, RepoName: repo.Name, }); err != nil { - log.Error("Update: %v", err) + log.Error("PushUpdateOptions: %v", err) } return nil