module CommitStats
Public Class Methods
change_percent(numerator, denominator)
click to toggle source
# File lib/commit_stats.rb, line 214 def self.change_percent(numerator, denominator) denominator > 0 ? (numerator / denominator.to_f * 100) : 0 end
diff_context()
click to toggle source
# File lib/commit_stats.rb, line 220 def self.diff_context() { insertions: [], deletions: [] } end
link_diffs!(files, file_diffs)
click to toggle source
# File lib/commit_stats.rb, line 63 def self.link_diffs!(files, file_diffs) file_diffs.reduce(diff_context()) do |acc, (file_name, file_diff)| file_diff.diffs.each do |diff| diff.insertions.each_with_index do |insertion, index| line_num = diff.insert_start + index - 1 file = files[:b][file_diff.b_file_name][:b_file] line = file.is_a?(Hash) ? file[:lines][line_num] : file.lines[line_num] line[:insertion] = true acc[:insertions] << line end del_index = diff.insert_count > 0 ? 1 : 0 diff.deletions.each_with_index do |deletion, index| line_num = diff.delete_start + index - 1 file = files[:a][file_diff.a_file_name][:a_file] line = file.is_a?(Hash) ? file[:lines][line_num] : file.lines[line_num] line.merge!({ deletion: true, a_pos: line_num + 1, b_pos: diff.insert_start + index - del_index }) del_index += 1 if line[:change].blank? acc[:deletions] << line end end acc end end
link_mutations!(files, commit_mutations, type)
click to toggle source
# File lib/commit_stats.rb, line 4 def self.link_mutations!(files, commit_mutations, type) dmp = DiffMatchPatch.new() commit_mutations.each do |dest_file, file_mutations| file_mutations.each do |dest_line, mutation| src_file = mutation[:from] src_line = mutation[:line] b_file = files[:b][dest_file][:b_file] a_file = files[:a][src_file][:a_file] b_line = b_file.is_a?(Hash) ? b_file[:lines][dest_line] : b_file.lines[dest_line] a_line = a_file.is_a?(Hash) ? a_file[:lines][src_line] : a_file.lines[src_line] if type == :change diff = dmp.diff_main(a_line[:text], b_line[:text], false) b_line[:diff_text] = diff.reduce("") do |acc, section| if section.first == 0 acc += section.last elsif section.first == 1 acc += "<ins>#{section.last}</ins>" end acc end a_line[:diff_text] = diff.reduce("") do |acc, section| if section.first == 0 acc += section.last elsif section.first == -1 acc += "<del>#{section.last}</del>" end acc end end if Line.trailing?(b_line, a_line) change_type = :trailing elsif Line.beauty?(b_line, a_line) change_type = :beauty else change_type = :normal end b_line[type] = { link: "#/a/#{src_line + 1}/#{src_file}", type: change_type, change_text: a_line[:text] } a_line[type] = { link: "#/b/#{dest_line + 1}/#{dest_file}", type: change_type, change_text: b_line[:text] } end end end
stats(commit, line_diffs, files)
click to toggle source
# File lib/commit_stats.rb, line 93 def self.stats(commit, line_diffs, files) changes_total = commit .changes .reduce(0) { |acc, (filename, changeset)| acc += changeset.count } * 2 movements_total = commit .movements .reduce(0) { |acc, (filename, moveset)| acc += moveset.count } * 2 uncovered_deletions_total = 0 uncovered_insertions_total = 0 uncovered_changes_total = 0 uncovered_error_changes_total = 0 uncovered_buggy_changes_total = 0 error_deletions_total = 0 error_changes_total = 0 buggy_insertions_total = 0 buggy_deletions_total = 0 buggy_changes_total = 0 whitespace_insertions_total = 0 whitespace_deletions_total = 0 total_files_modified = files[:a].count total_lines_modified = commit.changes_total trailingspace_total = 0 beautyspace_total = 0 line_diffs[:insertions].each do |insertion| if insertion[:coverage] == 0 uncovered_insertions_total += 1 if insertion[:change] uncovered_changes_total += 1 uncovered_error_changes_total += 1 if insertion[:errors].count > 0 uncovered_buggy_changes_total += 1 if insertion[:bugs].count > 0 end end if insertion[:change] buggy_changes_total += 1 if insertion[:bugs].count > 0 error_changes_total += 1 if insertion[:errors].count > 0 beautyspace_total += 2 if insertion[:change][:type] == :beauty # + deletion trailingspace_total += 2 if insertion[:change][:type] == :trailing # + deletion end whitespace_insertions_total += 1 if insertion[:text].strip().blank? buggy_insertions_total += 1 if insertion[:bugs].count > 0 end line_diffs[:deletions].each do |deletion| if deletion[:coverage] == 0 uncovered_deletions_total += 1 end error_deletions_total += 1 if deletion[:errors].count > 0 whitespace_deletions_total += 1 if deletion[:text].strip().blank? buggy_deletions_total += 1 if deletion[:bugs].count > 0 end relevant_insertions_total = commit.insertions_total - whitespace_insertions_total covered_insertions_total = relevant_insertions_total - uncovered_insertions_total covered_insertions_percent = change_percent(covered_insertions_total, relevant_insertions_total) uncovered_insertions_percent = change_percent(uncovered_insertions_total, relevant_insertions_total) buggy_insertions_percent = change_percent(buggy_insertions_total, relevant_insertions_total) relevant_deletions_total = commit.insertions_total - whitespace_insertions_total covered_deletions_total = relevant_deletions_total - uncovered_deletions_total covered_deletions_percent = change_percent(covered_deletions_total, relevant_deletions_total) uncovered_deletions_percent = change_percent(uncovered_deletions_total, relevant_deletions_total) buggy_deletions_percent = change_percent(buggy_deletions_total, relevant_deletions_total) error_deletions_percent = change_percent(error_deletions_total, relevant_deletions_total) covered_changes_total = changes_total - uncovered_changes_total covered_changes_percent = change_percent(covered_changes_total, changes_total) uncovered_changes_percent = change_percent(uncovered_changes_total, changes_total) uncovered_error_changes_percent = change_percent(uncovered_error_changes_total, changes_total) uncovered_buggy_changes_percent = change_percent(uncovered_buggy_changes_total, changes_total) diffs_total = commit.deletions_total + commit.insertions_total movements_percent = change_percent(movements_total, diffs_total) whitespace_total = whitespace_insertions_total + whitespace_deletions_total whitespace_percent = change_percent(whitespace_total, diffs_total) trailingspace_percent = change_percent(trailingspace_total, diffs_total) beautyspace_percent = change_percent(beautyspace_total, diffs_total) { changes: commit.changes, movements: commit.movements, deletions_total: commit.deletions_total, insertions_total: commit.insertions_total, changes_total: changes_total, movements_total: movements_total, whitespace_total: whitespace_total, trailingspace_total: trailingspace_total, beautyspace_total: beautyspace_total, covered_insertions_percent: covered_insertions_percent, covered_deletions_percent: covered_deletions_percent, covered_changes_percent: covered_changes_percent, uncovered_insertions_percent: uncovered_insertions_percent, uncovered_deletions_percent: uncovered_deletions_percent, uncovered_changes_percent: uncovered_changes_percent, uncovered_error_changes_percent: uncovered_error_changes_percent, uncovered_buggy_changes_percent: uncovered_buggy_changes_percent, buggy_insertions_percent: buggy_insertions_percent, buggy_deletions_percent: buggy_deletions_percent, error_deletions_percent: error_deletions_percent, movements_percent: movements_percent, whitespace_percent: whitespace_percent, trailingspace_percent: trailingspace_percent, beautyspace_percent: beautyspace_percent, uncovered_deletions_total: uncovered_deletions_total, uncovered_insertions_total: uncovered_insertions_total, uncovered_changes_total: uncovered_changes_total, uncovered_error_changes_total: uncovered_error_changes_total, buggy_insertions_total: buggy_insertions_total, buggy_deletions_total: buggy_deletions_total, buggy_changes_total: buggy_changes_total, uncovered_buggy_changes_total: uncovered_buggy_changes_total, error_deletions_total: error_deletions_total, error_changes_total: error_changes_total, total_files_modified: total_files_modified, total_lines_modified: total_lines_modified } end