File: path_normalization.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (33 lines) | stat: -rw-r--r-- 894 bytes parent folder | download
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
33
# frozen_string_literal: true

module BulkImports
  module PathNormalization
    private

    def normalize_path(path)
      return path.downcase if Gitlab::Regex.oci_repository_path_regex.match?(path)

      path = path.parameterize.downcase

      # 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