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 def object_contents(sha)
83 command('cat-file', ['-p', sha])
88 command_lines('branch', '-a').each do |b|
90 current = true if b[0, 2] == '* '
91 arr << [b.gsub('* ', '').strip, current]
97 branches_all.select { |b| b[1] }.first[0] rescue nil
102 # [tree-ish] = [[line_no, match], [line_no, match2]]
103 # [tree-ish] = [[line_no, match], [line_no, match2]]
104 def grep(string, opts = {})
105 opts[:object] = 'HEAD' if !opts[:object]
108 grep_opts << '-i' if opts[:ignore_case]
109 grep_opts << '-v' if opts[:invert_match]
110 grep_opts << "-e '#{string}'"
111 grep_opts << opts[:object] if opts[:object].is_a?(String)
112 grep_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
114 command_lines('grep', grep_opts).each do |line|
115 if m = /(.*)\:(\d+)\:(.*)/.match(line)
117 hsh[m[1]] << [m[2].to_i, m[3]]
123 def diff_full(obj1 = 'HEAD', obj2 = nil, opts = {})
126 diff_opts << obj2 if obj2.is_a?(String)
127 diff_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
129 command('diff', diff_opts)
132 def diff_stats(obj1 = 'HEAD', obj2 = nil, opts = {})
133 diff_opts = ['--numstat']
135 diff_opts << obj2 if obj2.is_a?(String)
136 diff_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
138 hsh = {:total => {:insertions => 0, :deletions => 0, :lines => 0, :files => 0}, :files => {}}
140 command_lines('diff', diff_opts).each do |file|
141 (insertions, deletions, filename) = file.split("\t")
142 hsh[:total][:insertions] += insertions.to_i
143 hsh[:total][:deletions] += deletions.to_i
144 hsh[:total][:lines] = (hsh[:total][:deletions] + hsh[:total][:insertions])
145 hsh[:total][:files] += 1
146 hsh[:files][filename] = {:insertions => insertions.to_i, :deletions => deletions.to_i}
152 # compares the index and the working directory
155 command_lines('diff-files').each do |line|
156 (info, file) = line.split("\t")
157 (mode_src, mode_dest, sha_src, sha_dest, type) = info.split
158 hsh[file] = {:path => file, :mode_file => mode_src.to_s[1, 7], :mode_index => mode_dest,
159 :sha_file => sha_src, :sha_index => sha_dest, :type => type}
164 # compares the index and the repository
165 def diff_index(treeish)
167 command_lines('diff-index', treeish).each do |line|
168 (info, file) = line.split("\t")
169 (mode_src, mode_dest, sha_src, sha_dest, type) = info.split
170 hsh[file] = {:path => file, :mode_repo => mode_src.to_s[1, 7], :mode_index => mode_dest,
171 :sha_repo => sha_src, :sha_index => sha_dest, :type => type}
178 command_lines('ls-files', '--stage').each do |line|
179 (info, file) = line.split("\t")
180 (mode, sha, stage) = info.split
181 hsh[file] = {:path => file, :mode_index => mode, :sha_index => sha, :stage => stage}
187 def config_remote(name)
189 command_lines('config', ['--get-regexp', "remote.#{name}"]).each do |line|
190 (key, value) = line.split
191 hsh[key.gsub("remote.#{name}.", '')] = value
197 command('config', ['--get', name])
202 command_lines('config', ['--list']).each do |line|
203 (key, value) = line.split('=')
211 def config_set(name, value)
212 command('config', [name, "'#{value}'"])
216 path = path.join(' ') if path.is_a?(Array)
220 def remove(path = '.', opts = {})
221 path = path.join(' ') if path.is_a?(Array)
223 arr_opts = ['-f'] # overrides the up-to-date check by default
224 arr_opts << ['-r'] if opts[:recursive]
227 command('rm', arr_opts)
230 def commit(message, opts = {})
231 arr_opts = ["-m '#{message}'"]
232 arr_opts << '-a' if opts[:add_all]
233 command('commit', arr_opts)
236 def reset(commit, opts = {})
238 arr_opts << '--hard' if opts[:hard]
239 arr_opts << commit.to_s if commit
240 command('reset', arr_opts)
244 def branch_new(branch)
245 command('branch', branch)
248 def branch_delete(branch)
249 command('branch', ['-d', branch])
252 def checkout(branch, opts = {})
254 arr_opts << '-f' if opts[:force]
255 arr_opts << branch.to_s
257 command('checkout', arr_opts)
260 def merge(branch, message = nil)
262 arr_opts << ["-m '#{message}'"] if message
263 arr_opts << branch.to_a.join(' ')
264 command('merge', arr_opts)
267 def remote_add(name, url, opts = {})
269 arr_opts << '-f' if opts[:with_fetch]
273 command('remote', arr_opts)
276 # this is documented as such, but seems broken for some reason
277 # i'll try to get around it some other way later
278 def remote_remove(name)
279 command('remote', ['rm', name])
283 command_lines('remote')
296 command('fetch', remote.to_s)
299 def tag_sha(tag_name)
300 command('show-ref', ['--tags', '-s', tag_name])
304 command('repack', ['-a', '-d'])
309 def command_lines(cmd, opts = {})
310 command(cmd, opts).split("\n")
313 def command(cmd, opts = {})
314 ENV['GIT_DIR'] = @git_dir
315 ENV['GIT_INDEX_FILE'] = @git_index_file
316 ENV['GIT_WORK_DIR'] = @git_work_dir
317 path = @git_work_dir || @git_dir || @path
319 opts = opts.to_a.join(' ')
320 git_cmd = "git #{cmd} #{opts}"
321 out = `git #{cmd} #{opts} 2>&1`.chomp
323 #puts "gd: #{@git_work_dir}"
324 #puts "gi: #{@git_index_file}"
330 if $?.exitstatus == 1 && out == ''
333 raise Git::GitExecuteError.new(git_cmd + out.to_s)