Module: Dependabot::FileFiltering

Extended by:
T::Sig
Defined in:
lib/dependabot/file_filtering.rb

Class Method Summary collapse

Class Method Details

.exact_or_directory_match?(normalized_path, pattern, normalized_pattern) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
# File 'lib/dependabot/file_filtering.rb', line 27

def self.exact_or_directory_match?(normalized_path, pattern, normalized_pattern)
  # Exact match
  return true if normalized_path == pattern || normalized_path == normalized_pattern

  # Directory prefix match: check if path is inside an excluded directory
  normalized_path.start_with?(
    "#{pattern}#{File::SEPARATOR}",
    "#{normalized_pattern}#{File::SEPARATOR}"
  )
end

.exclude_path?(path, exclude_patterns) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dependabot/file_filtering.rb', line 10

def self.exclude_path?(path, exclude_patterns)
  return false if exclude_patterns.nil? || exclude_patterns.empty?

  # Normalize the path by removing leading slashes and resolving relative paths
  normalized_path = normalize_path(path)

  exclude_patterns.any? do |pattern|
    normalized_pattern = normalize_path(pattern.chomp("/"))

    exact_or_directory_match?(normalized_path, pattern, normalized_pattern) ||
      recursive_match?(normalized_path, pattern) ||
      glob_match?(normalized_path, pattern, normalized_pattern)
  end
end

.glob_match?(normalized_path, pattern, normalized_pattern) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dependabot/file_filtering.rb', line 56

def self.glob_match?(normalized_path, pattern, normalized_pattern)
  fnmatch_flags = [
    File::FNM_EXTGLOB,
    File::FNM_EXTGLOB | File::FNM_PATHNAME,
    File::FNM_EXTGLOB | File::FNM_PATHNAME | File::FNM_DOTMATCH,
    File::FNM_PATHNAME
  ]

  fnmatch_flags.any? do |flag|
    File.fnmatch?(pattern, normalized_path, flag) || File.fnmatch?(normalized_pattern, normalized_path, flag)
  end
end

.normalize_path(path) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/dependabot/file_filtering.rb', line 73

def self.normalize_path(path)
  return path if path.empty?

  pathname = Pathname.new(path)
  normalized = pathname.cleanpath.to_s

  # Remove leading slash for relative comparison
  normalized = normalized.sub(%r{^/+}, "")
  normalized
end

.recursive_match?(normalized_path, pattern) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dependabot/file_filtering.rb', line 40

def self.recursive_match?(normalized_path, pattern)
  return false unless pattern.end_with?("/**")

  base_pattern_str = pattern[0...-3]
  return false if base_pattern_str.nil? || base_pattern_str.empty?

  base_pattern = normalize_path(base_pattern_str)
  return false if base_pattern.empty?

  normalized_path == base_pattern ||
    normalized_path.start_with?("#{base_pattern}/") ||
    normalized_path.start_with?("#{base_pattern}#{File::SEPARATOR}")
end

.should_exclude_path?(path, context, exclude_paths) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dependabot/file_filtering.rb', line 92

def self.should_exclude_path?(path, context, exclude_paths)
  return false unless Dependabot::Experiments.enabled?(:enable_exclude_paths_subdirectory_manifest_files)

  return false if exclude_paths.nil? || exclude_paths.empty?

  should_exclude = exclude_path?(path, exclude_paths)

  if should_exclude
    Dependabot.logger.warn(
      "Skipping excluded #{context} '#{path}'. " \
      "This file is excluded by exclude_paths configuration: #{exclude_paths}"
    )
  end

  should_exclude
end