diff options
author | a1012112796 <1012112796@qq.com> | 2021-10-09 01:03:04 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-08 19:03:04 +0200 |
commit | bb393596689ee7c33ecb041806ae2c9e8dc5dfab (patch) | |
tree | c6b71f73e53d5d5c1bb1da9e47e917f23538cd3f /services/repository/branch.go | |
parent | 56d79301b9f212e7801cbced1475238cc61c0748 (diff) | |
download | gitea-bb393596689ee7c33ecb041806ae2c9e8dc5dfab.tar.gz gitea-bb393596689ee7c33ecb041806ae2c9e8dc5dfab.zip |
Add a simple way to rename branch like gh (#15870)
- Update default branch if needed
- Update protected branch if needed
- Update all not merged pull request base branch name
- Rename git branch
- Record this rename work and auto redirect for old branch on ui
Signed-off-by: a1012112796 <1012112796@qq.com>
Co-authored-by: delvh <dev.lh@web.de>
Diffstat (limited to 'services/repository/branch.go')
-rw-r--r-- | services/repository/branch.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/services/repository/branch.go b/services/repository/branch.go index 28d24f121d..5e246cbec6 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -10,10 +10,49 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/notification" repo_module "code.gitea.io/gitea/modules/repository" pull_service "code.gitea.io/gitea/services/pull" ) +// 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 +} + // enmuerates all branch related errors var ( ErrBranchIsDefault = errors.New("branch is default") |