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.



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/git/heatmap/commits.rb', line 93

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.



120
121
122
# File 'lib/git/heatmap/commits.rb', line 120

def authors
  @authors
end

#directoriesObject (readonly)

Returns the value of attribute directories.



121
122
123
# File 'lib/git/heatmap/commits.rb', line 121

def directories
  @directories
end

#earliest_commit_atObject (readonly)

Returns the value of attribute earliest_commit_at.



109
110
111
# File 'lib/git/heatmap/commits.rb', line 109

def earliest_commit_at
  @earliest_commit_at
end

#filterObject (readonly)

Returns the value of attribute filter.



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

def filter
  @filter
end

#latest_commit_atObject (readonly)

Returns the value of attribute latest_commit_at.



110
111
112
# File 'lib/git/heatmap/commits.rb', line 110

def latest_commit_at
  @latest_commit_at
end

Instance Method Details

#<<(commit) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/git/heatmap/commits.rb', line 129

def << commit
	@maximum = nil
	
	if parent = commit.parents.first
		# Documentation seems to imply this shouldn't be needed.
		diff = parent.diff(commit.tree)
	else
		empty_tree = Rugged::Tree.empty(commit.tree.repo)
		diff = empty_tree.diff(commit.tree)
	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].add(commit, patch)
	end
end

#add(repository) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'lib/git/heatmap/commits.rb', line 165

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



123
124
125
126
127
# File 'lib/git/heatmap/commits.rb', line 123

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

#each_period(&block) ⇒ Object



112
113
114
# File 'lib/git/heatmap/commits.rb', line 112

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

#maximumObject



116
117
118
# File 'lib/git/heatmap/commits.rb', line 116

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