Module: Kettle::Drift::DuplicateLineValidator
- Defined in:
- lib/kettle/drift/duplicate_line_validator.rb
Overview
Scans managed files for repeated adjacent-line chunks that usually signal duplication drift or template corruption.
Constant Summary collapse
- DEFAULT_MIN_CHARS =
6- BINARY_EXTENSIONS =
Set.new(%w[ .7z .bz2 .class .dll .exe .gem .gif .gz .ico .jar .jpeg .jpg .pdf .png .so .tar .tgz .ttf .woff .woff2 .xz .zip ]).freeze
- APPRAISALS_DEP_LINE_RE =
/\A(?:eval_gemfile|gem)\s+["']/- CHANGELOG_METRIC_RE =
/\A-\s+(?:(?:(?:line|branch)\s+)?coverage:|\d+\.\d+%\s+documented)/i- EXCLUDED_FILENAMES =
Set.new(["CODE_OF_CONDUCT.md", ".gitlab-ci.yml"]).freeze
- KETTLE_JEM_CONFIG_RE =
/\A(?:strategy|recipe|preference|add_missing|freeze_token|file_type|max_recursion_depth):\s/- RAKEFILE_ENV_ASSIGNMENT_RE =
/\AENV\[["']/- RESCUE_LOAD_ERROR_RE =
/\Arescue\s+LoadError/- COVERAGE_MARKER_RE =
/\A# (?:simplecov:(?:disable|enable)|:nocov:)\z/- CHANGELOG_SUBHEADINGS =
Set.new([ "### Added", "### Changed", "### Deprecated", "### Removed", "### Fixed", "### Security" ]).freeze
Class Method Summary collapse
- .baseline(template_dir: nil, min_chars: DEFAULT_MIN_CHARS) ⇒ Object
- .binary_content?(path, raw) ⇒ Boolean
- .compute_fence_lines(content) ⇒ Object
-
.dependabot_schedule_chunk?(path, line1, line2) ⇒ Boolean
Dependabot intentionally has one schedule mapping per ecosystem entry.
-
.generated_workflow_matrix_chunk?(path, line1, line2) ⇒ Boolean
These generated workflows repeat the same matrix-field pairs once for every Ruby/engine job.
- .ignored_duplicate_chunk?(path, line1, line2) ⇒ Boolean
- .read_text_content(path) ⇒ Object
- .report_summary(results, project_root: nil) ⇒ Object
- .scan(files:, min_chars: DEFAULT_MIN_CHARS) ⇒ Object
- .scan_template_results(template_results:, min_chars: DEFAULT_MIN_CHARS) ⇒ Object
- .subtract_baseline(results, baseline_set:) ⇒ Object
- .template_managed_files(project_root:, template_dir: nil) ⇒ Object
- .to_json(results) ⇒ Object
- .warning_count(results) ⇒ Object
- .write_json(results, json_path) ⇒ Object
Class Method Details
.baseline(template_dir: nil, min_chars: DEFAULT_MIN_CHARS) ⇒ Object
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 162 def baseline(template_dir: nil, min_chars: DEFAULT_MIN_CHARS) return Set.new unless template_dir && File.directory?(template_dir) template_files = Dir.glob( File.join(template_dir, "**", "*"), File::FNM_DOTMATCH ).select { |f| File.file?(f) } Set.new(scan(files: template_files, min_chars: min_chars).keys) end |
.binary_content?(path, raw) ⇒ Boolean
248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 248 def binary_content?(path, raw) return true if BINARY_EXTENSIONS.include?(File.extname(path.to_s).downcase) sample = raw.byteslice(0, 4096) || "".b return true if sample.include?("\x00") return false if sample.empty? control_bytes = sample.each_byte.count do |byte| (0..8).cover?(byte) || byte == 11 || byte == 12 || (14..31).cover?(byte) end control_bytes.fdiv(sample.bytesize) > 0.1 end |
.compute_fence_lines(content) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 134 def compute_fence_lines(content) in_fence = false fence_marker = nil fence_lines = Set.new content.each_line.with_index(1) do |raw, lineno| stripped = raw.strip if in_fence fence_lines.add(lineno) if stripped.match?(/\A#{Regexp.escape(fence_marker)}\s*\z/) in_fence = false fence_marker = nil end elsif (match = stripped.match(/\A(`{3,}|~{3,})/)) fence_marker = match[1] in_fence = true fence_lines.add(lineno) end end fence_lines end |
.dependabot_schedule_chunk?(path, line1, line2) ⇒ Boolean
Dependabot intentionally has one schedule mapping per ecosystem entry. This exact pair has no independent behavior to de-duplicate.
128 129 130 131 132 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 128 def dependabot_schedule_chunk?(path, line1, line2) File.basename(path.to_s) == "dependabot.yml" && File.dirname(path.to_s).end_with?(".github") && line1.delete_prefix("- ") == "schedule:" && line2.start_with?("interval:") end |
.generated_workflow_matrix_chunk?(path, line1, line2) ⇒ Boolean
These generated workflows repeat the same matrix-field pairs once for every Ruby/engine job. Limit the exemption to those exact pairs so a duplicated step or an unrelated workflow regression is still reported.
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 115 def generated_workflow_matrix_chunk?(path, line1, line2) return false unless %w[dep-heads.yml heads.yml].include?(File.basename(path.to_s)) return false unless File.dirname(path.to_s).end_with?(".github/workflows") matrix_key = line1.delete_prefix("- ") return true if matrix_key.start_with?("appraisal: ") && line2.start_with?("exec_cmd: ") return true if line1.start_with?("exec_cmd: ") && (line2.start_with?("rubygems: ", "# Run directly from the generated appraisal Gemfile")) line1 == "if: ${{ !env.ACT }}" && line2.start_with?("uses: appraisal-rb/setup-ruby-flash@") end |
.ignored_duplicate_chunk?(path, line1, line2) ⇒ Boolean
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 92 def ignored_duplicate_chunk?(path, line1, line2) basename = File.basename(path.to_s) return true if generated_workflow_matrix_chunk?(path, line1, line2) return true if dependabot_schedule_chunk?(path, line1, line2) if basename == "Appraisals" return true if APPRAISALS_DEP_LINE_RE.match?(line1) && APPRAISALS_DEP_LINE_RE.match?(line2) return true if line1.start_with?("#") && APPRAISALS_DEP_LINE_RE.match?(line2) end return true if basename == "CHANGELOG.md" && CHANGELOG_METRIC_RE.match?(line1) && CHANGELOG_METRIC_RE.match?(line2) return true if basename == "Rakefile" && RAKEFILE_ENV_ASSIGNMENT_RE.match?(line1) && RAKEFILE_ENV_ASSIGNMENT_RE.match?(line2) return true if RESCUE_LOAD_ERROR_RE.match?(line1) && COVERAGE_MARKER_RE.match?(line2) return true if File.extname(path.to_s) == ".md" && line1.start_with?("|") && line2.start_with?("|") return true if basename == ".kettle-jem.yml" && KETTLE_JEM_CONFIG_RE.match?(line1) && KETTLE_JEM_CONFIG_RE.match?(line2) false end |
.read_text_content(path) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 236 def read_text_content(path) raw = File.binread(path) return if binary_content?(path, raw) content = raw.dup.force_encoding(Encoding::UTF_8) return content if content.valid_encoding? content.scrub rescue nil end |
.report_summary(results, project_root: nil) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 212 def report_summary(results, project_root: nil) return "No duplicate lines detected.\n" if results.empty? lines = ["### Duplicate Line Report\n", "| Chunk (line1 ↵ line2) | File | Start Lines |", "|---|---|---|"] results.each do |content, entries| display = content.gsub("\n", " ↵ ") display = "#{display[0, 77]}..." if display.length > 80 display = display.gsub("|", "\\|") entries.each do |entry| file = Kettle::Drift.display_path(entry[:file]) if project_root display_root = Kettle::Drift.display_path(project_root) file = file.sub(%r{^#{Regexp.escape(display_root)}/?}, "") end lines << "| `#{display}` | #{file} | #{entry[:lines].join(", ")} |" end end lines << "" lines.join("\n") end |
.scan(files:, min_chars: DEFAULT_MIN_CHARS) ⇒ Object
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 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 54 def scan(files:, min_chars: DEFAULT_MIN_CHARS) duplicates = {} files.each do |path| next unless File.file?(path) next if EXCLUDED_FILENAMES.include?(File.basename(path.to_s)) content = read_text_content(path) next unless content fence_lines = (File.extname(path.to_s) == ".md") ? compute_fence_lines(content) : Set.new indexed = content.each_line.map.with_index(1) { |raw, n| [n, raw.strip] } chunk_map = Hash.new { |h, k| h[k] = [] } indexed.each_cons(2) do |(lineno1, line1), (lineno2, line2)| next if line1.gsub(/\s/, "").length <= min_chars next if line2.gsub(/\s/, "").length <= min_chars next if CHANGELOG_SUBHEADINGS.include?(line1) next if fence_lines.include?(lineno1) && fence_lines.include?(lineno2) next if ignored_duplicate_chunk?(path, line1, line2) chunk_map["#{line1}\n#{line2}"] << lineno1 end chunk_map.each do |chunk_content, start_lines| next if start_lines.size < 2 duplicates[chunk_content] ||= [] duplicates[chunk_content] << { file: path, lines: start_lines } end end duplicates end |
.scan_template_results(template_results:, min_chars: DEFAULT_MIN_CHARS) ⇒ Object
157 158 159 160 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 157 def scan_template_results(template_results:, min_chars: DEFAULT_MIN_CHARS) written_files = template_results.select { |_path, rec| %i[create replace].include?(rec[:action]) }.keys scan(files: written_files, min_chars: min_chars) end |
.subtract_baseline(results, baseline_set:) ⇒ Object
173 174 175 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 173 def subtract_baseline(results, baseline_set:) results.except(*baseline_set) end |
.template_managed_files(project_root:, template_dir: nil) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 177 def template_managed_files(project_root:, template_dir: nil) template_dir ||= File.join(project_root, "template") return [] unless File.directory?(template_dir) managed = [] Dir.glob(File.join(template_dir, "**", "*"), File::FNM_DOTMATCH).each do |src| next unless File.file?(src) rel = src.sub(%r{^#{Regexp.escape(template_dir)}/?}, "") rel = rel.sub(/\.example\z/, "") next if rel.include?(".no-osc") dest = File.join(project_root, rel) managed << dest if File.file?(dest) end managed.uniq end |
.to_json(results) ⇒ Object
200 201 202 203 204 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 200 def to_json(results) JSON.pretty_generate(results.transform_values do |entries| entries.map { |entry| {file: Kettle::Drift.display_path(entry[:file]), lines: entry[:lines]} } end) end |
.warning_count(results) ⇒ Object
196 197 198 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 196 def warning_count(results) results.values.flatten.size end |
.write_json(results, json_path) ⇒ Object
206 207 208 209 210 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 206 def write_json(results, json_path) FileUtils.mkdir_p(File.dirname(json_path)) File.write(json_path, to_json(results)) json_path end |