diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/auth/repo_branch_form.go | 20 | ||||
-rw-r--r-- | modules/context/repo.go | 6 | ||||
-rw-r--r-- | modules/validation/binding.go | 12 |
3 files changed, 35 insertions, 3 deletions
diff --git a/modules/auth/repo_branch_form.go b/modules/auth/repo_branch_form.go new file mode 100644 index 0000000000..57e63741aa --- /dev/null +++ b/modules/auth/repo_branch_form.go @@ -0,0 +1,20 @@ +// Copyright 2017 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 auth + +import ( + "github.com/go-macaron/binding" + macaron "gopkg.in/macaron.v1" +) + +// NewBranchForm form for creating a new branch +type NewBranchForm struct { + NewBranchName string `binding:"Required;MaxSize(100);GitRefName"` +} + +// Validate validates the fields +func (f *NewBranchForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) +} diff --git a/modules/context/repo.go b/modules/context/repo.go index 88208922c8..c33396c0f9 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -76,6 +76,11 @@ func (r *Repository) CanEnableEditor() bool { return r.Repository.CanEnableEditor() && r.IsViewBranch && r.IsWriter() } +// CanCreateBranch returns true if repository is editable and user has proper access level. +func (r *Repository) CanCreateBranch() bool { + return r.Repository.CanCreateBranch() && r.IsWriter() +} + // CanCommitToBranch returns true if repository is editable and user has proper access level // and branch is not protected func (r *Repository) CanCommitToBranch(doer *models.User) (bool, error) { @@ -528,6 +533,7 @@ func RepoRef() macaron.Handler { ctx.Data["IsViewBranch"] = ctx.Repo.IsViewBranch ctx.Data["IsViewTag"] = ctx.Repo.IsViewTag ctx.Data["IsViewCommit"] = ctx.Repo.IsViewCommit + ctx.Data["CanCreateBranch"] = ctx.Repo.CanCreateBranch() ctx.Repo.CommitsCount, err = ctx.Repo.Commit.CommitsCount() if err != nil { diff --git a/modules/validation/binding.go b/modules/validation/binding.go index 783499e69a..3f671890b6 100644 --- a/modules/validation/binding.go +++ b/modules/validation/binding.go @@ -44,12 +44,18 @@ func addGitRefNameBindingRule() { } // Additional rules as described at https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html if strings.HasPrefix(str, "/") || strings.HasSuffix(str, "/") || - strings.HasPrefix(str, ".") || strings.HasSuffix(str, ".") || - strings.HasSuffix(str, ".lock") || - strings.Contains(str, "..") || strings.Contains(str, "//") { + strings.HasSuffix(str, ".") || strings.Contains(str, "..") || + strings.Contains(str, "//") { errs.Add([]string{name}, ErrGitRefName, "GitRefName") return false, errs } + parts := strings.Split(str, "/") + for _, part := range parts { + if strings.HasSuffix(part, ".lock") || strings.HasPrefix(part, ".") { + errs.Add([]string{name}, ErrGitRefName, "GitRefName") + return false, errs + } + } return true, errs }, |