Class: GitSme::CommitAnalyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/git_sme/commit_analyzer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commit_loader, enable_cache: true) ⇒ CommitAnalyzer

Returns a new instance of CommitAnalyzer.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/git_sme/commit_analyzer.rb', line 8

def initialize(commit_loader, enable_cache: true)
  @enable_cache = true
  @commit_loader = commit_loader
  @analyzed = false
  @valid = @commit_loader.valid?
  @error_message = @commit_loader.error_message

  @analysis = {}
  @cache = GitSme::Cache.new(@commit_loader.repo.path.gsub('/.git/', ''),
    enabled: @enable_cache, file_suffix: "#{@commit_loader.branch}-analysis"
  )
end

Instance Attribute Details

#analysisObject (readonly)

Returns the value of attribute analysis.



3
4
5
# File 'lib/git_sme/commit_analyzer.rb', line 3

def analysis
  @analysis
end

#analyzedObject (readonly) Also known as: analyzed?

Returns the value of attribute analyzed.



3
4
5
# File 'lib/git_sme/commit_analyzer.rb', line 3

def analyzed
  @analyzed
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



3
4
5
# File 'lib/git_sme/commit_analyzer.rb', line 3

def error_message
  @error_message
end

#validObject (readonly) Also known as: valid?

Returns the value of attribute valid.



3
4
5
# File 'lib/git_sme/commit_analyzer.rb', line 3

def valid
  @valid
end

Instance Method Details

#analyze(force: false) ⇒ Object



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
# File 'lib/git_sme/commit_analyzer.rb', line 21

def analyze(force: false)
  return unless valid?
  return if analyzed? && !force

  @commit_loader.load
  @analysis = @cache.load
  new_analysis = []

  if !@commit_loader.new_commits? || !@analysis.any?
    if block_given?
      @analysis = analyze_new_commits(@commit_loader.commits) do |commit_count, total_commits|
        yield(commit_count, total_commits)
      end
    else
      @analysis = analyze_new_commits(@commit_loader.commits)
    end
  elsif @commit_loader.new_commits?
    new_analysis = if block_given?
      analyze_new_commits(@commit_loader.new_commits) do |commit_count, total_commits|
        yield(commit_count, total_commits)
      end
    else
      analyze_new_commits(@commit_loader.new_commits)
    end

    summed_merge(@analysis[:by_user], new_analysis[:by_user])
    summed_merge(@analysis[:by_file], new_analysis[:by_file])
  end

  @cache.save(@analysis)
  @analyzed = true
end