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:



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

def initialize(url, workdir:, **options)
  opts = default_options.merge(options.reject { |k, v| v.nil? })

  # Ensure that #url returns a URI object
  @url = URI.parse(url.to_s)
  @ref = opts[:ref]
  @workdir = File.realpath(workdir)

  # We can test for Repo existence without cloning
  raise Vanagon::InvalidRepo, "#{url} not a valid Git repo" unless valid_remote?
end

Instance Attribute Details

#refObject

Returns the value of attribute ref.



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

def ref
  @ref
end

#repoObject (readonly)

Returns the value of attribute repo.



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

def repo
  @repo
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

#versionObject (readonly)

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



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

def version
  @version
end

#workdirObject

Returns the value of attribute workdir.



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

def workdir
  @workdir
end

Class Method Details

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

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.

Parameters:

  • url
  • timeout (defaults to: 0)

    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

Returns:

  • (Boolean)

    whether #url is a valid Git repo or not



28
29
30
31
32
33
34
35
36
# File 'lib/vanagon/component/source/git.rb', line 28

def valid_remote?(url, timeout = 0)
  Timeout.timeout(timeout) do
    !!::Git.ls_remote(url)
  end
rescue ::Git::GitExecuteError
  false
rescue Timeout::Error
  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



76
77
78
# File 'lib/vanagon/component/source/git.rb', line 76

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

#cloneObject

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



100
101
102
# File 'lib/vanagon/component/source/git.rb', line 100

def clone
  @clone ||= ::Git.clone(url, dirname, path: workdir)
end

#dirnameString

The dirname to reference when building from the repo

Returns:

  • (String)

    the directory where the repo was cloned



89
90
91
# File 'lib/vanagon/component/source/git.rb', line 89

def 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.



66
67
68
69
70
71
# File 'lib/vanagon/component/source/git.rb', line 66

def fetch
  clone!
  checkout!
  version
  update_submodules
end

#verifyObject

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



81
82
83
84
# File 'lib/vanagon/component/source/git.rb', line 81

def verify
  # nothing to do here, so just tell users that and return
  warn "Nothing to verify for '#{dirname}' (using Git reference '#{ref}')"
end