Class: GitObjectBrowser::Models::GitObject

Inherits:
Bindata
  • Object
show all
Defined in:
lib/git-object-browser/models/git_object.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Bindata

#binstr, #byte, #bytes, #find_char, #hex, #int, #peek, #raw, #seek, #skip, #switch_source

Constructor Details

#initialize(input) ⇒ GitObject

Returns a new instance of GitObject.



11
12
13
# File 'lib/git-object-browser/models/git_object.rb', line 11

def initialize(input)
  super(input)
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



8
9
10
# File 'lib/git-object-browser/models/git_object.rb', line 8

def contents
  @contents
end

#entriesObject (readonly)

Returns the value of attribute entries.



8
9
10
# File 'lib/git-object-browser/models/git_object.rb', line 8

def entries
  @entries
end

#messageObject (readonly)

Returns the value of attribute message.



9
10
11
# File 'lib/git-object-browser/models/git_object.rb', line 9

def message
  @message
end

#propertiesObject (readonly)

Returns the value of attribute properties.



9
10
11
# File 'lib/git-object-browser/models/git_object.rb', line 9

def properties
  @properties
end

#sha1Object (readonly)

Returns the value of attribute sha1.



8
9
10
# File 'lib/git-object-browser/models/git_object.rb', line 8

def sha1
  @sha1
end

#sizeObject (readonly)

Returns the value of attribute size.



8
9
10
# File 'lib/git-object-browser/models/git_object.rb', line 8

def size
  @size
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/git-object-browser/models/git_object.rb', line 8

def type
  @type
end

Class Method Details

.path?(relpath) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/git-object-browser/models/git_object.rb', line 58

def self.path?(relpath)
  relpath =~ %r{\Aobjects/[0-9a-f]{2}/[0-9a-f]{38}\z}
end

Instance Method Details

#parseObject



15
16
17
18
19
# File 'lib/git-object-browser/models/git_object.rb', line 15

def parse
  content = Zlib::Inflate.inflate(@in.read(nil))
  parse_inflated(content)
  self
end

#parse_inflated(content) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/git-object-browser/models/git_object.rb', line 21

def parse_inflated(content)
  @sha1 = Digest::SHA1.hexdigest(content)
  @in   = StringIO.new(content)

  @type = find_char ' '
  @size = find_char "\0"

  @type = type
  @size = size

  if @type == 'tree'
    @entries = parse_tree_entries
  else
    @content = @in.read(nil)
    if @type == 'commit' or @type == 'tag'
      (@properties, @message) = parse_contents
    end

    @content = force_utf8(@content)
    @content = @content[0, 3000] + "\n..." if @content.length > 3000
  end

  self
end

#to_hashObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/git-object-browser/models/git_object.rb', line 46

def to_hash
  return {
    :type       => @type,
    :sha1       => @sha1,
    :size       => @size,
    :entries    => @entries,
    :content    => @content,
    :properties => @properties,
    :message    => @message
  }
end