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.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +95 -0
  3. data/README.md +46 -23
  4. data/bin/churn_vs_complexity +6 -1
  5. data/lib/churn_vs_complexity/analysis_scope.rb +20 -0
  6. data/lib/churn_vs_complexity/churn.rb +41 -8
  7. data/lib/churn_vs_complexity/cli/main.rb +58 -19
  8. data/lib/churn_vs_complexity/cli/parser.rb +85 -121
  9. data/lib/churn_vs_complexity/cli.rb +2 -3
  10. data/lib/churn_vs_complexity/complexity/flog_calculator.rb +0 -3
  11. data/lib/churn_vs_complexity/complexity/lizard_calculator.rb +64 -0
  12. data/lib/churn_vs_complexity/complexity.rb +1 -7
  13. data/lib/churn_vs_complexity/complexity_validator.rb +1 -16
  14. data/lib/churn_vs_complexity/concurrent_calculator.rb +20 -23
  15. data/lib/churn_vs_complexity/delta/checker.rb +35 -21
  16. data/lib/churn_vs_complexity/delta/commit_hydrator.rb +1 -1
  17. data/lib/churn_vs_complexity/delta/complexity_annotator.rb +23 -5
  18. data/lib/churn_vs_complexity/delta/config.rb +15 -16
  19. data/lib/churn_vs_complexity/delta/factory.rb +2 -2
  20. data/lib/churn_vs_complexity/delta/multi_checker.rb +24 -25
  21. data/lib/churn_vs_complexity/delta/serializer.rb +7 -4
  22. data/lib/churn_vs_complexity/delta.rb +11 -49
  23. data/lib/churn_vs_complexity/diff/checker.rb +10 -3
  24. data/lib/churn_vs_complexity/diff/config.rb +29 -20
  25. data/lib/churn_vs_complexity/file_selector.rb +63 -114
  26. data/lib/churn_vs_complexity/focus/config.rb +42 -32
  27. data/lib/churn_vs_complexity/focus/serializer.rb +17 -4
  28. data/lib/churn_vs_complexity/gate/config.rb +35 -25
  29. data/lib/churn_vs_complexity/gate/serializer.rb +9 -2
  30. data/lib/churn_vs_complexity/git_strategy.rb +29 -10
  31. data/lib/churn_vs_complexity/hotspots/config.rb +29 -25
  32. data/lib/churn_vs_complexity/language_registry.rb +108 -0
  33. data/lib/churn_vs_complexity/language_validator.rb +1 -3
  34. data/lib/churn_vs_complexity/normal/config.rb +31 -100
  35. data/lib/churn_vs_complexity/normal/serializer/graph.rb +3 -2
  36. data/lib/churn_vs_complexity/normal/serializer/summary.rb +18 -14
  37. data/lib/churn_vs_complexity/normal/serializer/summary_hash.rb +36 -21
  38. data/lib/churn_vs_complexity/normal/serializer.rb +2 -1
  39. data/lib/churn_vs_complexity/normal.rb +17 -13
  40. data/lib/churn_vs_complexity/risk_classifier.rb +3 -19
  41. data/lib/churn_vs_complexity/timetravel/config.rb +34 -47
  42. data/lib/churn_vs_complexity/timetravel/schedule.rb +15 -0
  43. data/lib/churn_vs_complexity/timetravel/serializer/stats_calculator.rb +36 -32
  44. data/lib/churn_vs_complexity/timetravel/serializer.rb +16 -24
  45. data/lib/churn_vs_complexity/timetravel/traveller.rb +46 -15
  46. data/lib/churn_vs_complexity/timetravel.rb +36 -14
  47. data/lib/churn_vs_complexity/triage/checker.rb +24 -28
  48. data/lib/churn_vs_complexity/triage/collaborator_set.rb +11 -0
  49. data/lib/churn_vs_complexity/triage/config.rb +29 -26
  50. data/lib/churn_vs_complexity/triage.rb +1 -0
  51. data/lib/churn_vs_complexity/validator_set.rb +9 -0
  52. data/lib/churn_vs_complexity/version.rb +1 -1
  53. data/lib/churn_vs_complexity.rb +3 -0
  54. metadata +18 -59
  55. data/.devcontainer/devcontainer.json +0 -26
  56. data/.rubocop.yml +0 -25
  57. data/CLAUDE.md +0 -60
  58. data/Dockerfile +0 -34
  59. data/Dockerfile.dev +0 -27
  60. data/Rakefile +0 -6
  61. data/TODO +0 -11
  62. data/lib/churn_vs_complexity/complexity/eslint_calculator.rb +0 -40
  63. data/lib/churn_vs_complexity/complexity/go_calculator.rb +0 -48
  64. data/lib/churn_vs_complexity/complexity/kotlin_calculator.rb +0 -54
  65. data/lib/churn_vs_complexity/complexity/pmd/files_calculator.rb +0 -26
  66. data/lib/churn_vs_complexity/complexity/pmd/folder_calculator.rb +0 -20
  67. data/lib/churn_vs_complexity/complexity/pmd.rb +0 -54
  68. data/lib/churn_vs_complexity/complexity/python_calculator.rb +0 -50
  69. data/lib/churn_vs_complexity/complexity/rust_calculator.rb +0 -54
  70. data/lib/churn_vs_complexity/complexity/swift_calculator.rb +0 -54
  71. data/package-lock.json +0 -6
  72. data/tmp/eslint-support/complexity-calculator.js +0 -51
  73. data/tmp/eslint-support/package.json +0 -11
  74. data/tmp/pmd-support/ruleset.xml +0 -21
  75. data/tmp/test-support/delta/ruby-summary.txt +0 -55
  76. data/tmp/test-support/delta/ruby.csv +0 -12
  77. data/tmp/test-support/go/main.go +0 -11
  78. data/tmp/test-support/go/utils.go +0 -13
  79. data/tmp/test-support/java/small-example/src/main/java/org/example/Main.java +0 -10
  80. data/tmp/test-support/java/small-example/src/main/java/org/example/spice/Checker.java +0 -28
  81. data/tmp/test-support/javascript/complex.js +0 -43
  82. data/tmp/test-support/javascript/moderate.js +0 -12
  83. data/tmp/test-support/javascript/simple.js +0 -5
  84. data/tmp/test-support/javascript/typescript-example.ts +0 -26
  85. data/tmp/test-support/kotlin/Main.kt +0 -7
  86. data/tmp/test-support/kotlin/Utils.kt +0 -11
  87. data/tmp/test-support/python/example.py +0 -6
  88. data/tmp/test-support/python/utils.py +0 -9
  89. data/tmp/test-support/rust/main.rs +0 -7
  90. data/tmp/test-support/rust/utils.rs +0 -20
  91. data/tmp/test-support/swift/main.swift +0 -7
  92. data/tmp/test-support/swift/utils.swift +0 -24
  93. data/tmp/test-support/txt/abc.txt +0 -0
  94. data/tmp/test-support/txt/d.txt +0 -0
  95. data/tmp/test-support/txt/ef.txt +0 -0
  96. data/tmp/test-support/txt/ghij.txt +0 -0
  97. data/tmp/test-support/txt/klm.txt +0 -0
  98. data/tmp/test-support/txt/nopq.txt +0 -0
  99. data/tmp/test-support/txt/r.txt +0 -0
  100. data/tmp/test-support/txt/st.txt +0 -0
  101. data/tmp/test-support/txt/uvx.txt +0 -0
  102. data/tmp/test-support/txt/yz.txt +0 -0
  103. 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
- def self.create
9
- options = { excluded: [] }
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
- opts.separator ''
14
- opts.separator 'Languages:'
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
- opts.on('--java', 'Check complexity of java classes') do
17
- options[:language] = :java
18
- end
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
- opts.on('--ruby', 'Check complexity of ruby files') do
21
- options[:language] = :ruby
22
- end
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
- opts.on('--js', '--ts', '--javascript', '--typescript',
25
- 'Check complexity of javascript and typescript files',) do
26
- options[:language] = :javascript
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
- opts.on('--python', 'Check complexity of python files') do
30
- options[:language] = :python
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
- opts.on('--go', 'Check complexity of go files') do
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
- opts.on('--kotlin', 'Check complexity of kotlin files') do
38
- options[:language] = :kotlin
39
- end
40
+ FOCUS_HELP = 'Capture complexity snapshot before (start) and after (end) a coding session'
40
41
 
41
- opts.on('--rust', 'Check complexity of rust files') do
42
- options[:language] = :rust
43
- end
42
+ DIFF_HELP = 'Compare codebase health between REF and HEAD'
44
43
 
45
- opts.on('--swift', 'Check complexity of swift files') do
46
- options[:language] = :swift
47
- end
44
+ EXCLUDED_HELP = 'Exclude file paths including this string. Can be used multiple times.'
48
45
 
49
- opts.separator ''
50
- opts.separator 'Modes (mutually exclusive):'
46
+ MAX_GAMMA_HELP = 'Maximum gamma score threshold for gate mode (default: 25)'
51
47
 
52
- opts.on('--timetravel N',
53
- 'Calculate summary for all commits at intervals of N days throughout project history or from the date specified with --since',) do |value|
54
- options[:mode] = :timetravel
55
- options[:jump_days] = value.to_i
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
- opts.on('--triage',
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
- opts.on('--hotspots', 'Generate ranked list of files by risk') do
64
- options[:mode] = :hotspots
65
- end
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
- opts.on('--focus start|end',
73
- 'Capture complexity snapshot before (start) and after (end) a coding session',) do |value|
74
- options[:mode] = :focus
75
- options[:subcommand] = value.to_sym
76
- end
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
- opts.on('--diff REF', 'Compare codebase health between REF and HEAD') do |value|
79
- options[:mode] = :diff
80
- options[:reference] = value
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] ||= []) << value
84
+ (options[:commits] ||= []) << v
87
85
  end
86
+ end
88
87
 
89
- opts.separator ''
90
- opts.separator 'Output formats:'
91
-
92
- opts.on('--csv', 'Format output as CSV') do
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
- opts.on('--graph', 'Format output as HTML page with Churn vs Complexity graph') do
97
- options[:serializer] = :graph
98
- end
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
- opts.on('--summary', 'Output summary statistics (mean and median) for churn and complexity') do
101
- options[:serializer] = :summary
102
- end
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
- opts.on('--json', 'Format output as JSON') do
105
- options[:serializer] = :json
106
- end
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
- opts.on('--markdown', 'Format output as Markdown') do
109
- options[:serializer] = :markdown
110
- end
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 'Modifiers:'
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
- # First argument that is not an option is the folder
13
- folder = ARGV.first
12
+ targets = ARGV.dup
14
13
 
15
- result = Main.run!(options, folder)
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
@@ -6,10 +6,7 @@ module ChurnVsComplexity
6
6
  module Complexity
7
7
  module FlogCalculator
8
8
  class << self
9
- def folder_based? = false
10
-
11
9
  def calculate(files:)
12
- #  TODO: Run this concurrently
13
10
  files.to_h do |file|
14
11
  flog = Flog.new
15
12
  flog.flog(file)
@@ -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/eslint_calculator'
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
- case language
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(folder, files)
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(folder, files)
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
- f = files.dup
41
- @threads = CONCURRENCY.times.map do
42
- t = Thread.new do
43
- until f.empty?
44
- next_file = f.pop
45
- @churn_results[next_file] = @churn.calculate(folder:, file: next_file, since:)
46
- end
47
- end
48
- t.report_on_exception = false
49
- t
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 caculate churn: #{e.message}"
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.keys.each_with_object({}) do |file, acc|
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
- acc[file] = [@churn_results[file] || 0, @complexity_results[file]]
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(serializer:, factory:, commit:, language:, excluded:, data_isolation_id: 0)
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 = setup_worktree(folder:)
19
-
20
- changes = @factory.git_strategy(folder: worktree.folder).changes(commit: @commit)
21
- result = commit_summary(folder:)
22
- unless changes.empty?
23
- ComplexityAnnotator.new(factory: @factory, changes:)
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 setup_worktree(folder:)
34
- worktree = @factory.worktree(root_folder: folder, git_strategy: @factory.git_strategy(folder:),
35
- data_isolation_id: @data_isolation_id,)
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?(:has_commit_summary?) && @serializer.has_commit_summary?
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:, commit:)
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
- files = @changes.reject { |change| change[:type] == :deleted }.map { |change| change[:full_path] }
17
-
18
- engine = @factory.engine(cache_components: [worktree_folder, commit], language:, excluded:, files:)
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
- values_by_file = engine.check(folder: worktree_folder)[:values_by_file]
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 initialize(
7
- language:,
8
- serializer:,
9
- commits:,
10
- excluded: [],
11
- complexity_validator: ComplexityValidator,
12
- factory: Factory,
13
- **_options
14
- )
15
- @language = language
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(serializer:, excluded: @excluded, factory: @factory, commits: @commits,
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}. It must be a valid 40-character SHA-1 hash or a 7-12 character shortened form."
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(cache_components:, language:, excluded:, files:)
18
- Delta.engine(cache_components:, language:, excluded:, files:)
17
+ def self.engine(language:, excluded:, files:)
18
+ Delta.engine(language:, excluded:, files:)
19
19
  end
20
20
  end
21
21
  end