Bake ModernizeSourceBakeModernizeLicenseMailmap

class Mailmap

Represents a mailmap file which maps commit emails to proper names.

Definitions

def self.for(root)

Load the mailmap from a directory.

Implementation

def self.for(root)
	full_path = File.join(root, ".mailmap")
	
	if File.exist?(full_path)
		mailmap = self.new
		mailmap.extract(full_path)
		return mailmap
	end
end

def initialize

Create a new, empty, mailmap.

Implementation

def initialize
	@names = {}
end

attr :names

Signature

attribute Hash(String, String)

The mapping of commit emails to proper names.

def extract(path)

Extract the mailmap from the given path.

Implementation

def extract(path)
	File.open(path, "r") do |file|
		file.each_line do |line|
			# Skip comments
			next if line =~ /^#/
			# Skip empty lines
			next if line =~ /^\s*$/
			# Parse line
			
			
			user = extract_from_line(line)
			if commit_email = user[:commit_email] and proper_name = user[:proper_name]
				@names[commit_email] = proper_name
			end
		end
	end
end

PATTERN

Format: Proper Name proper@email.xx Commit Name commit@email.xx

Implementation

PATTERN = /
	(?<proper_name>[^<]+)?
	(\s+<(?<proper_email>[^>]+)>)?
	(\s+(?<commit_name>[^<]+)?)?
	\s+<(?<commit_email>[^>]+)>
/x

def extract_from_line(line)

Extract the mailmap format from a line of input.

Implementation

def extract_from_line(line)
	line.match(PATTERN)
end