diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2015-12-17 16:59:15 -0200 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2015-12-17 17:17:37 -0200 |
commit | 1757e6ef65a1cedbfe5d5b5da894d6d0d3d5ef16 (patch) | |
tree | ae3512b6b49c95451869d9e486d94155448bd3e3 /app/assets | |
parent | c91cf1f66b3ed4329b313b7b75fca816fc5c6076 (diff) | |
download | gitlab-ce-1757e6ef65a1cedbfe5d5b5da894d6d0d3d5ef16.tar.gz |
Add JS validation for invalid characters in branch name
More info about valid ref names:
https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.ht
ml
Diffstat (limited to 'app/assets')
-rw-r--r-- | app/assets/javascripts/new_branch_form.js.coffee | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/app/assets/javascripts/new_branch_form.js.coffee b/app/assets/javascripts/new_branch_form.js.coffee new file mode 100644 index 00000000000..af531b3bf9f --- /dev/null +++ b/app/assets/javascripts/new_branch_form.js.coffee @@ -0,0 +1,73 @@ +class @NewBranchForm + constructor: (form, availableRefs) -> + @branchNameError = form.find('.js-branch-name-error') + @name = form.find('.js-branch-name') + @ref = form.find('#ref') + + @setupAvailableRefs(availableRefs) + @setupRestrictions() + @addBinding() + @init() + + addBinding: -> + @name.on 'blur', @validate + + init: -> + @name.trigger 'blur'if @name.val().length > 0 + + setupAvailableRefs: (availableRefs) -> + @ref.autocomplete { + source: availableRefs, + minLength: 1 + } + + setupRestrictions: -> + startsWith = { + pattern: /^(\/|\.)/g, + prefix: "can't start with ", + conjunction: "or" + } + + endsWith = { + pattern: /(\/|\.|\.lock)$/g, + prefix: "can't end in ", + conjunction: "or" + } + + characters = { + pattern: /(\s|~|\^|:|\?|\*|\[|\\|\.\.|@\{|\/{2,}){1}/g + prefix: "can't contains ", + conjunction: ", " + } + + @restrictions = [startsWith, characters, endsWith] + + validate: => + @branchNameError.empty() + + unique = (values, value) -> + values.push(value) unless value in values + values + + formatter = (values, restriction) -> + formatted = values.map (value) -> + switch + when /\s/.test value then 'spaces' + when /\/{2,}/g.test value then 'consecutive slashes' + else "'#{value}'" + + "#{restriction.prefix} #{formatted.join(restriction.conjunction)}" + + validator = (errors, restriction) => + matched = @name.val().match(restriction.pattern) + + if matched + errors.concat formatter(matched.reduce(unique, []), restriction) + else + errors + + errors = @restrictions.reduce validator, [] + + if errors.length > 0 + errorMessage = $("<span/>").text(errors.join(', ')) + @branchNameError.append(errorMessage) |