Method: PDK::Util::Git.ls_remote

Defined in:
lib/pdk/util/git.rb

.ls_remote(repo, ref) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/pdk/util/git.rb', line 108

def self.ls_remote(repo, ref)
  repo = "file://#{repo}" if PDK::Util::Filesystem.directory?(repo)

  output = git('ls-remote', '--refs', repo, ref)

  unless output[:exit_code].zero?
    PDK.logger.error output[:stdout]
    PDK.logger.error output[:stderr]
    raise PDK::CLI::ExitWithError, format('Unable to access the template repository "%{repository}"', repository: repo)
  end

  matching_refs = output[:stdout].split(/\r?\n/).map { |r| r.split("\t") }
  matching_ref = matching_refs.find { |_sha, remote_ref| ["refs/tags/#{ref}", "refs/remotes/origin/#{ref}", "refs/heads/#{ref}"].include?(remote_ref) }
  raise PDK::CLI::ExitWithError, format('Unable to find a branch or tag named "%{ref}" in %{repo}', ref: ref, repo: repo) if matching_ref.nil?

  matching_ref.first
end