summaryrefslogtreecommitdiff
path: root/lib/bulk_imports/path_normalization.rb
blob: dfeef330ff88fd1bcc8c06681b277b429cd0e9be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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