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

      def diff_parent
        diff(parent)
      end
      
      private
      
        # see if this object has been initialized and do so if not
        def check_commit
          data = @base.lib.commit_data(@sha)
          @committer = Git::Author.new(data['committer'])
          @author = Git::Author.new(data['author'])
          @tree = Tree.new(@base, data['tree'])
          @parents = data['parent'].map{ |sha| Commit.new(@base, sha) }
          @message = data['message'].chomp
        end
      
    end
  
    class Tag < AbstractObject
      attr_accessor :name
      
      def initialize(base, sha, name)
        super(base, sha)
        @name = name
      end
      
      def setup
        @type = 'tag'
      end
    end
    
    class << self
      # if we're calling this, we don't know what type it is yet
      # so this is our little factory method
      def new(base, objectish, is_tag = false)
        if is_tag
          sha = base.lib.tag_sha(objectish)
          if sha == ''
            raise Git::GitTagNameDoesNotExist.new(objectish)
          end
          return Tag.new(base, sha, objectish)
        else
          sha = base.lib.revparse(objectish)
          type = base.lib.object_type(sha) 
        end
        
        klass =
          case type
          when /blob/:   Blob   
          when /commit/: Commit
          when /tree/:   Tree
          end
        klass::new(base, sha)
      end
    end 
    
  end
end