class Librarian::Source::Git

Constants

DEFAULTS

Attributes

environment[RW]
path[RW]
ref[RW]
repository_cached[RW]
repository_cached?[RW]
sha[RW]
uri[RW]

Public Class Methods

new(environment, uri, options) click to toggle source
# File lib/librarian/source/git.rb, line 29
def initialize(environment, uri, options)
  self.environment = environment
  self.uri = uri
  self.ref = options[:ref] || DEFAULTS[:ref]
  self.sha = options[:sha]
  self.path = options[:path]

  @repository = nil
  @repository_cache_path = nil

  ref.kind_of?(String) or raise TypeError, "ref must be a String"
end

Public Instance Methods

==(other) click to toggle source
# File lib/librarian/source/git.rb, line 46
def ==(other)
  other &&
  self.class  == other.class  &&
  self.uri    == other.uri    &&
  self.ref    == other.ref    &&
  self.path   == other.path   &&
  (self.sha.nil? || other.sha.nil? || self.sha == other.sha)
end
cache!() click to toggle source
# File lib/librarian/source/git.rb, line 76
def cache!
  repository_cached? and return or repository_cached!

  unless repository.git?
    repository.path.rmtree if repository.path.exist?
    repository.path.mkpath
    repository.clone!(uri)
    raise Error, "failed to clone #{uri}" unless repository.git?
  end

  # Probably unnecessary: nobody should be writing to our cache but us.
  # Just a precaution.
  repository_clean_once!

  unless sha
    repository_update_once!
    self.sha = fetch_sha_memo
  end

  unless repository.checked_out?(sha)
    repository_update_once! unless repository.has_commit?(sha)
    repository.checkout!(sha)
    # Probably unnecessary: if git fails to checkout, it should exit
    # nonzero, and we should expect Librarian::Posix::CommandFailure.
    raise Error, "failed to checkout #{sha}" unless repository.checked_out?(sha)
  end
end
git_ops_count() click to toggle source

For tests

# File lib/librarian/source/git.rb, line 105
def git_ops_count
  repository.git_ops_history.size
end
pinned?() click to toggle source
# File lib/librarian/source/git.rb, line 68
def pinned?
  !!sha
end
to_lock_options() click to toggle source
# File lib/librarian/source/git.rb, line 62
def to_lock_options
  options = {:remote => uri, :ref => ref, :sha => sha}
  options.merge!(:path => path) if path
  options
end
to_s() click to toggle source
# File lib/librarian/source/git.rb, line 42
def to_s
  path ? "#{uri}##{ref}(#{path})" : "#{uri}##{ref}"
end
to_spec_args() click to toggle source
# File lib/librarian/source/git.rb, line 55
def to_spec_args
  options = {}
  options.merge!(:ref => ref) if ref != DEFAULTS[:ref]
  options.merge!(:path => path) if path
  [uri, options]
end
unpin!() click to toggle source
# File lib/librarian/source/git.rb, line 72
def unpin!
  @sha = nil
end

Private Instance Methods

cache_key() click to toggle source
# File lib/librarian/source/git.rb, line 157
def cache_key
  @cache_key ||= begin
    uri_part = uri
    ref_part = "##{ref}"
    key_source = [uri_part, ref_part].join
    Digest::MD5.hexdigest(key_source)[0..15]
  end
end
fetch_sha_memo() click to toggle source
# File lib/librarian/source/git.rb, line 150
def fetch_sha_memo
  remote = repository.default_remote
  runtime_cache.memo ['fetch-sha', uri, remote, ref].to_s do
    repository.hash_from(remote, ref)
  end
end
filesystem_path() click to toggle source
# File lib/librarian/source/git.rb, line 130
def filesystem_path
  @filesystem_path ||= path ? repository.path.join(path) : repository.path
end
repository() click to toggle source
# File lib/librarian/source/git.rb, line 124
def repository
  @repository ||= begin
    Repository.new(environment, repository_cache_path)
  end
end
repository_cache_path() click to toggle source
# File lib/librarian/source/git.rb, line 118
def repository_cache_path
  @repository_cache_path ||= begin
    environment.cache_path + "source/git" + cache_key
  end
end
repository_cached!() click to toggle source
# File lib/librarian/source/git.rb, line 114
def repository_cached!
  self.repository_cached = true
end
repository_clean_once!() click to toggle source
# File lib/librarian/source/git.rb, line 134
def repository_clean_once!
  remote = repository.default_remote
  runtime_cache.once ['repository-clean', uri, ref].to_s do
    repository.reset_hard!
    repository.clean!
  end
end
repository_update_once!() click to toggle source
# File lib/librarian/source/git.rb, line 142
def repository_update_once!
  remote = repository.default_remote
  runtime_cache.once ['repository-update', uri, remote, ref].to_s do
    repository.fetch! remote
    repository.fetch! remote, :tags => true
  end
end
runtime_cache() click to toggle source
# File lib/librarian/source/git.rb, line 166
def runtime_cache
  @runtime_cache ||= environment.runtime_cache.keyspace(self.class.name)
end