Class: GitStatistics::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/git_statistics/collector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, limit, fresh, pretty) ⇒ Collector

Returns a new instance of Collector.



5
6
7
8
9
# File 'lib/git_statistics/collector.rb', line 5

def initialize(repo, limit, fresh, pretty)
  @repo = repo
  @commits_path = repo.workdir + '.git_statistics'
  @commits = Commits.new(@commits_path, fresh, limit, pretty)
end

Instance Attribute Details

#commitsObject

Returns the value of attribute commits.



3
4
5
# File 'lib/git_statistics/collector.rb', line 3

def commits
  @commits
end

#commits_pathObject

Returns the value of attribute commits_path.



3
4
5
# File 'lib/git_statistics/collector.rb', line 3

def commits_path
  @commits_path
end

#repoObject

Returns the value of attribute repo.



3
4
5
# File 'lib/git_statistics/collector.rb', line 3

def repo
  @repo
end

Instance Method Details

#acquire_commit_meta(commit_summary) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/git_statistics/collector.rb', line 39

def acquire_commit_meta(commit_summary)
  # Initialize commit data
  data = (@commits[commit_summary.oid] ||= Hash.new(0))

  data[:author] = commit_summary.author[:name]
  data[:author_email] = commit_summary.author[:email]
  data[:time] = commit_summary.author[:time].to_s
  data[:merge] = commit_summary.merge?
  data[:additions] = commit_summary.additions
  data[:deletions] = commit_summary.deletions
  data[:net] = commit_summary.net
  data[:added_files] = commit_summary.added_files
  data[:deleted_files] = commit_summary.deleted_files
  data[:modified_files] = commit_summary.modified_files
  data[:files] = commit_summary.file_stats.map(&:to_json)

  data
end

#collect(options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/git_statistics/collector.rb', line 11

def collect(options = {})
  branch_name = options[:branch] ? options[:branch] : CLI::DEFAULT_BRANCH

  walker = Rugged::Walker.new(repo)
  walker.push(repo.branches[branch_name].target)

  walker.each_with_index do |commit, count|
    next unless valid_commit?(commit, options)

    extract_commit(commit, count + 1)
    @commits.flush_commits
  end

  @commits.flush_commits(true)
end

#extract_commit(commit, count) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/git_statistics/collector.rb', line 58

def extract_commit(commit, count)
  Log.info "Extracting(#{count}) #{commit.oid}"
  commit_summary = CommitSummary.new(@repo, commit)

  # Acquire meta information about commit
  commit_data = acquire_commit_meta(commit_summary)

  commit_data
end

#valid_commit?(commit, options) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/git_statistics/collector.rb', line 27

def valid_commit?(commit, options)
  unless options[:time_since].nil?
    return false unless commit.author[:time] > DateTime.parse(options[:time_since].to_s).to_time
  end

  unless options[:time_until].nil?
    return false unless commit.author[:time] < DateTime.parse(options[:time_until].to_s).to_time
  end

  true
end