Class: Librarian::Source::Git

Inherits:
Object
  • Object
show all
Includes:
BasicApi, Local
Defined in:
lib/librarian/source/git.rb,
lib/librarian/source/git/repository.rb

Defined Under Namespace

Classes: Repository

Constant Summary collapse

DEFAULTS =
{
  :ref => 'master'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Local

#found_path, #manifest_search_paths, #manifests

Methods included from Librarian::Support::AbstractMethod

included

Methods included from BasicApi

included

Constructor Details

#initialize(environment, uri, options) ⇒ Git

Returns a new instance of Git.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/librarian/source/git.rb', line 28

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

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



22
23
24
# File 'lib/librarian/source/git.rb', line 22

def environment
  @environment
end

#pathObject

Returns the value of attribute path.



25
26
27
# File 'lib/librarian/source/git.rb', line 25

def path
  @path
end

#refObject

Returns the value of attribute ref.



25
26
27
# File 'lib/librarian/source/git.rb', line 25

def ref
  @ref
end

#shaObject

Returns the value of attribute sha.



25
26
27
# File 'lib/librarian/source/git.rb', line 25

def sha
  @sha
end

#uriObject

Returns the value of attribute uri.



25
26
27
# File 'lib/librarian/source/git.rb', line 25

def uri
  @uri
end

Instance Method Details

#==(other) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/librarian/source/git.rb', line 45

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!Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/librarian/source/git.rb', line 75

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)
  end
  repository.reset_hard!
  repository.clean!
  unless repository.checked_out?(sha)
    remote = repository.default_remote
    repository.fetch!(remote)
    repository.fetch!(remote, :tags => true)

    self.sha = repository.hash_from(remote, ref) unless sha
    repository.checkout!(sha) unless repository.checked_out?(sha)

    raise Error, "failed to checkout #{sha}" unless repository.checked_out?(sha)
  end
end

#pinned?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/librarian/source/git.rb', line 67

def pinned?
  !!sha
end

#to_lock_optionsObject



61
62
63
64
65
# File 'lib/librarian/source/git.rb', line 61

def to_lock_options
  options = {:remote => uri, :ref => ref, :sha => sha}
  options.merge!(:path => path) if path
  options
end

#to_sObject



41
42
43
# File 'lib/librarian/source/git.rb', line 41

def to_s
  path ? "#{uri}##{ref}(#{path})" : "#{uri}##{ref}"
end

#to_spec_argsObject



54
55
56
57
58
59
# File 'lib/librarian/source/git.rb', line 54

def to_spec_args
  options = {}
  options.merge!(:ref => ref) if ref != DEFAULTS[:ref]
  options.merge!(:path => path) if path
  [uri, options]
end

#unpin!Object



71
72
73
# File 'lib/librarian/source/git.rb', line 71

def unpin!
  @sha = nil
end