Skip to content

Commit

Permalink
Resolve sha in delta output for HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
beatmadsen committed Oct 21, 2024
1 parent 52eda92 commit bce75e2
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [1.5.2] - 2024-10-21

- Fixed bug where delta mode validations would fail when the commit was a non-sha value.
- Allow HEAD as specified commit in delta mode

## [1.5.1] - 2024-10-15

- Fix bug where worktree checkout silently failed
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
churn_vs_complexity (1.5.1)
churn_vs_complexity (1.5.2)
flog (~> 4.8)
git (~> 2.1)

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ Usage: churn_vs_complexity [options] folder
-q, --quarter Calculate churn for the quarter leading up to the most recent commit
-y, --year Calculate churn for the year leading up to the most recent commit
--timetravel N Calculate summary for all commits at intervals of N days throughout project history or from the date specified with --since
--delta SHA Identify changes between the specified commit and the previous commit and annotate changed files with complexity score. Can be used multiple times to specify multiple commits.
--delta SHA 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.
--dry-run Echo the chosen options from the CLI
-h, --help Display help
--version Display version
```

Note that when using the `--timetravel` mode, the semantics of some flags are subtly different from normal mode:
Expand Down
1 change: 1 addition & 0 deletions lib/churn_vs_complexity/cli/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def run!(options, folder)
validate_options!(options)
config = config(options)
config.validate!

config.checker.check(folder:)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/churn_vs_complexity/cli/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def self.create
end

opts.on('--delta SHA',
'Identify changes between the specified commit and the previous commit and annotate changed files with complexity score. Can be used multiple times to specify multiple commits.',) do |value|
'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|
options[:mode] = :delta
(options[:commits] ||= []) << value
end
Expand Down
1 change: 1 addition & 0 deletions lib/churn_vs_complexity/delta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_relative 'delta/factory'
require_relative 'delta/complexity_annotator'
require_relative 'delta/multi_checker'
require_relative 'delta/commit_hydrator'

module ChurnVsComplexity
module Delta
Expand Down
10 changes: 4 additions & 6 deletions lib/churn_vs_complexity/delta/checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ def setup_worktree(folder:)
end

def commit_summary(folder:)
summary = { commit: @commit }
if @serializer.respond_to?(:has_commit_summary?) && @serializer.has_commit_summary?
parent, next_commit = @factory.git_strategy(folder:).surrounding(commit: @commit)
summary.merge!(parent:, next_commit:)
end
summary
CommitHydrator.new(
git_strategy: @factory.git_strategy(folder:),
serializer: @serializer,
).hydrate(@commit)
end

def valid_commit?(folder:)
Expand Down
22 changes: 22 additions & 0 deletions lib/churn_vs_complexity/delta/commit_hydrator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module ChurnVsComplexity
module Delta
class CommitHydrator
def initialize(git_strategy:, serializer:)
@git_strategy = git_strategy
@serializer = serializer
end

def hydrate(commit_sha)
commit = @git_strategy.object(commit_sha)
summary = { commit: commit.sha }
if @serializer.respond_to?(:has_commit_summary?) && @serializer.has_commit_summary?
parent, next_commit = @git_strategy.surrounding(commit:)
summary.merge!(parent:, next_commit:)
end
summary
end
end
end
end
14 changes: 8 additions & 6 deletions lib/churn_vs_complexity/git_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ def valid_commit?(commit:)
false
end

def object(commit)
commit.is_a?(Git::Object::Commit) ? commit : @repo.object(commit)
end

def surrounding(commit:)
current = @repo.object(commit)
parent = current.parent
next_commit = @repo.log(100_000).find do |c|
c.parents.map(&:sha).include?(current.sha)
end
[parent&.sha, next_commit&.sha]
current = object(commit)
is_head = current.sha == @repo.object('HEAD').sha
next_commit = is_head ? nil : @repo.log(100_000).find { |c| c.parents.map(&:sha).include?(current.sha) }
[current.parent&.sha, next_commit&.sha]
end

def changes(commit:)
Expand Down
2 changes: 1 addition & 1 deletion lib/churn_vs_complexity/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module ChurnVsComplexity
VERSION = '1.5.1'
VERSION = '1.5.2'
end
6 changes: 6 additions & 0 deletions test/churn_vs_complexity/delta/checker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ def changes(commit:)
@changes
end

def object(commit)
GitCommitStub.new(sha: commit)
end

def surrounding(commit:)
%w[aabbccdd bbbbccdd]
end
Expand All @@ -166,6 +170,8 @@ def checkout(sha)
def folder = FOLDER
end

GitCommitStub = Data.define(:sha)

class EngineStub
def initialize(fail_to_process: false)
@fail_to_process = fail_to_process
Expand Down
31 changes: 31 additions & 0 deletions test/churn_vs_complexity/delta/commit_hydrator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require 'test_helper'

module ChurnVsComplexity
module Delta
class CommitHydratorTest < TLDR
def test_resolves_sha_of_head_commit
expected_sha = '123abcde'
git_strategy = GitStrategyStub.new(object_sha: expected_sha)
hydrator = CommitHydrator.new(git_strategy:, serializer: nil)

result = hydrator.hydrate('HEAD')

assert_equal expected_sha, result[:commit]
end

GitCommitStub = Data.define(:sha)

class GitStrategyStub
def initialize(object_sha: nil)
@object_sha = object_sha
end

def object(commit)
GitCommitStub.new(sha: @object_sha || commit)
end
end
end
end
end

0 comments on commit bce75e2

Please sign in to comment.