added branches, more log stuff, better tests, changed the log api a bit
[rubygit.git] / lib / git / object.rb
blobf2d4114d60cbf50d22cc93e37f7cbd2b4052b818
1 module Git
2   class Object
3     
4     class AbstractObject
5       attr_accessor :sha, :size, :type
6     
7       @base = nil
8     
9       def initialize(base, sha)
10         @base = base
11         @sha = sha
12         @size = @base.lib.object_size(@sha)
13         setup
14       end
15     
16       def contents
17         @base.lib.object_contents(@sha)
18       end
19       
20       def contents_array
21         self.contents.split("\n")
22       end
23       
24       def setup
25         raise NotImplementedError
26       end
27       
28       def to_s
29         "#{@type.ljust(6)} #{@sha}"
30       end
31       
32       def grep(string, path_limiter = nil, opts = {})
33         default = {:object => @sha, :path_limiter => path_limiter}
34         grep_options = default.merge(opts)
35         @base.lib.grep(string, grep_options)
36       end
37       
38       def log(count = 30)
39         Git::Log.new(self, count).object(@sha)
40       end
41       
42     end
43   
44     
45     class Blob < AbstractObject
46       def setup
47         @type = 'blob'
48       end
49     end
50   
51     class Tree < AbstractObject
52       def setup
53         @type = 'tree'
54       end
55     end
56   
57     class Commit < AbstractObject
58       def setup
59         @type = 'commit'
60       end
61     end
62   
63   
64     class << self
65       # if we're calling this, we don't know what type it is yet
66       # so this is our little factory method
67       def new(base, objectish)
68         sha = base.lib.revparse(objectish)
69         type = base.lib.object_type(sha)
70       
71         klass =
72           case type
73           when /blob/:   Blob   
74           when /commit/: Commit
75           when /tree/:   Tree
76           end
77         klass::new(base, sha)
78       end
79     end 
80     
81   end
82 end