2021-06-07 17:52:59 +03:00
|
|
|
// 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"
|
2021-10-08 20:03:04 +03:00
|
|
|
"code.gitea.io/gitea/modules/notification"
|
2021-06-07 17:52:59 +03:00
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
|
|
|
pull_service "code.gitea.io/gitea/services/pull"
|
|
|
|
)
|
|
|
|
|
2021-10-08 20:03:04 +03:00
|
|
|
// RenameBranch rename a branch
|
|
|
|
func RenameBranch(repo *models.Repository, doer *models.User, gitRepo *git.Repository, from, to string) (string, error) {
|
|
|
|
if from == to {
|
|
|
|
return "target_exist", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if gitRepo.IsBranchExist(to) {
|
|
|
|
return "target_exist", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !gitRepo.IsBranchExist(from) {
|
|
|
|
return "from_not_exist", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := repo.RenameBranch(from, to, func(isDefault bool) error {
|
|
|
|
err2 := gitRepo.RenameBranch(from, to)
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
|
|
|
|
if isDefault {
|
|
|
|
err2 = gitRepo.SetDefaultBranch(to)
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
notification.NotifyDeleteRef(doer, repo, "branch", "refs/heads/"+from)
|
|
|
|
notification.NotifyCreateRef(doer, repo, "branch", "refs/heads/"+to)
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2021-06-07 17:52:59 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-06-23 22:38:19 +03:00
|
|
|
isProtected, err := repo.IsProtectedBranch(branchName)
|
2021-06-07 17:52:59 +03:00
|
|
|
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
|
|
|
|
}
|