5 class GitExecuteError < StandardError
15 def initialize(base = nil)
16 if base.is_a?(Git::Base)
17 @git_dir = base.repo.path
18 @git_index_file = base.index.path if base.index
19 @git_work_dir = base.dir.path if base.dir
20 elsif base.is_a?(Hash)
21 @git_dir = base[:repository]
22 @git_index_file = base[:index]
23 @git_work_dir = base[:working_directory]
31 # tries to clone the given repo
33 # returns {:repository} (if bare)
34 # {:working_directory} otherwise
37 # :remote - name of remote (rather than 'origin')
38 # :bare - no working directory
40 # TODO - make this work with SSH password or auth_key
42 def clone(repository, name, opts = {})
43 @path = opts[:path] || '.'
44 opts[:path] ? clone_dir = File.join(@path, name) : clone_dir = name
47 arr_opts << "--bare" if opts[:bare]
48 arr_opts << "-o #{opts[:remote]}" if opts[:remote]
49 arr_opts << repository
52 command('clone', arr_opts)
54 opts[:bare] ? {:repository => clone_dir} : {:working_directory => clone_dir}
61 def log_commits(opts = {})
62 arr_opts = ['--pretty=oneline']
63 arr_opts << "-#{opts[:count]}" if opts[:count]
64 arr_opts << "--since=\"#{opts[:since]}\"" if opts[:since].is_a? String
65 arr_opts << "#{opts[:between][0]}..#{opts[:between][1].to_s}" if (opts[:between] && opts[:between].size == 2)
66 arr_opts << opts[:object] if opts[:object].is_a? String
67 arr_opts << '-- ' + opts[:path_limiter] if opts[:path_limiter].is_a? String
69 command_lines('log', arr_opts).map { |l| l.split.first }
73 command('rev-parse', string)
77 command('name-rev', string).split[1]
81 command('cat-file', ['-t', sha])
85 command('cat-file', ['-s', sha]).to_i
88 # returns useful array of raw commit object data
92 hsh = {'message' => '', 'parent' => []}
93 command_lines('cat-file', ['commit', sha.to_s]).each do |line|
95 hsh['message'] += line + "\n"
98 if (line != '') && !in_message
101 value = data.join(' ')
114 def object_contents(sha)
115 command('cat-file', ['-p', sha])
119 data = {'blob' => {}, 'tree' => {}}
120 command_lines('ls-tree', sha.to_s).each do |line|
121 (info, filenm) = line.split("\t")
122 (mode, type, sha) = info.split
123 data[type][filenm] = {:mode => mode, :sha => sha}
130 command_lines('branch', '-a').each do |b|
132 current = true if b[0, 2] == '* '
133 arr << [b.gsub('* ', '').strip, current]
139 branches_all.select { |b| b[1] }.first[0] rescue nil
144 # [tree-ish] = [[line_no, match], [line_no, match2]]
145 # [tree-ish] = [[line_no, match], [line_no, match2]]
146 def grep(string, opts = {})
147 opts[:object] = 'HEAD' if !opts[:object]
150 grep_opts << '-i' if opts[:ignore_case]
151 grep_opts << '-v' if opts[:invert_match]
152 grep_opts << "-e '#{string}'"
153 grep_opts << opts[:object] if opts[:object].is_a?(String)
154 grep_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
156 command_lines('grep', grep_opts).each do |line|
157 if m = /(.*)\:(\d+)\:(.*)/.match(line)
159 hsh[m[1]] << [m[2].to_i, m[3]]
165 def diff_full(obj1 = 'HEAD', obj2 = nil, opts = {})
168 diff_opts << obj2 if obj2.is_a?(String)
169 diff_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
171 command('diff', diff_opts)
174 def diff_stats(obj1 = 'HEAD', obj2 = nil, opts = {})
175 diff_opts = ['--numstat']
177 diff_opts << obj2 if obj2.is_a?(String)
178 diff_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
180 hsh = {:total => {:insertions => 0, :deletions => 0, :lines => 0, :files => 0}, :files => {}}
182 command_lines('diff', diff_opts).each do |file|
183 (insertions, deletions, filename) = file.split("\t")
184 hsh[:total][:insertions] += insertions.to_i
185 hsh[:total][:deletions] += deletions.to_i
186 hsh[:total][:lines] = (hsh[:total][:deletions] + hsh[:total][:insertions])
187 hsh[:total][:files] += 1
188 hsh[:files][filename] = {:insertions => insertions.to_i, :deletions => deletions.to_i}
194 # compares the index and the working directory
197 command_lines('diff-files').each do |line|
198 (info, file) = line.split("\t")
199 (mode_src, mode_dest, sha_src, sha_dest, type) = info.split
200 hsh[file] = {:path => file, :mode_file => mode_src.to_s[1, 7], :mode_index => mode_dest,
201 :sha_file => sha_src, :sha_index => sha_dest, :type => type}
206 # compares the index and the repository
207 def diff_index(treeish)
209 command_lines('diff-index', treeish).each do |line|
210 (info, file) = line.split("\t")
211 (mode_src, mode_dest, sha_src, sha_dest, type) = info.split
212 hsh[file] = {:path => file, :mode_repo => mode_src.to_s[1, 7], :mode_index => mode_dest,
213 :sha_repo => sha_src, :sha_index => sha_dest, :type => type}
220 command_lines('ls-files', '--stage').each do |line|
221 (info, file) = line.split("\t")
222 (mode, sha, stage) = info.split
223 hsh[file] = {:path => file, :mode_index => mode, :sha_index => sha, :stage => stage}
229 def config_remote(name)
231 command_lines('config', ['--get-regexp', "remote.#{name}"]).each do |line|
232 (key, value) = line.split
233 hsh[key.gsub("remote.#{name}.", '')] = value
239 command('config', ['--get', name])
244 command_lines('config', ['--list']).each do |line|
245 (key, value) = line.split('=')
253 def config_set(name, value)
254 command('config', [name, "'#{value}'"])
258 path = path.join(' ') if path.is_a?(Array)
262 def remove(path = '.', opts = {})
263 path = path.join(' ') if path.is_a?(Array)
265 arr_opts = ['-f'] # overrides the up-to-date check by default
266 arr_opts << ['-r'] if opts[:recursive]
269 command('rm', arr_opts)
272 def commit(message, opts = {})
273 arr_opts = ["-m '#{message}'"]
274 arr_opts << '-a' if opts[:add_all]
275 command('commit', arr_opts)
278 def reset(commit, opts = {})
280 arr_opts << '--hard' if opts[:hard]
281 arr_opts << commit.to_s if commit
282 command('reset', arr_opts)
286 def branch_new(branch)
287 command('branch', branch)
290 def branch_delete(branch)
291 command('branch', ['-d', branch])
294 def checkout(branch, opts = {})
296 arr_opts << '-f' if opts[:force]
297 arr_opts << branch.to_s
299 command('checkout', arr_opts)
302 def merge(branch, message = nil)
304 arr_opts << ["-m '#{message}'"] if message
305 arr_opts << branch.to_a.join(' ')
306 command('merge', arr_opts)
309 def remote_add(name, url, opts = {})
311 arr_opts << '-f' if opts[:with_fetch]
315 command('remote', arr_opts)
318 # this is documented as such, but seems broken for some reason
319 # i'll try to get around it some other way later
320 def remote_remove(name)
321 command('remote', ['rm', name])
325 command_lines('remote')
338 command('fetch', remote.to_s)
341 def push(remote, branch = 'master')
342 command('push', [remote.to_s, branch.to_s])
345 def tag_sha(tag_name)
346 command('show-ref', ['--tags', '-s', tag_name])
350 command('repack', ['-a', '-d'])
353 # reads a tree into the current index file
354 def read_tree(treeish, opts = {})
356 arr_opts << "--prefix=#{opts[:prefix]}" if opts[:prefix]
357 arr_opts << treeish.to_a.join(' ')
358 command('read-tree', arr_opts)
362 command('write-tree')
365 def commit_tree(tree, opts = {})
366 opts[:message] = "commit tree #{tree}" if !opts[:message]
367 t = Tempfile.new('commit-message') do |t|
368 t.write(opts[:message])
373 arr_opts << "-p #{opts[:parent]}" if opts[:parent]
374 opts[:parents].each { |p| arr_opts << "-p #{p.to_s}" } if opts[:parents]
375 arr_opts << "< #{t.path}"
376 command('commit-tree', arr_opts)
379 def update_ref(branch, commit)
380 command('update-ref', [branch.to_s, commit.to_s])
383 def checkout_index(opts = {})
385 arr_opts << "--prefix=#{opts[:prefix]}" if opts[:prefix]
386 arr_opts << "--force" if opts[:force]
387 arr_opts << "--all" if opts[:all]
388 arr_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
389 command('checkout-index', arr_opts)
392 # creates an archive file
399 def archive(sha, file = nil, opts = {})
400 opts[:format] = 'zip' if !opts[:format]
402 if opts[:format] == 'tgz'
403 opts[:format] = 'tar'
404 opts[:add_gzip] = true
408 file = Tempfile.new('archive').path
412 arr_opts << "--format=#{opts[:format]}" if opts[:format]
413 arr_opts << "--prefix=#{opts[:prefix]}" if opts[:prefix]
414 arr_opts << "--remote=#{opts[:remote]}" if opts[:remote]
416 arr_opts << opts[:path] if opts[:path]
417 arr_opts << '| gzip' if opts[:add_gzip]
418 arr_opts << "> #{file.to_s}"
419 command('archive', arr_opts)
425 def command_lines(cmd, opts = {})
426 command(cmd, opts).split("\n")
429 def command(cmd, opts = {})
430 ENV['GIT_DIR'] = @git_dir
431 ENV['GIT_INDEX_FILE'] = @git_index_file
432 ENV['GIT_WORK_DIR'] = @git_work_dir
433 path = @git_work_dir || @git_dir || @path
435 opts = opts.to_a.join(' ')
436 git_cmd = "git #{cmd} #{opts}"
437 out = `git #{cmd} #{opts} 2>&1`.chomp
439 #puts "gd: #{@git_work_dir}"
440 #puts "gi: #{@git_index_file}"
446 if $?.exitstatus == 1 && out == ''
449 raise Git::GitExecuteError.new(git_cmd + ':' + out.to_s)