3 class GitExecuteError < StandardError
13 def initialize(base = nil)
14 if base.is_a?(Git::Base)
15 @git_dir = base.repo.path
16 @git_index_file = base.index.path
17 @git_work_dir = base.dir.path
18 elsif base.is_a?(Hash)
19 @git_dir = base[:repository]
20 @git_index_file = base[:index]
21 @git_work_dir = base[:working_directory]
29 # tries to clone the given repo
31 # returns {:repository} (if bare)
32 # {:working_directory} otherwise
35 # :remote - name of remote (rather than 'origin')
36 # :bare - no working directory
38 # TODO - make this work with SSH password or auth_key
40 def clone(repository, name, opts = {})
41 @path = opts[:path] || '.'
42 opts[:path] ? clone_dir = File.join(@path, name) : clone_dir = name
45 arr_opts << "--bare" if opts[:bare]
46 arr_opts << "-o #{opts[:remote]}" if opts[:remote]
47 arr_opts << repository
50 command('clone', arr_opts)
52 opts[:bare] ? {:repository => clone_dir} : {:working_directory => clone_dir}
59 def log_commits(opts = {})
60 arr_opts = ['--pretty=oneline']
61 arr_opts << "-#{opts[:count]}" if opts[:count]
62 arr_opts << "--since=\"#{opts[:since]}\"" if opts[:since].is_a? String
63 arr_opts << "#{opts[:between][0]}..#{opts[:between][1].to_s}" if (opts[:between] && opts[:between].size == 2)
64 arr_opts << opts[:object] if opts[:object].is_a? String
65 arr_opts << '-- ' + opts[:path_limiter] if opts[:path_limiter].is_a? String
67 command_lines('log', arr_opts).map { |l| l.split.first }
71 command('rev-parse', string)
75 command('cat-file', ['-t', sha])
79 command('cat-file', ['-s', sha]).to_i
82 # returns useful array of raw commit object data
86 hsh = {'message' => '', 'parent' => []}
87 command_lines('cat-file', ['commit', sha.to_s]).each do |line|
89 hsh['message'] += line + "\n"
92 if (line != '') && !in_message
95 value = data.join(' ')
108 def object_contents(sha)
109 command('cat-file', ['-p', sha])
114 command_lines('branch', '-a').each do |b|
116 current = true if b[0, 2] == '* '
117 arr << [b.gsub('* ', '').strip, current]
123 branches_all.select { |b| b[1] }.first[0] rescue nil
128 # [tree-ish] = [[line_no, match], [line_no, match2]]
129 # [tree-ish] = [[line_no, match], [line_no, match2]]
130 def grep(string, opts = {})
131 opts[:object] = 'HEAD' if !opts[:object]
134 grep_opts << '-i' if opts[:ignore_case]
135 grep_opts << '-v' if opts[:invert_match]
136 grep_opts << "-e '#{string}'"
137 grep_opts << opts[:object] if opts[:object].is_a?(String)
138 grep_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
140 command_lines('grep', grep_opts).each do |line|
141 if m = /(.*)\:(\d+)\:(.*)/.match(line)
143 hsh[m[1]] << [m[2].to_i, m[3]]
149 def diff_full(obj1 = 'HEAD', obj2 = nil, opts = {})
152 diff_opts << obj2 if obj2.is_a?(String)
153 diff_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
155 command('diff', diff_opts)
158 def diff_stats(obj1 = 'HEAD', obj2 = nil, opts = {})
159 diff_opts = ['--numstat']
161 diff_opts << obj2 if obj2.is_a?(String)
162 diff_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
164 hsh = {:total => {:insertions => 0, :deletions => 0, :lines => 0, :files => 0}, :files => {}}
166 command_lines('diff', diff_opts).each do |file|
167 (insertions, deletions, filename) = file.split("\t")
168 hsh[:total][:insertions] += insertions.to_i
169 hsh[:total][:deletions] += deletions.to_i
170 hsh[:total][:lines] = (hsh[:total][:deletions] + hsh[:total][:insertions])
171 hsh[:total][:files] += 1
172 hsh[:files][filename] = {:insertions => insertions.to_i, :deletions => deletions.to_i}
178 # compares the index and the working directory
181 command_lines('diff-files').each do |line|
182 (info, file) = line.split("\t")
183 (mode_src, mode_dest, sha_src, sha_dest, type) = info.split
184 hsh[file] = {:path => file, :mode_file => mode_src.to_s[1, 7], :mode_index => mode_dest,
185 :sha_file => sha_src, :sha_index => sha_dest, :type => type}
190 # compares the index and the repository
191 def diff_index(treeish)
193 command_lines('diff-index', treeish).each do |line|
194 (info, file) = line.split("\t")
195 (mode_src, mode_dest, sha_src, sha_dest, type) = info.split
196 hsh[file] = {:path => file, :mode_repo => mode_src.to_s[1, 7], :mode_index => mode_dest,
197 :sha_repo => sha_src, :sha_index => sha_dest, :type => type}
204 command_lines('ls-files', '--stage').each do |line|
205 (info, file) = line.split("\t")
206 (mode, sha, stage) = info.split
207 hsh[file] = {:path => file, :mode_index => mode, :sha_index => sha, :stage => stage}
213 def config_remote(name)
215 command_lines('config', ['--get-regexp', "remote.#{name}"]).each do |line|
216 (key, value) = line.split
217 hsh[key.gsub("remote.#{name}.", '')] = value
223 command('config', ['--get', name])
228 command_lines('config', ['--list']).each do |line|
229 (key, value) = line.split('=')
237 def config_set(name, value)
238 command('config', [name, "'#{value}'"])
242 path = path.join(' ') if path.is_a?(Array)
246 def remove(path = '.', opts = {})
247 path = path.join(' ') if path.is_a?(Array)
249 arr_opts = ['-f'] # overrides the up-to-date check by default
250 arr_opts << ['-r'] if opts[:recursive]
253 command('rm', arr_opts)
256 def commit(message, opts = {})
257 arr_opts = ["-m '#{message}'"]
258 arr_opts << '-a' if opts[:add_all]
259 command('commit', arr_opts)
262 def reset(commit, opts = {})
264 arr_opts << '--hard' if opts[:hard]
265 arr_opts << commit.to_s if commit
266 command('reset', arr_opts)
270 def branch_new(branch)
271 command('branch', branch)
274 def branch_delete(branch)
275 command('branch', ['-d', branch])
278 def checkout(branch, opts = {})
280 arr_opts << '-f' if opts[:force]
281 arr_opts << branch.to_s
283 command('checkout', arr_opts)
286 def merge(branch, message = nil)
288 arr_opts << ["-m '#{message}'"] if message
289 arr_opts << branch.to_a.join(' ')
290 command('merge', arr_opts)
293 def remote_add(name, url, opts = {})
295 arr_opts << '-f' if opts[:with_fetch]
299 command('remote', arr_opts)
302 # this is documented as such, but seems broken for some reason
303 # i'll try to get around it some other way later
304 def remote_remove(name)
305 command('remote', ['rm', name])
309 command_lines('remote')
322 command('fetch', remote.to_s)
325 def push(remote, branch = 'master')
326 command('push', [remote.to_s, branch.to_s])
329 def tag_sha(tag_name)
330 command('show-ref', ['--tags', '-s', tag_name])
334 command('repack', ['-a', '-d'])
339 def command_lines(cmd, opts = {})
340 command(cmd, opts).split("\n")
343 def command(cmd, opts = {})
344 ENV['GIT_DIR'] = @git_dir
345 ENV['GIT_INDEX_FILE'] = @git_index_file
346 ENV['GIT_WORK_DIR'] = @git_work_dir
347 path = @git_work_dir || @git_dir || @path
349 opts = opts.to_a.join(' ')
350 git_cmd = "git #{cmd} #{opts}"
351 out = `git #{cmd} #{opts} 2>&1`.chomp
353 #puts "gd: #{@git_work_dir}"
354 #puts "gi: #{@git_index_file}"
360 if $?.exitstatus == 1 && out == ''
363 raise Git::GitExecuteError.new(git_cmd + ':' + out.to_s)