From 1ef74004a2bd1d05c720dcd65a12ad564e360d2d Mon Sep 17 00:00:00 2001 From: Giteabot Date: Tue, 24 Sep 2024 17:01:05 +0800 Subject: [PATCH] Fix bug when deleting a migrated branch (#32075) (#32123) Backport #32075 by @lunny After migrating a repository with pull request, the branch is missed and after the pull request merged, the branch cannot be deleted. Co-authored-by: Lunny Xiao --- services/repository/branch.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/services/repository/branch.go b/services/repository/branch.go index 0afd0cd76a..db30b28b34 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -483,13 +483,12 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R } rawBranch, err := git_model.GetBranch(ctx, repo.ID, branchName) - if err != nil { + if err != nil && !git_model.IsErrBranchNotExist(err) { return fmt.Errorf("GetBranch: %vc", err) } - if rawBranch.IsDeleted { - return nil - } + // 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 { @@ -497,8 +496,10 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R } if err := db.WithTx(ctx, func(ctx context.Context) error { - if err := git_model.AddDeletedBranch(ctx, repo.ID, branchName, doer.ID); err != nil { - return err + if !notExist { + if err := git_model.AddDeletedBranch(ctx, repo.ID, branchName, doer.ID); err != nil { + return err + } } return gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{