forked from go-gitea/gitea
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge all deleteBranch as one function and also fix bug when delete b…
…ranch don't close related PRs (go-gitea#16067) * Fix bug when delete branch don't close related PRs * Merge all deletebranch as one method * Add missed branch.go * fix comment Co-authored-by: Lauris BH <[email protected]>
- Loading branch information
Showing
4 changed files
with
111 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package repository | ||
|
||
import ( | ||
"errors" | ||
|
||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/modules/git" | ||
"code.gitea.io/gitea/modules/log" | ||
repo_module "code.gitea.io/gitea/modules/repository" | ||
pull_service "code.gitea.io/gitea/services/pull" | ||
) | ||
|
||
// enmuerates all branch related errors | ||
var ( | ||
ErrBranchIsDefault = errors.New("branch is default") | ||
ErrBranchIsProtected = errors.New("branch is protected") | ||
) | ||
|
||
// DeleteBranch delete branch | ||
func DeleteBranch(doer *models.User, repo *models.Repository, gitRepo *git.Repository, branchName string) error { | ||
if branchName == repo.DefaultBranch { | ||
return ErrBranchIsDefault | ||
} | ||
|
||
isProtected, err := repo.IsProtectedBranch(branchName, doer) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if isProtected { | ||
return ErrBranchIsProtected | ||
} | ||
|
||
commit, err := gitRepo.GetBranchCommit(branchName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{ | ||
Force: true, | ||
}); err != nil { | ||
return err | ||
} | ||
|
||
if err := pull_service.CloseBranchPulls(doer, repo.ID, branchName); err != nil { | ||
return err | ||
} | ||
|
||
// Don't return error below this | ||
if err := PushUpdate( | ||
&repo_module.PushUpdateOptions{ | ||
RefFullName: git.BranchPrefix + branchName, | ||
OldCommitID: commit.ID.String(), | ||
NewCommitID: git.EmptySHA, | ||
PusherID: doer.ID, | ||
PusherName: doer.Name, | ||
RepoUserName: repo.OwnerName, | ||
RepoName: repo.Name, | ||
}); err != nil { | ||
log.Error("Update: %v", err) | ||
} | ||
|
||
if err := repo.AddDeletedBranch(branchName, commit.ID.String(), doer.ID); err != nil { | ||
log.Warn("AddDeletedBranch: %v", err) | ||
} | ||
|
||
return nil | ||
} |