Class: Vanagon::Component::Source::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/vanagon/component/source/git.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, workdir:, **options) ⇒ Git

Constructor for the Git source type

Parameters:

  • url (String)

    url of git repo to use as source

  • ref (String)

    ref to checkout from git repo

  • workdir (String)

    working directory to clone into

Raises:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/vanagon/component/source/git.rb', line 76

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 = 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

Instance Attribute Details

#clone_optionsObject

Returns the value of attribute clone_options.



16
17
18
# File 'lib/vanagon/component/source/git.rb', line 16

def clone_options
  @clone_options
end

#log_urlObject

Returns the value of attribute log_url.



16
17
18
# File 'lib/vanagon/component/source/git.rb', line 16

def log_url
  @log_url
end

#refObject

Returns the value of attribute ref.



16
17
18
# File 'lib/vanagon/component/source/git.rb', line 16

def ref
  @ref
end

#repoObject (readonly)

Returns the value of attribute repo.



17
18
19
# File 'lib/vanagon/component/source/git.rb', line 17

def repo
  @repo
end

#urlObject

Returns the value of attribute url.



16
17
18
# File 'lib/vanagon/component/source/git.rb', line 16

def url
  @url
end

#versionObject (readonly)

Use ‘git describe` to lazy-load a version for this component



122
123
124
# File 'lib/vanagon/component/source/git.rb', line 122

def version
  @version
end

#workdirObject

Returns the value of attribute workdir.



16
17
18
# File 'lib/vanagon/component/source/git.rb', line 16

def workdir
  @workdir
end

Class Method Details

.valid_remote?(url, timeout = 0) ⇒ Boolean

def valid_remote?(url, timeout = 0)

Timeout.timeout(timeout) do
  !!::Git.ls_remote(url)
end

rescue ::Git::GitExecuteError

false

rescue Timeout::Error

false

end

Returns:

  • (Boolean)


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/vanagon/component/source/git.rb', line 51

def valid_remote?(url, timeout = 0)
  Timeout.timeout(timeout) do
    Vanagon::Utilities.local_command("git ls-remote --heads #{url} > /dev/null 2>&1")
    return false unless $?.exitstatus.zero?
    return true
  end
rescue Timeout::Error
  return false
rescue RuntimeError
  return false
end

Instance Method Details

#cleanupString

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

Returns:

  • (String)

    command to cleanup the source



104
105
106
# File 'lib/vanagon/component/source/git.rb', line 104

def cleanup
  "rm -rf #{dirname}"
end

#cloneObject

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



128
129
130
131
132
133
134
# File 'lib/vanagon/component/source/git.rb', line 128

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

#dirnameString

The dirname to reference when building from the repo

Returns:

  • (String)

    the directory where the repo was cloned



117
118
119
# File 'lib/vanagon/component/source/git.rb', line 117

def dirname
  @dirname || File.basename(url.path, ".git")
end

#fetchObject

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.



94
95
96
97
98
99
# File 'lib/vanagon/component/source/git.rb', line 94

def fetch
  clone!
  checkout!
  version
  update_submodules
end

#verifyObject

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



109
110
111
112
# File 'lib/vanagon/component/source/git.rb', line 109

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