Module: Git::Parsers::CatFile Private

Defined in:
lib/git/parsers/cat_file.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Parser for git cat-file commit and tag output

Provides class methods that transform raw git cat-file output lines into structured Hash objects consumed by the Git::Repository::ObjectOperations facade.

Constant Summary collapse

CAT_FILE_HEADER_LINE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Matches a single git cat-file header line

/\A(?<key>\w+) (?<value>.*)\z/

Class Method Summary collapse

Class Method Details

.each_header(lines) {|key, value| ... }

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Yields parsed header key/value pairs from git cat-file output lines

Consumes header lines from the front of lines until a blank line is encountered. Continuation lines that begin with a space are folded into the previous header value using newline separators.

Parameters:

  • lines (Array<String>)

    mutable output lines from a cat-file response

Yields:

  • (key, value)

    each parsed header pair

Yield Parameters:

  • key (String)

    header field name

  • value (String)

    unfolded header value text

Yield Returns:

  • (void)


101
102
103
104
105
106
107
108
# File 'lib/git/parsers/cat_file.rb', line 101

def each_header(lines)
  while (line = lines.shift) && (match = CAT_FILE_HEADER_LINE.match(line))
    key = match[:key]
    value_lines = [match[:value]]
    value_lines << lines.shift.lstrip while lines.first&.start_with?(' ')
    yield key, value_lines.join("\n")
  end
end

.parse_commit(lines, sha) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse git cat-file commit output into a structured Hash

Parameters:

  • lines (Array<String>)

    mutable cat-file output lines, consumed in place during header parsing

  • sha (String)

    the object name passed by the caller

Returns:

  • (Hash)

    commit data hash with string keys



33
34
35
36
37
# File 'lib/git/parsers/cat_file.rb', line 33

def parse_commit(lines, sha)
  headers = parse_commit_headers(lines)
  message = "#{lines.join("\n")}\n"
  { 'sha' => sha, 'message' => message }.merge(headers)
end

.parse_commit_headers(lines) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Extracts and returns commit headers from the front of lines

Mutates lines in place, consuming header lines and the blank separator line. After the call lines contains only message lines.

Parameters:

  • lines (Array<String>)

    mutable cat-file output lines

Returns:

  • (Hash)

    parsed header key/value pairs; parent is always an Array



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/git/parsers/cat_file.rb', line 69

def parse_commit_headers(lines)
  headers = { 'parent' => [] }
  each_header(lines) do |key, value|
    if key == 'parent'
      headers['parent'] << value
    else
      headers[key] = value
    end
  end
  headers
end

.parse_tag(lines, name) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse git cat-file tag output into a structured Hash

Parameters:

  • lines (Array<String>)

    mutable cat-file output lines, consumed in place during header parsing; remaining lines become the message

  • name (String)

    the tag name passed by the caller

Returns:

  • (Hash)

    tag data hash with string keys



50
51
52
53
54
55
# File 'lib/git/parsers/cat_file.rb', line 50

def parse_tag(lines, name)
  hsh = { 'name' => name }
  each_header(lines) { |key, value| hsh[key] = value }
  hsh['message'] = "#{lines.join("\n")}\n"
  hsh
end