Class: KnifeCookbookDependencies::Git

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo) ⇒ Git

Returns a new instance of Git.



35
36
37
# File 'lib/kcd/git.rb', line 35

def initialize(repo)
  @repository = repo
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



32
33
34
# File 'lib/kcd/git.rb', line 32

def directory
  @directory
end

#repositoryObject (readonly)

Returns the value of attribute repository.



33
34
35
# File 'lib/kcd/git.rb', line 33

def repository
  @repository
end

Class Method Details

.find_gitObject

This is to defeat aliases/shell functions called ‘git’ and a number of other problems.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kcd/git.rb', line 14

def find_git
  git_path = nil
  ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
    potential_path = File.join(path, 'git')
    if File.executable?(potential_path)
      git_path = potential_path
      break
    end
  end

  unless git_path
    raise "Could not find git. Please ensure it is in your path."
  end

  return git_path
end

.gitObject



6
7
8
# File 'lib/kcd/git.rb', line 6

def git
  @git ||= find_git
end

Instance Method Details

#checkout(ref) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/kcd/git.rb', line 54

def checkout(ref)
  clone

  Dir.chdir @directory do
    system(self.class.git, "checkout", "-q", ref)
  end

  error_check
end

#cleanObject



76
77
78
# File 'lib/kcd/git.rb', line 76

def clean
  FileUtils.rm_rf @directory if @directory
end

#cloneObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kcd/git.rb', line 39

def clone
  # XXX not sure how resilient this is, maybe a fetch/merge strategy would be better.
  if @directory
    Dir.chdir @directory do
      system(self.class.git, "pull")
    end
  else
    @directory = Dir.mktmpdir
    system(self.class.git, "clone", @repository, @directory)
  end

  error_check

end

#error_checkObject



80
81
82
83
84
# File 'lib/kcd/git.rb', line 80

def error_check
  if $?.exitstatus != 0
    raise "Did not succeed executing git; check the output above."
  end
end

#refObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/kcd/git.rb', line 64

def ref
  return nil unless @directory

  this_ref = nil

  Dir.chdir @directory do
    this_ref = `#{self.class.git} rev-parse HEAD`.strip
  end

  return this_ref
end