3 class GitExecuteError < StandardError
6 class GitNoOutput < StandardError
17 def log_commits(opts = {})
18 arr_opts = ['--pretty=oneline']
19 arr_opts << "-#{opts[:count]}" if opts[:count]
20 arr_opts << "--since=\"#{opts[:since]}\"" if opts[:since].is_a? String
21 arr_opts << "#{opts[:between][0]}..#{opts[:between][1].to_s}" if (opts[:between] && opts[:between].size == 2)
22 arr_opts << opts[:object] if opts[:object].is_a? String
23 arr_opts << '-- ' + opts[:path_limiter] if opts[:path_limiter].is_a? String
25 command_lines('log', arr_opts).map { |l| l.split.first }
29 command('rev-parse', string)
33 command('cat-file', ['-t', sha])
37 command('cat-file', ['-s', sha]).to_i
40 def object_contents(sha)
41 command('cat-file', ['-p', sha])
45 command_lines('branch', '-a').map do |b|
47 current = true if b[0, 2] == '* '
48 Git::Branch.new(@base, b.gsub('* ', '').strip, current)
52 def config_remote(name)
54 command_lines('config', ['--get-regexp', "remote.#{name}"]).each do |line|
55 (key, value) = line.split
56 hsh[key.gsub("remote.#{name}.", '')] = value
62 # [tree-ish] = [[line_no, match], [line_no, match2]]
63 # [tree-ish] = [[line_no, match], [line_no, match2]]
64 def grep(string, opts = {})
65 opts[:object] = 'HEAD' if !opts[:object]
68 grep_opts << '-i' if opts[:ignore_case]
69 grep_opts << '-v' if opts[:invert_match]
70 grep_opts << "-e '#{string}'"
71 grep_opts << opts[:object] if opts[:object].is_a? String
72 grep_opts << ('-- ' + opts[:path_limiter]) if opts[:path_limiter].is_a? String
74 command_lines('grep', grep_opts).each do |line|
75 if m = /(.*)\:(\d+)\:(.*)/.match(line)
77 hsh[m[1]] << [m[2].to_i, m[3]]
85 def command_lines(cmd, opts)
86 command(cmd, opts).split("\n")
89 def command(cmd, opts)
90 ENV['GIT_DIR'] = @base.repo.path
91 ENV['GIT_INDEX_FILE'] = @base.index.path
92 ENV['GIT_WORK_DIR'] = @base.dir.path
93 Dir.chdir(@base.dir.path) do
94 opts = opts.to_a.join(' ')
95 #puts "git #{cmd} #{opts}"
96 out = `git #{cmd} #{opts} 2>&1`.chomp
99 raise Git::GitExecuteError.new(out)