Module: Gitlab::GitRefValidator

Extended by:
GitRefValidator
Included in:
GitRefValidator
Defined in:
lib/gitlab/git_ref_validator.rb

Constant Summary collapse

EXPANDED_PREFIXES =
%w[refs/heads/ refs/remotes/].freeze
DISALLOWED_PREFIXES =
%w[-].freeze

Instance Method Summary collapse

Instance Method Details

#validate(ref_name) ⇒ Object

Validates a given name against the git reference specification

Returns true for a valid reference name, false otherwise


15
16
17
18
19
20
21
22
23
24
# File 'lib/gitlab/git_ref_validator.rb', line 15

def validate(ref_name)
  return false if ref_name.start_with?(*(EXPANDED_PREFIXES + DISALLOWED_PREFIXES))
  return false if ref_name == 'HEAD'

  begin
    Rugged::Reference.valid_name?("refs/heads/#{ref_name}")
  rescue ArgumentError
    false
  end
end

#validate_merge_request_branch(ref_name) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gitlab/git_ref_validator.rb', line 26

def validate_merge_request_branch(ref_name)
  return false if ref_name.start_with?(*DISALLOWED_PREFIXES)

  expanded_name = if ref_name.start_with?(*EXPANDED_PREFIXES)
                    ref_name
                  else
                    "refs/heads/#{ref_name}"
                  end

  begin
    Rugged::Reference.valid_name?(expanded_name)
  rescue ArgumentError
    false
  end
end