added branches, more log stuff, better tests, changed the log api a bit
[rubygit.git] / lib / git / lib.rb
blob2937d52038eb6ce18213d33e3dfb1d5cd443c608
1 module Git
2   
3   class GitExecuteError < StandardError 
4   end
5   
6   class GitNoOutput < StandardError 
7   end
8   
9   class Lib
10       
11     @base = nil
12     
13     def initialize(base)
14       @base = base
15     end
16     
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
24       
25       command_lines('log', arr_opts).map { |l| l.split.first }
26     end
27     
28     def revparse(string)
29       command('rev-parse', string)
30     end
31     
32     def object_type(sha)
33       command('cat-file', ['-t', sha])
34     end
35     
36     def object_size(sha)
37       command('cat-file', ['-s', sha]).to_i
38     end
39     
40     def object_contents(sha)
41       command('cat-file', ['-p', sha])
42     end
44     def branches_all
45       command_lines('branch', '-a').map do |b| 
46         current = false
47         current = true if b[0, 2] == '* '
48         Git::Branch.new(@base, b.gsub('* ', '').strip, current)
49       end
50     end
51     
52     def config_remote(name)
53       hsh = {}
54       command_lines('config', ['--get-regexp', "remote.#{name}"]).each do |line|
55         (key, value) = line.split
56         hsh[key.gsub("remote.#{name}.", '')] = value
57       end
58       hsh
59     end
60     
61     # returns hash
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]
67       grep_opts = ['-n']
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
73       hsh = {}
74       command_lines('grep', grep_opts).each do |line|
75         if m = /(.*)\:(\d+)\:(.*)/.match(line)        
76           hsh[m[1]] ||= []
77           hsh[m[1]] << [m[2].to_i, m[3]] 
78         end
79       end
80       hsh
81     end
82     
83     private
84     
85     def command_lines(cmd, opts)
86       command(cmd, opts).split("\n")
87     end
88     
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
97         #puts out
98         if $?.exitstatus > 1
99           raise Git::GitExecuteError.new(out)
100         end
101         out
102       end
103     end
104     
105   end