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.



40
41
42
# File 'lib/kcd/git.rb', line 40

def initialize(repo)
  @repository = repo
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



37
38
39
# File 'lib/kcd/git.rb', line 37

def directory
  @directory
end

#repositoryObject (readonly)

Returns the value of attribute repository.



38
39
40
# File 'lib/kcd/git.rb', line 38

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
30
31
32
33
34
# 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
    potential_path = File.join(path, 'git.exe')
    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



59
60
61
62
63
64
65
66
67
# File 'lib/kcd/git.rb', line 59

def checkout(ref)
  clone

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

  error_check
end

#cleanObject



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

def clean
  FileUtils.rm_rf @directory if @directory
end

#cloneObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/kcd/git.rb', line 44

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



85
86
87
88
89
# File 'lib/kcd/git.rb', line 85

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

#refObject



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kcd/git.rb', line 69

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