Class: Bake::Modernize::License::Mailmap

Inherits:
Object
  • Object
show all
Defined in:
lib/bake/modernize/license.rb

Overview

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

Constant Summary collapse

PATTERN =

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

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMailmap

Create a new, empty, mailmap.



67
68
69
# File 'lib/bake/modernize/license.rb', line 67

def initialize
	@names = {}
end

Instance Attribute Details

#namesObject (readonly)

Returns the value of attribute names.



72
73
74
# File 'lib/bake/modernize/license.rb', line 72

def names
  @names
end

#The mapping of commit emails to proper names.(mappingofcommitemailstopropernames.) ⇒ Object (readonly)



72
# File 'lib/bake/modernize/license.rb', line 72

attr :names

Class Method Details

.for(root) ⇒ Object

Load the mailmap from a directory.



56
57
58
59
60
61
62
63
64
# File 'lib/bake/modernize/license.rb', line 56

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

Instance Method Details

#extract(path) ⇒ Object

Extract the mailmap from the given path.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bake/modernize/license.rb', line 75

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

#extract_from_line(line) ⇒ Object

Extract the mailmap format from a line of input.



102
103
104
# File 'lib/bake/modernize/license.rb', line 102

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