summaryrefslogtreecommitdiff
path: root/lib/gitlab/git_ref_validator.rb
blob: 3f13ebeb9d008867c9ddc2d26237366e73ad3d06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# frozen_string_literal: true

# Gitaly note: JV: does not need to be migrated, works without a repo.

module Gitlab
  module GitRefValidator
    extend self
    # Validates a given name against the git reference specification
    #
    # Returns true for a valid reference name, false otherwise
    def validate(ref_name)
      not_allowed_prefixes = %w(refs/heads/ refs/remotes/ -)
      return false if ref_name.start_with?(*not_allowed_prefixes)
      return false if ref_name == 'HEAD'

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