class Vanagon::Component::Source::Git

Attributes

clone_options[RW]
default_options[R]
log_url[RW]
ref[RW]
repo[R]
url[RW]
version[R]
workdir[RW]

Public Class Methods

github_remote?(url) click to toggle source
# File lib/vanagon/component/source/git.rb, line 56
def github_remote?(url)
  github_source_type(url) == :github_remote
end
github_source_type(url) click to toggle source

VANAGON-227 We need to be careful when guessing whether a github.com/… URL is actually a true git repo. Make some rules around it based on the github API. Decide that anything with a documented media_type is just an http url. We do this instead of talking to GitHub directly to avoid rate limiting. See: docs.github.com/en/repositories/working-with-files/using-files/downloading-source-code-archives docs.github.com/en/rest/repos/contents?apiVersion=2022-11-28#download-a-repository-archive-tar docs.github.com/en/rest/repos/contents?apiVersion=2022-11-28#download-a-repository-archive-zip

# File lib/vanagon/component/source/git.rb, line 68
def github_source_type(url)
  url_directory = url.to_s.delete_prefix(github_url_prefix)
  url_components = url_directory.split('/')

  # Find cases of supported github media types.
  # [ owner, repo, media_type, ref ]
  path_types = ['archive', 'releases', 'tarball', 'zipball']
  if path_types.include?(url_components[2]) ||
     url_components[-1].end_with?('.tar.gz') ||
     url_components[-1].end_with?('.zip')
    :github_media
  else
    :github_remote
  end
end
github_url_prefix() click to toggle source
# File lib/vanagon/component/source/git.rb, line 52
def github_url_prefix
  'https://github.com/'
end
new(url, workdir:, **options) click to toggle source

Constructor for the Git source type

@param url [String] url of git repo to use as source @param ref [String] ref to checkout from git repo @param workdir [String] working directory to clone into

# File lib/vanagon/component/source/git.rb, line 97
def initialize(url, workdir:, **options) # rubocop:disable Metrics/AbcSize
  opts = default_options.merge(options.reject { |k, v| v.nil? })

  # Ensure that #url returns a URI object
  @url = Build::URI.parse(url.to_s)
  @log_url = @url.host + @url.path unless @url.host.nil? || @url.path.nil?
  @ref = opts[:ref]
  @dirname = opts[:dirname]
  @workdir = File.realpath(workdir)
  @clone_options = opts[:clone_options] ||= {}

  # We can test for Repo existence without cloning
  raise Vanagon::InvalidRepo, "\"#{url}\" is not a valid Git repo" unless valid_remote?
end
valid_remote?(url, timeout = 0) click to toggle source

Attempt to connect to whatever URL is provided and return true or false base on a number of guesses of whether it’s a valid Git repo.

@param url [#to_s] A URI::HTTPS, URI:HTTP, or String with the the URL of the

remote git repository.

@param timeout [Number] Time (in seconds) to wait before assuming the

git command has failed. Useful in instances where a URL
prompts for credentials despite not being a git remote

@return [Boolean] whether url is a valid Git repo or not

# File lib/vanagon/component/source/git.rb, line 29
def valid_remote?(url, timeout = 0)
  # RE-15209. To relieve github rate-limiting, if the URL starts with
  # https://github.com/... just accept it rather than ping github over and over.
  return github_remote?(url) if url.to_s.start_with?(github_url_prefix)

  begin
    # [RE-13837] there's a bug in Git.ls_remote that when ssh prints something like
    #  Warning: Permanently added 'github.com,192.30.255.113' (RSA)
    # Git.ls_remote attempts to parse it as actual git output and fails
    # with: NoMethodError: undefined method `split' for nil:NilClass
    #
    # Work around it by calling 'git ls-remote' directly ourselves.
    Timeout.timeout(timeout) do
      Vanagon::Utilities.local_command("git ls-remote --heads #{url} > /dev/null 2>&1")
      $?.exitstatus.zero?
    end
  rescue RuntimeError
    # Either a Timeout::Error or some other execution exception that we'll just call
    # 'invalid'
    false
  end
end

Public Instance Methods

cleanup() click to toggle source

Return the correct incantation to cleanup the source directory for a given source

@return [String] command to cleanup the source

# File lib/vanagon/component/source/git.rb, line 125
def cleanup
  "rm -rf #{dirname}"
end
clone() click to toggle source

Perform a git clone of @url as a lazy-loaded accessor for @clone

# File lib/vanagon/component/source/git.rb, line 149
def clone
  if @clone_options.empty?
    @clone ||= ::Git.clone(url, dirname, path: workdir)
  else
    @clone ||= ::Git.clone(url, dirname, path: workdir, **clone_options)
  end
end
dirname() click to toggle source

The dirname to reference when building from the repo

@return [String] the directory where the repo was cloned

# File lib/vanagon/component/source/git.rb, line 138
def dirname
  @dirname || File.basename(url.path, ".git")
end
fetch() click to toggle source

Fetch the source. In this case, clone the repository into the workdir and check out the ref. Also sets the version if there is a git tag as a side effect.

# File lib/vanagon/component/source/git.rb, line 115
def fetch
  clone!
  checkout!
  version
  update_submodules
end
verify() click to toggle source

There is no md5 to manually verify here, so this is a noop.

# File lib/vanagon/component/source/git.rb, line 130
def verify
  # nothing to do here, so just tell users that and return
  VanagonLogger.info "Nothing to verify for '#{dirname}' (using Git reference '#{ref}')"
end

Private Instance Methods

checkout!() click to toggle source

Checkout desired ref/sha, make noise about it, and fail entirely if we’re unable to checkout that given ref/sha

# File lib/vanagon/component/source/git.rb, line 191
def checkout!
  VanagonLogger.info "Checking out '#{ref}' from Git repo '#{dirname}'"
  clone.checkout(ref)
rescue ::Git::GitExecuteError
  raise Vanagon::CheckoutFailed, "unable to checkout #{ref} from '#{log_url}'"
end
clone!() click to toggle source

Clone a remote repo, make noise about it, and fail entirely if we’re unable to retrieve the remote repo

# File lib/vanagon/component/source/git.rb, line 181
def clone!
  VanagonLogger.info "Cloning Git repo '#{log_url}'"
  VanagonLogger.info "Successfully cloned '#{dirname}'" if clone
rescue ::Git::GitExecuteError
  raise Vanagon::InvalidRepo, "Unable to clone from '#{log_url}'"
end
describe() click to toggle source

Determines a version for the given directory based on the git describe for the repository

@return [String] The version of the directory according to git describe

# File lib/vanagon/component/source/git.rb, line 211
def describe
  clone.describe(ref, tags: true)
rescue ::Git::GitExecuteError
  VanagonLogger.info "Directory '#{dirname}' cannot be versioned by Git. Maybe it hasn't been tagged yet?"
end
refs() click to toggle source

Provide a list of local refs (branches and tags)

# File lib/vanagon/component/source/git.rb, line 174
def refs
  (clone.tags.map(&:name) + clone.branches.map(&:name)).uniq
end
remote_refs() click to toggle source

Provide a list of remote refs (branches and tags)

# File lib/vanagon/component/source/git.rb, line 168
def remote_refs
  (remote['tags'].keys + remote['branches'].keys).uniq
end
update_submodules() click to toggle source

Attempt to update submodules, and do not panic if there are no submodules to initialize

# File lib/vanagon/component/source/git.rb, line 201
def update_submodules
  VanagonLogger.info "Attempting to update submodules for repo '#{dirname}'"
  clone.update_submodules(init: true)
end
valid_remote?() click to toggle source

Attempt to connect to whatever URL is provided and return True or False depending on whether or not ‘git` thinks it’s a valid Git repo.

@return [Boolean] whether url is a valid Git repo or not

# File lib/vanagon/component/source/git.rb, line 162
def valid_remote?
  self.class.valid_remote? url
end