summaryrefslogtreecommitdiffstats
path: root/lib/git/object.rb
blob: 4f8e559acaf913fb04013121150bb3a70b591a9c (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
module Git
  class Object
    
    class AbstractObject
      attr_accessor :sha, :size, :type
    
      @base = nil
    
      def initialize(base, sha)
        @base = base
        @sha = sha
        @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
        "#{@type.ljust(6)} #{@sha}"
      end
      
    end
  
    
    class Blob < AbstractObject
      def setup
        @type = 'blob'
      end
    end
  
    class Tree < AbstractObject
      def setup
        @type = 'tree'
      end
    end
  
    class Commit < AbstractObject
      def setup
        @type = 'commit'
      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)
        sha = base.lib.revparse(objectish)
        type = base.lib.object_type(sha)
      
        klass =
          case type
          when /blob/:   Blob   
          when /commit/: Commit
          when /tree/:   Tree
          end
        klass::new(base, sha)
      end
    end 
    
  end
end