diff options
Diffstat (limited to 'lib/bulk_imports/path_normalization.rb')
-rw-r--r-- | lib/bulk_imports/path_normalization.rb | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/lib/bulk_imports/path_normalization.rb b/lib/bulk_imports/path_normalization.rb new file mode 100644 index 00000000000..dfeef330ff8 --- /dev/null +++ b/lib/bulk_imports/path_normalization.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module BulkImports + module PathNormalization + private + + def normalize_path(path) + path = path.parameterize.downcase + return path if path =~ Gitlab::Regex.oci_repository_path_regex + + # remove invalid characters from end and start of path + delete_invalid_edge_characters(delete_invalid_edge_characters(path)) + # remove invalid multiplied characters + delete_invalid_multiple_characters(path) + end + + def delete_invalid_edge_characters(path) + path.reverse! + path.each_char do |char| + break path unless char.match(Gitlab::Regex.oci_repository_path_regex).nil? + + path.delete_prefix!(char) + end + end + + def delete_invalid_multiple_characters(path) + path.gsub!('-_', '-') if path.include?('-_') + path.gsub!('_-', '-') if path.include?('_-') + path + end + end +end |