Class: Metabuild::Git

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

Overview

Git access

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir = nil, path = nil) ⇒ Git

Returns a new instance of Git.



89
90
91
# File 'lib/metabuild.rb', line 89

def initialize(dir = nil, path = nil)
  @path, @dir = path, dir
end

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



87
88
89
# File 'lib/metabuild.rb', line 87

def dir
  @dir
end

#pathObject

Returns the value of attribute path.



87
88
89
# File 'lib/metabuild.rb', line 87

def path
  @path
end

Instance Method Details

#apply_patch(patch) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/metabuild.rb', line 114

def apply_patch(patch)
  inside_git_dir do
    pipe = IO.popen("git apply -")
    pipe.puts patch
    pipe.close_write
  end
end

#cksumObject

id is the SHA1 of head + of any diff (locals)



129
130
131
132
133
# File 'lib/metabuild.rb', line 129

def cksum
  inside_git_dir do
    `git log --pretty=format:'%H' | head -n 1`.to_i(16) ^ Digest::SHA1.hexdigest(get_diff).to_i(16)
  end
end

#cloneObject



122
123
124
125
126
# File 'lib/metabuild.rb', line 122

def clone
  raise "Cannot clone empty path" if @path.nil?
  @dir = pwd + File.basename(@path, ".git") if @dir.nil?
  run("clone #{@path} #{@dir}")
end

#get_diffObject

Like running “git diff” in a git repo



102
103
104
105
106
# File 'lib/metabuild.rb', line 102

def get_diff
  inside_git_dir do
    `git diff` 
  end
end

#inside_git_dirObject



93
94
95
96
97
98
99
# File 'lib/metabuild.rb', line 93

def inside_git_dir
  raise "Directory not set for git repo #{@path}" if @dir.nil? or not File.exists? @dir
  old_dir = pwd
  cd dir
  yield
  cd old_dir
end

#run(cmd) ⇒ Object



108
109
110
111
112
# File 'lib/metabuild.rb', line 108

def run(cmd)
  inside_git_dir do
    raise "Git command #{cmd} failed in  #{@path}" unless system("git #{cmd}") 
  end
end