Skip to content
This repository has been archived by the owner on Apr 26, 2021. It is now read-only.

Commit

Permalink
Improve code readability by explaining a validation regex
Browse files Browse the repository at this point in the history
  • Loading branch information
scorphus committed Aug 27, 2014
1 parent 2cb2e32 commit 39ca5b1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 8 additions & 0 deletions bin/gandalf.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ func requestedRepository() (repository.Repository, error) {
// The following format is allowed:
// (git-[a-z-]+) '/?([\w-+@][\w-+.@]*/)?([\w-]+)\.git'
func parseGitCommand() (command, name string, err error) {
// The following regex validates the git command, which is in the form:
// <git-command> [<namespace>/]<name>
// with namespace being optional. If a namespace is used, we validate it
// according to the following:
// - a namespace is optional
// - a namespace contains only alphanumerics, underlines, @´s, -´s, +´s
// and periods but it does not start with a period (.)
// - one and exactly one slash (/) separates namespace and the actual name
r, err := regexp.Compile(`(git-[a-z-]+) '/?([\w-+@][\w-+.@]*/)?([\w-]+)\.git'`)
if err != nil {
panic(err)
Expand Down
12 changes: 8 additions & 4 deletions repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,15 @@ func (r *Repository) ReadOnlyURL() string {
// A valid repository MUST have:
// - a name without any special chars only alphanumeric and underlines are allowed.
// - at least one user in users array
// A valid repository MAY have one namespace since:
// - one slash (/) separates namespace and name
// - a namespace does not start with period
// - a namespace contains ony alphanumeric, underlines @, - and period
// A valid repository MAY have one namespace since the following is obeyed:
// - a namespace is optional
// - a namespace contains only alphanumerics, underlines, @´s, -´s, +´s and
// periods but it does not start with a period (.)
// - one and exactly one slash (/) separates namespace and the actual name
func (r *Repository) isValid() (bool, error) {
// The following regex validates the name of a repository, which may
// contain a namespace. If a namespace is used, we validate it
// accordingly (see comments above)
m, e := regexp.Match(`^([\w-+@][\w-+.@]*/)?[\w-]+$`, []byte(r.Name))
if e != nil {
panic(e)
Expand Down

0 comments on commit 39ca5b1

Please sign in to comment.