Class: GitObjectBrowser::Models::Index

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

Overview

Parse .git/index file

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) ⇒ Index



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

def initialize(input)
  super(input)
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



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

def entries
  @entries
end

#entry_countObject (readonly)

Returns the value of attribute entry_count.



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

def entry_count
  @entry_count
end

#extensionsObject (readonly)

Returns the value of attribute extensions.



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

def extensions
  @extensions
end

#sha1Object (readonly)

Returns the value of attribute sha1.



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

def sha1
  @sha1
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Class Method Details

.path?(relpath) ⇒ Boolean



73
74
75
# File 'lib/git-object-browser/models/index.rb', line 73

def self.path?(relpath)
  relpath == "index"
end

Instance Method Details

#parseObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/git-object-browser/models/index.rb', line 14

def parse
  dirc = raw(4)
  if dirc != "DIRC"
    throw Exception.new("Illegal format.")
  end

  @version     = int
  @entry_count = int
  @entries     = parse_entries
  @extensions  = parse_extensions
  @sha1        = hex(20)

  self
end

#parse_entriesObject



29
30
31
32
33
34
35
36
37
# File 'lib/git-object-browser/models/index.rb', line 29

def parse_entries
  entries = []
  last_path = nil
  @entry_count.times do |i|
    entries << IndexEntry.new(@in, @version, last_path)
    last_path = entries.last.path
  end
  return entries
end

#parse_extensionsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/git-object-browser/models/index.rb', line 39

def parse_extensions
  extensions = []
  while signature = peek(4)
    if signature == "TREE"
      extensions << IndexTreeExtension.new(@in).parse
    elsif  signature == "REUC"
      extensions << IndexReucExtension.new(@in).parse
    else
      break
    end
  end
  return extensions
end

#to_hashObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/git-object-browser/models/index.rb', line 53

def to_hash
  entries = []
  @entries.each do |entry|
    entries << entry.to_hash
  end

  extensions = []
  @extensions.each do |extension|
    extensions << extension.to_hash
  end

  return {
    :version       => @version,
    :entry_count   => @entry_count,
    :entries       => entries,
    :extensions    => extensions,
    :sha1          => @sha1,
  }
end