got all the unit tests to run from either place, fixed some old functionality
[rubygit.git] / lib / git / lib.rb
blob681ba58d99cdd1d160e50c97cf0c17eb69a29abc
1 module Git
2   
3   class GitExecuteError < StandardError 
4   end
5   
6   class Lib
7       
8     @base = nil
9     
10     def initialize(base)
11       @base = base
12     end
13     
14     def log_commits(opts)
15       arr_opts = ['--pretty=oneline']
16       arr_opts << "-#{opts[:count]}" if opts[:count]
17       arr_opts << "--since=\"#{opts[:since]}\"" if opts[:since].is_a? String
18       arr_opts << "#{opts[:between][0]}..#{opts[:between][1].to_s}" if (opts[:between] && opts[:between].size == 2)
19       arr_opts << opts[:file] if opts[:file].is_a? String
20       
21       command('log', arr_opts).split("\n").map { |l| Git::Object::Commit.new(@base, l.split.first) }
22     end
23     
24     def revparse(string)
25       command('rev-parse', string)
26     end
27     
28     def object_type(sha)
29       command('cat-file', ['-t', sha])
30     end
31     
32     def object_size(sha)
33       command('cat-file', ['-s', sha])
34     end
35     
36     def object_contents(sha)
37       command('cat-file', ['-p', sha])
38     end
39     
40     private
41     
42     def command(cmd, opts)
43       ENV['GIT_DIR'] = @base.repo.path
44       ENV['GIT_INDEX_FILE'] = @base.index.path   
45       ENV['GIT_WORK_DIR'] = @base.dir.path   
46       Dir.chdir(@base.dir.path) do  
47         opts = opts.to_a.join(' ')
48         #puts "git #{cmd} #{opts}"
49         out = `git #{cmd} #{opts} 2>&1`.chomp
50         if $?.exitstatus != 0
51           raise Git::GitExecuteError.new(out)
52         end
53         out
54       end
55     end
56     
57   end
58 end