added some branching beginnings
[rubygit.git] / lib / git / lib.rb
blobc83ecaa521032fbc3c30adbe4cc1ec3af99d1d91
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_lines('log', arr_opts).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]).to_i
34     end
35     
36     def object_contents(sha)
37       command('cat-file', ['-p', sha])
38     end
40     def branches_all
41       command_lines('branch', '-a').map { |l| Git::Branch.new(@base, l) }
42     end
43     
44     private
45     
46     def command_lines(cmd, opts)
47       command(cmd, opts).split("\n")
48     end
49     
50     def command(cmd, opts)
51       ENV['GIT_DIR'] = @base.repo.path
52       ENV['GIT_INDEX_FILE'] = @base.index.path   
53       ENV['GIT_WORK_DIR'] = @base.dir.path   
54       Dir.chdir(@base.dir.path) do  
55         opts = opts.to_a.join(' ')
56         #puts "git #{cmd} #{opts}"
57         out = `git #{cmd} #{opts} 2>&1`.chomp
58         if $?.exitstatus != 0
59           raise Git::GitExecuteError.new(out)
60         end
61         out
62       end
63     end
64     
65   end
66 end