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,7 +5,6 @@ require_relative 'normal/serializer'
5
5
 
6
6
  module ChurnVsComplexity
7
7
  module Normal
8
- # TODO: unit test
9
8
  module SerializerValidator
10
9
  def self.validate!(serializer:)
11
10
  raise ValidationError, "Unsupported serializer: #{serializer}" \
@@ -13,7 +12,6 @@ module ChurnVsComplexity
13
12
  end
14
13
  end
15
14
 
16
- # TODO: unit test
17
15
  module RelativePeriodValidator
18
16
  def self.validate!(relative_period:)
19
17
  return if relative_period.nil? || %i[month quarter year].include?(relative_period)
@@ -24,22 +22,28 @@ module ChurnVsComplexity
24
22
 
25
23
  module SinceValidator
26
24
  def self.validate!(since:, relative_period:)
27
- # since can be nil, a date string or a keyword (:month, :quarter, :year)
28
25
  return if since.nil?
29
26
 
30
- unless since.nil? || relative_period.nil?
31
- raise ValidationError,
32
- '--since and relative period (--month, --quarter, --year) cannot be used together in normal mode'
33
- end
34
-
27
+ validate_exclusive!(relative_period)
35
28
  raise ValidationError, "Invalid since value #{since}" unless since.is_a?(String)
36
29
 
37
- begin
38
- Date.strptime(since, '%Y-%m-%d')
39
- rescue Date::Error
40
- raise ValidationError, "Invalid date #{since}, please use correct format, YYYY-MM-DD"
41
- end
30
+ validate_date_format!(since)
31
+ end
32
+
33
+ def self.validate_exclusive!(relative_period)
34
+ return if relative_period.nil?
35
+
36
+ raise ValidationError,
37
+ '--since and relative period (--month, --quarter, --year) cannot be used together'
42
38
  end
39
+
40
+ def self.validate_date_format!(since)
41
+ Date.strptime(since, '%Y-%m-%d')
42
+ rescue Date::Error
43
+ raise ValidationError, "Invalid date #{since}, please use correct format, YYYY-MM-DD"
44
+ end
45
+
46
+ private_class_method :validate_exclusive!, :validate_date_format!
43
47
  end
44
48
  end
45
49
  end
@@ -4,31 +4,15 @@ module ChurnVsComplexity
4
4
  # Classifies files into low/medium/high risk based on gamma score
5
5
  # (harmonic mean of churn and complexity).
6
6
  #
7
- # IMPORTANT: Complexity scores are NOT comparable across languages.
8
- # Each language uses a different tool with a different numeric scale.
9
- # All tools sum per-function scores across the file, but the magnitude differs:
10
- #
11
- # Language Tool Metric Trivial Complex (~100 LOC) Real-world range
12
- # Ruby Flog Code pain (weighted) 0 ~286 0-88
13
- # JavaScript/TS ESLint Cyclomatic complexity 3 ~62 10-50
14
- # Python Radon Cyclomatic complexity 1 ~44 5-50
15
- # Java PMD Cyclomatic complexity 1 ~39 1-40
16
- # Go gocognit Cognitive complexity 0 ~87 0-50
17
- # Kotlin lizard Cyclomatic complexity 1 ~40 1-40
18
- # Rust lizard Cyclomatic complexity 1 ~40 1-40
19
- # Swift lizard Cyclomatic complexity 1 ~40 1-40
20
- #
21
- # The DEFAULT_LOW and DEFAULT_HIGH thresholds below are rough midpoints
22
- # suitable for Java/Python/JS. They are too aggressive for Ruby (Flog
23
- # scores 3-7x higher) and Go (cognitive complexity penalises nesting).
24
- # Use the constructor to pass language-appropriate thresholds.
7
+ # Default thresholds (10 / 25) fit cyclomatic complexity from lizard (used for
8
+ # all non-Ruby languages). Ruby uses Flog whose weighted pain scores run higher,
9
+ # so it overrides the defaults.
25
10
  class RiskClassifier
26
11
  DEFAULT_LOW = 10
27
12
  DEFAULT_HIGH = 25
28
13
 
29
14
  LANGUAGE_DEFAULTS = {
30
15
  ruby: { low: 30, high: 70 },
31
- go: { low: 15, high: 40 },
32
16
  }.freeze
33
17
 
34
18
  RECOMMENDATIONS = {
@@ -3,71 +3,58 @@
3
3
  module ChurnVsComplexity
4
4
  module Timetravel
5
5
  class Config
6
- def initialize(
7
- language:,
8
- serializer:,
9
- jump_days:, excluded: [],
10
- since: nil,
11
- relative_period: nil,
12
- complexity_validator: ComplexityValidator,
13
- since_validator: SinceValidator,
14
- factory: Factory,
15
- **options
16
- )
17
- @language = language
6
+ def self.from_options(options)
7
+ new(
8
+ scope: AnalysisScope.from_options(options),
9
+ serializer: options[:serializer],
10
+ schedule: Schedule.from_options(options),
11
+ )
12
+ end
13
+
14
+ def initialize(scope:, serializer:, schedule:, validators: ValidatorSet.new(since: SinceValidator))
15
+ @scope = scope
18
16
  @serializer = serializer
19
- @excluded = excluded
20
- @since = since
21
- @relative_period = relative_period
22
- @jump_days = jump_days
23
- @complexity_validator = complexity_validator
24
- @since_validator = since_validator
25
- @factory = factory
26
- @options = options
17
+ @schedule = schedule
18
+ @validators = validators
27
19
  end
28
20
 
29
21
  def validate!
30
- raise ValidationError, 'Must specify jump days!' if @jump_days.nil?
31
-
32
- LanguageValidator.validate!(@language)
22
+ raise ValidationError, 'Must specify jump days!' if @schedule.jump_days.nil?
33
23
 
24
+ LanguageValidator.validate!(@scope.language)
34
25
  SerializerValidator.validate!(serializer: @serializer)
35
-
36
- @since_validator.validate!(since: @since)
37
- RelativePeriodValidator.validate!(relative_period: @relative_period)
38
- @complexity_validator.validate!(@language)
26
+ @validators.since.validate!(since: @scope.since)
27
+ RelativePeriodValidator.validate!(relative_period: @schedule.relative_period)
28
+ @validators.complexity.validate!(@scope.language)
39
29
  end
40
30
 
41
- def checker = traveller(git_period: GitDate.git_period(@since, Time.now.to_date))
42
-
43
- private
44
-
45
- def traveller(git_period:)
31
+ def checker
32
+ resolved = @schedule.with(git_period: GitDate.git_period(@scope.since, Time.now.to_date))
46
33
  Traveller.new(
47
- git_period:,
48
- relative_period: @relative_period,
34
+ schedule: resolved,
49
35
  engine: engine_config.checker,
50
- jump_days: @jump_days,
51
- serializer: serializer(git_period:),
52
- factory: @factory,
36
+ serializer: resolve_serializer(resolved),
37
+ factory: Factory,
53
38
  )
54
39
  end
55
40
 
56
- def serializer(git_period:)
57
- Serializer.resolve(serializer: @serializer, git_period:, relative_period: @relative_period,
58
- jump_days: @jump_days,)
41
+ private
42
+
43
+ def resolve_serializer(schedule)
44
+ Serializer.resolve(
45
+ serializer: @serializer,
46
+ git_period: schedule.git_period,
47
+ relative_period: schedule.relative_period,
48
+ jump_days: schedule.jump_days,
49
+ )
59
50
  end
60
51
 
61
52
  def engine_config
62
53
  Normal::Config.new(
63
- language: @language,
54
+ scope: @scope.with(since: nil),
64
55
  serializer: :pass_through,
65
- excluded: @excluded,
66
- since: nil, # since has a different meaning in timetravel mode
67
- relative_period: @relative_period,
68
- complexity_validator: @complexity_validator,
69
- since_validator: @since_validator,
70
- **@options,
56
+ relative_period: @schedule.relative_period,
57
+ validators: ValidatorSet.new(complexity: @validators.complexity),
71
58
  )
72
59
  end
73
60
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChurnVsComplexity
4
+ module Timetravel
5
+ Schedule = Data.define(:git_period, :relative_period, :jump_days) do
6
+ def initialize(jump_days:, relative_period: nil, git_period: nil)
7
+ super
8
+ end
9
+
10
+ def self.from_options(options)
11
+ new(jump_days: options[:jump_days], relative_period: options[:relative_period])
12
+ end
13
+ end
14
+ end
15
+ end
@@ -4,46 +4,50 @@ module ChurnVsComplexity
4
4
  module Timetravel
5
5
  module Serializer
6
6
  class StatsCalculator
7
- # ['some_sha', { 'end_date' => '2024-01-01', 'values' => [[1, 2], [3, 4]] }]
8
7
  def summaries(result)
9
- observations = result.sort_by do |_sha, summary|
10
- summary['end_date']
11
- end.map { |entry| entry[1] }
12
-
8
+ observations = ordered_observations(result)
13
9
  quality_calculator = QualityCalculator.new(**extrema(observations))
14
- observations.map do |o|
15
- end_date = o['end_date']
16
- scores = o['values'].map do |(churn, complexity)|
17
- alpha = quality_calculator.alpha_score(churn, complexity)
18
- beta = quality_calculator.beta_score(churn, complexity)
19
- [churn, complexity, alpha, beta]
20
- end
21
- {
22
- 'end_date' => end_date,
23
- 'mean_churn' => mean(scores.map { |s| s[0] }),
24
- 'median_churn' => median(scores.map { |s| s[0] }),
25
- 'mean_complexity' => mean(scores.map { |s| s[1] }),
26
- 'median_complexity' => median(scores.map { |s| s[1] }),
27
- 'mean_alpha_score' => mean(scores.map { |s| s[2] }),
28
- 'median_alpha_score' => median(scores.map { |s| s[2] }),
29
- 'mean_beta_score' => mean(scores.map { |s| s[3] }),
30
- 'median_beta_score' => median(scores.map { |s| s[3] }),
31
- }
32
- end
10
+ observations.map { |observation| summarize_observation(observation, quality_calculator) }
33
11
  end
34
12
 
35
13
  private
36
14
 
37
- def extrema(observations)
38
- churn_series = observations.flat_map { |o| o['values'] }.map { |(churn, _)| churn }
39
- max_churn = churn_series.max
40
- min_churn = churn_series.min
15
+ def ordered_observations(result)
16
+ sorted = result.sort_by { |_sha, summary| summary['end_date'] }
17
+ sorted.map { |entry| entry[1] }
18
+ end
41
19
 
42
- complexity_series = observations.flat_map { |o| o['values'] }.map { |(_, complexity)| complexity }
43
- max_complexity = complexity_series.max
44
- min_complexity = complexity_series.min
20
+ def summarize_observation(observation, quality_calculator)
21
+ scores = score_values(observation['values'], quality_calculator)
22
+ {
23
+ 'end_date' => observation['end_date'],
24
+ **series_stats('churn', scores.map { |s| s[0] }),
25
+ **series_stats('complexity', scores.map { |s| s[1] }),
26
+ **series_stats('alpha_score', scores.map { |s| s[2] }),
27
+ **series_stats('beta_score', scores.map { |s| s[3] }),
28
+ }
29
+ end
45
30
 
46
- { max_churn:, min_churn:, max_complexity:, min_complexity: }
31
+ def score_values(values, quality_calculator)
32
+ values.map do |churn, complexity|
33
+ alpha = quality_calculator.alpha_score(churn, complexity)
34
+ beta = quality_calculator.beta_score(churn, complexity)
35
+ [churn, complexity, alpha, beta]
36
+ end
37
+ end
38
+
39
+ def series_stats(name, series)
40
+ { "mean_#{name}" => mean(series), "median_#{name}" => median(series) }
41
+ end
42
+
43
+ def extrema(observations)
44
+ values = observations.flat_map { |o| o['values'] }
45
+ churns = values.map(&:first)
46
+ complexities = values.map(&:last)
47
+ {
48
+ max_churn: churns.max, min_churn: churns.min,
49
+ max_complexity: complexities.max, min_complexity: complexities.min,
50
+ }
47
51
  end
48
52
 
49
53
  def mean(series)
@@ -20,28 +20,21 @@ module ChurnVsComplexity
20
20
  end
21
21
 
22
22
  module CSV
23
- def self.serialize(result)
24
- summaries = Serializer.summaries(result)
23
+ COLUMNS = {
24
+ 'end_date' => 'Date',
25
+ 'mean_churn' => 'Mean Churn',
26
+ 'median_churn' => 'Median Churn',
27
+ 'mean_complexity' => 'Mean Complexity',
28
+ 'median_complexity' => 'Median Complexity',
29
+ 'mean_alpha_score' => 'Mean Alpha Score',
30
+ 'median_alpha_score' => 'Median Alpha Score',
31
+ 'mean_beta_score' => 'Mean Beta Score',
32
+ 'median_beta_score' => 'Median Beta Score',
33
+ }.freeze
25
34
 
26
- # 2. Add title row to front of summaries
27
- summaries.unshift(
28
- {
29
- 'end_date' => 'Date',
30
- 'mean_churn' => 'Mean Churn',
31
- 'median_churn' => 'Median Churn',
32
- 'mean_complexity' => 'Mean Complexity',
33
- 'median_complexity' => 'Median Complexity',
34
- 'mean_alpha_score' => 'Mean Alpha Score',
35
- 'median_alpha_score' => 'Median Alpha Score',
36
- 'mean_beta_score' => 'Mean Beta Score',
37
- 'median_beta_score' => 'Median Beta Score',
38
- },
39
- )
40
-
41
- # 3. convert to csv
42
- summaries.map do |summary|
43
- "#{summary['end_date']},#{summary['mean_churn']},#{summary['median_churn']},#{summary['mean_complexity']},#{summary['median_complexity']},#{summary['mean_alpha_score']},#{summary['median_alpha_score']},#{summary['mean_beta_score']},#{summary['median_beta_score']}"
44
- end.join("\n")
35
+ def self.serialize(result)
36
+ rows = [COLUMNS, *Serializer.summaries(result)]
37
+ rows.map { |row| row.values_at(*COLUMNS.keys).join(',') }.join("\n")
45
38
  end
46
39
  end
47
40
 
@@ -62,9 +55,8 @@ module ChurnVsComplexity
62
55
  def serialize(result)
63
56
  summaries = Serializer.summaries(result)
64
57
 
65
- data = summaries.map do |summary|
66
- JSON.dump(summary)
67
- end.join(",\n") + "\n"
58
+ entries = summaries.map { |summary| JSON.dump(summary) }.join(",\n")
59
+ data = "#{entries}\n"
68
60
 
69
61
  @template.gsub("// INSERT DATA\n", data)
70
62
  .gsub('INSERT TITLE', title)
@@ -1,40 +1,71 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ChurnVsComplexity
4
- # TODO: unit test and integration test
5
4
  module Timetravel
6
5
  class Traveller
7
- def initialize(git_period:, relative_period:, engine:, serializer:, jump_days:, factory:)
8
- @relative_period = relative_period
6
+ def initialize(schedule:, engine:, serializer:, factory:)
7
+ @schedule = schedule
9
8
  @engine = engine
10
- @jump_days = jump_days
11
9
  @serializer = serializer
12
- @git_period = git_period
13
10
  @factory = factory
14
11
  end
15
12
 
16
13
  def check(folder:)
17
14
  git_strategy = @factory.git_strategy(folder:)
18
- commits = git_strategy.resolve_commits_with_interval(git_period: @git_period, jump_days: @jump_days)
19
-
15
+ commits = resolve_commits(git_strategy)
20
16
  chunked = make_chunks(commits)
21
- work_on(chunked:, folder:, git_strategy:)
22
- combined = chunked.map { |c_and_p| read_result(c_and_p[:pipe]) }.reduce({}, :merge)
23
-
17
+ pids = work_on(chunked:, folder:, git_strategy:)
18
+ combined = combine_results(chunked)
24
19
  @serializer.serialize(combined)
20
+ ensure
21
+ wait_for(pids)
25
22
  end
26
23
 
27
24
  private
28
25
 
26
+ def resolve_commits(git_strategy)
27
+ git_strategy.resolve_commits_with_interval(
28
+ git_period: @schedule.git_period,
29
+ jump_days: @schedule.jump_days,
30
+ )
31
+ end
32
+
33
+ def combine_results(chunked)
34
+ chunked.map { |c_and_p| read_result(c_and_p[:pipe]) }.reduce({}, :merge)
35
+ end
36
+
29
37
  def work_on(chunked:, folder:, git_strategy:)
30
- chunked.map.with_index do |c_and_p, i|
31
- worktree = @factory.worktree(root_folder: folder, git_strategy:, number: i)
32
- worktree.prepare
33
- schedule_work(worktree:, **c_and_p)
38
+ pids = []
39
+ chunked.each_with_index do |c_and_p, number|
40
+ pids << start_worker(c_and_p:, folder:, git_strategy:, number:)
34
41
  end
42
+ pids
43
+ rescue StandardError
44
+ wait_for(pids)
45
+ raise
46
+ end
47
+
48
+ def start_worker(c_and_p:, folder:, git_strategy:, number:)
49
+ worktree = @factory.worktree(root_folder: folder, git_strategy:, number:)
50
+ worktree.prepare
51
+ schedule_work(worktree:, **c_and_p)
52
+ end
53
+
54
+ def wait_for(pids)
55
+ return unless pids
56
+
57
+ pids.grep(Integer).each { |pid| wait_pid(pid) }
58
+ end
59
+
60
+ def wait_pid(pid)
61
+ Process.waitpid(pid)
62
+ rescue Errno::ECHILD
63
+ nil
35
64
  end
36
65
 
37
66
  def make_chunks(commits)
67
+ return [] if commits.empty?
68
+
38
69
  chunk_size = (commits.size / 3.0).ceil
39
70
  commits.each_slice(chunk_size).map do |chunk|
40
71
  { chunk:, pipe: @factory.pipe }
@@ -47,7 +78,7 @@ module ChurnVsComplexity
47
78
  rescue StandardError
48
79
  {}
49
80
  end
50
- pipe.each(&:close)
81
+ pipe.each { |io| io.close unless io.closed? }
51
82
  part
52
83
  end
53
84
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'timetravel/schedule'
3
4
  require_relative 'timetravel/traveller'
4
5
  require_relative 'timetravel/worktree'
5
6
  require_relative 'timetravel/config'
@@ -8,7 +9,6 @@ require_relative 'timetravel/factory'
8
9
 
9
10
  module ChurnVsComplexity
10
11
  module Timetravel
11
- # TODO: unit test
12
12
  module SerializerValidator
13
13
  def self.validate!(serializer:)
14
14
  raise ValidationError, 'Does not support --summary in --timetravel mode' \
@@ -18,11 +18,10 @@ module ChurnVsComplexity
18
18
  end
19
19
  end
20
20
 
21
- # TODO: unit test
22
21
  module RelativePeriodValidator
23
22
  def self.validate!(relative_period:)
24
23
  raise ValidationError, 'Relative period is required in timetravel mode' if relative_period.nil?
25
- return if relative_period.nil? || %i[month quarter year].include?(relative_period)
24
+ return if %i[month quarter year].include?(relative_period)
26
25
 
27
26
  raise ValidationError, "Invalid relative period #{relative_period}"
28
27
  end
@@ -30,7 +29,6 @@ module ChurnVsComplexity
30
29
 
31
30
  module SinceValidator
32
31
  def self.validate!(since:)
33
- # since can be nil, a date string or a keyword (:month, :quarter, :year)
34
32
  return if since.nil?
35
33
 
36
34
  raise ValidationError, "Invalid since value #{since}" unless since.is_a?(String)
@@ -50,17 +48,41 @@ module ChurnVsComplexity
50
48
  end
51
49
 
52
50
  def schedule(chunk:, pipe:)
53
- fork do
54
- results = chunk.to_h do |commit|
55
- sha = commit.sha
56
- @worktree.checkout(sha)
57
- result = @engine.check(folder: @worktree.folder)
58
- [sha, result]
59
- end
60
- @worktree.remove
61
- pipe[1].puts(JSON.dump(results))
62
- pipe[1].close
51
+ pid = fork do
52
+ pipe[0].close unless pipe[0].closed?
53
+ run_child(chunk:, write_io: pipe[1])
63
54
  end
55
+ pipe[1].close unless pipe[1].closed?
56
+ pid
57
+ end
58
+
59
+ private
60
+
61
+ def run_child(chunk:, write_io:)
62
+ removed = false
63
+ results = process_chunk(chunk)
64
+ payload = JSON.dump(results)
65
+ removed = true
66
+ @worktree.remove
67
+ write_io.puts(payload)
68
+ ensure
69
+ cleanup_child(write_io:, removed:)
70
+ end
71
+
72
+ def cleanup_child(write_io:, removed:)
73
+ @worktree.remove unless removed
74
+ write_io.close unless write_io.closed?
75
+ end
76
+
77
+ def process_chunk(chunk)
78
+ chunk.to_h { |commit| evaluate_commit(commit) }
79
+ end
80
+
81
+ def evaluate_commit(commit)
82
+ sha = commit.sha
83
+ @worktree.checkout(sha)
84
+ result = @engine.check(folder: @worktree.folder)
85
+ [sha, result]
64
86
  end
65
87
  end
66
88
  end
@@ -3,20 +3,19 @@
3
3
  module ChurnVsComplexity
4
4
  module Triage
5
5
  class Checker
6
- def initialize(language:, serializer:, targets:, since:, excluded:)
7
- @language = language
6
+ def initialize(scope:, serializer:, targets:, collaborators: CollaboratorSet.new)
7
+ @scope = scope
8
8
  @serializer = serializer
9
9
  @targets = targets
10
- @since = since
11
- @excluded = excluded
10
+ @collaborators = collaborators
12
11
  end
13
12
 
14
13
  def check(folder: nil)
15
14
  files, dirs = partition_targets
16
- folder = folder || dirs.first || '.'
17
- engine = build_engine(files, folder)
18
- raw_result = engine.check(folder:)
19
- @serializer.serialize(raw_result.merge(language: @language))
15
+ target_folder = resolve_folder(folder, files, dirs)
16
+ engine = build_engine(files)
17
+ raw_result = engine.check(folder: target_folder)
18
+ @serializer.serialize(raw_result.merge(language: @scope.language))
20
19
  end
21
20
 
22
21
  private
@@ -25,39 +24,36 @@ module ChurnVsComplexity
25
24
  @targets.partition { |t| File.file?(t) }
26
25
  end
27
26
 
28
- def build_engine(files, _folder)
27
+ def resolve_folder(folder, files, dirs)
28
+ return folder if folder
29
+ return dirs.first unless dirs.empty?
30
+ return File.dirname(files.first) unless files.empty?
31
+
32
+ '.'
33
+ end
34
+
35
+ def build_engine(files)
29
36
  Engine.concurrent(
30
- complexity: complexity_for(@language),
31
- churn: Churn::GitCalculator,
37
+ complexity: @collaborators.complexity || complexity_for(@scope.language),
38
+ churn: @collaborators.churn,
32
39
  file_selector: select_files(files),
33
40
  serializer: Normal::Serializer::None,
34
- since: @since,
41
+ since: @scope.effective_since,
35
42
  )
36
43
  end
37
44
 
38
45
  def select_files(files)
39
- selector = file_selector_for(@language)
40
- files.any? ? selector.predefined(included: files, excluded: @excluded) : selector.excluding(@excluded)
46
+ selector = @collaborators.file_selector || file_selector_for(@scope.language)
47
+ excluded = @scope.excluded
48
+ files.any? ? selector.predefined(included: files, excluded:) : selector.excluding(excluded)
41
49
  end
42
50
 
43
51
  def file_selector_for(language)
44
- case language
45
- when :ruby then FileSelector::Ruby
46
- when :java then FileSelector::Java
47
- when :javascript then FileSelector::JavaScript
48
- when :python then FileSelector::Python
49
- when :go then FileSelector::Go
50
- end
52
+ LanguageRegistry.file_selector_for(language)
51
53
  end
52
54
 
53
55
  def complexity_for(language)
54
- case language
55
- when :ruby then Complexity::FlogCalculator
56
- when :java then Complexity::PMD::FolderCalculator
57
- when :javascript then Complexity::ESLintCalculator
58
- when :python then Complexity::PythonCalculator
59
- when :go then Complexity::GoCalculator
60
- end
56
+ LanguageRegistry.complexity_for(language)
61
57
  end
62
58
  end
63
59
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ChurnVsComplexity
4
+ module Triage
5
+ CollaboratorSet = Data.define(:complexity, :churn, :file_selector) do
6
+ def initialize(complexity: nil, churn: Churn::GitCalculator, file_selector: nil)
7
+ super
8
+ end
9
+ end
10
+ end
11
+ end