aboutsummaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authora1012112796 <1012112796@qq.com>2021-10-09 01:03:04 +0800
committerGitHub <noreply@github.com>2021-10-08 19:03:04 +0200
commitbb393596689ee7c33ecb041806ae2c9e8dc5dfab (patch)
treec6b71f73e53d5d5c1bb1da9e47e917f23538cd3f /services
parent56d79301b9f212e7801cbced1475238cc61c0748 (diff)
downloadgitea-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')
-rw-r--r--services/forms/repo_branch_form.go12
-rw-r--r--services/repository/branch.go39
2 files changed, 51 insertions, 0 deletions
diff --git a/services/forms/repo_branch_form.go b/services/forms/repo_branch_form.go
index 88a069b831..f9262aaede 100644
--- a/services/forms/repo_branch_form.go
+++ b/services/forms/repo_branch_form.go
@@ -24,3 +24,15 @@ func (f *NewBranchForm) Validate(req *http.Request, errs binding.Errors) binding
ctx := context.GetContext(req)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}
+
+// RenameBranchForm form for rename a branch
+type RenameBranchForm struct {
+ From string `binding:"Required;MaxSize(100);GitRefName"`
+ To string `binding:"Required;MaxSize(100);GitRefName"`
+}
+
+// Validate validates the fields
+func (f *RenameBranchForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
+ ctx := context.GetContext(req)
+ return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
+}
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")