Class: Git::AuthorInfo

Inherits:
Data
  • Object
show all
Defined in:
lib/git/author_info.rb

Overview

Immutable value object representing an author or committer identity

This is a lightweight, immutable data structure holding the identity data git records for commit authors, committers, and taggers. It replaces the mutable Author, which is deprecated.

Examples:

Construct from individual values

info = Git::AuthorInfo.new(
  name: 'John Doe',
  email: '[email protected]',
  date: Time.at(1627849923)
)
info.name  #=> 'John Doe'

Parse from a raw git author string

info = Git::AuthorInfo.parse('John Doe <[email protected]> 1627849923 +0200')
info.email     #=> '[email protected]'
info.date.to_i #=> 1627849923

See Also:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#dateTime? (readonly)

Returns the timestamp of the change, or nil if not available.

Returns:

  • (Time, nil)

    the timestamp of the change, or nil if not available



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/git/author_info.rb', line 38

AuthorInfo = Data.define(:name, :email, :date) do
  # Parses a raw git identity string into a Git::AuthorInfo
  #
  # The expected format is `"Name <email> timestamp offset"` as emitted by
  # `git cat-file` for the `author`, `committer`, and `tagger` headers. The
  # timestamp is interpreted as seconds since the Unix epoch; the timezone
  # offset is not preserved in the resulting `date`.
  #
  # @example Parse a well-formed identity string
  #   Git::AuthorInfo.parse('John Doe <[email protected]> 1627849923 +0200')
  #   #=> #<data Git::AuthorInfo name="John Doe", email="[email protected]", ...>
  #
  # @example A string that does not match the expected format
  #   Git::AuthorInfo.parse('garbage')
  #   #=> #<data Git::AuthorInfo name=nil, email=nil, date=nil>
  #
  # @param author_string [String] the raw identity string to parse
  #
  # @return [Git::AuthorInfo] the parsed identity; all attributes are `nil`
  #   when the string does not match the expected format
  #
  def self.parse(author_string)
    match = /(.*?) <(.*?)> (\d+) (.*)/.match(author_string)
    return new(name: nil, email: nil, date: nil) unless match

    new(name: match[1], email: match[2], date: Time.at(match[3].to_i))
  end
end

#emailString? (readonly)

Returns the person's email address, or nil if not available.

Returns:

  • (String, nil)

    the person's email address, or nil if not available



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/git/author_info.rb', line 38

AuthorInfo = Data.define(:name, :email, :date) do
  # Parses a raw git identity string into a Git::AuthorInfo
  #
  # The expected format is `"Name <email> timestamp offset"` as emitted by
  # `git cat-file` for the `author`, `committer`, and `tagger` headers. The
  # timestamp is interpreted as seconds since the Unix epoch; the timezone
  # offset is not preserved in the resulting `date`.
  #
  # @example Parse a well-formed identity string
  #   Git::AuthorInfo.parse('John Doe <[email protected]> 1627849923 +0200')
  #   #=> #<data Git::AuthorInfo name="John Doe", email="[email protected]", ...>
  #
  # @example A string that does not match the expected format
  #   Git::AuthorInfo.parse('garbage')
  #   #=> #<data Git::AuthorInfo name=nil, email=nil, date=nil>
  #
  # @param author_string [String] the raw identity string to parse
  #
  # @return [Git::AuthorInfo] the parsed identity; all attributes are `nil`
  #   when the string does not match the expected format
  #
  def self.parse(author_string)
    match = /(.*?) <(.*?)> (\d+) (.*)/.match(author_string)
    return new(name: nil, email: nil, date: nil) unless match

    new(name: match[1], email: match[2], date: Time.at(match[3].to_i))
  end
end

#nameString? (readonly)

Returns the person's name, or nil if not available.

Returns:

  • (String, nil)

    the person's name, or nil if not available



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/git/author_info.rb', line 38

AuthorInfo = Data.define(:name, :email, :date) do
  # Parses a raw git identity string into a Git::AuthorInfo
  #
  # The expected format is `"Name <email> timestamp offset"` as emitted by
  # `git cat-file` for the `author`, `committer`, and `tagger` headers. The
  # timestamp is interpreted as seconds since the Unix epoch; the timezone
  # offset is not preserved in the resulting `date`.
  #
  # @example Parse a well-formed identity string
  #   Git::AuthorInfo.parse('John Doe <[email protected]> 1627849923 +0200')
  #   #=> #<data Git::AuthorInfo name="John Doe", email="[email protected]", ...>
  #
  # @example A string that does not match the expected format
  #   Git::AuthorInfo.parse('garbage')
  #   #=> #<data Git::AuthorInfo name=nil, email=nil, date=nil>
  #
  # @param author_string [String] the raw identity string to parse
  #
  # @return [Git::AuthorInfo] the parsed identity; all attributes are `nil`
  #   when the string does not match the expected format
  #
  def self.parse(author_string)
    match = /(.*?) <(.*?)> (\d+) (.*)/.match(author_string)
    return new(name: nil, email: nil, date: nil) unless match

    new(name: match[1], email: match[2], date: Time.at(match[3].to_i))
  end
end

Class Method Details

.parse(author_string) ⇒ Git::AuthorInfo

Parses a raw git identity string into a Git::AuthorInfo

The expected format is "Name <email> timestamp offset" as emitted by git cat-file for the author, committer, and tagger headers. The timestamp is interpreted as seconds since the Unix epoch; the timezone offset is not preserved in the resulting date.

Examples:

Parse a well-formed identity string

Git::AuthorInfo.parse('John Doe <[email protected]> 1627849923 +0200')
#=> #<data Git::AuthorInfo name="John Doe", email="[email protected]", ...>

A string that does not match the expected format

Git::AuthorInfo.parse('garbage')
#=> #<data Git::AuthorInfo name=nil, email=nil, date=nil>

Parameters:

  • author_string (String)

    the raw identity string to parse

Returns:

  • (Git::AuthorInfo)

    the parsed identity; all attributes are nil when the string does not match the expected format



59
60
61
62
63
64
# File 'lib/git/author_info.rb', line 59

def self.parse(author_string)
  match = /(.*?) <(.*?)> (\d+) (.*)/.match(author_string)
  return new(name: nil, email: nil, date: nil) unless match

  new(name: match[1], email: match[2], date: Time.at(match[3].to_i))
end