Class: Fetchers::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/fetchers/git.rb

Overview

The git fetcher uses the git binary to fetch remote git sources. Git-based sources should be specified with the ‘git:` key in the source hash. Additionally, we accept `:branch`, `:ref`, and `:tag` keys to allow users to pin to a particular revision.

Parts of this class are derived from:

https://github.com/chef/omnibus/blob/master/lib/omnibus/fetchers/git_fetcher.rb

which is Copyright 2012-2014 Chef Software, Inc. and offered under the same Apache 2 software license as inspec.

Many thanks to the omnibus authors!

Note that we haven’t replicated all of omnibus’ features here. If you got to this file during debugging, you may want to look at the omnibus source for hints.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remote_url, opts = {}) ⇒ Git

Returns a new instance of Git.



39
40
41
42
43
44
45
# File 'lib/fetchers/git.rb', line 39

def initialize(remote_url, opts = {})
  @branch = opts[:branch]
  @tag = opts[:tag]
  @ref = opts[:ref]
  @remote_url = remote_url
  @repo_directory = nil
end

Class Method Details

.resolve(target, opts = {}) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/fetchers/git.rb', line 31

def self.resolve(target, opts = {})
  if target.is_a?(String)
    new(target, opts) if target.start_with?('git@') || target.end_with?('.git')
  elsif target.respond_to?(:has_key?) && target.key?(:git)
    new(target[:git], opts.merge(target))
  end
end

Instance Method Details

#archive_pathObject



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

def archive_path
  @repo_directory
end

#cache_keyObject



63
64
65
# File 'lib/fetchers/git.rb', line 63

def cache_key
  resolved_ref
end

#fetch(dir) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fetchers/git.rb', line 47

def fetch(dir)
  @repo_directory = dir
  FileUtils.mkdir_p(dir) unless Dir.exist?(dir)

  if cloned?
    checkout
  else
    Dir.mktmpdir do |tmpdir|
      checkout(tmpdir)
      Inspec::Log.debug("Checkout of #{resolved_ref} successful. Moving checkout to #{dir}")
      FileUtils.cp_r(tmpdir + '/.', @repo_directory)
    end
  end
  @repo_directory
end

#resolved_sourceObject



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

def resolved_source
  { git: @remote_url, ref: resolved_ref }
end