Module: CommitStats

Defined in:
lib/commit_stats.rb

Class Method Summary collapse

Class Method Details

.change_percent(numerator, denominator) ⇒ Object



214
215
216
217
218
# File 'lib/commit_stats.rb', line 214

def self.change_percent(numerator, denominator)
  denominator > 0 ?
    (numerator / denominator.to_f * 100) :
    0
end

.diff_contextObject



220
221
222
223
224
# File 'lib/commit_stats.rb', line 220

def self.diff_context()
  {
    insertions: [],
    deletions: [] }
end


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/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


4
5
6
7
8
9
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
# 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) ⇒ Object



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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# 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