1
1
mirror of https://github.com/go-gitea/gitea synced 2025-08-29 12:58:29 +00:00

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 <wxiaoguang@gmail.com>
This commit is contained in:
Lunny Xiao
2025-08-27 22:32:22 -07:00
committed by GitHub
parent 0cbaa0b662
commit 7636d581d9

View File

@@ -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 // database branch record not exist or it's a deleted branch
notExist := git_model.IsErrBranchNotExist(err) || rawBranch.IsDeleted notExist := git_model.IsErrBranchNotExist(err) || rawBranch.IsDeleted
commit, err := gitRepo.GetBranchCommit(branchName) branchCommit, err := gitRepo.GetBranchCommit(branchName)
if err != nil { if err != nil && !errors.Is(err, util.ErrNotExist) {
return err 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) return fmt.Errorf("DeleteBranch: %v", err)
} }
} }
if branchCommit == nil {
return nil
}
return gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{ return gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
Force: true, Force: true,
@@ -557,20 +560,24 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R
return err return err
} }
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName) if branchCommit == nil {
return nil
}
// Don't return error below this // Don't return error below this
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
if err := PushUpdate( if err := PushUpdate(
&repo_module.PushUpdateOptions{ &repo_module.PushUpdateOptions{
RefFullName: git.RefNameFromBranch(branchName), RefFullName: git.RefNameFromBranch(branchName),
OldCommitID: commit.ID.String(), OldCommitID: branchCommit.ID.String(),
NewCommitID: objectFormat.EmptyObjectID().String(), NewCommitID: objectFormat.EmptyObjectID().String(),
PusherID: doer.ID, PusherID: doer.ID,
PusherName: doer.Name, PusherName: doer.Name,
RepoUserName: repo.OwnerName, RepoUserName: repo.OwnerName,
RepoName: repo.Name, RepoName: repo.Name,
}); err != nil { }); err != nil {
log.Error("Update: %v", err) log.Error("PushUpdateOptions: %v", err)
} }
return nil return nil