Browse Source

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>
pull/34709/merge
Lunny Xiao 4 months ago committed by GitHub
parent
commit
7636d581d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 17
      services/repository/branch.go

17
services/repository/branch.go

@ -532,8 +532,8 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R @@ -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 @@ -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 @@ -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

Loading…
Cancel
Save