Skip to content

Commit 1757e6e

Browse files
committed
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
1 parent c91cf1f commit 1757e6e

File tree

5 files changed

+81
-8
lines changed

5 files changed

+81
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class @NewBranchForm
2+
constructor: (form, availableRefs) ->
3+
@branchNameError = form.find('.js-branch-name-error')
4+
@name = form.find('.js-branch-name')
5+
@ref = form.find('#ref')
6+
7+
@setupAvailableRefs(availableRefs)
8+
@setupRestrictions()
9+
@addBinding()
10+
@init()
11+
12+
addBinding: ->
13+
@name.on 'blur', @validate
14+
15+
init: ->
16+
@name.trigger 'blur'if @name.val().length > 0
17+
18+
setupAvailableRefs: (availableRefs) ->
19+
@ref.autocomplete {
20+
source: availableRefs,
21+
minLength: 1
22+
}
23+
24+
setupRestrictions: ->
25+
startsWith = {
26+
pattern: /^(\/|\.)/g,
27+
prefix: "can't start with ",
28+
conjunction: "or"
29+
}
30+
31+
endsWith = {
32+
pattern: /(\/|\.|\.lock)$/g,
33+
prefix: "can't end in ",
34+
conjunction: "or"
35+
}
36+
37+
characters = {
38+
pattern: /(\s|~|\^|:|\?|\*|\[|\\|\.\.|@\{|\/{2,}){1}/g
39+
prefix: "can't contains ",
40+
conjunction: ", "
41+
}
42+
43+
@restrictions = [startsWith, characters, endsWith]
44+
45+
validate: =>
46+
@branchNameError.empty()
47+
48+
unique = (values, value) ->
49+
values.push(value) unless value in values
50+
values
51+
52+
formatter = (values, restriction) ->
53+
formatted = values.map (value) ->
54+
switch
55+
when /\s/.test value then 'spaces'
56+
when /\/{2,}/g.test value then 'consecutive slashes'
57+
else "'#{value}'"
58+
59+
"#{restriction.prefix} #{formatted.join(restriction.conjunction)}"
60+
61+
validator = (errors, restriction) =>
62+
matched = @name.val().match(restriction.pattern)
63+
64+
if matched
65+
errors.concat formatter(matched.reduce(unique, []), restriction)
66+
else
67+
errors
68+
69+
errors = @restrictions.reduce validator, []
70+
71+
if errors.length > 0
72+
errorMessage = $("<span/>").text(errors.join(', '))
73+
@branchNameError.append(errorMessage)

app/services/create_branch_service.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class CreateBranchService < BaseService
44
def execute(branch_name, ref)
55
valid_branch = Gitlab::GitRefValidator.validate(branch_name)
66
if valid_branch == false
7-
return error("Branch name can't contains space, '~', '^', ':', '?', '*', '[', '\', '..', '@{', and consecutive slashes, start with '/' or '.' or end in '/' or '.' or '.lock'")
7+
return error('Branch name is invalid')
88
end
99

1010
repository = project.repository

app/views/projects/branches/new.html.haml

+4-6
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
New Branch
1010
%hr
1111
12-
= form_tag namespace_project_branches_path, method: :post, id: "new-branch-form", class: "form-horizontal js-requires-input" do
12+
= form_tag namespace_project_branches_path, method: :post, id: "new-branch-form", class: "form-horizontal js-create-branch-form js-requires-input" do
1313
.form-group
1414
= label_tag :branch_name, nil, class: 'control-label'
1515
.col-sm-10
16-
= text_field_tag :branch_name, params[:branch_name], required: true, tabindex: 1, autofocus: true, class: 'form-control'
16+
= text_field_tag :branch_name, params[:branch_name], required: true, tabindex: 1, autofocus: true, class: 'form-control js-branch-name'
17+
.help-block.text-danger.js-branch-name-error
1718
.form-group
1819
= label_tag :ref, 'Create from', class: 'control-label'
1920
.col-sm-10
@@ -26,7 +27,4 @@
2627
:javascript
2728
var availableRefs = #{@project.repository.ref_names.to_json};
2829
29-
$("#ref").autocomplete({
30-
source: availableRefs,
31-
minLength: 1
32-
});
30+
new NewBranchForm($('.js-create-branch-form'), availableRefs)

features/project/commits/branches.feature

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Feature: Project Commits Branches
2525
And I click branch 'improve/awesome' delete link
2626
Then I should not see branch 'improve/awesome'
2727

28+
@javascript
2829
Scenario: I create a branch with invalid name
2930
Given I visit project branches page
3031
And I click new branch link

features/steps/project/commits/branches.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ class Spinach::Features::ProjectCommitsBranches < Spinach::FeatureSteps
6161
end
6262

6363
step 'I should see new an error that branch is invalid' do
64-
expect(page).to have_content "Branch name can't contains space, '~', '^', ':', '?', '*', '[', '\', '..', '@{', and consecutive slashes, start with '/' or '.' or end in '/' or '.' or '.lock'"
64+
expect(page).to have_content 'Branch name is invalid'
65+
expect(page).to have_content "can't contains spaces"
6566
end
6667

6768
step 'I should see new an error that ref is invalid' do

0 commit comments

Comments
 (0)