Module: Git::Parsers::LsRemote Private

Defined in:
lib/git/parsers/ls_remote.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 ls-remote command output

Class Method Summary collapse

Class Method Details

.parse_default_branch(output) ⇒ String

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 ls-remote --symref <repo> HEAD output into a branch name

Parameters:

  • output (String)

    command stdout

Returns:

  • (String)

    the default branch name

Raises:



68
69
70
71
72
73
74
75
76
# File 'lib/git/parsers/ls_remote.rb', line 68

def parse_default_branch(output)
  match_data = output.match(%r{^ref: refs/remotes/[^/]+/(?<default_branch>[^\t]+)\t})
  return match_data[:default_branch] if match_data

  match_data = output.match(%r{^ref: refs/heads/(?<default_branch>[^\t]+)\tHEAD$})
  return match_data[:default_branch] if match_data

  raise Git::UnexpectedResultError, 'Unable to determine the default branch'
end

.parse_line(line) ⇒ Array(String, String|nil, 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 a single git ls-remote output line

Parameters:

  • line (String)

    a single line from ls-remote stdout

Returns:

  • (Array(String, String|nil, Hash))

    [type, name, value] where value is { ref: String, sha: String }

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/git/parsers/ls_remote.rb', line 44

def parse_line(line)
  unless line.include?("\t") && line.match?(/\A[0-9a-f]{4,}\t/)
    raise Git::UnexpectedResultError, "Unexpected ls-remote output line: #{line.inspect}"
  end

  sha, info = line.split("\t", 2)
  ref, type, name = info.split('/', 3)

  type ||= 'head'
  type = 'branches' if type == 'heads'

  value = { ref: ref, sha: sha }

  [type, name, value]
end

.parse_output(lines) ⇒ Hash{String => 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 ls-remote stdout lines into a structured Hash

Parameters:

  • lines (Array<String>)

    the individual stdout lines

Returns:

  • (Hash{String => Hash})

    a map of ref type to ref data

    The structure differs by key:

    • "head" maps directly to { ref: String, sha: String }
    • Other keys (e.g. "branches", "tags") map to { name => { ref: String, sha: String } }


24
25
26
27
28
29
30
31
32
33
# File 'lib/git/parsers/ls_remote.rb', line 24

def parse_output(lines)
  lines.each_with_object(Hash.new { |h, k| h[k] = {} }) do |line, hsh|
    type, name, value = parse_line(line)
    if name
      hsh[type][name] = value
    else
      hsh[type].update(value)
    end
  end
end