churn_vs_complexity 1.8.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +95 -0
- data/README.md +46 -23
- data/bin/churn_vs_complexity +6 -1
- data/lib/churn_vs_complexity/analysis_scope.rb +20 -0
- data/lib/churn_vs_complexity/churn.rb +41 -8
- data/lib/churn_vs_complexity/cli/main.rb +58 -19
- data/lib/churn_vs_complexity/cli/parser.rb +85 -121
- data/lib/churn_vs_complexity/cli.rb +2 -3
- data/lib/churn_vs_complexity/complexity/flog_calculator.rb +0 -3
- data/lib/churn_vs_complexity/complexity/lizard_calculator.rb +64 -0
- data/lib/churn_vs_complexity/complexity.rb +1 -7
- data/lib/churn_vs_complexity/complexity_validator.rb +1 -16
- data/lib/churn_vs_complexity/concurrent_calculator.rb +20 -23
- data/lib/churn_vs_complexity/delta/checker.rb +35 -21
- data/lib/churn_vs_complexity/delta/commit_hydrator.rb +1 -1
- data/lib/churn_vs_complexity/delta/complexity_annotator.rb +23 -5
- data/lib/churn_vs_complexity/delta/config.rb +15 -16
- data/lib/churn_vs_complexity/delta/factory.rb +2 -2
- data/lib/churn_vs_complexity/delta/multi_checker.rb +24 -25
- data/lib/churn_vs_complexity/delta/serializer.rb +7 -4
- data/lib/churn_vs_complexity/delta.rb +11 -49
- data/lib/churn_vs_complexity/diff/checker.rb +10 -3
- data/lib/churn_vs_complexity/diff/config.rb +29 -20
- data/lib/churn_vs_complexity/file_selector.rb +63 -114
- data/lib/churn_vs_complexity/focus/config.rb +42 -32
- data/lib/churn_vs_complexity/focus/serializer.rb +17 -4
- data/lib/churn_vs_complexity/gate/config.rb +35 -25
- data/lib/churn_vs_complexity/gate/serializer.rb +9 -2
- data/lib/churn_vs_complexity/git_strategy.rb +29 -10
- data/lib/churn_vs_complexity/hotspots/config.rb +29 -25
- data/lib/churn_vs_complexity/language_registry.rb +108 -0
- data/lib/churn_vs_complexity/language_validator.rb +1 -3
- data/lib/churn_vs_complexity/normal/config.rb +31 -100
- data/lib/churn_vs_complexity/normal/serializer/graph.rb +3 -2
- data/lib/churn_vs_complexity/normal/serializer/summary.rb +18 -14
- data/lib/churn_vs_complexity/normal/serializer/summary_hash.rb +36 -21
- data/lib/churn_vs_complexity/normal/serializer.rb +2 -1
- data/lib/churn_vs_complexity/normal.rb +17 -13
- data/lib/churn_vs_complexity/risk_classifier.rb +3 -19
- data/lib/churn_vs_complexity/timetravel/config.rb +34 -47
- data/lib/churn_vs_complexity/timetravel/schedule.rb +15 -0
- data/lib/churn_vs_complexity/timetravel/serializer/stats_calculator.rb +36 -32
- data/lib/churn_vs_complexity/timetravel/serializer.rb +16 -24
- data/lib/churn_vs_complexity/timetravel/traveller.rb +46 -15
- data/lib/churn_vs_complexity/timetravel.rb +36 -14
- data/lib/churn_vs_complexity/triage/checker.rb +24 -28
- data/lib/churn_vs_complexity/triage/collaborator_set.rb +11 -0
- data/lib/churn_vs_complexity/triage/config.rb +29 -26
- data/lib/churn_vs_complexity/triage.rb +1 -0
- data/lib/churn_vs_complexity/validator_set.rb +9 -0
- data/lib/churn_vs_complexity/version.rb +1 -1
- data/lib/churn_vs_complexity.rb +3 -0
- metadata +18 -59
- data/.devcontainer/devcontainer.json +0 -26
- data/.rubocop.yml +0 -25
- data/CLAUDE.md +0 -60
- data/Dockerfile +0 -34
- data/Dockerfile.dev +0 -27
- data/Rakefile +0 -6
- data/TODO +0 -11
- data/lib/churn_vs_complexity/complexity/eslint_calculator.rb +0 -40
- data/lib/churn_vs_complexity/complexity/go_calculator.rb +0 -48
- data/lib/churn_vs_complexity/complexity/kotlin_calculator.rb +0 -54
- data/lib/churn_vs_complexity/complexity/pmd/files_calculator.rb +0 -26
- data/lib/churn_vs_complexity/complexity/pmd/folder_calculator.rb +0 -20
- data/lib/churn_vs_complexity/complexity/pmd.rb +0 -54
- data/lib/churn_vs_complexity/complexity/python_calculator.rb +0 -50
- data/lib/churn_vs_complexity/complexity/rust_calculator.rb +0 -54
- data/lib/churn_vs_complexity/complexity/swift_calculator.rb +0 -54
- data/package-lock.json +0 -6
- data/tmp/eslint-support/complexity-calculator.js +0 -51
- data/tmp/eslint-support/package.json +0 -11
- data/tmp/pmd-support/ruleset.xml +0 -21
- data/tmp/test-support/delta/ruby-summary.txt +0 -55
- data/tmp/test-support/delta/ruby.csv +0 -12
- data/tmp/test-support/go/main.go +0 -11
- data/tmp/test-support/go/utils.go +0 -13
- data/tmp/test-support/java/small-example/src/main/java/org/example/Main.java +0 -10
- data/tmp/test-support/java/small-example/src/main/java/org/example/spice/Checker.java +0 -28
- data/tmp/test-support/javascript/complex.js +0 -43
- data/tmp/test-support/javascript/moderate.js +0 -12
- data/tmp/test-support/javascript/simple.js +0 -5
- data/tmp/test-support/javascript/typescript-example.ts +0 -26
- data/tmp/test-support/kotlin/Main.kt +0 -7
- data/tmp/test-support/kotlin/Utils.kt +0 -11
- data/tmp/test-support/python/example.py +0 -6
- data/tmp/test-support/python/utils.py +0 -9
- data/tmp/test-support/rust/main.rs +0 -7
- data/tmp/test-support/rust/utils.rs +0 -20
- data/tmp/test-support/swift/main.swift +0 -7
- data/tmp/test-support/swift/utils.swift +0 -24
- data/tmp/test-support/txt/abc.txt +0 -0
- data/tmp/test-support/txt/d.txt +0 -0
- data/tmp/test-support/txt/ef.txt +0 -0
- data/tmp/test-support/txt/ghij.txt +0 -0
- data/tmp/test-support/txt/klm.txt +0 -0
- data/tmp/test-support/txt/nopq.txt +0 -0
- data/tmp/test-support/txt/r.txt +0 -0
- data/tmp/test-support/txt/st.txt +0 -0
- data/tmp/test-support/txt/uvx.txt +0 -0
- data/tmp/test-support/txt/yz.txt +0 -0
- data/tmp/timetravel/.keep +0 -0
|
@@ -5,155 +5,119 @@ require 'optparse'
|
|
|
5
5
|
module ChurnVsComplexity
|
|
6
6
|
module CLI
|
|
7
7
|
module Parser
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
parser = OptionParser.new do |opts|
|
|
11
|
-
opts.banner = 'Usage: churn_vs_complexity [options] folder|file...'
|
|
8
|
+
TRIAGE_HELP = 'Assess risk of files based on churn and complexity. ' \
|
|
9
|
+
'Accepts file paths or a folder as arguments.'
|
|
12
10
|
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
SIMPLE_MODES = [
|
|
12
|
+
['--triage', :triage, TRIAGE_HELP],
|
|
13
|
+
['--hotspots', :hotspots, 'Generate ranked list of files by risk'],
|
|
14
|
+
['--gate', :gate, 'Pass/fail quality check against gamma threshold (exits 0 on pass, 1 on fail)'],
|
|
15
|
+
].freeze
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
FORMAT_FLAGS = [
|
|
18
|
+
['--csv', :csv, 'Format output as CSV'],
|
|
19
|
+
['--graph', :graph, 'Format output as HTML page with Churn vs Complexity graph'],
|
|
20
|
+
['--summary', :summary, 'Output summary statistics (mean and median) for churn and complexity'],
|
|
21
|
+
['--json', :json, 'Format output as JSON'],
|
|
22
|
+
['--markdown', :markdown, 'Format output as Markdown'],
|
|
23
|
+
].freeze
|
|
19
24
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
25
|
+
PERIOD_FLAGS = [
|
|
26
|
+
['-m', '--month', :month, 'Calculate churn for the month leading up to the most recent commit'],
|
|
27
|
+
['-q', '--quarter', :quarter, 'Calculate churn for the quarter leading up to the most recent commit'],
|
|
28
|
+
['-y', '--year', :year, 'Calculate churn for the year leading up to the most recent commit'],
|
|
29
|
+
].freeze
|
|
23
30
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
end
|
|
31
|
+
DELTA_HELP = 'Identify changes between the specified commit (SHA) and the previous commit and annotate ' \
|
|
32
|
+
'changed files with complexity score. SHA can be a full or short commit hash, or the ' \
|
|
33
|
+
'value HEAD. Can be used multiple times to specify multiple commits.'
|
|
28
34
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
end
|
|
35
|
+
TIMETRAVEL_HELP = 'Calculate summary for all commits at intervals of N days throughout project history ' \
|
|
36
|
+
'or from the date specified with --since'
|
|
32
37
|
|
|
33
|
-
|
|
34
|
-
options[:language] = :go
|
|
35
|
-
end
|
|
38
|
+
SINCE_HELP = 'Normal mode: Calculate churn after this date. Timetravel mode: calculate summaries from this date'
|
|
36
39
|
|
|
37
|
-
|
|
38
|
-
options[:language] = :kotlin
|
|
39
|
-
end
|
|
40
|
+
FOCUS_HELP = 'Capture complexity snapshot before (start) and after (end) a coding session'
|
|
40
41
|
|
|
41
|
-
|
|
42
|
-
options[:language] = :rust
|
|
43
|
-
end
|
|
42
|
+
DIFF_HELP = 'Compare codebase health between REF and HEAD'
|
|
44
43
|
|
|
45
|
-
|
|
46
|
-
options[:language] = :swift
|
|
47
|
-
end
|
|
44
|
+
EXCLUDED_HELP = 'Exclude file paths including this string. Can be used multiple times.'
|
|
48
45
|
|
|
49
|
-
|
|
50
|
-
opts.separator 'Modes (mutually exclusive):'
|
|
46
|
+
MAX_GAMMA_HELP = 'Maximum gamma score threshold for gate mode (default: 25)'
|
|
51
47
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
48
|
+
class << self
|
|
49
|
+
def create
|
|
50
|
+
options = { excluded: [] }
|
|
51
|
+
parser = OptionParser.new do |opts|
|
|
52
|
+
opts.banner = 'Usage: churn_vs_complexity [options] folder|file...'
|
|
53
|
+
add_languages(opts, options)
|
|
54
|
+
add_modes(opts, options)
|
|
55
|
+
add_formats(opts, options)
|
|
56
|
+
add_modifiers(opts, options)
|
|
56
57
|
end
|
|
58
|
+
[parser, options]
|
|
59
|
+
end
|
|
57
60
|
|
|
58
|
-
|
|
59
|
-
'Assess risk of files based on churn and complexity. Accepts file paths or a folder as arguments.',) do
|
|
60
|
-
options[:mode] = :triage
|
|
61
|
-
end
|
|
61
|
+
private
|
|
62
62
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
opts.on('--gate',
|
|
68
|
-
'Pass/fail quality check against gamma threshold (exits 0 on pass, 1 on fail)',) do
|
|
69
|
-
options[:mode] = :gate
|
|
63
|
+
def add_languages(opts, options)
|
|
64
|
+
add_section(opts, 'Languages:')
|
|
65
|
+
LanguageRegistry.all.each do |entry|
|
|
66
|
+
opts.on(*entry.cli_flags, entry.description) { options[:language] = entry.key }
|
|
70
67
|
end
|
|
68
|
+
end
|
|
71
69
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
70
|
+
def add_modes(opts, options)
|
|
71
|
+
add_section(opts, 'Modes (mutually exclusive):')
|
|
72
|
+
opts.on('--timetravel N', TIMETRAVEL_HELP) { |v| options.merge!(mode: :timetravel, jump_days: v.to_i) }
|
|
73
|
+
SIMPLE_MODES.each { |flag, mode, desc| opts.on(flag, desc) { options[:mode] = mode } }
|
|
74
|
+
add_comparison_modes(opts, options)
|
|
75
|
+
end
|
|
77
76
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
options
|
|
77
|
+
def add_comparison_modes(opts, options)
|
|
78
|
+
opts.on('--focus start|end', FOCUS_HELP) do |v|
|
|
79
|
+
options.merge!(mode: :focus, subcommand: v.to_sym)
|
|
81
80
|
end
|
|
82
|
-
|
|
83
|
-
opts.on('--delta SHA',
|
|
84
|
-
'Identify changes between the specified commit (SHA) and the previous commit and annotate changed files with complexity score. SHA can be a full or short commit hash, or the value HEAD. Can be used multiple times to specify multiple commits.',) do |value|
|
|
81
|
+
opts.on('--diff REF', DIFF_HELP) { |v| options.merge!(mode: :diff, reference: v) }
|
|
82
|
+
opts.on('--delta SHA', DELTA_HELP) do |v|
|
|
85
83
|
options[:mode] = :delta
|
|
86
|
-
(options[:commits] ||= []) <<
|
|
84
|
+
(options[:commits] ||= []) << v
|
|
87
85
|
end
|
|
86
|
+
end
|
|
88
87
|
|
|
89
|
-
|
|
90
|
-
opts
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
options[:serializer] = :csv
|
|
94
|
-
end
|
|
88
|
+
def add_formats(opts, options)
|
|
89
|
+
add_section(opts, 'Output formats:')
|
|
90
|
+
FORMAT_FLAGS.each { |flag, fmt, desc| opts.on(flag, desc) { options[:serializer] = fmt } }
|
|
91
|
+
end
|
|
95
92
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
93
|
+
def add_modifiers(opts, options)
|
|
94
|
+
add_section(opts, 'Modifiers:')
|
|
95
|
+
add_filter_and_period_modifiers(opts, options)
|
|
96
|
+
add_exit_flags(opts, options)
|
|
97
|
+
end
|
|
99
98
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
99
|
+
def add_filter_and_period_modifiers(opts, options)
|
|
100
|
+
opts.on('--excluded PATTERN', EXCLUDED_HELP) { |v| options[:excluded] << v }
|
|
101
|
+
opts.on('--since YYYY-MM-DD', SINCE_HELP) { |v| options[:since] = v }
|
|
102
|
+
PERIOD_FLAGS.each { |s, l, period, desc| opts.on(s, l, desc) { options[:relative_period] = period } }
|
|
103
|
+
opts.on('--max-gamma N', Float, MAX_GAMMA_HELP) { |v| options[:max_gamma] = v }
|
|
104
|
+
end
|
|
103
105
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
106
|
+
def add_exit_flags(opts, options)
|
|
107
|
+
opts.on('--dry-run', 'Echo the chosen options from the CLI') { print_and_exit(options) }
|
|
108
|
+
opts.on('-h', '--help', 'Display help') { print_and_exit(opts) }
|
|
109
|
+
opts.on('--version', 'Display version') { print_and_exit(ChurnVsComplexity::VERSION) }
|
|
110
|
+
end
|
|
107
111
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
112
|
+
def print_and_exit(value)
|
|
113
|
+
puts value
|
|
114
|
+
exit
|
|
115
|
+
end
|
|
111
116
|
|
|
117
|
+
def add_section(opts, heading)
|
|
112
118
|
opts.separator ''
|
|
113
|
-
opts.separator
|
|
114
|
-
|
|
115
|
-
opts.on('--excluded PATTERN',
|
|
116
|
-
'Exclude file paths including this string. Can be used multiple times.',) do |value|
|
|
117
|
-
options[:excluded] << value
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
opts.on('--since YYYY-MM-DD',
|
|
121
|
-
'Normal mode: Calculate churn after this date. Timetravel mode: calculate summaries from this date',) do |value|
|
|
122
|
-
options[:since] = value
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
opts.on('-m', '--month', 'Calculate churn for the month leading up to the most recent commit') do
|
|
126
|
-
options[:relative_period] = :month
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
opts.on('-q', '--quarter', 'Calculate churn for the quarter leading up to the most recent commit') do
|
|
130
|
-
options[:relative_period] = :quarter
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
opts.on('-y', '--year', 'Calculate churn for the year leading up to the most recent commit') do
|
|
134
|
-
options[:relative_period] = :year
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
opts.on('--max-gamma N', Float, 'Maximum gamma score threshold for gate mode (default: 25)') do |value|
|
|
138
|
-
options[:max_gamma] = value
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
opts.on('--dry-run', 'Echo the chosen options from the CLI') do
|
|
142
|
-
puts options
|
|
143
|
-
exit
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
opts.on('-h', '--help', 'Display help') do
|
|
147
|
-
puts opts
|
|
148
|
-
exit
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
opts.on('--version', 'Display version') do
|
|
152
|
-
puts ChurnVsComplexity::VERSION
|
|
153
|
-
exit
|
|
154
|
-
end
|
|
119
|
+
opts.separator heading
|
|
155
120
|
end
|
|
156
|
-
[parser, options]
|
|
157
121
|
end
|
|
158
122
|
end
|
|
159
123
|
end
|
|
@@ -9,10 +9,9 @@ module ChurnVsComplexity
|
|
|
9
9
|
def run!
|
|
10
10
|
parser, options = Parser.create
|
|
11
11
|
parser.parse!
|
|
12
|
-
|
|
13
|
-
folder = ARGV.first
|
|
12
|
+
targets = ARGV.dup
|
|
14
13
|
|
|
15
|
-
result = Main.run!(options,
|
|
14
|
+
result = Main.run!(options, targets.first, targets:)
|
|
16
15
|
puts result
|
|
17
16
|
exit 1 if result.respond_to?(:passed?) && !result.passed?
|
|
18
17
|
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'open3'
|
|
4
|
+
|
|
5
|
+
module ChurnVsComplexity
|
|
6
|
+
module Complexity
|
|
7
|
+
module LizardCalculator
|
|
8
|
+
LIZARD_LINE_PATTERN = /^\d+,(\d+),\d+,\d+,\d+,"[^"]*","([^"]*)"/
|
|
9
|
+
MISSING_MESSAGE = 'Needs lizard installed (pip install lizard)'
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
attr_writer :command_runner
|
|
13
|
+
|
|
14
|
+
def calculate(files:)
|
|
15
|
+
return {} if files.empty?
|
|
16
|
+
|
|
17
|
+
csv_output = run_lizard(files)
|
|
18
|
+
parse_lizard_output(csv_output, files:)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def parse_lizard_output(csv_output, files:)
|
|
22
|
+
scores = aggregate_scores(csv_output)
|
|
23
|
+
files.to_h { |file| [file, scores[file]] }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def check_dependencies!
|
|
27
|
+
_stdout, stderr, status = command_runner.call('lizard', '--version')
|
|
28
|
+
raise Error, failure_message(status, stderr) unless status.success?
|
|
29
|
+
rescue Errno::ENOENT
|
|
30
|
+
raise Error, MISSING_MESSAGE
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def command_runner
|
|
36
|
+
@command_runner || Open3.method(:capture3)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def run_lizard(files)
|
|
40
|
+
stdout, stderr, status = command_runner.call('lizard', '--csv', *files)
|
|
41
|
+
raise Error, failure_message(status, stderr) unless status.success?
|
|
42
|
+
|
|
43
|
+
stdout
|
|
44
|
+
rescue Errno::ENOENT
|
|
45
|
+
raise Error, MISSING_MESSAGE
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def aggregate_scores(csv_output)
|
|
49
|
+
csv_output.each_line.with_object(Hash.new(0)) do |line, scores|
|
|
50
|
+
match = line.match(LIZARD_LINE_PATTERN)
|
|
51
|
+
scores[match[2]] += match[1].to_i if match
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def failure_message(status, stderr)
|
|
56
|
+
detail = stderr.to_s.strip
|
|
57
|
+
return "lizard failed (exit #{status.exitstatus}). Is it installed?" if detail.empty?
|
|
58
|
+
|
|
59
|
+
"lizard failed (exit #{status.exitstatus}): #{detail}"
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require_relative 'complexity/pmd'
|
|
4
3
|
require_relative 'complexity/flog_calculator'
|
|
5
|
-
require_relative 'complexity/
|
|
6
|
-
require_relative 'complexity/python_calculator'
|
|
7
|
-
require_relative 'complexity/go_calculator'
|
|
8
|
-
require_relative 'complexity/kotlin_calculator'
|
|
9
|
-
require_relative 'complexity/rust_calculator'
|
|
10
|
-
require_relative 'complexity/swift_calculator'
|
|
4
|
+
require_relative 'complexity/lizard_calculator'
|
|
11
5
|
|
|
12
6
|
module ChurnVsComplexity
|
|
13
7
|
module Complexity
|
|
@@ -3,22 +3,7 @@
|
|
|
3
3
|
module ChurnVsComplexity
|
|
4
4
|
module ComplexityValidator
|
|
5
5
|
def self.validate!(language)
|
|
6
|
-
|
|
7
|
-
when :java
|
|
8
|
-
Complexity::PMD.check_dependencies!
|
|
9
|
-
when :javascript
|
|
10
|
-
Complexity::ESLintCalculator.check_dependencies!
|
|
11
|
-
when :python
|
|
12
|
-
Complexity::PythonCalculator.check_dependencies!
|
|
13
|
-
when :go
|
|
14
|
-
Complexity::GoCalculator.check_dependencies!
|
|
15
|
-
when :kotlin
|
|
16
|
-
Complexity::KotlinCalculator.check_dependencies!
|
|
17
|
-
when :rust
|
|
18
|
-
Complexity::RustCalculator.check_dependencies!
|
|
19
|
-
when :swift
|
|
20
|
-
Complexity::SwiftCalculator.check_dependencies!
|
|
21
|
-
end
|
|
6
|
+
LanguageRegistry.fetch(language).validator.check_dependencies!
|
|
22
7
|
end
|
|
23
8
|
end
|
|
24
9
|
end
|
|
@@ -18,50 +18,47 @@ module ChurnVsComplexity
|
|
|
18
18
|
@git_period = GitDate.git_period(since, latest_commit_date)
|
|
19
19
|
schedule_churn_calculation(folder, files[:included], @git_period.effective_start_date)
|
|
20
20
|
end
|
|
21
|
-
calculate_complexity(
|
|
21
|
+
calculate_complexity(files)
|
|
22
22
|
await_results
|
|
23
23
|
combine_results
|
|
24
24
|
end
|
|
25
25
|
|
|
26
26
|
private
|
|
27
27
|
|
|
28
|
-
def calculate_complexity(
|
|
29
|
-
@complexity_results =
|
|
30
|
-
if @complexity.folder_based?
|
|
31
|
-
result = @complexity.calculate(folder:)
|
|
32
|
-
files[:explicitly_excluded].each { |file| result.delete(file) }
|
|
33
|
-
result
|
|
34
|
-
else
|
|
35
|
-
@complexity.calculate(files: files[:included])
|
|
36
|
-
end
|
|
28
|
+
def calculate_complexity(files)
|
|
29
|
+
@complexity_results = @complexity.calculate(files: files[:included])
|
|
37
30
|
end
|
|
38
31
|
|
|
39
32
|
def schedule_churn_calculation(folder, files, since)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
33
|
+
queue = Thread::Queue.new(files)
|
|
34
|
+
queue.close
|
|
35
|
+
@threads = CONCURRENCY.times.map { start_churn_worker(queue, folder, since) }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def start_churn_worker(queue, folder, since)
|
|
39
|
+
thread = Thread.new { drain_churn_queue(queue, folder, since) }
|
|
40
|
+
thread.report_on_exception = false
|
|
41
|
+
thread
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def drain_churn_queue(queue, folder, since)
|
|
45
|
+
while (file = queue.pop)
|
|
46
|
+
@churn_results[file] = @churn.calculate(folder:, file:, since:)
|
|
50
47
|
end
|
|
51
48
|
end
|
|
52
49
|
|
|
53
50
|
def await_results
|
|
54
51
|
@threads&.each(&:join)
|
|
55
52
|
rescue StandardError => e
|
|
56
|
-
raise Error, "Failed to
|
|
53
|
+
raise Error, "Failed to calculate churn: #{e.message}"
|
|
57
54
|
end
|
|
58
55
|
|
|
59
56
|
def combine_results
|
|
60
57
|
result = {}
|
|
61
|
-
result[:values_by_file] = @complexity_results.
|
|
58
|
+
result[:values_by_file] = @complexity_results.to_h do |file, complexity|
|
|
62
59
|
# File with complexity score might not have churned in queried period,
|
|
63
60
|
# set zero churn on miss
|
|
64
|
-
|
|
61
|
+
[file, [@churn_results[file] || 0, complexity]]
|
|
65
62
|
end
|
|
66
63
|
result[:git_period] = @git_period
|
|
67
64
|
result
|
|
@@ -3,42 +3,56 @@
|
|
|
3
3
|
module ChurnVsComplexity
|
|
4
4
|
module Delta
|
|
5
5
|
class Checker
|
|
6
|
-
def initialize(
|
|
6
|
+
def initialize(scope:, commit:, serializer:, factory:)
|
|
7
|
+
@scope = scope
|
|
8
|
+
@commit = commit
|
|
7
9
|
@serializer = serializer
|
|
8
|
-
@excluded = excluded
|
|
9
10
|
@factory = factory
|
|
10
|
-
@commit = commit
|
|
11
|
-
@language = language
|
|
12
|
-
@data_isolation_id = data_isolation_id
|
|
13
11
|
end
|
|
14
12
|
|
|
15
|
-
def check(folder:)
|
|
13
|
+
def check(folder:, data_isolation_id: 0)
|
|
16
14
|
raise Error, 'Invalid commit' unless valid_commit?(folder:)
|
|
17
15
|
|
|
18
|
-
worktree =
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
.enhance(worktree_folder: worktree.folder, language: @language, excluded: @excluded, commit: @commit)
|
|
25
|
-
result[:changes] = changes
|
|
16
|
+
worktree = prepare_worktree(folder:, data_isolation_id:)
|
|
17
|
+
begin
|
|
18
|
+
worktree.checkout(@commit)
|
|
19
|
+
analyze_commit(folder:, worktree:)
|
|
20
|
+
ensure
|
|
21
|
+
cleanup_worktree(worktree)
|
|
26
22
|
end
|
|
27
|
-
|
|
28
|
-
@serializer.serialize(result)
|
|
29
23
|
end
|
|
30
24
|
|
|
31
25
|
private
|
|
32
26
|
|
|
33
|
-
def
|
|
34
|
-
worktree = @factory.worktree(
|
|
35
|
-
|
|
27
|
+
def prepare_worktree(folder:, data_isolation_id:)
|
|
28
|
+
worktree = @factory.worktree(
|
|
29
|
+
root_folder: folder,
|
|
30
|
+
git_strategy: @factory.git_strategy(folder:),
|
|
31
|
+
data_isolation_id: "delta_#{Process.pid}_#{data_isolation_id}",
|
|
32
|
+
)
|
|
36
33
|
worktree.prepare
|
|
37
|
-
worktree.checkout(@commit)
|
|
38
|
-
|
|
39
34
|
worktree
|
|
40
35
|
end
|
|
41
36
|
|
|
37
|
+
def analyze_commit(folder:, worktree:)
|
|
38
|
+
changes = @factory.git_strategy(folder: worktree.folder).changes(commit: @commit)
|
|
39
|
+
result = commit_summary(folder:)
|
|
40
|
+
annotate_changes(result:, changes:, worktree_folder: worktree.folder) unless changes.empty?
|
|
41
|
+
@serializer.serialize(result)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def annotate_changes(result:, changes:, worktree_folder:)
|
|
45
|
+
ComplexityAnnotator.new(factory: @factory, changes:)
|
|
46
|
+
.enhance(worktree_folder:, language: @scope.language, excluded: @scope.excluded)
|
|
47
|
+
result[:changes] = changes
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def cleanup_worktree(worktree)
|
|
51
|
+
worktree.remove
|
|
52
|
+
rescue StandardError
|
|
53
|
+
FileUtils.rm_rf(worktree.folder) if worktree&.folder
|
|
54
|
+
end
|
|
55
|
+
|
|
42
56
|
def commit_summary(folder:)
|
|
43
57
|
CommitHydrator.new(
|
|
44
58
|
git_strategy: @factory.git_strategy(folder:),
|
|
@@ -11,7 +11,7 @@ module ChurnVsComplexity
|
|
|
11
11
|
def hydrate(commit_sha)
|
|
12
12
|
commit = @git_strategy.object(commit_sha)
|
|
13
13
|
summary = { commit: commit.sha }
|
|
14
|
-
if @serializer.respond_to?(:
|
|
14
|
+
if @serializer.respond_to?(:commit_summary?) && @serializer.commit_summary?
|
|
15
15
|
parent, next_commit = @git_strategy.surrounding(commit:)
|
|
16
16
|
summary.merge!(parent:, next_commit:)
|
|
17
17
|
end
|
|
@@ -8,19 +8,37 @@ module ChurnVsComplexity
|
|
|
8
8
|
@changes = changes
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
-
def enhance(worktree_folder:, language:, excluded
|
|
11
|
+
def enhance(worktree_folder:, language:, excluded:)
|
|
12
|
+
assign_full_paths(worktree_folder)
|
|
13
|
+
values_by_file = calculate_values_by_file(worktree_folder:, language:, excluded:)
|
|
14
|
+
filter_by_language(language)
|
|
15
|
+
assign_complexities(values_by_file)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
private
|
|
19
|
+
|
|
20
|
+
def assign_full_paths(worktree_folder)
|
|
12
21
|
@changes.each do |change|
|
|
13
22
|
change[:full_path] = File.join(worktree_folder, change[:path])
|
|
14
23
|
end
|
|
24
|
+
end
|
|
15
25
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
engine = @factory.engine(
|
|
26
|
+
def calculate_values_by_file(worktree_folder:, language:, excluded:)
|
|
27
|
+
files = existing_file_paths
|
|
28
|
+
engine = @factory.engine(language:, excluded:, files:)
|
|
29
|
+
engine.check(folder: worktree_folder)[:values_by_file]
|
|
30
|
+
end
|
|
19
31
|
|
|
20
|
-
|
|
32
|
+
def existing_file_paths
|
|
33
|
+
@changes.reject { |change| change[:type] == :deleted }.map { |change| change[:full_path] }
|
|
34
|
+
end
|
|
21
35
|
|
|
36
|
+
def filter_by_language(language)
|
|
22
37
|
valid_extensions = FileSelector.extensions(language)
|
|
23
38
|
@changes.select! { |change| valid_extensions.any? { |ext| change[:path].end_with?(ext) } }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def assign_complexities(values_by_file)
|
|
24
42
|
@changes.each do |annotated_file|
|
|
25
43
|
annotated_file[:complexity] = values_by_file.dig(annotated_file[:full_path], 1)
|
|
26
44
|
end
|
|
@@ -3,32 +3,30 @@
|
|
|
3
3
|
module ChurnVsComplexity
|
|
4
4
|
module Delta
|
|
5
5
|
class Config
|
|
6
|
-
def
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
)
|
|
15
|
-
@
|
|
6
|
+
def self.from_options(options)
|
|
7
|
+
new(
|
|
8
|
+
scope: AnalysisScope.from_options(options),
|
|
9
|
+
serializer: options[:serializer],
|
|
10
|
+
commits: options[:commits],
|
|
11
|
+
)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def initialize(scope:, serializer:, commits:, factory: Factory)
|
|
15
|
+
@scope = scope
|
|
16
16
|
@serializer = serializer
|
|
17
|
-
@excluded = excluded
|
|
18
17
|
@commits = commits
|
|
19
18
|
@factory = factory
|
|
20
19
|
end
|
|
21
20
|
|
|
22
21
|
def validate!
|
|
23
22
|
validate_commits!
|
|
24
|
-
LanguageValidator.validate!(@language)
|
|
23
|
+
LanguageValidator.validate!(@scope.language)
|
|
25
24
|
SerializerValidator.validate!(serializer: @serializer)
|
|
26
|
-
@factory.complexity_validator.validate!(@language)
|
|
25
|
+
@factory.complexity_validator.validate!(@scope.language)
|
|
27
26
|
end
|
|
28
27
|
|
|
29
28
|
def checker
|
|
30
|
-
MultiChecker.new(
|
|
31
|
-
language: @language,)
|
|
29
|
+
MultiChecker.new(scope: @scope, serializer:, factory: @factory, commits: @commits)
|
|
32
30
|
end
|
|
33
31
|
|
|
34
32
|
private
|
|
@@ -41,7 +39,8 @@ module ChurnVsComplexity
|
|
|
41
39
|
return if commit == 'HEAD' || commit.match?(/\A[0-9a-f]{40}\z/i) || commit.match?(/\A[0-9a-f]{7,12}\z/i)
|
|
42
40
|
|
|
43
41
|
raise ValidationError,
|
|
44
|
-
"Invalid commit: #{commit}.
|
|
42
|
+
"Invalid commit: #{commit}. " \
|
|
43
|
+
'It must be a valid 40-character SHA-1 hash or a 7-12 character shortened form.'
|
|
45
44
|
end
|
|
46
45
|
|
|
47
46
|
def serializer = Serializer.resolve(@serializer)
|
|
@@ -14,8 +14,8 @@ module ChurnVsComplexity
|
|
|
14
14
|
)
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
def self.engine(
|
|
18
|
-
Delta.engine(
|
|
17
|
+
def self.engine(language:, excluded:, files:)
|
|
18
|
+
Delta.engine(language:, excluded:, files:)
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
end
|