diff options
Diffstat (limited to 'services')
-rw-r--r-- | services/forms/repo_branch_form.go | 12 | ||||
-rw-r--r-- | services/repository/branch.go | 39 |
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") |