2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
# Gitaly note: JV: does not need to be migrated, works without a repo.
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
module Gitlab
|
|
|
|
module GitRefValidator
|
|
|
|
extend self
|
2019-06-05 12:25:43 +05:30
|
|
|
|
|
|
|
EXPANDED_PREFIXES = %w[refs/heads/ refs/remotes/].freeze
|
|
|
|
DISALLOWED_PREFIXES = %w[-].freeze
|
|
|
|
|
2015-04-26 12:48:37 +05:30
|
|
|
# Validates a given name against the git reference specification
|
|
|
|
#
|
|
|
|
# Returns true for a valid reference name, false otherwise
|
2023-06-20 00:43:36 +05:30
|
|
|
def validate(ref_name, skip_head_ref_check: false)
|
2023-01-13 00:05:48 +05:30
|
|
|
return false if ref_name.to_s.empty? # #blank? raises an ArgumentError for invalid encodings
|
2019-06-05 12:25:43 +05:30
|
|
|
return false if ref_name.start_with?(*(EXPANDED_PREFIXES + DISALLOWED_PREFIXES))
|
2023-06-20 00:43:36 +05:30
|
|
|
return false if ref_name == 'HEAD' && !skip_head_ref_check
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
begin
|
|
|
|
Rugged::Reference.valid_name?("refs/heads/#{ref_name}")
|
|
|
|
rescue ArgumentError
|
2020-07-28 23:09:34 +05:30
|
|
|
false
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
2019-06-05 12:25:43 +05:30
|
|
|
|
|
|
|
def validate_merge_request_branch(ref_name)
|
2023-01-13 00:05:48 +05:30
|
|
|
return false if ref_name.to_s.empty?
|
2019-06-05 12:25:43 +05:30
|
|
|
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-28 23:09:34 +05:30
|
|
|
false
|
2019-06-05 12:25:43 +05:30
|
|
|
end
|
|
|
|
end
|
2015-04-26 12:48:37 +05:30
|
|
|
end
|
|
|
|
end
|