From 0a0a3cea1b54d9cd7c95faf9318f6c3cdf1469a9 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Tue, 24 Sep 2024 15:42:08 +0800 Subject: [PATCH] Fix bug when deleting a migrated branch (#32075) After migrating a repository with pull request, the branch is missed and after the pull request merged, the branch cannot be deleted. (cherry picked from commit 5a8568459d22e57cac506465463660526ca6a08f) Conflicts: services/repository/branch.go conflict because of [GITEA] Fix typo in formatting error e71b5a038ecfd80630c98752472c2719b0e9659f --- 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 27e50e5ced..f0e7120926 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -430,13 +430,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: %v", 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 { @@ -444,8 +443,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{