2018-10-22 03:00:50 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-13 11:58:45 -04:00
|
|
|
# Gitaly note: JV: does not need to be migrated, works without a repo.
|
|
|
|
|
2014-07-27 10:40:00 -04:00
|
|
|
module Gitlab
|
|
|
|
module GitRefValidator
|
|
|
|
extend self
|
2019-04-09 23:39:45 -04:00
|
|
|
|
|
|
|
EXPANDED_PREFIXES = %w[refs/heads/ refs/remotes/].freeze
|
|
|
|
DISALLOWED_PREFIXES = %w[-].freeze
|
|
|
|
|
2014-07-27 10:40:00 -04:00
|
|
|
# Validates a given name against the git reference specification
|
|
|
|
#
|
|
|
|
# Returns true for a valid reference name, false otherwise
|
|
|
|
def validate(ref_name)
|
2019-04-09 23:39:45 -04:00
|
|
|
return false if ref_name.start_with?(*(EXPANDED_PREFIXES + DISALLOWED_PREFIXES))
|
2018-07-17 09:12:29 -04:00
|
|
|
return false if ref_name == 'HEAD'
|
2017-01-09 01:38:13 -05:00
|
|
|
|
2018-11-26 16:42:11 -05:00
|
|
|
begin
|
|
|
|
Rugged::Reference.valid_name?("refs/heads/#{ref_name}")
|
|
|
|
rescue ArgumentError
|
2020-07-10 20:09:17 -04:00
|
|
|
false
|
2018-11-26 16:42:11 -05:00
|
|
|
end
|
2014-07-27 10:40:00 -04:00
|
|
|
end
|
2019-04-09 23:39:45 -04:00
|
|
|
|
|
|
|
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
|
2020-07-10 20:09:17 -04:00
|
|
|
false
|
2019-04-09 23:39:45 -04:00
|
|
|
end
|
|
|
|
end
|
2014-07-27 10:40:00 -04:00
|
|
|
end
|
|
|
|
end
|