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/- NOCOV_MARKER_RE =
/\A# :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
- .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
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 138 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
224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 224 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
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 110 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 |
.ignored_duplicate_chunk?(path, line1, line2) ⇒ Boolean
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 93 def ignored_duplicate_chunk?(path, line1, line2) basename = File.basename(path.to_s) 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) && NOCOV_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
212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 212 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 StandardError nil end |
.report_summary(results, project_root: nil) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 188 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
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 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 55 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
133 134 135 136 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 133 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
149 150 151 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 149 def subtract_baseline(results, baseline_set:) results.reject { |line_content, _| baseline_set.include?(line_content) } end |
.template_managed_files(project_root:, template_dir: nil) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 153 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
176 177 178 179 180 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 176 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
172 173 174 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 172 def warning_count(results) results.values.flatten.size end |
.write_json(results, json_path) ⇒ Object
182 183 184 185 186 |
# File 'lib/kettle/drift/duplicate_line_validator.rb', line 182 def write_json(results, json_path) FileUtils.mkdir_p(File.dirname(json_path)) File.write(json_path, to_json(results)) json_path end |