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].to_s}..#{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, true).map { |l| l.split.first }
72 def full_log_commits(opts = {})
73 arr_opts = ['--pretty=raw']
74 arr_opts << "-#{opts[:count]}" if opts[:count]
75 arr_opts << "--since=\"#{opts[:since]}\"" if opts[:since].is_a? String
76 arr_opts << "#{opts[:between][0].to_s}..#{opts[:between][1].to_s}" if (opts[:between] && opts[:between].size == 2)
77 arr_opts << opts[:object] if opts[:object].is_a? String
78 arr_opts << '-- ' + opts[:path_limiter] if opts[:path_limiter].is_a? String
80 full_log = command_lines('log', arr_opts, true)
81 process_commit_data(full_log)
85 command('rev-parse', string)
89 command('name-rev', string).split[1]
93 command('cat-file', ['-t', sha])
97 command('cat-file', ['-s', sha]).to_i
100 # returns useful array of raw commit object data
103 cdata = command_lines('cat-file', ['commit', sha])
104 process_commit_data(cdata, sha)
107 def process_commit_data(data, sha = nil)
111 hsh = {'sha' => sha, 'message' => '', 'parent' => []}
117 if in_message && line != ''
118 hsh['message'] += line + "\n"
121 if (line != '') && !in_message
124 value = data.join(' ')
127 hsh_array << hsh if hsh
128 hsh = {'sha' => sha, 'message' => '', 'parent' => []}
135 elsif in_message && line == ''
143 hsh_array << hsh if hsh
150 def object_contents(sha)
151 command('cat-file', ['-p', sha])
155 data = {'blob' => {}, 'tree' => {}}
156 command_lines('ls-tree', sha.to_s).each do |line|
157 (info, filenm) = line.split("\t")
158 (mode, type, sha) = info.split
159 data[type][filenm] = {:mode => mode, :sha => sha}
166 command_lines('branch', '-a').each do |b|
168 current = true if b[0, 2] == '* '
169 arr << [b.gsub('* ', '').strip, current]
175 branches_all.select { |b| b[1] }.first[0] rescue nil
180 # [tree-ish] = [[line_no, match], [line_no, match2]]
181 # [tree-ish] = [[line_no, match], [line_no, match2]]
182 def grep(string, opts = {})
183 opts[:object] = 'HEAD' if !opts[:object]
186 grep_opts << '-i' if opts[:ignore_case]
187 grep_opts << '-v' if opts[:invert_match]
188 grep_opts << "-e '#{string}'"
189 grep_opts << opts[:object] if opts[:object].is_a?(String)
190 grep_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
192 command_lines('grep', grep_opts).each do |line|
193 if m = /(.*)\:(\d+)\:(.*)/.match(line)
195 hsh[m[1]] << [m[2].to_i, m[3]]
201 def diff_full(obj1 = 'HEAD', obj2 = nil, opts = {})
204 diff_opts << obj2 if obj2.is_a?(String)
205 diff_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
207 command('diff', diff_opts)
210 def diff_stats(obj1 = 'HEAD', obj2 = nil, opts = {})
211 diff_opts = ['--numstat']
213 diff_opts << obj2 if obj2.is_a?(String)
214 diff_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
216 hsh = {:total => {:insertions => 0, :deletions => 0, :lines => 0, :files => 0}, :files => {}}
218 command_lines('diff', diff_opts).each do |file|
219 (insertions, deletions, filename) = file.split("\t")
220 hsh[:total][:insertions] += insertions.to_i
221 hsh[:total][:deletions] += deletions.to_i
222 hsh[:total][:lines] = (hsh[:total][:deletions] + hsh[:total][:insertions])
223 hsh[:total][:files] += 1
224 hsh[:files][filename] = {:insertions => insertions.to_i, :deletions => deletions.to_i}
230 # compares the index and the working directory
233 command_lines('diff-files').each do |line|
234 (info, file) = line.split("\t")
235 (mode_src, mode_dest, sha_src, sha_dest, type) = info.split
236 hsh[file] = {:path => file, :mode_file => mode_src.to_s[1, 7], :mode_index => mode_dest,
237 :sha_file => sha_src, :sha_index => sha_dest, :type => type}
242 # compares the index and the repository
243 def diff_index(treeish)
245 command_lines('diff-index', treeish).each do |line|
246 (info, file) = line.split("\t")
247 (mode_src, mode_dest, sha_src, sha_dest, type) = info.split
248 hsh[file] = {:path => file, :mode_repo => mode_src.to_s[1, 7], :mode_index => mode_dest,
249 :sha_repo => sha_src, :sha_index => sha_dest, :type => type}
256 command_lines('ls-files', '--stage').each do |line|
257 (info, file) = line.split("\t")
258 (mode, sha, stage) = info.split
259 hsh[file] = {:path => file, :mode_index => mode, :sha_index => sha, :stage => stage}
265 def config_remote(name)
267 command_lines('config', ['--get-regexp', "remote.#{name}"]).each do |line|
268 (key, value) = line.split
269 hsh[key.gsub("remote.#{name}.", '')] = value
275 command('config', ['--get', name])
280 command_lines('config', ['--list']).each do |line|
281 (key, value) = line.split('=')
289 def config_set(name, value)
290 command('config', [name, "'#{value}'"])
294 path = path.join(' ') if path.is_a?(Array)
298 def remove(path = '.', opts = {})
299 path = path.join(' ') if path.is_a?(Array)
301 arr_opts = ['-f'] # overrides the up-to-date check by default
302 arr_opts << ['-r'] if opts[:recursive]
305 command('rm', arr_opts)
308 def commit(message, opts = {})
309 arr_opts = ["-m '#{message}'"]
310 arr_opts << '-a' if opts[:add_all]
311 command('commit', arr_opts)
314 def reset(commit, opts = {})
316 arr_opts << '--hard' if opts[:hard]
317 arr_opts << commit.to_s if commit
318 command('reset', arr_opts)
322 def branch_new(branch)
323 command('branch', branch)
326 def branch_delete(branch)
327 command('branch', ['-d', branch])
330 def checkout(branch, opts = {})
332 arr_opts << '-f' if opts[:force]
333 arr_opts << branch.to_s
335 command('checkout', arr_opts)
338 def merge(branch, message = nil)
340 arr_opts << ["-m '#{message}'"] if message
341 arr_opts << branch.to_a.join(' ')
342 command('merge', arr_opts)
345 def remote_add(name, url, opts = {})
347 arr_opts << '-f' if opts[:with_fetch]
351 command('remote', arr_opts)
354 # this is documented as such, but seems broken for some reason
355 # i'll try to get around it some other way later
356 def remote_remove(name)
357 command('remote', ['rm', name])
361 command_lines('remote')
374 command('fetch', remote.to_s)
377 def push(remote, branch = 'master')
378 command('push', [remote.to_s, branch.to_s])
381 def tag_sha(tag_name)
382 command('show-ref', ['--tags', '-s', tag_name])
386 command('repack', ['-a', '-d'])
389 # reads a tree into the current index file
390 def read_tree(treeish, opts = {})
392 arr_opts << "--prefix=#{opts[:prefix]}" if opts[:prefix]
393 arr_opts << treeish.to_a.join(' ')
394 command('read-tree', arr_opts)
398 command('write-tree')
401 def commit_tree(tree, opts = {})
402 opts[:message] = "commit tree #{tree}" if !opts[:message]
403 t = Tempfile.new('commit-message') do |t|
404 t.write(opts[:message])
409 arr_opts << "-p #{opts[:parent]}" if opts[:parent]
410 opts[:parents].each { |p| arr_opts << "-p #{p.to_s}" } if opts[:parents]
411 arr_opts << "< #{t.path}"
412 command('commit-tree', arr_opts)
415 def update_ref(branch, commit)
416 command('update-ref', [branch.to_s, commit.to_s])
419 def checkout_index(opts = {})
421 arr_opts << "--prefix=#{opts[:prefix]}" if opts[:prefix]
422 arr_opts << "--force" if opts[:force]
423 arr_opts << "--all" if opts[:all]
424 arr_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
425 command('checkout-index', arr_opts)
428 # creates an archive file
435 def archive(sha, file = nil, opts = {})
436 opts[:format] = 'zip' if !opts[:format]
438 if opts[:format] == 'tgz'
439 opts[:format] = 'tar'
440 opts[:add_gzip] = true
444 file = Tempfile.new('archive').path
448 arr_opts << "--format=#{opts[:format]}" if opts[:format]
449 arr_opts << "--prefix=#{opts[:prefix]}" if opts[:prefix]
450 arr_opts << "--remote=#{opts[:remote]}" if opts[:remote]
452 arr_opts << opts[:path] if opts[:path]
453 arr_opts << '| gzip' if opts[:add_gzip]
454 arr_opts << "> #{file.to_s}"
455 command('archive', arr_opts)
461 def command_lines(cmd, opts = {}, chdir = true)
462 command(cmd, opts, chdir).split("\n")
465 def command(cmd, opts = {}, chdir = true)
466 ENV['GIT_DIR'] = @git_dir if (@git_dir != ENV['GIT_DIR'])
467 ENV['GIT_INDEX_FILE'] = @git_index_file if (@git_index_file != ENV['GIT_INDEX_FILE'])
468 ENV['GIT_WORK_TREE'] = @git_work_dir if (@git_work_dir != ENV['GIT_WORK_TREE'])
469 path = @git_work_dir || @git_dir || @path
471 opts = opts.to_a.join(' ')
472 git_cmd = "git #{cmd} #{opts}"
475 if chdir && (Dir.getwd != path)
476 Dir.chdir(path) { out = `git #{cmd} #{opts} 2>&1`.chomp }
478 out = `git #{cmd} #{opts} 2>&1`.chomp
486 if $?.exitstatus == 1 && out == ''
489 raise Git::GitExecuteError.new(git_cmd + ':' + out.to_s)