Class: Git::Heatmap::Commits

Inherits:
Object
  • Object
show all
Defined in:
lib/git/heatmap/commits.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter: Weekly.new, depth: 4) ⇒ Commits

Returns a new instance of Commits.



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/git/heatmap/commits.rb', line 76

def initialize(filter: Weekly.new, depth: 4)
  @filter = filter
  
  @authors = Set.new
  @directories = Hash.new{|h,k| h[k] = Aggregate.new(filter)}
  
  @depth = depth
  
  @earliest_commit_at = nil
  @latest_commit_at = nil
  
  @maximum = nil
end

Instance Attribute Details

#authorsObject (readonly)

Returns the value of attribute authors.



103
104
105
# File 'lib/git/heatmap/commits.rb', line 103

def authors
  @authors
end

#directoriesObject (readonly)

Returns the value of attribute directories.



104
105
106
# File 'lib/git/heatmap/commits.rb', line 104

def directories
  @directories
end

#earliest_commit_atObject (readonly)

Returns the value of attribute earliest_commit_at.



92
93
94
# File 'lib/git/heatmap/commits.rb', line 92

def earliest_commit_at
  @earliest_commit_at
end

#filterObject (readonly)

Returns the value of attribute filter.



90
91
92
# File 'lib/git/heatmap/commits.rb', line 90

def filter
  @filter
end

#latest_commit_atObject (readonly)

Returns the value of attribute latest_commit_at.



93
94
95
# File 'lib/git/heatmap/commits.rb', line 93

def latest_commit_at
  @latest_commit_at
end

Instance Method Details

#<<(commit) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/git/heatmap/commits.rb', line 112

def << commit
  @maximum = nil
  
  if parent = commit.parents.first
    # Documentation seems to imply this shouldn't be needed.
    diff = commit.diff(commit.parents.first.tree)
  else
    diff = commit.diff
  end
  
  author = commit.author
  time = author[:time]
  @authors << author[:name]
  
  if @earliest_commit_at.nil? or time < @earliest_commit_at
    @earliest_commit_at = time
  end
  
  if @latest_commit_at.nil? or time > @latest_commit_at
    @latest_commit_at = time
  end
  
  diff.each_patch do |patch|
    delta = patch.delta
    path = delta.new_file[:path]
    
    parts = path.split(File::SEPARATOR)
    parts.unshift(File.basename(commit.tree.repo.workdir))
    parts.pop # Remove file name
    root = parts[0...@depth]
    
    @directories[root] << commit
  end
end

#add(repository) ⇒ Object



147
148
149
150
151
152
153
154
155
156
# File 'lib/git/heatmap/commits.rb', line 147

def add(repository)
  walker = Rugged::Walker.new(repository)
  
  walker.sorting(Rugged::SORT_DATE)
  walker.push(repository.head.target.oid)
  
  walker.each do |commit|
    self << commit
  end
end

#each_directoryObject



106
107
108
109
110
# File 'lib/git/heatmap/commits.rb', line 106

def each_directory
  @directories.keys.sort.each do |key|
    yield key, @directories[key]
  end
end

#each_period(&block) ⇒ Object



95
96
97
# File 'lib/git/heatmap/commits.rb', line 95

def each_period(&block)
  @filter.between(@earliest_commit_at, @latest_commit_at, &block)
end

#maximumObject



99
100
101
# File 'lib/git/heatmap/commits.rb', line 99

def maximum
  @maximum ||= @directories.each_value.max_by(&:maximum).maximum
end