Class: GitStatistics::Commits
- Defined in:
- lib/git_statistics/commits.rb
Instance Attribute Summary collapse
-
#fresh ⇒ Object
Returns the value of attribute fresh.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#path ⇒ Object
Returns the value of attribute path.
-
#pretty ⇒ Object
Returns the value of attribute pretty.
-
#stats ⇒ Object
Returns the value of attribute stats.
-
#totals ⇒ Object
Returns the value of attribute totals.
Instance Method Summary collapse
- #add_commit_stats(data, commit) ⇒ Object
- #add_language_stats(data, file) ⇒ Object
- #author_top_n_type(type, top_n = 0) ⇒ Object
- #calculate_statistics(email, merge) ⇒ Object
- #clean ⇒ Object
- #files_in_path ⇒ Object
- #flush_commits(force = false) ⇒ Object
-
#initialize(path, fresh, limit, pretty) ⇒ Commits
constructor
A new instance of Commits.
- #load(file) ⇒ Object
- #process_commits(type, merge) ⇒ Object
- #save(file, pretty) ⇒ Object
Methods inherited from Hash
Constructor Details
#initialize(path, fresh, limit, pretty) ⇒ Commits
Returns a new instance of Commits.
5 6 7 8 9 10 11 12 |
# File 'lib/git_statistics/commits.rb', line 5 def initialize(path, fresh, limit, pretty) super() @path = path @fresh = fresh @limit = limit @pretty = pretty clean end |
Instance Attribute Details
#fresh ⇒ Object
Returns the value of attribute fresh.
3 4 5 |
# File 'lib/git_statistics/commits.rb', line 3 def fresh @fresh end |
#limit ⇒ Object
Returns the value of attribute limit.
3 4 5 |
# File 'lib/git_statistics/commits.rb', line 3 def limit @limit end |
#path ⇒ Object
Returns the value of attribute path.
3 4 5 |
# File 'lib/git_statistics/commits.rb', line 3 def path @path end |
#pretty ⇒ Object
Returns the value of attribute pretty.
3 4 5 |
# File 'lib/git_statistics/commits.rb', line 3 def pretty @pretty end |
#stats ⇒ Object
Returns the value of attribute stats.
3 4 5 |
# File 'lib/git_statistics/commits.rb', line 3 def stats @stats end |
#totals ⇒ Object
Returns the value of attribute totals.
3 4 5 |
# File 'lib/git_statistics/commits.rb', line 3 def totals @totals end |
Instance Method Details
#add_commit_stats(data, commit) ⇒ Object
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/git_statistics/commits.rb', line 126 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[:added_files] += commit[:added_files] unless commit[:added_files].nil? data[:deleted_files] += commit[:deleted_files] unless commit[:deleted_files].nil? data end |
#add_language_stats(data, file) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/git_statistics/commits.rb', line 109 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] # Keep count of languages status (i.e., added, deleted) and keep keys consistent (i.e., added_files, deleted_files) unless file[:status].nil? data[:languages][file[:language].to_sym][(file[:status] + '_files').to_sym] += 1 end data end |
#author_top_n_type(type, top_n = 0) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/git_statistics/commits.rb', line 43 def (type, top_n = 0) top_n = 0 if top_n < 0 if @stats.empty? || !@stats.first[1].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
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/git_statistics/commits.rb', line 52 def calculate_statistics(email, merge) # Identify authors and author type type = email ? :author_email : :author # Process the commits from file or memory files = Dir.entries(path) - ['.', '..'] if files.size == 0 process_commits(type, merge) else # Load commit file and extract the commits files.each do |file| next unless file =~ /\d+\.json/ load(File.join(path, file)) process_commits(type, merge) clear end end end |
#clean ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/git_statistics/commits.rb', line 14 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_path ⇒ Object
31 32 33 |
# File 'lib/git_statistics/commits.rb', line 31 def files_in_path Dir.entries(path).reject { |file| %w(. ..).include?(file) } end |
#flush_commits(force = false) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/git_statistics/commits.rb', line 35 def flush_commits(force = false) return unless size >= limit || force file_count = Utilities.number_of_matching_files(path, /\d+\.json/) save(File.join(path, "#{file_count}.json"), @pretty) clear end |
#load(file) ⇒ Object
137 138 139 |
# File 'lib/git_statistics/commits.rb', line 137 def load(file) merge!(JSON.parse(File.read(file), symbolize_names: true)) end |
#process_commits(type, merge) ⇒ Object
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 105 106 107 |
# File 'lib/git_statistics/commits.rb', line 72 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) = @stats[value[type]] if .nil? @stats[value[type]] = Hash.new(0) = @stats[value[type]] [:languages] = {} end # Collect language stats value[:files].each do |file| # Add to author's languages add_language_stats(, file) # Add to repository's languages add_language_stats(@totals, file) end # Add commit stats to author add_commit_stats(, value) # Add commit stats to repository add_commit_stats(@totals, value) # Save new changes back to stats @stats[value[type]] = = nil end end |
#save(file, pretty) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/git_statistics/commits.rb', line 141 def save(file, pretty) return if 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 |f| json_content = pretty ? JSON.pretty_generate(self) : to_json f.write(json_content) end end |