Bake ModernizeSourceBakeModernizeLicenseAuthorship

class Authorship

Represents the authorship of a repository.

Definitions

Modification

Represents a modification to a file.

Implementation

Modification = Struct.new(:author, :time, :path, :id) do
	def full_name
		author[:name]
	end
	
	def key
		self.id || "#{self.author[:email]}:#{self.time.iso8601}"
	end
	
	def to_h
		{
			id: id,
			time: time,
			path: path,
			author: author,
		}
	end
end

Copyright

Represents the copyright for an author.

Implementation

Copyright = Struct.new(:dates, :author) do
	def <=> other
		self.to_a <=> other.to_a
	end
	
	def statement
		years = self.dates.map(&:year).uniq
		return "Copyright, #{years.join('-')}, by #{author}."
	end
end

def initialize

Create a new, empty, authorship.

Implementation

def initialize
	@paths = Hash.new{|h,k| h[k] = []}
	@commits = Hash.new{|h,k| h[k] = []}
end

attr :paths

Signature

attribute Hash(String, Array(Modification))

The mapping of paths to modifications.

attr :commits

Signature

attribute Hash(String, Array(Modification))

The mapping of commits to modifications.

def add(path, author, time, id = nil)

Add a modification to the authorship.

Implementation

def add(path, author, time, id = nil)
	modification = Modification.new(author, time, path, id)
	
	@commits[modification.key] << modification
	@paths[path] << modification
end

def extract(root = Dir.pwd)

Extract the authorship from the given root directory.

Implementation

def extract(root = Dir.pwd)
	mailmap = Mailmap.for(root)
	skip_list = SkipList.for(root)
	
	if contributors = Contributors.for(root)
		contributors.each do |path, author, time|
			add(path, author, time)
		end
	end
	
	walk(Rugged::Repository.discover(root), mailmap: mailmap, skip_list: skip_list)
	
	return self
end

def sorted_authors

Authors, sorted by contribution date.

Implementation

def sorted_authors
	authors = Hash.new{|h,k| h[k] = 0}
	
	@commits.each do |key, modifications|
		modifications.map(&:full_name).uniq.each do |full_name|
			authors[full_name] += 1
		end
	end
	
	return authors.sort_by{|k,v| [-v, k]}.map(&:first)
end

def copyrights

All copyrights.

Implementation

def copyrights
	copyrights_for_modifications(@paths.values.flatten)
end

def copyrights_for_path(path)

All copyrights for a given path.

Implementation

def copyrights_for_path(path)
	copyrights_for_modifications(@paths[path])
end

def copyrights_for_modifications(modifications)

All copyrights for a given modification.

Implementation

def copyrights_for_modifications(modifications)
	authors = modifications.group_by{|modification| modification.full_name}
	
	authors.map do |name, modifications|
		Copyright.new(modifications.map(&:time).minmax, name)
	end.sort
end