Skip to content

Commit 7636d58

Browse files
lunnywxiaoguang
andauthored
Deleting branch could delete broken branch which has database record but git branch is missing (go-gitea#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 <[email protected]>
1 parent 0cbaa0b commit 7636d58

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

services/repository/branch.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,8 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R
532532
// database branch record not exist or it's a deleted branch
533533
notExist := git_model.IsErrBranchNotExist(err) || rawBranch.IsDeleted
534534

535-
commit, err := gitRepo.GetBranchCommit(branchName)
536-
if err != nil {
535+
branchCommit, err := gitRepo.GetBranchCommit(branchName)
536+
if err != nil && !errors.Is(err, util.ErrNotExist) {
537537
return err
538538
}
539539

@@ -549,6 +549,9 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R
549549
return fmt.Errorf("DeleteBranch: %v", err)
550550
}
551551
}
552+
if branchCommit == nil {
553+
return nil
554+
}
552555

553556
return gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
554557
Force: true,
@@ -557,20 +560,24 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R
557560
return err
558561
}
559562

560-
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
563+
if branchCommit == nil {
564+
return nil
565+
}
561566

562567
// Don't return error below this
568+
569+
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
563570
if err := PushUpdate(
564571
&repo_module.PushUpdateOptions{
565572
RefFullName: git.RefNameFromBranch(branchName),
566-
OldCommitID: commit.ID.String(),
573+
OldCommitID: branchCommit.ID.String(),
567574
NewCommitID: objectFormat.EmptyObjectID().String(),
568575
PusherID: doer.ID,
569576
PusherName: doer.Name,
570577
RepoUserName: repo.OwnerName,
571578
RepoName: repo.Name,
572579
}); err != nil {
573-
log.Error("Update: %v", err)
580+
log.Error("PushUpdateOptions: %v", err)
574581
}
575582

576583
return nil

0 commit comments

Comments
 (0)