Class: Metior::CommitCollection
- Inherits:
-
Collection
- Object
- Collection
- Metior::CommitCollection
- Defined in:
- lib/metior/collections/commit_collection.rb
Overview
This class implements a collection of commits and provides functionality specific to commits.
Instance Method Summary collapse
-
#<<(commit) ⇒ CommitCollection
Adds a commit to this collection.
-
#activity ⇒ Hash<Symbol, Object>
Calculate some predefined activity statistics for the commits in this collection.
-
#additions ⇒ Fixnum
Returns the lines of code that have been added by the commits in this collection.
-
#after(date) ⇒ CommitCollection
(also: #newer)
Returns the commits in this collection that have been committed after the given time.
-
#authors(commit_id = nil) ⇒ ActorCollection
Returns the authors of all or a specific commit in this collection.
-
#before(date) ⇒ CommitCollection
(also: #older)
Returns the commits in this collection that have been committed before the given time.
-
#by(*author_ids) ⇒ CommitCollection
Returns the list of commits that have been authored by the given authors.
-
#changing(*files) ⇒ CommitCollection
(also: #touching)
Returns the commits in this collection that change any of the given files.
-
#committers(commit_id = nil) ⇒ ActorCollection
Returns the committers of all or a specific commit in this collection.
-
#deletions ⇒ Fixnum
Returns the lines of code that have been deleted by the commits in this collection.
-
#initialize(commits = []) ⇒ CommitCollection
constructor
Creates a new collection with the given commits.
-
#line_history ⇒ Hash<Symbol, Array>
This evaluates the changed lines in each commit of this collection.
-
#load_line_stats ⇒ Object
private
Loads the line stats for all commits in this collection.
-
#modifications ⇒ Fixnum
Returns the total of lines changed by the commits in this collection.
-
#most_significant(count = 10) ⇒ CommitCollection
(also: #top)
Returns the given number of commits with most line changes on the repository.
-
#with_impact(line_count) ⇒ CommitCollection
Returns the commits in this collection that change at least the given number of lines.
Methods inherited from Collection
Constructor Details
#initialize(commits = []) ⇒ CommitCollection
Creates a new collection with the given commits
24 25 26 27 28 29 |
# File 'lib/metior/collections/commit_collection.rb', line 24 def initialize(commits = []) @additions = nil @deletions = nil super end |
Instance Method Details
#<<(commit) ⇒ CommitCollection
Adds a commit to this collection
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/metior/collections/commit_collection.rb', line 35 def <<(commit) return self if key? commit.id unless @additions.nil? @additions += commit.additions @deletions += commit.deletions end super end |
#activity ⇒ Hash<Symbol, Object>
Calculate some predefined activity statistics for the commits in this collection
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/metior/collections/commit_collection.rb', line 52 def activity activity = {} commit_count = values.size active_days = {} each do |commit| date = commit.committed_date.utc day = Time.utc(date.year, date.month, date.day) if active_days.key? day active_days[day] += 1 else active_days[day] = 1 end end most_active_day = active_days.sort_by { |day, count| count }.last.first activity[:first_commit_date] = last.committed_date activity[:last_commit_date] = first.committed_date age_in_days = (Time.now - activity[:first_commit_date]) / 86400.0 activity[:active_days] = active_days activity[:most_active_day] = most_active_day activity[:commits_per_day] = commit_count / age_in_days activity[:commits_per_active_day] = commit_count.to_f / active_days.size activity end |
#additions ⇒ Fixnum
Returns the lines of code that have been added by the commits in this collection
This will load the line stats from the commits if not done yet.
89 90 91 92 93 94 |
# File 'lib/metior/collections/commit_collection.rb', line 89 def additions first.support! :line_stats load_line_stats if @additions.nil? @additions end |
#after(date) ⇒ CommitCollection Also known as: newer
Returns the commits in this collection that have been committed after the given time
105 106 107 108 109 110 111 112 |
# File 'lib/metior/collections/commit_collection.rb', line 105 def after(date) date = Time.parse date if date.is_a? String commits = CommitCollection.new each do |commit| commits << commit if commit.committed_date > date end commits end |
#authors(commit_id = nil) ⇒ ActorCollection
Returns the authors of all or a specific commit in this collection
122 123 124 125 126 127 128 129 130 |
# File 'lib/metior/collections/commit_collection.rb', line 122 def (commit_id = nil) = ActorCollection.new if commit_id.nil? each { |commit| << commit. } elsif key? commit_id << self[commit_id]. end end |
#before(date) ⇒ CommitCollection Also known as: older
Returns the commits in this collection that have been committed before the given time
141 142 143 144 145 146 147 148 |
# File 'lib/metior/collections/commit_collection.rb', line 141 def before(date) date = Time.parse date if date.is_a? String commits = CommitCollection.new each do |commit| commits << commit if commit.committed_date < date end commits end |
#by(*author_ids) ⇒ CommitCollection
Returns the list of commits that have been authored by the given authors
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/metior/collections/commit_collection.rb', line 159 def by(*) = .flatten.map do || .is_a?(Actor) ? .id : end commits = CommitCollection.new each do |commit| commits << commit if .include? commit..id end commits end |
#changing(*files) ⇒ CommitCollection Also known as: touching
Returns the commits in this collection that change any of the given files
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/metior/collections/commit_collection.rb', line 178 def changing(*files) first.support! :file_stats commits = CommitCollection.new each do |commit| commit_files = commit.added_files + commit.deleted_files + commit.modified_files commits << commit unless (commit_files & files).empty? end commits end |
#committers(commit_id = nil) ⇒ ActorCollection
Returns the committers of all or a specific commit in this collection
197 198 199 200 201 202 203 204 205 |
# File 'lib/metior/collections/commit_collection.rb', line 197 def committers(commit_id = nil) committers = ActorCollection.new if commit_id.nil? each { |commit| committers << commit.committer } elsif key? commit_id committers << self[commit_id].committer end committers end |
#deletions ⇒ Fixnum
Returns the lines of code that have been deleted by the commits in this collection
This will load the line stats from the commits if not done yet.
214 215 216 217 218 219 |
# File 'lib/metior/collections/commit_collection.rb', line 214 def deletions first.support! :line_stats load_line_stats if @deletions.nil? @deletions end |
#line_history ⇒ Hash<Symbol, Array>
This evaluates the changed lines in each commit of this collection
For easier use, the values are stored in separate arrays where each number represents the number of changed (i.e. added or deleted) lines in one commit.
235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/metior/collections/commit_collection.rb', line 235 def line_history first.support! :line_stats history = { :additions => [], :deletions => [] } values.reverse.each do |commit| history[:additions] << commit.additions history[:deletions] << -commit.deletions end history end |
#load_line_stats ⇒ Object (private)
Loads the line stats for all commits in this collection
298 299 300 301 302 303 304 305 |
# File 'lib/metior/collections/commit_collection.rb', line 298 def load_line_stats @additions = 0 @deletions = 0 each do |commit| @additions += commit.additions @deletions += commit.deletions end end |
#modifications ⇒ Fixnum
Returns the total of lines changed by the commits in this collection
252 253 254 |
# File 'lib/metior/collections/commit_collection.rb', line 252 def modifications additions + deletions end |
#most_significant(count = 10) ⇒ CommitCollection Also known as: top
Returns the given number of commits with most line changes on the repository
262 263 264 265 266 267 268 269 270 271 |
# File 'lib/metior/collections/commit_collection.rb', line 262 def most_significant(count = 10) first.support! :line_stats commits = CommitCollection.new sort_by { |commit| -commit.modifications }.each do |commit| commits << commit break if commits.size == count end commits end |
#with_impact(line_count) ⇒ CommitCollection
Returns the commits in this collection that change at least the given number of lines
282 283 284 285 286 287 288 289 290 |
# File 'lib/metior/collections/commit_collection.rb', line 282 def with_impact(line_count) first.support! :line_stats commits = CommitCollection.new each do |commit| commits << commit if commit.modifications >= line_count end commits end |