Skip to content

Commit 269b30e

Browse files
committed
Deleting branch could delete broken branch which has database record but git branch is missing
1 parent c4fbccc commit 269b30e

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

services/repository/branch.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,14 @@ 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 {
537-
return err
535+
branchExistInGit := gitRepo.IsBranchExist(branchName)
536+
var commitID string
537+
if branchExistInGit {
538+
commit, err := gitRepo.GetBranchCommit(branchName)
539+
if err != nil {
540+
return err
541+
}
542+
commitID = commit.ID.String()
538543
}
539544

540545
if err := db.WithTx(ctx, func(ctx context.Context) error {
@@ -549,6 +554,9 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R
549554
return fmt.Errorf("DeleteBranch: %v", err)
550555
}
551556
}
557+
if !branchExistInGit {
558+
return nil
559+
}
552560

553561
return gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
554562
Force: true,
@@ -557,20 +565,22 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R
557565
return err
558566
}
559567

560-
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
561-
562-
// Don't return error below this
563-
if err := PushUpdate(
564-
&repo_module.PushUpdateOptions{
565-
RefFullName: git.RefNameFromBranch(branchName),
566-
OldCommitID: commit.ID.String(),
567-
NewCommitID: objectFormat.EmptyObjectID().String(),
568-
PusherID: doer.ID,
569-
PusherName: doer.Name,
570-
RepoUserName: repo.OwnerName,
571-
RepoName: repo.Name,
572-
}); err != nil {
573-
log.Error("Update: %v", err)
568+
if branchExistInGit {
569+
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
570+
571+
// Don't return error below this
572+
if err := PushUpdate(
573+
&repo_module.PushUpdateOptions{
574+
RefFullName: git.RefNameFromBranch(branchName),
575+
OldCommitID: commitID,
576+
NewCommitID: objectFormat.EmptyObjectID().String(),
577+
PusherID: doer.ID,
578+
PusherName: doer.Name,
579+
RepoUserName: repo.OwnerName,
580+
RepoName: repo.Name,
581+
}); err != nil {
582+
log.Error("Update: %v", err)
583+
}
574584
}
575585

576586
return nil

0 commit comments

Comments
 (0)