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 # [tree-ish] = [[line_no, match], [line_no, match2]]
98 # [tree-ish] = [[line_no, match], [line_no, match2]]
99 def grep(string, opts = {})
100 opts[:object] = 'HEAD' if !opts[:object]
103 grep_opts << '-i' if opts[:ignore_case]
104 grep_opts << '-v' if opts[:invert_match]
105 grep_opts << "-e '#{string}'"
106 grep_opts << opts[:object] if opts[:object].is_a?(String)
107 grep_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
109 command_lines('grep', grep_opts).each do |line|
110 if m = /(.*)\:(\d+)\:(.*)/.match(line)
112 hsh[m[1]] << [m[2].to_i, m[3]]
118 def diff_full(obj1 = 'HEAD', obj2 = nil, opts = {})
121 diff_opts << obj2 if obj2.is_a?(String)
122 diff_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
124 command('diff', diff_opts)
127 def diff_stats(obj1 = 'HEAD', obj2 = nil, opts = {})
128 diff_opts = ['--numstat']
130 diff_opts << obj2 if obj2.is_a?(String)
131 diff_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
133 hsh = {:total => {:insertions => 0, :deletions => 0, :lines => 0, :files => 0}, :files => {}}
135 command_lines('diff', diff_opts).each do |file|
136 (insertions, deletions, filename) = file.split("\t")
137 hsh[:total][:insertions] += insertions.to_i
138 hsh[:total][:deletions] += deletions.to_i
139 hsh[:total][:lines] = (hsh[:total][:deletions] + hsh[:total][:insertions])
140 hsh[:total][:files] += 1
141 hsh[:files][filename] = {:insertions => insertions.to_i, :deletions => deletions.to_i}
147 # compares the index and the working directory
150 command_lines('diff-files').each do |line|
151 (info, file) = line.split("\t")
152 (mode_src, mode_dest, sha_src, sha_dest, type) = info.split
153 hsh[file] = {:path => file, :mode_file => mode_src.to_s[1, 7], :mode_index => mode_dest,
154 :sha_file => sha_src, :sha_index => sha_dest, :type => type}
159 # compares the index and the repository
160 def diff_index(treeish)
162 command_lines('diff-index', treeish).each do |line|
163 (info, file) = line.split("\t")
164 (mode_src, mode_dest, sha_src, sha_dest, type) = info.split
165 hsh[file] = {:path => file, :mode_repo => mode_src.to_s[1, 7], :mode_index => mode_dest,
166 :sha_repo => sha_src, :sha_index => sha_dest, :type => type}
173 command_lines('ls-files', '--stage').each do |line|
174 (info, file) = line.split("\t")
175 (mode, sha, stage) = info.split
176 hsh[file] = {:path => file, :mode_index => mode, :sha_index => sha, :stage => stage}
182 def config_remote(name)
184 command_lines('config', ['--get-regexp', "remote.#{name}"]).each do |line|
185 (key, value) = line.split
186 hsh[key.gsub("remote.#{name}.", '')] = value
192 command('config', ['--get', name])
197 command_lines('config', ['--list']).each do |line|
198 (key, value) = line.split('=')
206 def config_set(name, value)
207 command('config', [name, "'#{value}'"])
211 path = path.join(' ') if path.is_a?(Array)
215 def remove(path = '.', opts = {})
216 path = path.join(' ') if path.is_a?(Array)
218 arr_opts = ['-f'] # overrides the up-to-date check by default
219 arr_opts << ['-r'] if opts[:recursive]
222 command('rm', arr_opts)
225 def commit(message, opts = {})
226 arr_opts = ["-m '#{message}'"]
227 arr_opts << '-a' if opts[:add_all]
228 command('commit', arr_opts)
231 def reset(commit, opts = {})
233 arr_opts << '--hard' if opts[:hard]
234 arr_opts << commit.to_s if commit
235 command('reset', arr_opts)
241 def command_lines(cmd, opts = {})
242 command(cmd, opts).split("\n")
245 def command(cmd, opts = {})
246 ENV['GIT_DIR'] = @git_dir
247 ENV['GIT_INDEX_FILE'] = @git_index_file
248 ENV['GIT_WORK_DIR'] = @git_work_dir
249 path = @git_work_dir || @git_dir || @path
251 opts = opts.to_a.join(' ')
252 out = `git #{cmd} #{opts} 2>&1`.chomp
254 #puts "gd: #{@git_work_dir}"
255 #puts "gi: #{@git_index_file}"
257 #puts "git #{cmd} #{opts}"
261 raise Git::GitExecuteError.new(out)