Method: PathSpec::GitIgnoreSpec#initialize

Defined in:
lib/pathspec/gitignorespec.rb

#initialize(original_pattern) ⇒ GitIgnoreSpec

rubocop:disable Metrics/CyclomaticComplexity



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/pathspec/gitignorespec.rb', line 10

def initialize(original_pattern) # rubocop:disable Metrics/CyclomaticComplexity
  pattern = original_pattern.strip unless original_pattern.nil?

  # A pattern starting with a hash ('#') serves as a comment
  # (neither includes nor excludes files). Escape the hash with a
  # back-slash to match a literal hash (i.e., '\#').
  if pattern.start_with?('#')
    @regex = nil
    @inclusive = nil

    # A blank pattern is a null-operation (neither includes nor
    # excludes files).
  elsif pattern.empty? # rubocop:disable Lint/DuplicateBranch
    @regex = nil
    @inclusive = nil

    # Patterns containing three or more consecutive stars are invalid and
    # will be ignored.
  elsif /\*\*\*+/.match?(pattern) # rubocop:disable Lint/DuplicateBranch
    @regex = nil
    @inclusive = nil

    # EDGE CASE: According to git check-ignore (v2.4.1)), a single '/'
    # does not match any file
  elsif pattern == '/' # rubocop:disable Lint/DuplicateBranch
    @regex = nil
    @inclusive = nil

    # We have a valid pattern!
  else
    # A pattern starting with an exclamation mark ('!') negates the
    # pattern (exclude instead of include). Escape the exclamation
    # mark with a back-slash to match a literal exclamation mark
    # (i.e., '\!').
    if pattern.start_with?('!')
      @inclusive = false
      # Remove leading exclamation mark.
      pattern = pattern[1..]
    else
      @inclusive = true
    end

    # Remove leading back-slash escape for escaped hash ('#') or
    # exclamation mark ('!').
    pattern = pattern[1..] if pattern.start_with?('\\')

    # Split pattern into segments. -1 to allow trailing slashes.
    pattern_segs = pattern.split('/', -1)

    # Normalize pattern to make processing easier.

    # A pattern beginning with a slash ('/') will only match paths
    # directly on the root directory instead of any descendant
    # paths. So, remove empty first segment to make pattern relative
    # to root.
    if pattern_segs[0].empty?
      pattern_segs.shift
    elsif pattern_segs.length == 1 ||
          pattern_segs.length == 2 && pattern_segs[-1].empty?
      # A pattern without a beginning slash ('/') will match any
      # descendant path. This is equivilent to "**/{pattern}". So,
      # prepend with double-asterisks to make pattern relative to
      # root.
      # EDGE CASE: This also holds for a single pattern with a
      # trailing slash (e.g. dir/).
      pattern_segs.insert(0, '**') if pattern_segs[0] != '**'
    end

    # A pattern ending with a slash ('/') will match all descendant
    # paths of if it is a directory but not if it is a regular file.
    # This is equivilent to "{pattern}/**". So, set last segment to
    # double asterisks to include all descendants.
    pattern_segs[-1] = '**' if pattern_segs[-1].empty? && pattern_segs.length > 1

    # Handle platforms with backslash separated paths
    path_sep = if File::SEPARATOR == '\\'
                 '\\\\'
               else
                 '/'
               end

    # Build regular expression from pattern.
    regex = +'^'
    need_slash = false
    regex_end = pattern_segs.size - 1
    pattern_segs.each_index do |i|
      seg = pattern_segs[i]

      case seg
      when '**'
        # A pattern consisting solely of double-asterisks ('**')
        # will match every path.
        if i == 0 && i == regex_end
          regex.concat('.+')

          # A normalized pattern beginning with double-asterisks
          # ('**') will match any leading path segments.
        elsif i == 0
          regex.concat("(?:.+#{path_sep})?")
          need_slash = false

          # A normalized pattern ending with double-asterisks ('**')
          # will match any trailing path segments.
        elsif i == regex_end
          regex.concat("#{path_sep}.*")

          # A pattern with inner double-asterisks ('**') will match
          # multiple (or zero) inner path segments.
        else
          regex.concat("(?:#{path_sep}.+)?")
          need_slash = true
        end

        # Match single path segment.
      when '*'
        regex.concat(path_sep) if need_slash

        regex.concat("[^#{path_sep}]+")
        need_slash = true

      else
        # Match segment glob pattern.
        regex.concat(path_sep) if need_slash

        regex.concat(translate_segment_glob(seg))

        if i == regex_end && @inclusive
          # A pattern ending without a slash ('/') will match a file
          # or a directory (with paths underneath it).
          # e.g. foo matches: foo, foo/bar, foo/bar/baz, etc.
          # EDGE CASE: However, this does not hold for exclusion cases
          # according to `git check-ignore` (v2.4.1).
          regex.concat("(?:#{path_sep}.*)?")
        end

        need_slash = true
      end
    end

    regex.concat('$')
    super(regex)

    # Copy original pattern
    @pattern = original_pattern.dup
  end
end