Class: GitStatistics::Commits

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#sorted_hash

Constructor Details

#initialize(path, fresh, limit, pretty) ⇒ Commits

Returns a new instance of Commits.



6
7
8
9
10
11
12
13
# File 'lib/git_statistics/commits.rb', line 6

def initialize(path, fresh, limit, pretty)
  super()
  @path = path
  @fresh = fresh
  @limit = limit
  @pretty = pretty
  clean
end

Instance Attribute Details

#freshObject

Returns the value of attribute fresh.



4
5
6
# File 'lib/git_statistics/commits.rb', line 4

def fresh
  @fresh
end

#limitObject

Returns the value of attribute limit.



4
5
6
# File 'lib/git_statistics/commits.rb', line 4

def limit
  @limit
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/git_statistics/commits.rb', line 4

def path
  @path
end

#prettyObject

Returns the value of attribute pretty.



4
5
6
# File 'lib/git_statistics/commits.rb', line 4

def pretty
  @pretty
end

#statsObject

Returns the value of attribute stats.



4
5
6
# File 'lib/git_statistics/commits.rb', line 4

def stats
  @stats
end

#totalsObject

Returns the value of attribute totals.



4
5
6
# File 'lib/git_statistics/commits.rb', line 4

def totals
  @totals
end

Instance Method Details

#add_commit_stats(data, commit) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/git_statistics/commits.rb', line 122

def add_commit_stats(data, commit)
  # Add commit stats to author
  data[:merges] += 1 if commit[:merge]
  data[:commits] += 1
  data[:additions] += commit[:additions]
  data[:deletions] += commit[:deletions]
  data[:create] += commit[:create] if commit[:create] != nil
  data[:delete] += commit[:delete] if commit[:delete] != nil
  data[:rename] += commit[:rename] if commit[:rename] != nil
  data[:copy] += commit[:copy] if commit[:copy] != nil
  return data
end

#add_language_stats(data, file) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/git_statistics/commits.rb', line 106

def add_language_stats(data, file)
  # Add stats to data's languages
  if data[:languages][file[:language].to_sym].nil?
    data[:languages][file[:language].to_sym] = Hash.new(0)
  end

  data[:languages][file[:language].to_sym][:additions] += file[:additions]
  data[:languages][file[:language].to_sym][:deletions] += file[:deletions]

  if file[:status] != nil
    data[:languages][file[:language].to_sym][file[:status].to_sym] += 1
  end

  return data
end

#author_top_n_type(type, top_n = 0) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/git_statistics/commits.rb', line 44

def author_top_n_type(type, top_n = 0)
  top_n = 0 if top_n < 0
  if @stats.empty? || !@stats.first[1].has_key?(type)
    nil
  else
    Hash[*@stats.sorted_hash {|a,b| b[1][type.to_sym] <=> a[1][type]}.to_a[0..top_n-1].flatten]
  end
end

#calculate_statistics(email, merge) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/git_statistics/commits.rb', line 53

def calculate_statistics(email, merge)
  # Identify authors and author type
  type = email ? :author_email : :author

  # For all the commit files created
  Dir.entries(path).each do |file|
    # Load commit file and extract the commits
    if file =~ /\d+\.json/
      load(File.join(path, file))
      process_commits(type, merge)
      clear
    end
  end
end

#cleanObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/git_statistics/commits.rb', line 15

def clean
  # Ensure the path exists
  FileUtils.mkdir_p(path)

  # Remove all files within path if saving
  if fresh
    files_in_path.each do |file|
      File.delete(File.join(path, file))
    end
  end

  # Initilize/resets stats and totals
  @stats = Hash.new
  @totals = Hash.new(0)
  @totals[:languages] = {}
end

#files_in_pathObject



32
33
34
# File 'lib/git_statistics/commits.rb', line 32

def files_in_path
  Dir.entries(path).reject { |file| %w[. ..].include?(file) }
end

#flush_commits(force = false) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/git_statistics/commits.rb', line 36

def flush_commits(force = false)
  if size >= limit || force
    file_count = Utilities.number_of_matching_files(path, /\d+\.json/)
    save(File.join(path, "#{file_count}.json"), @pretty)
    clear
  end
end

#load(file) ⇒ Object



135
136
137
# File 'lib/git_statistics/commits.rb', line 135

def load(file)
  merge!(JSON.parse(File.read(file), :symbolize_names => true))
end

#process_commits(type, merge) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/git_statistics/commits.rb', line 68

def process_commits(type, merge)
  # Collect the stats from each commit
  each do |key,value|
    next if !merge && value[:merge]
    # If there are no changed files move to next commit
    next if value[:files].empty?

    # Acquire author (make if not seen before)
    author = @stats[value[type]]

    if author.nil?
      @stats[value[type]] = Hash.new(0)
      author = @stats[value[type]]
      author[:languages] = {}
    end

    # Collect language stats
    value[:files].each do |file|

      # Add to author's languages
      add_language_stats(author, file)

      # Add to repository's languages
      add_language_stats(@totals, file)
    end

    # Add commit stats to author
    add_commit_stats(author, value)

    # Add commit stats to repository
    add_commit_stats(@totals, value)

    # Save new changes back to stats
    @stats[value[type]] = author
    author = nil
  end
end

#save(file, pretty) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/git_statistics/commits.rb', line 139

def save(file, pretty)
  # Don't save if there is no information (i.e., using updates)
  unless empty?
    # Ensure the path to the file exists
    FileUtils.mkdir_p(File.dirname(file))
    # Save file in a simple or pretty format
    File.open(file, 'w') do |file|
      json_content = pretty ? JSON.pretty_generate(self) : self.to_json
      file.write(json_content)
    end
  end
end