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.



29
30
31
32
33
34
35
36
37
38
39
40
# 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

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



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

def environment
  @environment
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

#refObject

Returns the value of attribute ref.



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

def ref
  @ref
end

#shaObject

Returns the value of attribute sha.



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

def sha
  @sha
end

#uriObject

Returns the value of attribute uri.



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

def uri
  @uri
end

Instance Method Details

#==(other) ⇒ Object



46
47
48
49
50
51
52
53
# 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!Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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_countObject

For tests



105
106
107
# File 'lib/librarian/source/git.rb', line 105

def git_ops_count
  repository.git_ops_history.size
end

#pinned?Boolean

Returns:

  • (Boolean)


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

def pinned?
  !!sha
end

#to_lock_optionsObject



62
63
64
65
66
# 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_sObject



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

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

#to_spec_argsObject



55
56
57
58
59
60
# 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!Object



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

def unpin!
  @sha = nil
end