Class: Mailmap::Map

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mailmap/map.rb

Overview

A Map represents a .mailmap file.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Map

Returns a new instance of Map.

Parameters:

  • string (String)

    the string in .mailmap format



27
28
29
30
31
# File 'lib/mailmap/map.rb', line 27

def initialize(string)
  # @type ivar @map: Hash[String, Hash[String?, String?]]
  @map = Hash.new { |h, k| h[k] = {} }
  parse(string)
end

Class Method Details

.load(path) ⇒ Map

Load a mailmap file and return Map object.

Parameters:

  • path (String)

    the path to .mailmap file

Returns:



19
20
21
# File 'lib/mailmap/map.rb', line 19

def load(path)
  new(File.read(path))
end

Instance Method Details

#eachObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/mailmap/map.rb', line 33

def each
  return enum_for unless block_given?

  @map.each do |commit_email, entries_by_commit_name|
    entries_by_commit_name.each do |commit_name, (proper_name, proper_email)|
      yield proper_name, proper_email, commit_name, commit_email
    end
  end
  self
end

#include_email?(email) ⇒ Boolean

Return true if the email is defined as either of proper or commit email, otherwise false. The comparison is case-insensitive.

Parameters:

  • email (String)

    the email

Returns:

  • (Boolean)


91
92
93
94
95
96
# File 'lib/mailmap/map.rb', line 91

def include_email?(email)
  email = email.downcase
  any? do |_proper_name, proper_email, _commit_name, commit_email|
    proper_email&.downcase == email || commit_email == email
  end
end

#include_name?(name) ⇒ Boolean

Return true if the name is defined as either of proper or commit name, otherwise false. The comparison is case-insensitive.

Parameters:

  • name (String)

    the name

Returns:

  • (Boolean)


79
80
81
82
83
84
# File 'lib/mailmap/map.rb', line 79

def include_name?(name)
  name = name.downcase
  any? do |proper_name, _proper_email, commit_name, _commit_email|
    proper_name&.downcase == name || commit_name == name
  end
end

#lookup(commit_name_or_nil, commit_email) ⇒ (String, String), ...

Look up the person’s canonical name and email address. If found, return them; otherwise return nil.

Parameters:

  • commit_name_or_nil (String, nil)

    the name in commit or nil

  • commit_email (String)

    the name in commit

Returns:

  • ((String, String))

    if found, a pair of proper name and email

  • ((String, nil))

    if only proper name is found

  • ((nil, String))

    if only proper email is found

  • (nil)

    if not found



53
54
55
56
57
58
# File 'lib/mailmap/map.rb', line 53

def lookup(commit_name_or_nil, commit_email)
  commit_name = commit_name_or_nil&.downcase
  commit_email = commit_email.downcase
  hash = @map[commit_email]
  hash[commit_name] || hash[nil]
end

#resolve(commit_name_or_nil, commit_email) ⇒ (String, String), (nil, String)

Like git-check-mailmap command, look up the person’s canonical name and email address. If found, return them; otherwise return the input as-is.

Parameters:

  • commit_name_or_nil (String, nil)

    the name in commit or nil

  • commit_email (String)

    the email in commit

Returns:

  • ((String, String))

    a pair of proper name and email

  • ((nil, String))

    if proper name is not found and commit_name is not provided



67
68
69
70
71
72
# File 'lib/mailmap/map.rb', line 67

def resolve(commit_name_or_nil, commit_email)
  proper_name, proper_email = lookup(commit_name_or_nil, commit_email)
  proper_name ||= commit_name_or_nil
  proper_email ||= commit_email
  [proper_name, proper_email]
end