added a bunch of good stuff to the commit object
[rubygit.git] / lib / git / object.rb
blob58ea7abdcc47915ebcb75273d55c967188a0d214
1 module Git
2   
3   class GitTagNameDoesNotExist< StandardError 
4   end
5   
6   class Object
7     
8     class AbstractObject
9       attr_accessor :sha, :size, :type, :mode
10     
11       @base = nil
12     
13       def initialize(base, sha)
14         @base = base
15         @sha = sha.to_s
16         @size = @base.lib.object_size(@sha)
17         setup
18       end
19     
20       def contents
21         @base.lib.object_contents(@sha)
22       end
23       
24       def contents_array
25         self.contents.split("\n")
26       end
27       
28       def setup
29         raise NotImplementedError
30       end
31       
32       def to_s
33         @sha
34       end
35       
36       def grep(string, path_limiter = nil, opts = {})
37         default = {:object => @sha, :path_limiter => path_limiter}
38         grep_options = default.merge(opts)
39         @base.lib.grep(string, grep_options)
40       end
41       
42       def diff(objectish)
43         Git::Diff.new(@base, @sha, objectish)
44       end
45       
46       def log(count = 30)
47         Git::Log.new(@base, count).object(@sha)
48       end
49       
50     end
51   
52     
53     class Blob < AbstractObject
54       def setup
55         @type = 'blob'
56       end
57     end
58   
59     class Tree < AbstractObject
60       def setup
61         @type = 'tree'
62       end
63     end
64   
65     class Commit < AbstractObject
66       
67       @tree = nil
68       @parents = nil
69       @author = nil
70       @committer = nil
71       @message = nil
72       
73       def setup
74         @type = 'commit'
75       end
76       
77       def message
78         check_commit
79         @message
80       end
81       
82       def gtree
83         check_commit
84         Tree.new(@base, @tree)
85       end
86       
87       def parent
88         parents.first
89       end
90       
91       # array of all parent commits
92       def parents
93         check_commit
94         @parents        
95       end
96       
97       # git author
98       def author     
99         check_commit
100         @author
101       end
102       
103       def author_date
104         author.date
105       end
106       
107       # git author
108       def committer
109         check_commit
110         @committer
111       end
112       
113       def committer_date 
114         committer.date
115       end
116       alias_method :date, :committer_date
118       def diff_parent
119         diff(parent)
120       end
121       
122       private
123       
124         # see if this object has been initialized and do so if not
125         def check_commit
126           data = @base.lib.commit_data(@sha)
127           @committer = Git::Author.new(data['committer'])
128           @author = Git::Author.new(data['author'])
129           @tree = Tree.new(@base, data['tree'])
130           @parents = data['parent'].map{ |sha| Commit.new(@base, sha) }
131           @message = data['message'].chomp
132         end
133       
134     end
135   
136     class Tag < AbstractObject
137       attr_accessor :name
138       
139       def initialize(base, sha, name)
140         super(base, sha)
141         @name = name
142       end
143       
144       def setup
145         @type = 'tag'
146       end
147     end
148     
149     class << self
150       # if we're calling this, we don't know what type it is yet
151       # so this is our little factory method
152       def new(base, objectish, is_tag = false)
153         if is_tag
154           sha = base.lib.tag_sha(objectish)
155           if sha == ''
156             raise Git::GitTagNameDoesNotExist.new(objectish)
157           end
158           return Tag.new(base, sha, objectish)
159         else
160           sha = base.lib.revparse(objectish)
161           type = base.lib.object_type(sha) 
162         end
163         
164         klass =
165           case type
166           when /blob/:   Blob   
167           when /commit/: Commit
168           when /tree/:   Tree
169           end
170         klass::new(base, sha)
171       end
172     end 
173     
174   end